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 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 71 | void wait(int64_t timeout) const { |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 72 | assert(mutex_); |
| 73 | pthread_mutex_t* mutexImpl = |
| 74 | reinterpret_cast<pthread_mutex_t*>(mutex_->getUnderlyingImpl()); |
| 75 | assert(mutexImpl); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 76 | |
| 77 | // XXX Need to assert that caller owns mutex |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 78 | assert(timeout >= 0LL); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 79 | if (timeout == 0LL) { |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 80 | int iret = pthread_cond_wait(&pthread_cond_, mutexImpl); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 81 | assert(iret == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 82 | } else { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 83 | struct timespec abstime; |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 84 | int64_t now = Util::currentTime(); |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 85 | Util::toTimespec(abstime, now + timeout); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 86 | int result = pthread_cond_timedwait(&pthread_cond_, |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 87 | mutexImpl, |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 88 | &abstime); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 89 | if (result == ETIMEDOUT) { |
David Reiss | 428d569 | 2008-12-02 02:22:01 +0000 | [diff] [blame] | 90 | // pthread_cond_timedwait has been observed to return early on |
| 91 | // various platforms, so comment out this assert. |
| 92 | //assert(Util::currentTime() >= (now + timeout)); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 93 | throw TimedOutException(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void notify() { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 99 | // XXX Need to assert that caller owns mutex |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 100 | int iret = pthread_cond_signal(&pthread_cond_); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 101 | assert(iret == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void notifyAll() { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 105 | // XXX Need to assert that caller owns mutex |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 106 | int iret = pthread_cond_broadcast(&pthread_cond_); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 107 | assert(iret == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 110 | private: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 111 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 112 | void init(Mutex* mutex) { |
| 113 | mutex_ = mutex; |
| 114 | |
| 115 | if (pthread_cond_init(&pthread_cond_, NULL) == 0) { |
| 116 | condInitialized_ = true; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 117 | } |
| 118 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 119 | if (!condInitialized_) { |
| 120 | cleanup(); |
| 121 | throw SystemResourceException(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void cleanup() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 126 | if (condInitialized_) { |
| 127 | condInitialized_ = false; |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 128 | int iret = pthread_cond_destroy(&pthread_cond_); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 129 | assert(iret == 0); |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 133 | scoped_ptr<Mutex> ownedMutex_; |
| 134 | Mutex* mutex_; |
| 135 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 136 | mutable pthread_cond_t pthread_cond_; |
| 137 | mutable bool condInitialized_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 140 | Monitor::Monitor() : impl_(new Monitor::Impl()) {} |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 141 | Monitor::Monitor(Mutex* mutex) : impl_(new Monitor::Impl(mutex)) {} |
| 142 | Monitor::Monitor(Monitor* monitor) : impl_(new Monitor::Impl(monitor)) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 143 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 144 | Monitor::~Monitor() { delete impl_; } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 145 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 146 | Mutex& Monitor::mutex() const { return impl_->mutex(); } |
| 147 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 148 | void Monitor::lock() const { impl_->lock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 149 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 150 | void Monitor::unlock() const { impl_->unlock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 151 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 152 | void Monitor::wait(int64_t timeout) const { impl_->wait(timeout); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 153 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 154 | void Monitor::notify() const { impl_->notify(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 155 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 156 | void Monitor::notifyAll() const { impl_->notifyAll(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 157 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 158 | }}} // apache::thrift::concurrency |