OLD | NEW |
| (Empty) |
1 // Copyright 2011 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 CC_COMPLETION_EVENT_H_ | |
6 #define CC_COMPLETION_EVENT_H_ | |
7 | |
8 #include "base/synchronization/waitable_event.h" | |
9 #include "base/threading/thread_restrictions.h" | |
10 #include "base/logging.h" | |
11 | |
12 namespace cc { | |
13 | |
14 // Used for making blocking calls from one thread to another. Use only when | |
15 // absolutely certain that doing-so will not lead to a deadlock. | |
16 // | |
17 // It is safe to destroy this object as soon as wait() returns. | |
18 class CompletionEvent { | |
19 public: | |
20 CompletionEvent() | |
21 : m_event(false /* manual_reset */, false /* initially_signaled */) | |
22 { | |
23 #ifndef NDEBUG | |
24 m_waited = false; | |
25 m_signaled = false; | |
26 #endif | |
27 } | |
28 | |
29 ~CompletionEvent() | |
30 { | |
31 #ifndef NDEBUG | |
32 DCHECK(m_waited); | |
33 DCHECK(m_signaled); | |
34 #endif | |
35 } | |
36 | |
37 void wait() | |
38 { | |
39 #ifndef NDEBUG | |
40 DCHECK(!m_waited); | |
41 m_waited = true; | |
42 #endif | |
43 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
44 m_event.Wait(); | |
45 } | |
46 | |
47 void signal() | |
48 { | |
49 #ifndef NDEBUG | |
50 DCHECK(!m_signaled); | |
51 m_signaled = true; | |
52 #endif | |
53 m_event.Signal(); | |
54 } | |
55 | |
56 private: | |
57 base::WaitableEvent m_event; | |
58 #ifndef NDEBUG | |
59 // Used to assert that wait() and signal() are each called exactly once. | |
60 bool m_waited; | |
61 bool m_signaled; | |
62 #endif | |
63 }; | |
64 | |
65 } | |
66 | |
67 #endif // CC_COMPLETION_EVENT_H_ | |
OLD | NEW |