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 #include "chrome/common/net/thread_blocker.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/task.h" | |
10 #include "base/thread.h" | |
11 #include "base/waitable_event.h" | |
12 | |
13 // Since a ThreadBlocker is outlived by its target thread, we don't | |
14 // have to ref-count it. | |
15 DISABLE_RUNNABLE_METHOD_REFCOUNT(chrome_common_net::ThreadBlocker); | |
16 | |
17 namespace chrome_common_net { | |
18 | |
19 ThreadBlocker::ThreadBlocker(base::Thread* target_thread) | |
20 : target_message_loop_(target_thread->message_loop()), | |
21 is_blocked_(false, false), is_finished_blocking_(false, false), | |
22 is_unblocked_(false, false) { | |
23 DCHECK(target_message_loop_); | |
24 } | |
25 | |
26 void ThreadBlocker::Block() { | |
27 DCHECK_NE(MessageLoop::current(), target_message_loop_); | |
28 target_message_loop_->PostTask( | |
29 FROM_HERE, | |
30 NewRunnableMethod(this, &ThreadBlocker::BlockOnTargetThread)); | |
31 while (!is_blocked_.Wait()) {} | |
32 } | |
33 | |
34 void ThreadBlocker::Unblock() { | |
35 DCHECK_NE(MessageLoop::current(), target_message_loop_); | |
36 is_finished_blocking_.Signal(); | |
37 while (!is_unblocked_.Wait()) {} | |
38 } | |
39 | |
40 void ThreadBlocker::BlockOnTargetThread() { | |
41 DCHECK_EQ(MessageLoop::current(), target_message_loop_); | |
42 is_blocked_.Signal(); | |
43 while (!is_finished_blocking_.Wait()) {} | |
44 is_unblocked_.Signal(); | |
45 } | |
46 | |
47 } // namespace chrome_common_net | |
OLD | NEW |