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 #include <pthread.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "mojo/public/c/system/macros.h" | 13 #include "mojo/public/c/system/macros.h" |
14 #include "mojo/public/cpp/system/time.h" | 14 #include "mojo/public/cpp/system/time.h" |
15 #include "mojo/public/cpp/system/wait.h" | 15 #include "mojo/public/cpp/system/wait.h" |
16 #include "mojo/public/cpp/utility/run_loop_handler.h" | 16 #include "mojo/public/cpp/utility/run_loop_handler.h" |
17 | 17 |
18 namespace mojo { | 18 namespace mojo { |
19 namespace { | 19 namespace { |
20 | 20 |
21 const MojoTimeTicks kInvalidTimeTicks = static_cast<MojoTimeTicks>(0); | 21 const MojoTimeTicks kInvalidTimeTicks = static_cast<MojoTimeTicks>(0); |
22 | 22 |
23 pthread_key_t g_current_run_loop_key; | 23 pthread_key_t g_current_run_loop_key; |
24 | 24 |
25 // Ensures that the "current run loop" functionality is available (i.e., that we | 25 // Ensures that the "current run loop" functionality is available (i.e., that we |
26 // have a TLS slot). | 26 // have a TLS slot). |
27 void EnsureCurrentRunLoopInitialized() { | 27 void InitializeCurrentRunLoopIfNecessary() { |
28 static pthread_once_t current_run_loop_key_once = PTHREAD_ONCE_INIT; | 28 static pthread_once_t current_run_loop_key_once = PTHREAD_ONCE_INIT; |
29 int error = pthread_once(¤t_run_loop_key_once, []() { | 29 int error = pthread_once(¤t_run_loop_key_once, []() { |
30 int error = pthread_key_create(&g_current_run_loop_key, nullptr); | 30 int error = pthread_key_create(&g_current_run_loop_key, nullptr); |
31 MOJO_ALLOW_UNUSED_LOCAL(error); | 31 MOJO_ALLOW_UNUSED_LOCAL(error); |
32 assert(!error); | 32 assert(!error); |
33 }); | 33 }); |
34 MOJO_ALLOW_UNUSED_LOCAL(error); | 34 MOJO_ALLOW_UNUSED_LOCAL(error); |
35 assert(!error); | 35 assert(!error); |
36 } | 36 } |
37 | 37 |
38 void SetCurrentRunLoop(RunLoop* run_loop) { | 38 void SetCurrentRunLoop(RunLoop* run_loop) { |
39 EnsureCurrentRunLoopInitialized(); | 39 InitializeCurrentRunLoopIfNecessary(); |
40 | 40 |
41 int error = pthread_setspecific(g_current_run_loop_key, run_loop); | 41 int error = pthread_setspecific(g_current_run_loop_key, run_loop); |
42 MOJO_ALLOW_UNUSED_LOCAL(error); | 42 MOJO_ALLOW_UNUSED_LOCAL(error); |
43 assert(!error); | 43 assert(!error); |
44 } | 44 } |
45 | 45 |
46 // State needed for one iteration of WaitMany(). | 46 // State needed for one iteration of WaitMany(). |
47 struct WaitState { | 47 struct WaitState { |
48 std::vector<Handle> handles; | 48 std::vector<Handle> handles; |
49 std::vector<MojoHandleSignals> handle_signals; | 49 std::vector<MojoHandleSignals> handle_signals; |
(...skipping 14 matching lines...) Expand all Loading... |
64 } | 64 } |
65 | 65 |
66 RunLoop::~RunLoop() { | 66 RunLoop::~RunLoop() { |
67 assert(current() == this); | 67 assert(current() == this); |
68 NotifyHandlers(MOJO_RESULT_ABORTED, IGNORE_DEADLINE); | 68 NotifyHandlers(MOJO_RESULT_ABORTED, IGNORE_DEADLINE); |
69 SetCurrentRunLoop(nullptr); | 69 SetCurrentRunLoop(nullptr); |
70 } | 70 } |
71 | 71 |
72 // static | 72 // static |
73 RunLoop* RunLoop::current() { | 73 RunLoop* RunLoop::current() { |
74 EnsureCurrentRunLoopInitialized(); | 74 InitializeCurrentRunLoopIfNecessary(); |
75 return static_cast<RunLoop*>(pthread_getspecific(g_current_run_loop_key)); | 75 return static_cast<RunLoop*>(pthread_getspecific(g_current_run_loop_key)); |
76 } | 76 } |
77 | 77 |
78 void RunLoop::AddHandler(RunLoopHandler* handler, | 78 void RunLoop::AddHandler(RunLoopHandler* handler, |
79 const Handle& handle, | 79 const Handle& handle, |
80 MojoHandleSignals handle_signals, | 80 MojoHandleSignals handle_signals, |
81 MojoDeadline deadline) { | 81 MojoDeadline deadline) { |
82 assert(current() == this); | 82 assert(current() == this); |
83 assert(handler); | 83 assert(handler); |
84 assert(handle.is_valid()); | 84 assert(handle.is_valid()); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 // 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 |
279 // 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 |
280 // run_times further in the future are considered lesser. | 280 // run_times further in the future are considered lesser. |
281 return run_time > other.run_time; | 281 return run_time > other.run_time; |
282 } | 282 } |
283 | 283 |
284 return sequence_number > other.sequence_number; | 284 return sequence_number > other.sequence_number; |
285 } | 285 } |
286 | 286 |
287 } // namespace mojo | 287 } // namespace mojo |
OLD | NEW |