/src/Fast-DDS/include/fastrtps/utils/Semaphore.h
Line | Count | Source (jump to first uncovered line) |
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 FASTRTPS_SEMAPHORE_H_ |
16 | | #define FASTRTPS_SEMAPHORE_H_ |
17 | | |
18 | | #include <condition_variable> |
19 | | #include <mutex> |
20 | | |
21 | | namespace eprosima { |
22 | | namespace fastrtps { |
23 | | |
24 | | class Semaphore { |
25 | | public: |
26 | | explicit Semaphore(size_t count = 0); |
27 | | Semaphore(const Semaphore&) = delete; |
28 | | Semaphore& operator=(const Semaphore&) = delete; |
29 | | |
30 | | void post(); |
31 | | void wait(); |
32 | | void disable(); |
33 | | void enable(); |
34 | | void post(int n); |
35 | | |
36 | | private: |
37 | | size_t count_; |
38 | | std::mutex mutex_; |
39 | | std::condition_variable cv_; |
40 | | bool disable_; |
41 | | }; |
42 | | |
43 | 0 | inline Semaphore::Semaphore(size_t count) : count_(count), disable_(false) {} |
44 | | |
45 | 0 | inline void Semaphore::post() { |
46 | 0 | std::lock_guard<std::mutex> lock(mutex_); |
47 | 0 | if (!disable_) |
48 | 0 | { |
49 | 0 | ++count_; |
50 | 0 | cv_.notify_one(); |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | 0 | inline void Semaphore::post(int n) { |
55 | 0 | std::lock_guard<std::mutex> lock(mutex_); |
56 | 0 | if (!disable_) |
57 | 0 | { |
58 | 0 | count_ += n; |
59 | 0 | for (int i = 0; i < n; ++i) |
60 | 0 | { |
61 | 0 | cv_.notify_one(); |
62 | 0 | } |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | 0 | inline void Semaphore::disable() { |
67 | 0 | std::lock_guard<std::mutex> lock(mutex_); |
68 | 0 | if (!disable_) |
69 | 0 | { |
70 | 0 | count_ = (size_t)-1L; |
71 | 0 | cv_.notify_all(); |
72 | 0 | disable_ = true; |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | 0 | inline void Semaphore::enable() { |
77 | 0 | std::lock_guard<std::mutex> lock(mutex_); |
78 | 0 | if (disable_) |
79 | 0 | { |
80 | 0 | count_ = 0; |
81 | 0 | disable_ = false; |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | 0 | inline void Semaphore::wait() { |
86 | 0 | std::unique_lock<std::mutex> lock(mutex_); |
87 | 0 | if (!disable_) |
88 | 0 | { |
89 | 0 | cv_.wait(lock, [&] { |
90 | 0 | if (disable_) return true; |
91 | 0 | return count_ > 0; |
92 | 0 | }); |
93 | 0 | --count_; |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | | } // fastrtps |
98 | | } // eprosima |
99 | | |
100 | | #endif // FASTRTPS_SEMAPHORE_H_ |