OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "mojo/common/message_pump_mojo.h" | 5 #include "mojo/common/message_pump_mojo.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
11 #include "base/lazy_instance.h" | |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/threading/thread_local.h" | |
12 #include "base/time/time.h" | 14 #include "base/time/time.h" |
13 #include "mojo/common/message_pump_mojo_handler.h" | 15 #include "mojo/common/message_pump_mojo_handler.h" |
14 #include "mojo/common/time_helper.h" | 16 #include "mojo/common/time_helper.h" |
15 | 17 |
16 namespace mojo { | 18 namespace mojo { |
17 namespace common { | 19 namespace common { |
18 namespace { | 20 namespace { |
19 | 21 |
22 base::LazyInstance<base::ThreadLocalPointer<MessagePumpMojo> >::Leaky | |
23 g_tls_current_pump = LAZY_INSTANCE_INITIALIZER; | |
24 | |
20 MojoDeadline TimeTicksToMojoDeadline(base::TimeTicks time_ticks, | 25 MojoDeadline TimeTicksToMojoDeadline(base::TimeTicks time_ticks, |
21 base::TimeTicks now) { | 26 base::TimeTicks now) { |
22 // The is_null() check matches that of HandleWatcher as well as how | 27 // The is_null() check matches that of HandleWatcher as well as how |
23 // |delayed_work_time| is used. | 28 // |delayed_work_time| is used. |
24 if (time_ticks.is_null()) | 29 if (time_ticks.is_null()) |
25 return MOJO_DEADLINE_INDEFINITE; | 30 return MOJO_DEADLINE_INDEFINITE; |
26 const int64_t delta = (time_ticks - now).InMicroseconds(); | 31 const int64_t delta = (time_ticks - now).InMicroseconds(); |
27 return delta < 0 ? static_cast<MojoDeadline>(0) : | 32 return delta < 0 ? static_cast<MojoDeadline>(0) : |
28 static_cast<MojoDeadline>(delta); | 33 static_cast<MojoDeadline>(delta); |
29 } | 34 } |
(...skipping 15 matching lines...) Expand all Loading... | |
45 base::TimeTicks delayed_work_time; | 50 base::TimeTicks delayed_work_time; |
46 | 51 |
47 // Used to wake up WaitForWork(). | 52 // Used to wake up WaitForWork(). |
48 ScopedMessagePipeHandle read_handle; | 53 ScopedMessagePipeHandle read_handle; |
49 ScopedMessagePipeHandle write_handle; | 54 ScopedMessagePipeHandle write_handle; |
50 | 55 |
51 bool should_quit; | 56 bool should_quit; |
52 }; | 57 }; |
53 | 58 |
54 MessagePumpMojo::MessagePumpMojo() : run_state_(NULL), next_handler_id_(0) { | 59 MessagePumpMojo::MessagePumpMojo() : run_state_(NULL), next_handler_id_(0) { |
60 DCHECK(!current()) | |
61 << "There is already a MessagePumpMojo instance on this thread."; | |
62 g_tls_current_pump.Pointer()->Set(this); | |
55 } | 63 } |
56 | 64 |
57 MessagePumpMojo::~MessagePumpMojo() { | 65 MessagePumpMojo::~MessagePumpMojo() { |
66 g_tls_current_pump.Pointer()->Set(NULL); | |
sky
2014/08/27 21:37:27
DCHECK that this is the current pointer.
yzshen1
2014/08/27 22:31:48
Done.
| |
58 } | 67 } |
59 | 68 |
60 // static | 69 // static |
61 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { | 70 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { |
62 return scoped_ptr<MessagePump>(new MessagePumpMojo()); | 71 return scoped_ptr<MessagePump>(new MessagePumpMojo()); |
63 } | 72 } |
64 | 73 |
74 // static | |
75 MessagePumpMojo* MessagePumpMojo::current() { | |
76 return g_tls_current_pump.Pointer()->Get(); | |
77 } | |
78 | |
65 void MessagePumpMojo::AddHandler(MessagePumpMojoHandler* handler, | 79 void MessagePumpMojo::AddHandler(MessagePumpMojoHandler* handler, |
66 const Handle& handle, | 80 const Handle& handle, |
67 MojoHandleSignals wait_signals, | 81 MojoHandleSignals wait_signals, |
68 base::TimeTicks deadline) { | 82 base::TimeTicks deadline) { |
69 CHECK(handler); | 83 CHECK(handler); |
70 DCHECK(handle.is_valid()); | 84 DCHECK(handle.is_valid()); |
71 // Assume it's an error if someone tries to reregister an existing handle. | 85 // Assume it's an error if someone tries to reregister an existing handle. |
72 CHECK_EQ(0u, handlers_.count(handle)); | 86 CHECK_EQ(0u, handlers_.count(handle)); |
73 Handler handler_data; | 87 Handler handler_data; |
74 handler_data.handler = handler; | 88 handler_data.handler = handler; |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 for (HandleToHandler::const_iterator i = handlers_.begin(); | 268 for (HandleToHandler::const_iterator i = handlers_.begin(); |
255 i != handlers_.end(); ++i) { | 269 i != handlers_.end(); ++i) { |
256 deadline = std::min( | 270 deadline = std::min( |
257 TimeTicksToMojoDeadline(i->second.deadline, now), deadline); | 271 TimeTicksToMojoDeadline(i->second.deadline, now), deadline); |
258 } | 272 } |
259 return deadline; | 273 return deadline; |
260 } | 274 } |
261 | 275 |
262 } // namespace common | 276 } // namespace common |
263 } // namespace mojo | 277 } // namespace mojo |
OLD | NEW |