| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 CHROME_COMMON_NET_THREAD_BLOCKER_H_ | |
| 6 #define CHROME_COMMON_NET_THREAD_BLOCKER_H_ | |
| 7 | |
| 8 // This class (mainly used for testing) lets you block and unblock a | |
| 9 // thread at will. | |
| 10 // | |
| 11 // TODO(akalin): Consider moving this to base/ as this class is not | |
| 12 // sync-specific (ask darin about it). | |
| 13 | |
| 14 #include "base/basictypes.h" | |
| 15 #include "base/waitable_event.h" | |
| 16 | |
| 17 class MessageLoop; | |
| 18 | |
| 19 namespace base { | |
| 20 class Thread; | |
| 21 } // namespace base | |
| 22 | |
| 23 namespace chrome_common_net { | |
| 24 | |
| 25 class ThreadBlocker { | |
| 26 public: | |
| 27 // The given thread must already be started and it must outlive this | |
| 28 // instance. | |
| 29 explicit ThreadBlocker(base::Thread* target_thread); | |
| 30 | |
| 31 // When this function returns, the target thread will be blocked | |
| 32 // until Unblock() is called. Each call to Block() must be matched | |
| 33 // by a call to Unblock(). | |
| 34 void Block(); | |
| 35 | |
| 36 // When this function returns, the target thread is unblocked. | |
| 37 void Unblock(); | |
| 38 | |
| 39 private: | |
| 40 // On the target thread, blocks until Unblock() is called. | |
| 41 void BlockOnTargetThread(); | |
| 42 | |
| 43 MessageLoop* const target_message_loop_; | |
| 44 base::WaitableEvent is_blocked_, is_finished_blocking_, is_unblocked_; | |
| 45 | |
| 46 DISALLOW_COPY_AND_ASSIGN(ThreadBlocker); | |
| 47 }; | |
| 48 | |
| 49 } // namespace chrome_common_net | |
| 50 | |
| 51 #endif // CHROME_COMMON_NET_THREAD_BLOCKER_H_ | |
| OLD | NEW |