Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 1 | #include "Monitor.h" |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 2 | #include "Exception.h" |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 3 | #include "Util.h" |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 4 | |
5 | #include <assert.h> | ||||
6 | #include <errno.h> | ||||
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 7 | |
8 | #include <iostream> | ||||
9 | |||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 10 | #include <pthread.h> |
11 | |||||
12 | namespace facebook { namespace thrift { namespace concurrency { | ||||
13 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 14 | /** |
15 | * Monitor implementation using the POSIX pthread library | ||||
16 | * | ||||
17 | * @author marc | ||||
18 | * @version $Id:$ | ||||
19 | */ | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 20 | class Monitor::Impl { |
21 | |||||
22 | public: | ||||
23 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 24 | Impl() : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 25 | mutexInitialized_(false), |
26 | condInitialized_(false) { | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 27 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 28 | try { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 29 | assert(pthread_mutex_init(&pthread_mutex_, NULL) == 0); |
30 | mutexInitialized_ = true; | ||||
31 | assert(pthread_cond_init(&pthread_cond_, NULL) == 0); | ||||
32 | condInitialized_ = true; | ||||
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 33 | } catch(...) { |
34 | cleanup(); | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 35 | } |
36 | } | ||||
37 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 38 | ~Impl() { cleanup(); } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 39 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 40 | void lock() const { pthread_mutex_lock(&pthread_mutex_); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 41 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 42 | void unlock() const { pthread_mutex_unlock(&pthread_mutex_); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 43 | |
44 | void wait(long long timeout) const { | ||||
45 | |||||
46 | // XXX Need to assert that caller owns mutex | ||||
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 47 | assert(timeout >= 0LL); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 48 | if (timeout == 0LL) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 49 | assert(pthread_cond_wait(&pthread_cond_, &pthread_mutex_) == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 50 | } else { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 51 | struct timespec abstime; |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 52 | long long now = Util::currentTime(); |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 53 | Util::toTimespec(abstime, now + timeout); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 54 | int result = pthread_cond_timedwait(&pthread_cond_, |
55 | &pthread_mutex_, | ||||
56 | &abstime); | ||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 57 | if (result == ETIMEDOUT) { |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 58 | assert(Util::currentTime() >= (now + timeout)); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 59 | } |
60 | } | ||||
61 | } | ||||
62 | |||||
63 | void notify() { | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 64 | // XXX Need to assert that caller owns mutex |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 65 | assert(pthread_cond_signal(&pthread_cond_) == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 66 | } |
67 | |||||
68 | void notifyAll() { | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 69 | // XXX Need to assert that caller owns mutex |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 70 | assert(pthread_cond_broadcast(&pthread_cond_) == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 71 | } |
72 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 73 | private: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 74 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 75 | void cleanup() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 76 | if (mutexInitialized_) { |
77 | mutexInitialized_ = false; | ||||
78 | assert(pthread_mutex_destroy(&pthread_mutex_) == 0); | ||||
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 79 | } |
80 | |||||
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 81 | if (condInitialized_) { |
82 | condInitialized_ = false; | ||||
83 | assert(pthread_cond_destroy(&pthread_cond_) == 0); | ||||
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 84 | } |
85 | } | ||||
86 | |||||
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 87 | mutable pthread_mutex_t pthread_mutex_; |
88 | mutable bool mutexInitialized_; | ||||
89 | mutable pthread_cond_t pthread_cond_; | ||||
90 | mutable bool condInitialized_; | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 91 | }; |
92 | |||||
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 93 | Monitor::Monitor() : impl_(new Monitor::Impl()) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 94 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 95 | Monitor::~Monitor() { delete impl_; } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 96 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 97 | void Monitor::lock() const { impl_->lock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 98 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 99 | void Monitor::unlock() const { impl_->unlock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 100 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 101 | void Monitor::wait(long long timeout) const { impl_->wait(timeout); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 102 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 103 | void Monitor::notify() const { impl_->notify(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 104 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 105 | void Monitor::notifyAll() const { impl_->notifyAll(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 106 | |
107 | }}} // facebook::thrift::concurrency |