[email protected] | 776f09a4 | 2012-02-23 21:56:22 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 5 | #include "base/message_loop/message_pump_glib.h" |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 6 | |
| 7 | #include <fcntl.h> |
| 8 | #include <math.h> |
| 9 | |
[email protected] | 831a32d | 2009-12-04 20:45:54 | [diff] [blame] | 10 | #include <glib.h> |
| 11 | |
[email protected] | 3154c3c | 2014-05-24 12:55:40 | [diff] [blame] | 12 | #include "base/lazy_instance.h" |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 13 | #include "base/logging.h" |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 14 | #include "base/posix/eintr_wrapper.h" |
[email protected] | 3154c3c | 2014-05-24 12:55:40 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 16 | #include "base/threading/platform_thread.h" |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 17 | |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 18 | namespace base { |
| 19 | |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 20 | namespace { |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 21 | |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 22 | // Return a timeout suitable for the glib loop, -1 to block forever, |
| 23 | // 0 to return right away, or a timeout in milliseconds from now. |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 24 | int GetTimeIntervalMilliseconds(const TimeTicks& from) { |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 25 | if (from.is_null()) |
| 26 | return -1; |
| 27 | |
| 28 | // Be careful here. TimeDelta has a precision of microseconds, but we want a |
| 29 | // value in milliseconds. If there are 5.5ms left, should the delay be 5 or |
| 30 | // 6? It should be 6 to avoid executing delayed work too early. |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 31 | int delay = static_cast<int>( |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 32 | ceil((from - TimeTicks::Now()).InMillisecondsF())); |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 33 | |
| 34 | // If this value is negative, then we need to run delayed work soon. |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 35 | return delay < 0 ? 0 : delay; |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // A brief refresher on GLib: |
| 39 | // GLib sources have four callbacks: Prepare, Check, Dispatch and Finalize. |
| 40 | // On each iteration of the GLib pump, it calls each source's Prepare function. |
| 41 | // This function should return TRUE if it wants GLib to call its Dispatch, and |
| 42 | // FALSE otherwise. It can also set a timeout in this case for the next time |
| 43 | // Prepare should be called again (it may be called sooner). |
| 44 | // After the Prepare calls, GLib does a poll to check for events from the |
| 45 | // system. File descriptors can be attached to the sources. The poll may block |
| 46 | // if none of the Prepare calls returned TRUE. It will block indefinitely, or |
| 47 | // by the minimum time returned by a source in Prepare. |
| 48 | // After the poll, GLib calls Check for each source that returned FALSE |
| 49 | // from Prepare. The return value of Check has the same meaning as for Prepare, |
| 50 | // making Check a second chance to tell GLib we are ready for Dispatch. |
| 51 | // Finally, GLib calls Dispatch for each source that is ready. If Dispatch |
| 52 | // returns FALSE, GLib will destroy the source. Dispatch calls may be recursive |
| 53 | // (i.e., you can call Run from them), but Prepare and Check cannot. |
| 54 | // Finalize is called when the source is destroyed. |
qyearsley | 15926f8 | 2015-07-25 00:33:31 | [diff] [blame] | 55 | // NOTE: It is common for subsystems to want to process pending events while |
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 56 | // doing intensive work, for example the flash plugin. They usually use the |
| 57 | // following pattern (recommended by the GTK docs): |
| 58 | // while (gtk_events_pending()) { |
| 59 | // gtk_main_iteration(); |
| 60 | // } |
| 61 | // |
| 62 | // gtk_events_pending just calls g_main_context_pending, which does the |
| 63 | // following: |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 64 | // - Call prepare on all the sources. |
| 65 | // - Do the poll with a timeout of 0 (not blocking). |
| 66 | // - Call check on all the sources. |
| 67 | // - *Does not* call dispatch on the sources. |
| 68 | // - Return true if any of prepare() or check() returned true. |
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 69 | // |
| 70 | // gtk_main_iteration just calls g_main_context_iteration, which does the whole |
| 71 | // thing, respecting the timeout for the poll (and block, although it is |
| 72 | // expected not to if gtk_events_pending returned true), and call dispatch. |
| 73 | // |
| 74 | // Thus it is important to only return true from prepare or check if we |
| 75 | // actually have events or work to do. We also need to make sure we keep |
| 76 | // internal state consistent so that if prepare/check return true when called |
| 77 | // from gtk_events_pending, they will still return true when called right |
| 78 | // after, from gtk_main_iteration. |
| 79 | // |
| 80 | // For the GLib pump we try to follow the Windows UI pump model: |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 81 | // - Whenever we receive a wakeup event or the timer for delayed work expires, |
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 82 | // we run DoWork and/or DoDelayedWork. That part will also run in the other |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 83 | // event pumps. |
| 84 | // - We also run DoWork, DoDelayedWork, and possibly DoIdleWork in the main |
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 85 | // loop, around event handling. |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 86 | |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 87 | struct WorkSource : public GSource { |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 88 | MessagePumpGlib* pump; |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 89 | }; |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 90 | |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 91 | gboolean WorkSourcePrepare(GSource* source, |
| 92 | gint* timeout_ms) { |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 93 | *timeout_ms = static_cast<WorkSource*>(source)->pump->HandlePrepare(); |
| 94 | // We always return FALSE, so that our timeout is honored. If we were |
| 95 | // to return TRUE, the timeout would be considered to be 0 and the poll |
| 96 | // would never block. Once the poll is finished, Check will be called. |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 97 | return FALSE; |
| 98 | } |
| 99 | |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 100 | gboolean WorkSourceCheck(GSource* source) { |
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 101 | // Only return TRUE if Dispatch should be called. |
| 102 | return static_cast<WorkSource*>(source)->pump->HandleCheck(); |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 103 | } |
| 104 | |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 105 | gboolean WorkSourceDispatch(GSource* source, |
| 106 | GSourceFunc unused_func, |
| 107 | gpointer unused_data) { |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 108 | |
| 109 | static_cast<WorkSource*>(source)->pump->HandleDispatch(); |
| 110 | // Always return TRUE so our source stays registered. |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 111 | return TRUE; |
| 112 | } |
| 113 | |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 114 | // I wish these could be const, but g_source_new wants non-const. |
| 115 | GSourceFuncs WorkSourceFuncs = { |
| 116 | WorkSourcePrepare, |
| 117 | WorkSourceCheck, |
| 118 | WorkSourceDispatch, |
| 119 | NULL |
| 120 | }; |
| 121 | |
[email protected] | 3154c3c | 2014-05-24 12:55:40 | [diff] [blame] | 122 | // The following is used to make sure we only run the MessagePumpGlib on one |
| 123 | // thread. X only has one message pump so we can only have one UI loop per |
| 124 | // process. |
| 125 | #ifndef NDEBUG |
| 126 | |
| 127 | // Tracks the pump the most recent pump that has been run. |
| 128 | struct ThreadInfo { |
| 129 | // The pump. |
| 130 | MessagePumpGlib* pump; |
| 131 | |
| 132 | // ID of the thread the pump was run on. |
| 133 | PlatformThreadId thread_id; |
| 134 | }; |
| 135 | |
| 136 | // Used for accesing |thread_info|. |
| 137 | static LazyInstance<Lock>::Leaky thread_info_lock = LAZY_INSTANCE_INITIALIZER; |
| 138 | |
| 139 | // If non-NULL it means a MessagePumpGlib exists and has been Run. This is |
| 140 | // destroyed when the MessagePump is destroyed. |
| 141 | ThreadInfo* thread_info = NULL; |
| 142 | |
| 143 | void CheckThread(MessagePumpGlib* pump) { |
| 144 | AutoLock auto_lock(thread_info_lock.Get()); |
| 145 | if (!thread_info) { |
| 146 | thread_info = new ThreadInfo; |
| 147 | thread_info->pump = pump; |
| 148 | thread_info->thread_id = PlatformThread::CurrentId(); |
| 149 | } |
| 150 | DCHECK(thread_info->thread_id == PlatformThread::CurrentId()) << |
| 151 | "Running MessagePumpGlib on two different threads; " |
| 152 | "this is unsupported by GLib!"; |
| 153 | } |
| 154 | |
| 155 | void PumpDestroyed(MessagePumpGlib* pump) { |
| 156 | AutoLock auto_lock(thread_info_lock.Get()); |
| 157 | if (thread_info && thread_info->pump == pump) { |
| 158 | delete thread_info; |
| 159 | thread_info = NULL; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | #endif |
| 164 | |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 165 | } // namespace |
| 166 | |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 167 | struct MessagePumpGlib::RunState { |
[email protected] | e7af596 | 2010-08-05 22:36:04 | [diff] [blame] | 168 | Delegate* delegate; |
[email protected] | e7af596 | 2010-08-05 22:36:04 | [diff] [blame] | 169 | |
| 170 | // Used to flag that the current Run() invocation should return ASAP. |
| 171 | bool should_quit; |
| 172 | |
| 173 | // Used to count how many Run() invocations are on the stack. |
| 174 | int run_depth; |
| 175 | |
| 176 | // This keeps the state of whether the pump got signaled that there was new |
| 177 | // work to be done. Since we eat the message on the wake up pipe as soon as |
| 178 | // we get it, we keep that state here to stay consistent. |
| 179 | bool has_work; |
| 180 | }; |
| 181 | |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 182 | MessagePumpGlib::MessagePumpGlib() |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 183 | : state_(NULL), |
[email protected] | 831a32d | 2009-12-04 20:45:54 | [diff] [blame] | 184 | context_(g_main_context_default()), |
| 185 | wakeup_gpollfd_(new GPollFD) { |
[email protected] | aa0f266 | 2008-11-18 01:30:42 | [diff] [blame] | 186 | // Create our wakeup pipe, which is used to flag when work was scheduled. |
| 187 | int fds[2]; |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 188 | int ret = pipe(fds); |
| 189 | DCHECK_EQ(ret, 0); |
| 190 | (void)ret; // Prevent warning in release mode. |
| 191 | |
[email protected] | aa0f266 | 2008-11-18 01:30:42 | [diff] [blame] | 192 | wakeup_pipe_read_ = fds[0]; |
| 193 | wakeup_pipe_write_ = fds[1]; |
[email protected] | 831a32d | 2009-12-04 20:45:54 | [diff] [blame] | 194 | wakeup_gpollfd_->fd = wakeup_pipe_read_; |
| 195 | wakeup_gpollfd_->events = G_IO_IN; |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 196 | |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 197 | work_source_ = g_source_new(&WorkSourceFuncs, sizeof(WorkSource)); |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 198 | static_cast<WorkSource*>(work_source_)->pump = this; |
[email protected] | 831a32d | 2009-12-04 20:45:54 | [diff] [blame] | 199 | g_source_add_poll(work_source_, wakeup_gpollfd_.get()); |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 200 | // Use a low priority so that we let other events in the queue go first. |
| 201 | g_source_set_priority(work_source_, G_PRIORITY_DEFAULT_IDLE); |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 202 | // This is needed to allow Run calls inside Dispatch. |
| 203 | g_source_set_can_recurse(work_source_, TRUE); |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 204 | g_source_attach(work_source_, context_); |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 205 | } |
| 206 | |
[email protected] | 54aa4f1 | 2013-07-22 22:24:13 | [diff] [blame] | 207 | MessagePumpGlib::~MessagePumpGlib() { |
[email protected] | 3154c3c | 2014-05-24 12:55:40 | [diff] [blame] | 208 | #ifndef NDEBUG |
| 209 | PumpDestroyed(this); |
| 210 | #endif |
[email protected] | 54aa4f1 | 2013-07-22 22:24:13 | [diff] [blame] | 211 | g_source_destroy(work_source_); |
| 212 | g_source_unref(work_source_); |
| 213 | close(wakeup_pipe_read_); |
| 214 | close(wakeup_pipe_write_); |
| 215 | } |
| 216 | |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 217 | // Return the timeout we want passed to poll. |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 218 | int MessagePumpGlib::HandlePrepare() { |
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 219 | // We know we have work, but we haven't called HandleDispatch yet. Don't let |
| 220 | // the pump block so that we can do some processing. |
[email protected] | a2f08b0c | 2010-01-30 00:47:28 | [diff] [blame] | 221 | if (state_ && // state_ may be null during tests. |
| 222 | state_->has_work) |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 223 | return 0; |
| 224 | |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 225 | // We don't think we have work to do, but make sure not to block |
| 226 | // longer than the next time we need to run delayed work. |
| 227 | return GetTimeIntervalMilliseconds(delayed_work_time_); |
| 228 | } |
| 229 | |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 230 | bool MessagePumpGlib::HandleCheck() { |
[email protected] | a2f08b0c | 2010-01-30 00:47:28 | [diff] [blame] | 231 | if (!state_) // state_ may be null during tests. |
| 232 | return false; |
| 233 | |
[email protected] | 7572ea2 | 2013-01-19 03:55:17 | [diff] [blame] | 234 | // We usually have a single message on the wakeup pipe, since we are only |
| 235 | // signaled when the queue went from empty to non-empty, but there can be |
| 236 | // two messages if a task posted a task, hence we read at most two bytes. |
| 237 | // The glib poll will tell us whether there was data, so this read |
| 238 | // shouldn't block. |
[email protected] | 831a32d | 2009-12-04 20:45:54 | [diff] [blame] | 239 | if (wakeup_gpollfd_->revents & G_IO_IN) { |
[email protected] | 7572ea2 | 2013-01-19 03:55:17 | [diff] [blame] | 240 | char msg[2]; |
| 241 | const int num_bytes = HANDLE_EINTR(read(wakeup_pipe_read_, msg, 2)); |
| 242 | if (num_bytes < 1) { |
[email protected] | aa0f266 | 2008-11-18 01:30:42 | [diff] [blame] | 243 | NOTREACHED() << "Error reading from the wakeup pipe."; |
| 244 | } |
[email protected] | 7572ea2 | 2013-01-19 03:55:17 | [diff] [blame] | 245 | DCHECK((num_bytes == 1 && msg[0] == '!') || |
| 246 | (num_bytes == 2 && msg[0] == '!' && msg[1] == '!')); |
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 247 | // Since we ate the message, we need to record that we have more work, |
| 248 | // because HandleCheck() may be called without HandleDispatch being called |
| 249 | // afterwards. |
| 250 | state_->has_work = true; |
| 251 | } |
| 252 | |
| 253 | if (state_->has_work) |
| 254 | return true; |
| 255 | |
| 256 | if (GetTimeIntervalMilliseconds(delayed_work_time_) == 0) { |
| 257 | // The timer has expired. That condition will stay true until we process |
| 258 | // that delayed work, so we don't need to record this differently. |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | return false; |
| 263 | } |
| 264 | |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 265 | void MessagePumpGlib::HandleDispatch() { |
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 266 | state_->has_work = false; |
| 267 | if (state_->delegate->DoWork()) { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 268 | // NOTE: on Windows at this point we would call ScheduleWork (see |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 269 | // MessagePumpGlib::HandleWorkMessage in message_pump_win.cc). But here, |
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 270 | // instead of posting a message on the wakeup pipe, we can avoid the |
| 271 | // syscalls and just signal that we have more work. |
| 272 | state_->has_work = true; |
[email protected] | aa0f266 | 2008-11-18 01:30:42 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 275 | if (state_->should_quit) |
| 276 | return; |
| 277 | |
[email protected] | b105b9e | 2009-06-01 22:01:53 | [diff] [blame] | 278 | state_->delegate->DoDelayedWork(&delayed_work_time_); |
[email protected] | 95fac423 | 2008-11-13 00:25:51 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 281 | void MessagePumpGlib::Run(Delegate* delegate) { |
[email protected] | b9f12d8f | 2014-04-16 05:29:49 | [diff] [blame] | 282 | #ifndef NDEBUG |
[email protected] | 3154c3c | 2014-05-24 12:55:40 | [diff] [blame] | 283 | CheckThread(this); |
[email protected] | b9f12d8f | 2014-04-16 05:29:49 | [diff] [blame] | 284 | #endif |
| 285 | |
| 286 | RunState state; |
| 287 | state.delegate = delegate; |
| 288 | state.should_quit = false; |
| 289 | state.run_depth = state_ ? state_->run_depth + 1 : 1; |
| 290 | state.has_work = false; |
| 291 | |
| 292 | RunState* previous_state = state_; |
| 293 | state_ = &state; |
| 294 | |
| 295 | // We really only do a single task for each iteration of the loop. If we |
| 296 | // have done something, assume there is likely something more to do. This |
| 297 | // will mean that we don't block on the message pump until there was nothing |
| 298 | // more to do. We also set this to true to make sure not to block on the |
| 299 | // first iteration of the loop, so RunUntilIdle() works correctly. |
| 300 | bool more_work_is_plausible = true; |
| 301 | |
| 302 | // We run our own loop instead of using g_main_loop_quit in one of the |
| 303 | // callbacks. This is so we only quit our own loops, and we don't quit |
| 304 | // nested loops run by others. TODO(deanm): Is this what we want? |
| 305 | for (;;) { |
| 306 | // Don't block if we think we have more work to do. |
| 307 | bool block = !more_work_is_plausible; |
| 308 | |
| 309 | more_work_is_plausible = g_main_context_iteration(context_, block); |
| 310 | if (state_->should_quit) |
| 311 | break; |
| 312 | |
| 313 | more_work_is_plausible |= state_->delegate->DoWork(); |
| 314 | if (state_->should_quit) |
| 315 | break; |
| 316 | |
| 317 | more_work_is_plausible |= |
| 318 | state_->delegate->DoDelayedWork(&delayed_work_time_); |
| 319 | if (state_->should_quit) |
| 320 | break; |
| 321 | |
| 322 | if (more_work_is_plausible) |
| 323 | continue; |
| 324 | |
| 325 | more_work_is_plausible = state_->delegate->DoIdleWork(); |
| 326 | if (state_->should_quit) |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | state_ = previous_state; |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 331 | } |
| 332 | |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 333 | void MessagePumpGlib::Quit() { |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 334 | if (state_) { |
| 335 | state_->should_quit = true; |
| 336 | } else { |
| 337 | NOTREACHED() << "Quit called outside Run!"; |
| 338 | } |
| 339 | } |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 340 | |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 341 | void MessagePumpGlib::ScheduleWork() { |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 342 | // This can be called on any thread, so we don't want to touch any state |
| 343 | // variables as we would then need locks all over. This ensures that if |
[email protected] | 2a006f2a | 2008-11-12 21:34:03 | [diff] [blame] | 344 | // we are sleeping in a poll that we will wake up. |
[email protected] | aa0f266 | 2008-11-18 01:30:42 | [diff] [blame] | 345 | char msg = '!'; |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 346 | if (HANDLE_EINTR(write(wakeup_pipe_write_, &msg, 1)) != 1) { |
[email protected] | aa0f266 | 2008-11-18 01:30:42 | [diff] [blame] | 347 | NOTREACHED() << "Could not write to the UI message loop wakeup pipe!"; |
| 348 | } |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 349 | } |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 350 | |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 351 | void MessagePumpGlib::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 352 | // We need to wake up the loop in case the poll timeout needs to be |
qyearsley | 15926f8 | 2015-07-25 00:33:31 | [diff] [blame] | 353 | // adjusted. This will cause us to try to do work, but that's OK. |
[email protected] | fbe9fec | 2008-11-12 19:39:42 | [diff] [blame] | 354 | delayed_work_time_ = delayed_work_time; |
| 355 | ScheduleWork(); |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | 93805f3 | 2014-02-08 00:10:03 | [diff] [blame] | 358 | bool MessagePumpGlib::ShouldQuit() const { |
| 359 | CHECK(state_); |
| 360 | return state_->should_quit; |
| 361 | } |
| 362 | |
[email protected] | 8fc3a48 | 2008-10-03 16:52:59 | [diff] [blame] | 363 | } // namespace base |