| OLD | NEW |
| 1 // Copyright 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CC_BASE_COMPLETION_EVENT_H_ | 5 #ifndef CC_BASE_COMPLETION_EVENT_H_ |
| 6 #define CC_BASE_COMPLETION_EVENT_H_ | 6 #define CC_BASE_COMPLETION_EVENT_H_ |
| 7 | 7 |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| 11 | 11 |
| 12 namespace cc { | 12 namespace cc { |
| 13 | 13 |
| 14 // Used for making blocking calls from one thread to another. Use only when | 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. | 15 // absolutely certain that doing-so will not lead to a deadlock. |
| 16 // | 16 // |
| 17 // It is safe to destroy this object as soon as Wait() returns. | 17 // It is safe to destroy this object as soon as Wait() returns. |
| 18 class CompletionEvent { | 18 class CompletionEvent { |
| 19 public: | 19 public: |
| 20 CompletionEvent() | 20 CompletionEvent() |
| 21 : event_(false /* manual_reset */, false /* initially_signaled */) { | 21 : event_(false /* manual_reset */, false /* initially_signaled */) { |
| 22 #if DCHECK_IS_ON | 22 #if DCHECK_IS_ON() |
| 23 waited_ = false; | 23 waited_ = false; |
| 24 signaled_ = false; | 24 signaled_ = false; |
| 25 #endif | 25 #endif |
| 26 } | 26 } |
| 27 | 27 |
| 28 ~CompletionEvent() { | 28 ~CompletionEvent() { |
| 29 #if DCHECK_IS_ON | 29 #if DCHECK_IS_ON() |
| 30 DCHECK(waited_); | 30 DCHECK(waited_); |
| 31 DCHECK(signaled_); | 31 DCHECK(signaled_); |
| 32 #endif | 32 #endif |
| 33 } | 33 } |
| 34 | 34 |
| 35 void Wait() { | 35 void Wait() { |
| 36 #if DCHECK_IS_ON | 36 #if DCHECK_IS_ON() |
| 37 DCHECK(!waited_); | 37 DCHECK(!waited_); |
| 38 waited_ = true; | 38 waited_ = true; |
| 39 #endif | 39 #endif |
| 40 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 40 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 41 event_.Wait(); | 41 event_.Wait(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void Signal() { | 44 void Signal() { |
| 45 #if DCHECK_IS_ON | 45 #if DCHECK_IS_ON() |
| 46 DCHECK(!signaled_); | 46 DCHECK(!signaled_); |
| 47 signaled_ = true; | 47 signaled_ = true; |
| 48 #endif | 48 #endif |
| 49 event_.Signal(); | 49 event_.Signal(); |
| 50 } | 50 } |
| 51 | 51 |
| 52 private: | 52 private: |
| 53 base::WaitableEvent event_; | 53 base::WaitableEvent event_; |
| 54 #if DCHECK_IS_ON | 54 #if DCHECK_IS_ON() |
| 55 // Used to assert that Wait() and Signal() are each called exactly once. | 55 // Used to assert that Wait() and Signal() are each called exactly once. |
| 56 bool waited_; | 56 bool waited_; |
| 57 bool signaled_; | 57 bool signaled_; |
| 58 #endif | 58 #endif |
| 59 }; | 59 }; |
| 60 | 60 |
| 61 } // namespace cc | 61 } // namespace cc |
| 62 | 62 |
| 63 #endif // CC_BASE_COMPLETION_EVENT_H_ | 63 #endif // CC_BASE_COMPLETION_EVENT_H_ |
| OLD | NEW |