David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 20 | #include "Monitor.h" |
| 21 | #include "Exception.h" |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 22 | #include "Util.h" |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 23 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 24 | #include <boost/scoped_ptr.hpp> |
| 25 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 26 | #include <assert.h> |
| 27 | #include <errno.h> |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 28 | |
| 29 | #include <iostream> |
| 30 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 31 | #include <pthread.h> |
| 32 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 33 | namespace apache { namespace thrift { namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 34 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 35 | using boost::scoped_ptr; |
| 36 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 37 | /** |
| 38 | * Monitor implementation using the POSIX pthread library |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 39 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 40 | * @version $Id:$ |
| 41 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 42 | class Monitor::Impl { |
| 43 | |
| 44 | public: |
| 45 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 46 | Impl() |
| 47 | : ownedMutex_(new Mutex()), |
| 48 | mutex_(NULL), |
| 49 | condInitialized_(false) { |
| 50 | init(ownedMutex_.get()); |
| 51 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 52 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 53 | Impl(Mutex* mutex) |
| 54 | : mutex_(NULL), |
| 55 | condInitialized_(false) { |
| 56 | init(mutex); |
| 57 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 58 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 59 | Impl(Monitor* monitor) |
| 60 | : mutex_(NULL), |
| 61 | condInitialized_(false) { |
| 62 | init(&(monitor->mutex())); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 65 | ~Impl() { cleanup(); } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 66 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 67 | Mutex& mutex() { return *mutex_; } |
| 68 | void lock() { mutex().lock(); } |
| 69 | void unlock() { mutex().unlock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 70 | |
Bryan Duxbury | 3746b29 | 2011-08-24 00:35:52 +0000 | [diff] [blame] | 71 | /** |
| 72 | * Exception-throwing version of waitForTimeRelative(), called simply |
| 73 | * wait(int64) for historical reasons. Timeout is in milliseconds. |
| 74 | * |
| 75 | * If the condition occurs, this function returns cleanly; on timeout or |
| 76 | * error an exception is thrown. |
| 77 | */ |
| 78 | void wait(int64_t timeout_ms) const { |
| 79 | int result = waitForTimeRelative(timeout_ms); |
| 80 | if (result == ETIMEDOUT) { |
| 81 | // pthread_cond_timedwait has been observed to return early on |
| 82 | // various platforms, so comment out this assert. |
| 83 | //assert(Util::currentTime() >= (now + timeout)); |
| 84 | throw TimedOutException(); |
| 85 | } else if (result != 0) { |
| 86 | throw TException( |
| 87 | "pthread_cond_wait() or pthread_cond_timedwait() failed"); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Waits until the specified timeout in milliseconds for the condition to |
| 93 | * occur, or waits forever if timeout_ms == 0. |
| 94 | * |
| 95 | * Returns 0 if condition occurs, ETIMEDOUT on timeout, or an error code. |
| 96 | */ |
| 97 | int waitForTimeRelative(int64_t timeout_ms) const { |
| 98 | if (timeout_ms == 0LL) { |
| 99 | return waitForever(); |
| 100 | } |
| 101 | |
| 102 | struct timespec abstime; |
| 103 | Util::toTimespec(abstime, Util::currentTime() + timeout_ms); |
| 104 | return waitForTime(&abstime); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Waits until the absolute time specified using struct timespec. |
| 109 | * Returns 0 if condition occurs, ETIMEDOUT on timeout, or an error code. |
| 110 | */ |
| 111 | int waitForTime(const timespec* abstime) const { |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 112 | assert(mutex_); |
| 113 | pthread_mutex_t* mutexImpl = |
| 114 | reinterpret_cast<pthread_mutex_t*>(mutex_->getUnderlyingImpl()); |
| 115 | assert(mutexImpl); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 116 | |
| 117 | // XXX Need to assert that caller owns mutex |
Bryan Duxbury | 3746b29 | 2011-08-24 00:35:52 +0000 | [diff] [blame] | 118 | return pthread_cond_timedwait(&pthread_cond_, |
| 119 | mutexImpl, |
| 120 | abstime); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Bryan Duxbury | 3746b29 | 2011-08-24 00:35:52 +0000 | [diff] [blame] | 123 | /** |
| 124 | * Waits forever until the condition occurs. |
| 125 | * Returns 0 if condition occurs, or an error code otherwise. |
| 126 | */ |
| 127 | int waitForever() const { |
| 128 | assert(mutex_); |
| 129 | pthread_mutex_t* mutexImpl = |
| 130 | reinterpret_cast<pthread_mutex_t*>(mutex_->getUnderlyingImpl()); |
| 131 | assert(mutexImpl); |
| 132 | return pthread_cond_wait(&pthread_cond_, mutexImpl); |
| 133 | } |
| 134 | |
| 135 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 136 | void notify() { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 137 | // XXX Need to assert that caller owns mutex |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 138 | int iret = pthread_cond_signal(&pthread_cond_); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 139 | assert(iret == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void notifyAll() { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 143 | // XXX Need to assert that caller owns mutex |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 144 | int iret = pthread_cond_broadcast(&pthread_cond_); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 145 | assert(iret == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 148 | private: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 149 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 150 | void init(Mutex* mutex) { |
| 151 | mutex_ = mutex; |
| 152 | |
| 153 | if (pthread_cond_init(&pthread_cond_, NULL) == 0) { |
| 154 | condInitialized_ = true; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 155 | } |
| 156 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 157 | if (!condInitialized_) { |
| 158 | cleanup(); |
| 159 | throw SystemResourceException(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void cleanup() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 164 | if (condInitialized_) { |
| 165 | condInitialized_ = false; |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 166 | int iret = pthread_cond_destroy(&pthread_cond_); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 167 | assert(iret == 0); |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 171 | scoped_ptr<Mutex> ownedMutex_; |
| 172 | Mutex* mutex_; |
| 173 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 174 | mutable pthread_cond_t pthread_cond_; |
| 175 | mutable bool condInitialized_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 178 | Monitor::Monitor() : impl_(new Monitor::Impl()) {} |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 179 | Monitor::Monitor(Mutex* mutex) : impl_(new Monitor::Impl(mutex)) {} |
| 180 | Monitor::Monitor(Monitor* monitor) : impl_(new Monitor::Impl(monitor)) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 181 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 182 | Monitor::~Monitor() { delete impl_; } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 183 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 184 | Mutex& Monitor::mutex() const { return impl_->mutex(); } |
| 185 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 186 | void Monitor::lock() const { impl_->lock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 187 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 188 | void Monitor::unlock() const { impl_->unlock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 189 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 190 | void Monitor::wait(int64_t timeout) const { impl_->wait(timeout); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 191 | |
Bryan Duxbury | 3746b29 | 2011-08-24 00:35:52 +0000 | [diff] [blame] | 192 | int Monitor::waitForTime(const timespec* abstime) const { |
| 193 | return impl_->waitForTime(abstime); |
| 194 | } |
| 195 | |
| 196 | int Monitor::waitForTimeRelative(int64_t timeout_ms) const { |
| 197 | return impl_->waitForTimeRelative(timeout_ms); |
| 198 | } |
| 199 | |
| 200 | int Monitor::waitForever() const { |
| 201 | return impl_->waitForever(); |
| 202 | } |
| 203 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 204 | void Monitor::notify() const { impl_->notify(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 205 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 206 | void Monitor::notifyAll() const { impl_->notifyAll(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 207 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 208 | }}} // apache::thrift::concurrency |