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() : |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +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 { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 29 | assert(pthread_mutex_init(&_pthread_mutex, NULL) == 0); |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 30 | mutexInitialized = true; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 31 | assert(pthread_cond_init(&_pthread_cond, NULL) == 0); |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 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 | f5f2be4 | 2006-09-05 21:05:31 +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 | f5f2be4 | 2006-09-05 21:05:31 +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) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +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 | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame^] | 54 | int result = pthread_cond_timedwait(&_pthread_cond, &_pthread_mutex, &abstime); |
55 | if (result == ETIMEDOUT) { | ||||
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 56 | assert(Util::currentTime() >= (now + timeout)); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 57 | } |
58 | } | ||||
59 | } | ||||
60 | |||||
61 | void notify() { | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 62 | // XXX Need to assert that caller owns mutex |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 63 | assert(pthread_cond_signal(&_pthread_cond) == 0); |
64 | } | ||||
65 | |||||
66 | void notifyAll() { | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 67 | // XXX Need to assert that caller owns mutex |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 68 | assert(pthread_cond_broadcast(&_pthread_cond) == 0); |
69 | } | ||||
70 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame^] | 71 | private: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 72 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 73 | void cleanup() { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame^] | 74 | if (mutexInitialized) { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 75 | mutexInitialized = false; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 76 | assert(pthread_mutex_destroy(&_pthread_mutex) == 0); |
77 | } | ||||
78 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame^] | 79 | if (condInitialized) { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 80 | condInitialized = false; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 81 | assert(pthread_cond_destroy(&_pthread_cond) == 0); |
82 | } | ||||
83 | } | ||||
84 | |||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 85 | mutable pthread_mutex_t _pthread_mutex; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 86 | mutable bool mutexInitialized; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 87 | mutable pthread_cond_t _pthread_cond; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 88 | mutable bool condInitialized; |
89 | }; | ||||
90 | |||||
91 | Monitor::Monitor() : _impl(new Monitor::Impl()) {} | ||||
92 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame^] | 93 | Monitor::~Monitor() { delete _impl; } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 94 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame^] | 95 | void Monitor::lock() const { _impl->lock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 96 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame^] | 97 | void Monitor::unlock() const { _impl->unlock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 98 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame^] | 99 | void Monitor::wait(long long timeout) const { _impl->wait(timeout); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 100 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame^] | 101 | void Monitor::notify() const { _impl->notify(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 102 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame^] | 103 | void Monitor::notifyAll() const { _impl->notifyAll(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 104 | |
105 | }}} // facebook::thrift::concurrency |