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 DCHECK_EQ(this, current()); |
| 67 g_tls_current_pump.Pointer()->Set(NULL); |
58 } | 68 } |
59 | 69 |
60 // static | 70 // static |
61 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { | 71 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { |
62 return scoped_ptr<MessagePump>(new MessagePumpMojo()); | 72 return scoped_ptr<MessagePump>(new MessagePumpMojo()); |
63 } | 73 } |
64 | 74 |
| 75 // static |
| 76 MessagePumpMojo* MessagePumpMojo::current() { |
| 77 return g_tls_current_pump.Pointer()->Get(); |
| 78 } |
| 79 |
65 void MessagePumpMojo::AddHandler(MessagePumpMojoHandler* handler, | 80 void MessagePumpMojo::AddHandler(MessagePumpMojoHandler* handler, |
66 const Handle& handle, | 81 const Handle& handle, |
67 MojoHandleSignals wait_signals, | 82 MojoHandleSignals wait_signals, |
68 base::TimeTicks deadline) { | 83 base::TimeTicks deadline) { |
69 CHECK(handler); | 84 CHECK(handler); |
70 DCHECK(handle.is_valid()); | 85 DCHECK(handle.is_valid()); |
71 // Assume it's an error if someone tries to reregister an existing handle. | 86 // Assume it's an error if someone tries to reregister an existing handle. |
72 CHECK_EQ(0u, handlers_.count(handle)); | 87 CHECK_EQ(0u, handlers_.count(handle)); |
73 Handler handler_data; | 88 Handler handler_data; |
74 handler_data.handler = handler; | 89 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(); | 269 for (HandleToHandler::const_iterator i = handlers_.begin(); |
255 i != handlers_.end(); ++i) { | 270 i != handlers_.end(); ++i) { |
256 deadline = std::min( | 271 deadline = std::min( |
257 TimeTicksToMojoDeadline(i->second.deadline, now), deadline); | 272 TimeTicksToMojoDeadline(i->second.deadline, now), deadline); |
258 } | 273 } |
259 return deadline; | 274 return deadline; |
260 } | 275 } |
261 | 276 |
262 } // namespace common | 277 } // namespace common |
263 } // namespace mojo | 278 } // namespace mojo |
OLD | NEW |