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