| 1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
|---|---|
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #ifndef RUNTIME_BIN_LOCKERS_H_ |
| 6 | #define RUNTIME_BIN_LOCKERS_H_ |
| 7 | |
| 8 | #include "bin/thread.h" |
| 9 | #include "platform/assert.h" |
| 10 | |
| 11 | namespace dart { |
| 12 | namespace bin { |
| 13 | |
| 14 | class MutexLocker { |
| 15 | public: |
| 16 | explicit MutexLocker(Mutex* mutex) : mutex_(mutex) { |
| 17 | ASSERT(mutex != NULL); |
| 18 | mutex_->Lock(); |
| 19 | } |
| 20 | |
| 21 | virtual ~MutexLocker() { mutex_->Unlock(); } |
| 22 | |
| 23 | private: |
| 24 | Mutex* const mutex_; |
| 25 | |
| 26 | DISALLOW_COPY_AND_ASSIGN(MutexLocker); |
| 27 | }; |
| 28 | |
| 29 | class MonitorLocker { |
| 30 | public: |
| 31 | explicit MonitorLocker(Monitor* monitor) : monitor_(monitor) { |
| 32 | ASSERT(monitor != NULL); |
| 33 | monitor_->Enter(); |
| 34 | } |
| 35 | |
| 36 | virtual ~MonitorLocker() { monitor_->Exit(); } |
| 37 | |
| 38 | Monitor::WaitResult Wait(int64_t millis = Monitor::kNoTimeout) { |
| 39 | return monitor_->Wait(millis); |
| 40 | } |
| 41 | |
| 42 | void Notify() { monitor_->Notify(); } |
| 43 | |
| 44 | void NotifyAll() { monitor_->NotifyAll(); } |
| 45 | |
| 46 | private: |
| 47 | Monitor* const monitor_; |
| 48 | |
| 49 | DISALLOW_COPY_AND_ASSIGN(MonitorLocker); |
| 50 | }; |
| 51 | |
| 52 | } // namespace bin |
| 53 | } // namespace dart |
| 54 | |
| 55 | #endif // RUNTIME_BIN_LOCKERS_H_ |
| 56 |