-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondvar-pthread.hh
63 lines (50 loc) · 1.96 KB
/
condvar-pthread.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <pthread.h>
#include <iostream>
namespace pthread_condvar {
// Lower bound for the number of times the counter will be incremented since
// threads which are waiting when the first thread hits the limit will continue
// to increment the counter.
constexpr std::size_t CYCLES = 1000;
// For pthread_cond_timedwait().
const struct timespec TIMEOUT{
.tv_sec = 0,
.tv_nsec = 1000,
};
enum class Parity { even, odd };
// man 3 pthread_cond_init();
//
// pthread_cond_init, pthread_cond_signal, pthread_cond_broadcast, and
// pthread_cond_wait never return an error code.
class PthreadCondvar {
public:
PthreadCondvar()
: cond_(PTHREAD_COND_INITIALIZER), mutex_(PTHREAD_MUTEX_INITIALIZER),
ctr_(0) {}
// In the LinuxThreads implementation, no resources are associated with
// condition variables, thus pthread_cond_destroy actually does nothing
// except checking that the condition has no waiting threads.
~PthreadCondvar() {
if (pthread_cond_destroy(&cond_)) {
std::cerr << "Condition variable still has waiters." << std::endl;
}
if (pthread_mutex_destroy(&mutex_)) {
std::cerr << "Locked mutex cannot be destroyed." << std::endl;
}
}
constexpr std::size_t get_count() const { return ctr_; }
constexpr bool is_odd() { return (1 == (ctr_ & 1)); }
constexpr bool is_even() { return (0 == (ctr_ & 1)); }
// Change the parity of ctr_ to the value new_parity, blocking until the
// opposite parity is first observed. Send a broadcast wakeup when complete.
void change_parity_or_block(const Parity new_parity);
// Change the parity of ctr_ to the value new_parity, blocking until the
// opposite parity is first observed or a timeout is reached. Send a signal
// upon success to one other waiter.
void change_parity_or_timeout(const Parity new_parity);
private:
pthread_cond_t cond_;
// The lock serializes accesses to ctr_.
pthread_mutex_t mutex_;
std::size_t ctr_;
};
} // namespace pthread_condvar