iceoryx_hoofs  2.0.2
semaphore.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_HOOFS_POSIX_WRAPPER_SEMAPHORE_HPP
18 #define IOX_HOOFS_POSIX_WRAPPER_SEMAPHORE_HPP
19 
20 #include "iceoryx_hoofs/cxx/expected.hpp"
21 #include "iceoryx_hoofs/cxx/helplets.hpp"
22 #include "iceoryx_hoofs/cxx/string.hpp"
23 #include "iceoryx_hoofs/design_pattern/creation.hpp"
24 #include "iceoryx_hoofs/internal/relocatable_pointer/relative_pointer.hpp"
25 #include "iceoryx_hoofs/internal/units/duration.hpp"
26 #include "iceoryx_hoofs/platform/fcntl.hpp"
27 #include "iceoryx_hoofs/platform/semaphore.hpp"
28 #include "iceoryx_hoofs/platform/stat.hpp"
29 
30 #include <cstring>
31 
32 namespace iox
33 {
34 namespace posix
35 {
36 enum class SemaphoreError
37 {
38  CREATION_FAILED,
39  NAME_TOO_LONG,
40  UNABLE_TO_OPEN_HANDLE,
41  INVALID_SEMAPHORE_HANDLE,
42  SEMAPHORE_OVERFLOW,
43  INTERRUPTED_BY_SIGNAL_HANDLER,
44  UNDEFINED
45 };
46 
47 enum class SemaphoreWaitState
48 {
49  TIMEOUT,
50  NO_TIMEOUT,
51 };
52 
54 {
55 };
57 {
58 };
60 {
61 };
63 {
64 };
65 static constexpr CreateUnnamedSingleProcessSemaphore_t CreateUnnamedSingleProcessSemaphore =
67 static constexpr CreateUnnamedSharedMemorySemaphore_t CreateUnnamedSharedMemorySemaphore =
69 static constexpr CreateNamedSemaphore_t CreateNamedSemaphore = CreateNamedSemaphore_t();
70 static constexpr OpenNamedSemaphore_t OpenNamedSemaphore = OpenNamedSemaphore_t();
71 
81 class Semaphore : public DesignPattern::Creation<Semaphore, SemaphoreError>
82 {
83  public:
87  Semaphore() noexcept;
88 
90  Semaphore(Semaphore&& rhs) noexcept;
91 
93  Semaphore& operator=(Semaphore&& rhs) noexcept;
94 
97  Semaphore(const Semaphore&) = delete;
98 
101  Semaphore& operator=(const Semaphore&) = delete;
102 
104  ~Semaphore() noexcept;
105 
121  cxx::expected<int, SemaphoreError> getValue() const noexcept;
122 
130  cxx::expected<SemaphoreError> post() noexcept;
131 
136  cxx::expected<SemaphoreWaitState, SemaphoreError> timedWait(const units::Duration abs_timeout) noexcept;
137 
141  cxx::expected<bool, SemaphoreError> tryWait() noexcept;
142 
170  cxx::expected<SemaphoreError> wait() noexcept;
171 
172  private:
173  cxx::string<128> m_name;
174  bool m_isCreated = true;
175  bool m_isNamedSemaphore = true;
176  bool m_isShared = false;
177 
178  mutable iox_sem_t m_handle{};
179  mutable iox_sem_t* m_handlePtr = nullptr;
180 
181  private:
182  friend class DesignPattern::Creation<Semaphore, SemaphoreError>;
183 
189  Semaphore(CreateUnnamedSingleProcessSemaphore_t, const unsigned int value) noexcept;
190 
196  Semaphore(CreateUnnamedSharedMemorySemaphore_t, const unsigned int value) noexcept;
197 
206  Semaphore(OpenNamedSemaphore_t, const char* name, const int oflag) noexcept;
207 
221  Semaphore(CreateNamedSemaphore_t, const char* name, const mode_t mode, const unsigned int value) noexcept;
222 
229  bool close() noexcept;
230 
245  bool destroy() noexcept;
246 
271  static bool init(iox_sem_t* handle, const int pshared, const unsigned int value) noexcept;
272 
299  bool open(const int oflag) noexcept;
300 
303  iox_sem_t* getHandle() const noexcept;
304 
305  bool open(const int oflag, const mode_t mode, const unsigned int value) noexcept;
306 
314  static bool unlink(const char* name) noexcept;
315 
318  bool isNamedSemaphore() const noexcept;
319 
320  void closeHandle() noexcept;
321 
322  static SemaphoreError errnoToEnum(const int errnoValue) noexcept;
323 };
324 } // namespace posix
325 } // namespace iox
326 
327 #endif // IOX_HOOFS_POSIX_WRAPPER_SEMAPHORE_HPP
This pattern can be used if you write an abstraction where you have to throw an exception in the cons...
Definition: creation.hpp:99
Posix semaphore C++ Wrapping class.
Definition: semaphore.hpp:82
cxx::expected< SemaphoreError > post() noexcept
calls sem_post which unlocks a semaphore From the sem_post manpage: sem_post() increments (unlocks) t...
cxx::expected< bool, SemaphoreError > tryWait() noexcept
see wait()
Semaphore() noexcept
Default constructor which creates an uninitialized semaphore. This semaphore object is unusable you n...
cxx::expected< SemaphoreError > wait() noexcept
calls sem_wait which locks a semaphore From the sem_wait manpage: sem_wait() decrements (locks) the s...
cxx::expected< SemaphoreWaitState, SemaphoreError > timedWait(const units::Duration abs_timeout) noexcept
see wait()
cxx::expected< int, SemaphoreError > getValue() const noexcept
calls sem_getvalue which gets the value of a semaphore From the sem_getvalue manpage: sem_getvalue() ...
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29
Definition: semaphore.hpp:60
Definition: semaphore.hpp:63