| 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/public/utility/run_loop.h" | 5 #include "mojo/public/utility/run_loop.h" |
| 6 | 6 |
| 7 #include <assert.h> | 7 #include <assert.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "mojo/public/utility/lib/thread_local.h" |
| 12 #include "mojo/public/utility/run_loop_handler.h" | 13 #include "mojo/public/utility/run_loop_handler.h" |
| 13 #include "mojo/public/utility/thread_local.h" | |
| 14 | 14 |
| 15 namespace mojo { | 15 namespace mojo { |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 ThreadLocalPointer<RunLoop>* tls_run_loop = NULL; | 18 internal::ThreadLocalPointer<RunLoop> current_run_loop; |
| 19 | 19 |
| 20 const MojoTimeTicks kInvalidTimeTicks = static_cast<MojoTimeTicks>(0); | 20 const MojoTimeTicks kInvalidTimeTicks = static_cast<MojoTimeTicks>(0); |
| 21 | 21 |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 // State needed for one iteration of WaitMany(). | 24 // State needed for one iteration of WaitMany(). |
| 25 struct RunLoop::WaitState { | 25 struct RunLoop::WaitState { |
| 26 WaitState() : deadline(MOJO_DEADLINE_INDEFINITE) {} | 26 WaitState() : deadline(MOJO_DEADLINE_INDEFINITE) {} |
| 27 | 27 |
| 28 std::vector<Handle> handles; | 28 std::vector<Handle> handles; |
| 29 std::vector<MojoWaitFlags> wait_flags; | 29 std::vector<MojoWaitFlags> wait_flags; |
| 30 MojoDeadline deadline; | 30 MojoDeadline deadline; |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 struct RunLoop::RunState { | 33 struct RunLoop::RunState { |
| 34 RunState() : should_quit(false) {} | 34 RunState() : should_quit(false) {} |
| 35 | 35 |
| 36 bool should_quit; | 36 bool should_quit; |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 RunLoop::RunLoop() : run_state_(NULL), next_handler_id_(0) { | 39 RunLoop::RunLoop() : run_state_(NULL), next_handler_id_(0) { |
| 40 assert(tls_run_loop); | 40 assert(!current()); |
| 41 assert(!tls_run_loop->Get()); | 41 current_run_loop.Set(this); |
| 42 tls_run_loop->Set(this); | |
| 43 } | 42 } |
| 44 | 43 |
| 45 RunLoop::~RunLoop() { | 44 RunLoop::~RunLoop() { |
| 46 assert(tls_run_loop->Get() == this); | 45 assert(current() == this); |
| 47 tls_run_loop->Set(NULL); | 46 current_run_loop.Set(NULL); |
| 48 } | 47 } |
| 49 | 48 |
| 50 // static | 49 // static |
| 51 void RunLoop::SetUp() { | 50 void RunLoop::SetUp() { |
| 52 assert(!tls_run_loop); | 51 current_run_loop.Allocate(); |
| 53 tls_run_loop = new ThreadLocalPointer<RunLoop>; | |
| 54 } | 52 } |
| 55 | 53 |
| 56 // static | 54 // static |
| 57 void RunLoop::TearDown() { | 55 void RunLoop::TearDown() { |
| 58 assert(!current()); | 56 assert(!current()); |
| 59 assert(tls_run_loop); | 57 current_run_loop.Free(); |
| 60 delete tls_run_loop; | |
| 61 tls_run_loop = NULL; | |
| 62 } | 58 } |
| 63 | 59 |
| 64 // static | 60 // static |
| 65 RunLoop* RunLoop::current() { | 61 RunLoop* RunLoop::current() { |
| 66 assert(tls_run_loop); | 62 return current_run_loop.Get(); |
| 67 return tls_run_loop->Get(); | |
| 68 } | 63 } |
| 69 | 64 |
| 70 void RunLoop::AddHandler(RunLoopHandler* handler, | 65 void RunLoop::AddHandler(RunLoopHandler* handler, |
| 71 const Handle& handle, | 66 const Handle& handle, |
| 72 MojoWaitFlags wait_flags, | 67 MojoWaitFlags wait_flags, |
| 73 MojoDeadline deadline) { | 68 MojoDeadline deadline) { |
| 74 assert(current() == this); | 69 assert(current() == this); |
| 75 assert(handler); | 70 assert(handler); |
| 76 assert(handle.is_valid()); | 71 assert(handle.is_valid()); |
| 77 // Assume it's an error if someone tries to reregister an existing handle. | 72 // Assume it's an error if someone tries to reregister an existing handle. |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 const MojoTimeTicks now = GetTimeTicksNow(); | 215 const MojoTimeTicks now = GetTimeTicksNow(); |
| 221 if (min_time < now) | 216 if (min_time < now) |
| 222 wait_state.deadline = static_cast<MojoDeadline>(0); | 217 wait_state.deadline = static_cast<MojoDeadline>(0); |
| 223 else | 218 else |
| 224 wait_state.deadline = static_cast<MojoDeadline>(min_time - now); | 219 wait_state.deadline = static_cast<MojoDeadline>(min_time - now); |
| 225 } | 220 } |
| 226 return wait_state; | 221 return wait_state; |
| 227 } | 222 } |
| 228 | 223 |
| 229 } // namespace mojo | 224 } // namespace mojo |
| OLD | NEW |