]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/CondVar.h
1 #ifndef CEPH_COND_VAR_H
2 #define CEPH_COND_VAR_H
4 #include "include/utime.h"
16 // don't allow copying.
17 void operator=(Cond
&C
);
21 Cond() : waiter_mutex(NULL
) {
22 int r
= pthread_cond_init(&_c
,NULL
);
26 pthread_cond_destroy(&_c
);
29 int Wait(Mutex
&mutex
) {
30 // make sure this cond is used with one mutex only
31 assert(waiter_mutex
== NULL
|| waiter_mutex
== &mutex
);
32 waiter_mutex
= &mutex
;
34 assert(mutex
.is_locked());
37 int r
= pthread_cond_wait(&_c
, &mutex
._m
);
42 int WaitUntil(Mutex
&mutex
, utime_t when
) {
43 // make sure this cond is used with one mutex only
44 assert(waiter_mutex
== NULL
|| waiter_mutex
== &mutex
);
45 waiter_mutex
= &mutex
;
47 assert(mutex
.is_locked());
50 when
.to_timespec(&ts
);
53 int r
= pthread_cond_timedwait(&_c
, &mutex
._m
, &ts
);
59 int WaitInterval(Mutex
&mutex
, utime_t interval
) {
60 utime_t when
= ceph_clock_now();
62 return WaitUntil(mutex
, when
);
65 template<typename Duration
>
66 int WaitInterval(Mutex
&mutex
, Duration interval
) {
67 ceph::real_time
when(ceph::real_clock::now());
70 struct timespec ts
= ceph::real_clock::to_timespec(when
);
73 int r
= pthread_cond_timedwait(&_c
, &mutex
._m
, &ts
);
80 int r
= pthread_cond_broadcast(&_c
);
84 // make sure signaler is holding the waiter's lock.
85 assert(waiter_mutex
== NULL
||
86 waiter_mutex
->is_locked());
88 int r
= pthread_cond_broadcast(&_c
);
92 // make sure signaler is holding the waiter's lock.
93 assert(waiter_mutex
== NULL
||
94 waiter_mutex
->is_locked());
96 int r
= pthread_cond_signal(&_c
);
100 // make sure signaler is holding the waiter's lock.
101 assert(waiter_mutex
== NULL
||
102 waiter_mutex
->is_locked());
104 int r
= pthread_cond_broadcast(&_c
);
109 #endif // CEPH_COND_VAR_H