OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_DART_EMBEDDER_MONITOR_H_ | |
6 #define MOJO_DART_EMBEDDER_MONITOR_H_ | |
7 | |
8 #include "base/synchronization/condition_variable.h" | |
9 #include "base/synchronization/lock.h" | |
10 | |
11 namespace mojo { | |
12 namespace dart { | |
13 | |
14 class Monitor { | |
15 public: | |
16 Monitor() { | |
17 lock_ = new base::Lock(); | |
18 condition_variable_ = new base::ConditionVariable(lock_); | |
19 } | |
20 | |
21 ~Monitor() { | |
22 delete condition_variable_; | |
23 delete lock_; | |
24 } | |
25 | |
26 void Enter() { | |
27 lock_->Acquire(); | |
28 } | |
29 | |
30 void Exit() { | |
31 lock_->Release(); | |
32 } | |
33 | |
34 void Notify() { | |
35 condition_variable_->Signal(); | |
36 } | |
37 | |
38 void Wait() { | |
39 condition_variable_->Wait(); | |
40 } | |
41 | |
42 private: | |
43 base::Lock* lock_; | |
44 base::ConditionVariable* condition_variable_; | |
45 DISALLOW_COPY_AND_ASSIGN(Monitor); | |
46 }; | |
47 | |
48 class MonitorLocker { | |
49 public: | |
50 explicit MonitorLocker(Monitor* monitor) : monitor_(monitor) { | |
51 CHECK(monitor_); | |
52 monitor_->Enter(); | |
53 } | |
54 | |
55 virtual ~MonitorLocker() { | |
56 monitor_->Exit(); | |
57 } | |
58 | |
59 void Wait() { | |
60 return monitor_->Wait(); | |
61 } | |
62 | |
63 void Notify() { | |
64 monitor_->Notify(); | |
65 } | |
66 | |
67 private: | |
68 Monitor* const monitor_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(MonitorLocker); | |
71 }; | |
72 | |
73 } // namespace dart | |
74 } // namespace mojo | |
75 | |
76 | |
77 #endif // MOJO_DART_EMBEDDER_MONITOR_H_ | |
OLD | NEW |