tlx
semaphore.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/semaphore.hpp
3  *
4  * A simple semaphore implementation using C++11 synchronization methods.
5  *
6  * Copied and modified from STXXL https://github.com/stxxl/stxxl, which is
7  * distributed under the Boost Software License, Version 1.0.
8  *
9  * Part of tlx - http://panthema.net/tlx
10  *
11  * Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de>
12  * Copyright (C) 2013-2018 Timo Bingmann <tb@panthema.net>
13  *
14  * All rights reserved. Published under the Boost Software License, Version 1.0
15  ******************************************************************************/
16 
17 #ifndef TLX_SEMAPHORE_HEADER
18 #define TLX_SEMAPHORE_HEADER
19 
20 #include <condition_variable>
21 #include <mutex>
22 
23 namespace tlx {
24 
25 //! A simple semaphore implementation using C++11 synchronization methods.
26 class Semaphore
27 {
28 public:
29  //! construct semaphore
30  explicit Semaphore(size_t initial_value = 0)
31  : value_(initial_value) { }
32 
33  //! non-copyable: delete copy-constructor
34  Semaphore(const Semaphore&) = delete;
35  //! non-copyable: delete assignment operator
36  Semaphore& operator = (const Semaphore&) = delete;
37  //! move-constructor: just move the value
38  Semaphore(Semaphore&& s) : value_(s.value_) { }
39  //! move-assignment: just move the value
40  Semaphore& operator = (Semaphore&& s) { value_ = s.value_; return *this; }
41 
42  //! function increments the semaphore and signals any threads that are
43  //! blocked waiting a change in the semaphore
44  size_t signal() {
45  std::unique_lock<std::mutex> lock(mutex_);
46  size_t res = ++value_;
47  cv_.notify_one();
48  return res;
49  }
50  //! function increments the semaphore and signals any threads that are
51  //! blocked waiting a change in the semaphore
52  size_t signal(size_t delta) {
53  std::unique_lock<std::mutex> lock(mutex_);
54  size_t res = (value_ += delta);
55  cv_.notify_all();
56  return res;
57  }
58  //! function decrements the semaphore by delta and blocks if the semaphore
59  //! is < (delta + slack) until another thread signals a change
60  size_t wait(size_t delta = 1, size_t slack = 0) {
61  std::unique_lock<std::mutex> lock(mutex_);
62  while (value_ < delta + slack)
63  cv_.wait(lock);
64  value_ -= delta;
65  return value_;
66  }
67  //! function decrements the semaphore by delta if (delta + slack) tokens are
68  //! available as a batch. the function will not block and returns true if
69  //! delta was acquired otherwise false.
70  bool try_acquire(size_t delta = 1, size_t slack = 0) {
71  std::unique_lock<std::mutex> lock(mutex_);
72  if (value_ < delta + slack)
73  return false;
74  value_ -= delta;
75  return true;
76  }
77 
78  //! return the current value -- should only be used for debugging.
79  size_t value() const { return value_; }
80 
81 private:
82  //! value of the semaphore
83  size_t value_;
84 
85  //! mutex for condition variable
86  std::mutex mutex_;
87 
88  //! condition variable
89  std::condition_variable cv_;
90 };
91 
92 //! alias for STL-like code style
93 using semaphore = Semaphore;
94 
95 } // namespace tlx
96 
97 #endif // !TLX_SEMAPHORE_HEADER
98 
99 /******************************************************************************/
tlx::Semaphore::mutex_
std::mutex mutex_
mutex for condition variable
Definition: semaphore.hpp:114
tlx::Semaphore::wait
size_t wait(size_t delta=1, size_t slack=0)
function decrements the semaphore by delta and blocks if the semaphore is < (delta + slack) until ano...
Definition: semaphore.hpp:88
tlx::Semaphore::signal
size_t signal()
function increments the semaphore and signals any threads that are blocked waiting a change in the se...
Definition: semaphore.hpp:72
tlx::Semaphore::Semaphore
Semaphore(size_t initial_value=0)
construct semaphore
Definition: semaphore.hpp:58
tlx
Definition: exclusive_scan.hpp:17
tlx::Semaphore
A simple semaphore implementation using C++11 synchronization methods.
Definition: semaphore.hpp:40
tlx::Semaphore::cv_
std::condition_variable cv_
condition variable
Definition: semaphore.hpp:117
tlx::Semaphore::value
size_t value() const
return the current value – should only be used for debugging.
Definition: semaphore.hpp:107
tlx::Semaphore::value_
size_t value_
value of the semaphore
Definition: semaphore.hpp:111
tlx::Semaphore::try_acquire
bool try_acquire(size_t delta=1, size_t slack=0)
function decrements the semaphore by delta if (delta + slack) tokens are available as a batch.
Definition: semaphore.hpp:98
tlx::Semaphore::operator=
Semaphore & operator=(const Semaphore &)=delete
non-copyable: delete assignment operator