Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "glib-bridge/glib_bridge.h" |
| 6 | |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 9 | #include <base/check.h> |
| 10 | #include <base/check_op.h> |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 11 | #include <base/threading/sequenced_task_runner_handle.h> |
| 12 | |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 13 | namespace glib_bridge { |
| 14 | |
| 15 | namespace { |
| 16 | |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 17 | struct GMainContextLock { |
| 18 | public: |
| 19 | explicit GMainContextLock(GMainContext* context) : context_(context) { |
| 20 | CHECK(context_); |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 21 | CHECK(g_main_context_acquire(context_)); |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | ~GMainContextLock() { g_main_context_release(context_); } |
| 25 | |
| 26 | private: |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 27 | GMainContext* context_; // weak |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 28 | }; |
| 29 | |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 30 | } // namespace |
| 31 | |
| 32 | GlibBridge::GlibBridge() |
| 33 | : glib_context_(g_main_context_new()), |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 34 | state_(State::kPreparingIteration), |
| 35 | weak_ptr_factory_(this) { |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 36 | CHECK(glib_context_); |
| 37 | g_main_context_push_thread_default(glib_context_); |
| 38 | base::SequencedTaskRunnerHandle::Get()->PostTask( |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 39 | FROM_HERE, base::Bind(&GlibBridge::PrepareIteration, |
| 40 | weak_ptr_factory_.GetWeakPtr())); |
| 41 | } |
| 42 | |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 43 | GlibBridge::~GlibBridge() { |
| 44 | g_main_context_pop_thread_default(glib_context_); |
| 45 | g_main_context_unref(glib_context_); |
| 46 | } |
Eric Caruso | f37003d | 2019-04-30 16:20:52 -0700 | [diff] [blame] | 47 | |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 48 | void GlibBridge::PrepareIteration() { |
| 49 | CHECK_EQ(state_, State::kPreparingIteration); |
| 50 | CHECK(watchers_.empty()); |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 51 | GMainContextLock _l(glib_context_); |
| 52 | |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 53 | bool immediate = g_main_context_prepare(glib_context_, &max_priority_); |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 54 | |
| 55 | int num_fds = |
| 56 | g_main_context_query(glib_context_, max_priority_, nullptr, nullptr, 0); |
| 57 | poll_fds_ = std::vector<GPollFD>(num_fds); |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 58 | |
| 59 | int timeout_ms; |
| 60 | g_main_context_query(glib_context_, max_priority_, &timeout_ms, &poll_fds_[0], |
| 61 | num_fds); |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 62 | if (immediate || (num_fds == 0 && timeout_ms == 0)) { |
| 63 | DVLOG(1) << "Iteration can be dispatched immediately"; |
| 64 | base::SequencedTaskRunnerHandle::Get()->PostTask( |
| 65 | FROM_HERE, |
| 66 | base::Bind(&GlibBridge::Dispatch, weak_ptr_factory_.GetWeakPtr())); |
| 67 | state_ = State::kReadyForDispatch; |
| 68 | return; |
| 69 | } |
Eric Caruso | f37003d | 2019-04-30 16:20:52 -0700 | [diff] [blame] | 70 | |
| 71 | // Collect information about which poll flags we need for each fd. |
| 72 | std::map<int, int> poll_flags; |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 73 | for (GPollFD& poll_fd : poll_fds_) { |
| 74 | fd_map_[poll_fd.fd].push_back(&poll_fd); |
Eric Caruso | f37003d | 2019-04-30 16:20:52 -0700 | [diff] [blame] | 75 | poll_flags[poll_fd.fd] |= poll_fd.events; |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | DVLOG(1) << "Preparing iteration with timeout " << timeout_ms << " ms, " |
| 79 | << poll_flags.size() << " event FDs"; |
Eric Caruso | f37003d | 2019-04-30 16:20:52 -0700 | [diff] [blame] | 80 | |
| 81 | for (const auto& fd_flags : poll_flags) { |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 82 | std::unique_ptr<base::FileDescriptorWatcher::Controller> reader; |
| 83 | if (fd_flags.second & G_IO_IN) { |
| 84 | reader = base::FileDescriptorWatcher::WatchReadable( |
| 85 | fd_flags.first, |
| 86 | base::Bind(&GlibBridge::OnEvent, weak_ptr_factory_.GetWeakPtr(), |
| 87 | fd_flags.first, G_IO_IN)); |
| 88 | CHECK(reader) << "Could not set up read watcher for fd " |
| 89 | << fd_flags.first; |
| 90 | } |
| 91 | |
| 92 | std::unique_ptr<base::FileDescriptorWatcher::Controller> writer; |
| 93 | if (fd_flags.second & G_IO_OUT) { |
| 94 | writer = base::FileDescriptorWatcher::WatchWritable( |
| 95 | fd_flags.first, |
| 96 | base::Bind(&GlibBridge::OnEvent, weak_ptr_factory_.GetWeakPtr(), |
| 97 | fd_flags.first, G_IO_OUT)); |
| 98 | CHECK(writer) << "Could not set up write watcher for fd " |
| 99 | << fd_flags.first; |
| 100 | } |
| 101 | |
| 102 | watchers_[fd_flags.first] = Watcher{std::move(reader), std::move(writer)}; |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | state_ = State::kWaitingForEvents; |
| 106 | if (timeout_ms < 0) |
| 107 | return; |
| 108 | |
| 109 | base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(timeout_ms); |
| 110 | timeout_closure_.Reset( |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 111 | base::Bind(&GlibBridge::Timeout, weak_ptr_factory_.GetWeakPtr())); |
| 112 | base::SequencedTaskRunnerHandle::Get()->PostDelayedTask( |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 113 | FROM_HERE, timeout_closure_.callback(), timeout); |
| 114 | } |
| 115 | |
| 116 | void GlibBridge::OnEvent(int fd, int flag) { |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 117 | CHECK(state_ == State::kWaitingForEvents || |
| 118 | state_ == State::kReadyForDispatch); |
| 119 | DVLOG(2) << "OnEvent(" << fd << ", " << flag << ")"; |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 120 | for (GPollFD* poll_fd : fd_map_[fd]) |
Eric Caruso | f37003d | 2019-04-30 16:20:52 -0700 | [diff] [blame] | 121 | poll_fd->revents |= flag & poll_fd->events; |
| 122 | |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 123 | if (flag & G_IO_IN) |
| 124 | watchers_[fd].reader.reset(); |
| 125 | if (flag & G_IO_OUT) |
| 126 | watchers_[fd].writer.reset(); |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 127 | |
Eric Caruso | 0291361 | 2020-07-07 17:40:31 -0700 | [diff] [blame] | 128 | // Avoid posting the dispatch task if it's already posted |
| 129 | if (state_ == State::kReadyForDispatch) |
| 130 | return; |
| 131 | |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 132 | base::SequencedTaskRunnerHandle::Get()->PostTask( |
| 133 | FROM_HERE, |
| 134 | base::Bind(&GlibBridge::Dispatch, weak_ptr_factory_.GetWeakPtr())); |
| 135 | state_ = State::kReadyForDispatch; |
| 136 | } |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 137 | |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 138 | void GlibBridge::Timeout() { |
| 139 | CHECK_EQ(state_, State::kWaitingForEvents); |
| 140 | base::SequencedTaskRunnerHandle::Get()->PostTask( |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 141 | FROM_HERE, |
| 142 | base::Bind(&GlibBridge::Dispatch, weak_ptr_factory_.GetWeakPtr())); |
| 143 | state_ = State::kReadyForDispatch; |
| 144 | } |
| 145 | |
| 146 | void GlibBridge::Dispatch() { |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 147 | CHECK_EQ(state_, State::kReadyForDispatch); |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 148 | GMainContextLock _l(glib_context_); |
| 149 | |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 150 | bool dispatched = g_main_context_check(glib_context_, max_priority_, |
| 151 | poll_fds_.data(), poll_fds_.size()); |
| 152 | g_main_context_dispatch(glib_context_); |
| 153 | DVLOG(2) << (dispatched ? "Found" : "Did not find") << " source to dispatch"; |
| 154 | |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 155 | timeout_closure_.Cancel(); |
| 156 | watchers_.clear(); |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 157 | poll_fds_.clear(); |
| 158 | max_priority_ = -1; |
Eric Caruso | bcc58bb | 2019-11-07 14:10:35 -0800 | [diff] [blame] | 159 | base::SequencedTaskRunnerHandle::Get()->PostTask( |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 160 | FROM_HERE, base::Bind(&GlibBridge::PrepareIteration, |
| 161 | weak_ptr_factory_.GetWeakPtr())); |
Eric Caruso | f37003d | 2019-04-30 16:20:52 -0700 | [diff] [blame] | 162 | state_ = State::kPreparingIteration; |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Eric Caruso | 246e141 | 2019-01-24 16:44:02 -0800 | [diff] [blame] | 165 | } // namespace glib_bridge |