Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/cpp/utility/run_loop.h" | 5 #include "mojo/public/cpp/utility/run_loop.h" |
| 6 | 6 |
| 7 #include <assert.h> | 7 #include <assert.h> |
| 8 #include <pthread.h> | |
| 8 | 9 |
| 9 #include <algorithm> | 10 #include <algorithm> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 13 #include "mojo/public/c/system/macros.h" | |
| 12 #include "mojo/public/cpp/system/time.h" | 14 #include "mojo/public/cpp/system/time.h" |
| 13 #include "mojo/public/cpp/system/wait.h" | 15 #include "mojo/public/cpp/system/wait.h" |
| 14 #include "mojo/public/cpp/utility/lib/thread_local.h" | |
| 15 #include "mojo/public/cpp/utility/run_loop_handler.h" | 16 #include "mojo/public/cpp/utility/run_loop_handler.h" |
| 16 | 17 |
| 17 namespace mojo { | 18 namespace mojo { |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 internal::ThreadLocalPointer<RunLoop> current_run_loop; | 21 const MojoTimeTicks kInvalidTimeTicks = static_cast<MojoTimeTicks>(0); |
| 21 | 22 |
| 22 const MojoTimeTicks kInvalidTimeTicks = static_cast<MojoTimeTicks>(0); | 23 pthread_key_t g_current_run_loop_key; |
| 24 | |
| 25 // Ensures that the "current run loop" functionality is available (i.e., that we | |
| 26 // have a TLS slot). | |
| 27 void EnsureCurrentRunLoopInitialized() { | |
| 28 static pthread_once_t current_run_loop_key_once = PTHREAD_ONCE_INIT; | |
| 29 int error = pthread_once(¤t_run_loop_key_once, []() { | |
| 30 int error = pthread_key_create(&g_current_run_loop_key, nullptr); | |
|
vardhan
2016/05/19 21:37:35
does it make sense to provide a destructor to pthr
| |
| 31 MOJO_ALLOW_UNUSED_LOCAL(error); | |
| 32 assert(!error); | |
| 33 }); | |
| 34 MOJO_ALLOW_UNUSED_LOCAL(error); | |
| 35 assert(!error); | |
| 36 } | |
| 37 | |
| 38 void SetCurrentRunLoop(RunLoop* run_loop) { | |
| 39 EnsureCurrentRunLoopInitialized(); | |
| 40 | |
| 41 int error = pthread_setspecific(g_current_run_loop_key, run_loop); | |
| 42 MOJO_ALLOW_UNUSED_LOCAL(error); | |
| 43 assert(!error); | |
| 44 } | |
| 23 | 45 |
| 24 // State needed for one iteration of WaitMany(). | 46 // State needed for one iteration of WaitMany(). |
| 25 struct WaitState { | 47 struct WaitState { |
| 26 std::vector<Handle> handles; | 48 std::vector<Handle> handles; |
| 27 std::vector<MojoHandleSignals> handle_signals; | 49 std::vector<MojoHandleSignals> handle_signals; |
| 28 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE; | 50 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE; |
| 29 }; | 51 }; |
| 30 | 52 |
| 31 } // namespace | 53 } // namespace |
| 32 | 54 |
| 33 struct RunLoop::RunState { | 55 struct RunLoop::RunState { |
| 34 bool should_quit = false; | 56 bool should_quit = false; |
| 35 WaitState wait_state; | 57 WaitState wait_state; |
| 36 }; | 58 }; |
| 37 | 59 |
| 38 RunLoop::RunLoop() | 60 RunLoop::RunLoop() |
| 39 : run_state_(nullptr), next_handler_id_(0), next_sequence_number_(0) { | 61 : run_state_(nullptr), next_handler_id_(0), next_sequence_number_(0) { |
| 40 assert(!current()); | 62 assert(!current()); |
| 41 current_run_loop.Set(this); | 63 SetCurrentRunLoop(this); |
| 42 } | 64 } |
| 43 | 65 |
| 44 RunLoop::~RunLoop() { | 66 RunLoop::~RunLoop() { |
| 45 assert(current() == this); | 67 assert(current() == this); |
| 46 NotifyHandlers(MOJO_RESULT_ABORTED, IGNORE_DEADLINE); | 68 NotifyHandlers(MOJO_RESULT_ABORTED, IGNORE_DEADLINE); |
| 47 current_run_loop.Set(nullptr); | 69 SetCurrentRunLoop(nullptr); |
| 48 } | |
| 49 | |
| 50 // static | |
| 51 void RunLoop::SetUp() { | |
| 52 current_run_loop.Allocate(); | |
| 53 } | |
| 54 | |
| 55 // static | |
| 56 void RunLoop::TearDown() { | |
| 57 assert(!current()); | |
| 58 current_run_loop.Free(); | |
| 59 } | 70 } |
| 60 | 71 |
| 61 // static | 72 // static |
| 62 RunLoop* RunLoop::current() { | 73 RunLoop* RunLoop::current() { |
| 63 return current_run_loop.Get(); | 74 EnsureCurrentRunLoopInitialized(); |
| 75 return static_cast<RunLoop*>(pthread_getspecific(g_current_run_loop_key)); | |
| 64 } | 76 } |
| 65 | 77 |
| 66 void RunLoop::AddHandler(RunLoopHandler* handler, | 78 void RunLoop::AddHandler(RunLoopHandler* handler, |
| 67 const Handle& handle, | 79 const Handle& handle, |
| 68 MojoHandleSignals handle_signals, | 80 MojoHandleSignals handle_signals, |
| 69 MojoDeadline deadline) { | 81 MojoDeadline deadline) { |
| 70 assert(current() == this); | 82 assert(current() == this); |
| 71 assert(handler); | 83 assert(handler); |
| 72 assert(handle.is_valid()); | 84 assert(handle.is_valid()); |
| 73 // 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. |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 // std::priority_queue<> puts the least element at the end of the queue. We | 278 // std::priority_queue<> puts the least element at the end of the queue. We |
| 267 // want the soonest eligible task to be at the head of the queue, so | 279 // want the soonest eligible task to be at the head of the queue, so |
| 268 // run_times further in the future are considered lesser. | 280 // run_times further in the future are considered lesser. |
| 269 return run_time > other.run_time; | 281 return run_time > other.run_time; |
| 270 } | 282 } |
| 271 | 283 |
| 272 return sequence_number > other.sequence_number; | 284 return sequence_number > other.sequence_number; |
| 273 } | 285 } |
| 274 | 286 |
| 275 } // namespace mojo | 287 } // namespace mojo |
| OLD | NEW |