iceoryx_doc  1.0.1
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_UTILS_POSIX_WRAPPER_SEMAPHORE_HPP
18 #define IOX_UTILS_POSIX_WRAPPER_SEMAPHORE_HPP
19 
20 #include "iceoryx_utils/cxx/expected.hpp"
21 #include "iceoryx_utils/cxx/helplets.hpp"
22 #include "iceoryx_utils/cxx/smart_c.hpp"
23 #include "iceoryx_utils/cxx/string.hpp"
24 #include "iceoryx_utils/design_pattern/creation.hpp"
25 #include "iceoryx_utils/internal/relocatable_pointer/relative_pointer.hpp"
26 #include "iceoryx_utils/internal/units/duration.hpp"
27 #include "iceoryx_utils/platform/fcntl.hpp"
28 #include "iceoryx_utils/platform/semaphore.hpp"
29 #include "iceoryx_utils/platform/stat.hpp"
30 
31 #include <cstring>
32 
33 namespace iox
34 {
35 namespace posix
36 {
37 enum class SemaphoreError
38 {
39  INVALID_STATE,
40  CREATION_FAILED,
41  NAME_TOO_LONG,
42  UNABLE_TO_OPEN_HANDLE,
43  INVALID_SEMAPHORE_HANDLE,
44  SEMAPHORE_OVERFLOW,
45  INTERRUPTED_BY_SIGNAL_HANDLER,
46  UNDEFINED
47 };
48 
49 enum class SemaphoreWaitState
50 {
51  TIMEOUT,
52  NO_TIMEOUT,
53 };
54 
56 {
57 };
59 {
60 };
62 {
63 };
65 {
66 };
67 static constexpr CreateUnnamedSingleProcessSemaphore_t CreateUnnamedSingleProcessSemaphore =
69 static constexpr CreateUnnamedSharedMemorySemaphore_t CreateUnnamedSharedMemorySemaphore =
71 static constexpr CreateNamedSemaphore_t CreateNamedSemaphore = CreateNamedSemaphore_t();
72 static constexpr OpenNamedSemaphore_t OpenNamedSemaphore = OpenNamedSemaphore_t();
73 
83 class Semaphore : public DesignPattern::Creation<Semaphore, SemaphoreError>
84 {
85  public:
89  Semaphore() noexcept;
90 
92  Semaphore(Semaphore&& rhs) noexcept;
93 
95  Semaphore& operator=(Semaphore&& rhs) noexcept;
96 
99  Semaphore(const Semaphore&) = delete;
100 
103  Semaphore& operator=(const Semaphore&) = delete;
104 
106  ~Semaphore() noexcept;
107 
123  cxx::expected<int, SemaphoreError> getValue() const noexcept;
124 
132  cxx::expected<SemaphoreError> post() noexcept;
133 
144  cxx::expected<SemaphoreWaitState, SemaphoreError> timedWait(const units::Duration abs_timeout,
145  const bool doContinueOnInterrupt) const noexcept;
146 
150  cxx::expected<bool, SemaphoreError> tryWait() const noexcept;
151 
179  cxx::expected<SemaphoreError> wait() const noexcept;
180 
183  iox_sem_t* getHandle() noexcept;
184 
185  private:
186  cxx::string<128> m_name;
187  bool m_isCreated = true;
188  bool m_isNamedSemaphore = true;
189  bool m_isShared = false;
190 
191  mutable iox_sem_t m_handle;
192  mutable iox::rp::RelativePointer<iox_sem_t> m_handlePtr = &m_handle;
193 
194  private:
195  friend class DesignPattern::Creation<Semaphore, SemaphoreError>;
196 
202  Semaphore(CreateUnnamedSingleProcessSemaphore_t, const unsigned int value) noexcept;
203 
209  Semaphore(CreateUnnamedSharedMemorySemaphore_t, const unsigned int value) noexcept;
211  Semaphore(CreateUnnamedSharedMemorySemaphore_t, iox_sem_t* handle, const unsigned int value) noexcept;
212 
221  Semaphore(OpenNamedSemaphore_t, const char* name, const int oflag) noexcept;
222 
236  Semaphore(CreateNamedSemaphore_t, const char* name, const mode_t mode, const unsigned int value) noexcept;
237 
244  bool close() noexcept;
245 
260  bool destroy() noexcept;
261 
286  bool init(iox_sem_t* handle, const int pshared, const unsigned int value) noexcept;
287 
314  bool open(const int oflag) noexcept;
315 
316  bool open(const int oflag, const mode_t mode, const unsigned int value) noexcept;
317 
325  bool unlink(const char* name) noexcept;
326 
329  bool isNamedSemaphore() noexcept;
330 
331  void closeHandle() noexcept;
332 
333  template <typename SmartC>
334  bool setHandleFromCall(const SmartC& call) noexcept;
335 
336  SemaphoreError errnoToEnum(const int errnoValue) const noexcept;
337 };
338 } // namespace posix
339 } // namespace iox
340 
341 #include "iceoryx_utils/internal/posix_wrapper/semaphore.inl"
342 
343 #endif // IOX_UTILS_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:84
cxx::expected< bool, SemaphoreError > tryWait() const noexcept
see wait()
cxx::expected< SemaphoreError > post() noexcept
calls sem_post which unlocks a semaphore From the sem_post manpage: sem_post() increments (unlocks) t...
Semaphore() noexcept
Default constructor which creates an uninitialized semaphore. This semaphore object is unusable you n...
iox_sem_t * getHandle() noexcept
returns the pointer to the managed semaphore. You can use this pointer with all the sem_** functions.
cxx::expected< SemaphoreWaitState, SemaphoreError > timedWait(const units::Duration abs_timeout, const bool doContinueOnInterrupt) const noexcept
see wait()
cxx::expected< SemaphoreError > wait() const noexcept
calls sem_wait which locks a semaphore From the sem_wait manpage: sem_wait() decrements (locks) the s...
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:28
Definition: semaphore.hpp:62
Definition: semaphore.hpp:65