/src/Fast-DDS/src/cpp/utils/Semaphore.hpp
Line | Count | Source |
1 | | // Copyright 2016 Esteve Fernandez <esteve@apache.org> |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef _FASTDDS_SEMAPHORE_H_ |
16 | | #define _FASTDDS_SEMAPHORE_H_ |
17 | | |
18 | | #include <condition_variable> |
19 | | #include <mutex> |
20 | | |
21 | | namespace eprosima { |
22 | | namespace fastdds { |
23 | | |
24 | | class Semaphore |
25 | | { |
26 | | public: |
27 | | |
28 | | explicit Semaphore( |
29 | | size_t count = 0); |
30 | | Semaphore( |
31 | | const Semaphore&) = delete; |
32 | | Semaphore& operator =( |
33 | | const Semaphore&) = delete; |
34 | | |
35 | | void post(); |
36 | | void wait(); |
37 | | void disable(); |
38 | | void enable(); |
39 | | void post( |
40 | | int n); |
41 | | |
42 | | private: |
43 | | |
44 | | size_t count_; |
45 | | std::mutex mutex_; |
46 | | std::condition_variable cv_; |
47 | | bool disable_; |
48 | | }; |
49 | | |
50 | | inline Semaphore::Semaphore( |
51 | | size_t count) |
52 | | : count_(count) |
53 | | , disable_(false) |
54 | | { |
55 | | } |
56 | | |
57 | | inline void Semaphore::post() |
58 | 0 | { |
59 | 0 | std::lock_guard<std::mutex> lock(mutex_); |
60 | 0 | if (!disable_) |
61 | 0 | { |
62 | 0 | ++count_; |
63 | 0 | cv_.notify_one(); |
64 | 0 | } |
65 | 0 | } |
66 | | |
67 | | inline void Semaphore::post( |
68 | | int n) |
69 | 0 | { |
70 | 0 | std::lock_guard<std::mutex> lock(mutex_); |
71 | 0 | if (!disable_) |
72 | 0 | { |
73 | 0 | count_ += n; |
74 | 0 | for (int i = 0; i < n; ++i) |
75 | 0 | { |
76 | 0 | cv_.notify_one(); |
77 | 0 | } |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | | inline void Semaphore::disable() |
82 | 0 | { |
83 | 0 | std::lock_guard<std::mutex> lock(mutex_); |
84 | 0 | if (!disable_) |
85 | 0 | { |
86 | 0 | count_ = (size_t)-1L; |
87 | 0 | cv_.notify_all(); |
88 | 0 | disable_ = true; |
89 | 0 | } |
90 | 0 | } |
91 | | |
92 | | inline void Semaphore::enable() |
93 | 0 | { |
94 | 0 | std::lock_guard<std::mutex> lock(mutex_); |
95 | 0 | if (disable_) |
96 | 0 | { |
97 | 0 | count_ = 0; |
98 | 0 | disable_ = false; |
99 | 0 | } |
100 | 0 | } |
101 | | |
102 | | inline void Semaphore::wait() |
103 | 0 | { |
104 | 0 | std::unique_lock<std::mutex> lock(mutex_); |
105 | 0 | if (!disable_) |
106 | 0 | { |
107 | 0 | cv_.wait(lock, [&] |
108 | 0 | { |
109 | 0 | if (disable_) |
110 | 0 | { |
111 | 0 | return true; |
112 | 0 | } |
113 | 0 | return count_ > 0; |
114 | 0 | }); |
115 | 0 | --count_; |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | | } // fastdds |
120 | | } // eprosima |
121 | | |
122 | | #endif // _FASTDDS_SEMAPHORE_H_ |