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 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 12 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 13 | namespace facebook { namespace thrift { namespace concurrency { |
| 14 | |
| 15 | /** Monitor implementation using the POSIX pthread library |
| 16 | |
| 17 | @author marc |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 18 | @version $Id:$ */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 19 | |
| 20 | class Monitor::Impl { |
| 21 | |
| 22 | public: |
| 23 | |
| 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 | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 29 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 30 | assert(pthread_mutex_init(&_pthread_mutex, NULL) == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 31 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 32 | mutexInitialized = true; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 33 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 34 | assert(pthread_cond_init(&_pthread_cond, NULL) == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 35 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 36 | condInitialized = true; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 37 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 38 | } catch(...) { |
| 39 | cleanup(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 43 | ~Impl() {cleanup();} |
| 44 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 45 | void lock() const {pthread_mutex_lock(&_pthread_mutex);} |
| 46 | |
| 47 | void unlock() const {pthread_mutex_unlock(&_pthread_mutex);} |
| 48 | |
| 49 | void wait(long long timeout) const { |
| 50 | |
| 51 | // XXX Need to assert that caller owns mutex |
| 52 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 53 | assert(timeout >= 0LL); |
| 54 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 55 | if(timeout == 0LL) { |
| 56 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 57 | assert(pthread_cond_wait(&_pthread_cond, &_pthread_mutex) == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 58 | |
| 59 | } else { |
| 60 | |
| 61 | struct timespec abstime; |
| 62 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 63 | long long now = Util::currentTime(); |
| 64 | |
| 65 | Util::toTimespec(abstime, now + timeout); |
| 66 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 67 | int result = pthread_cond_timedwait(&_pthread_cond, &_pthread_mutex, &abstime); |
| 68 | |
| 69 | if(result == ETIMEDOUT) { |
| 70 | |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 71 | assert(Util::currentTime() >= (now + timeout)); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void notify() { |
| 77 | |
| 78 | // XXX Need to assert that caller owns mutex |
| 79 | |
| 80 | assert(pthread_cond_signal(&_pthread_cond) == 0); |
| 81 | } |
| 82 | |
| 83 | void notifyAll() { |
| 84 | |
| 85 | // XXX Need to assert that caller owns mutex |
| 86 | |
| 87 | assert(pthread_cond_broadcast(&_pthread_cond) == 0); |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 92 | void cleanup() { |
| 93 | |
| 94 | if(mutexInitialized) { |
| 95 | |
| 96 | mutexInitialized = false; |
| 97 | |
| 98 | assert(pthread_mutex_destroy(&_pthread_mutex) == 0); |
| 99 | } |
| 100 | |
| 101 | if(condInitialized) { |
| 102 | |
| 103 | condInitialized = false; |
| 104 | |
| 105 | assert(pthread_cond_destroy(&_pthread_cond) == 0); |
| 106 | } |
| 107 | } |
| 108 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 109 | mutable pthread_mutex_t _pthread_mutex; |
| 110 | |
| 111 | mutable bool mutexInitialized; |
| 112 | |
| 113 | mutable pthread_cond_t _pthread_cond; |
| 114 | |
| 115 | mutable bool condInitialized; |
| 116 | }; |
| 117 | |
| 118 | Monitor::Monitor() : _impl(new Monitor::Impl()) {} |
| 119 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 120 | Monitor::~Monitor() { delete _impl;} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 121 | |
| 122 | void Monitor::lock() const {_impl->lock();} |
| 123 | |
| 124 | void Monitor::unlock() const {_impl->unlock();} |
| 125 | |
| 126 | void Monitor::wait(long long timeout) const {_impl->wait(timeout);} |
| 127 | |
| 128 | void Monitor::notify() const {_impl->notify();} |
| 129 | |
| 130 | void Monitor::notifyAll() const {_impl->notifyAll();} |
| 131 | |
| 132 | }}} // facebook::thrift::concurrency |
| 133 | |