1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/message_loop/message_pump_android.h"
#include <android/looper.h>
#include <errno.h>
#include <fcntl.h>
#include <jni.h>
#include <sys/eventfd.h>
#include <sys/timerfd.h>
#include <sys/types.h>
#include <unistd.h>
#include <utility>
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/run_loop.h"
#include "build/build_config.h"
namespace base {
namespace {
// https://crbug.com/873588. The stack may not be aligned when the ALooper calls
// into our code due to the inconsistent ABI on older Android OS versions.
#if defined(ARCH_CPU_X86)
#define STACK_ALIGN __attribute__((force_align_arg_pointer))
#else
#define STACK_ALIGN
#endif
STACK_ALIGN int NonDelayedLooperCallback(int fd, int events, void* data) {
if (events & ALOOPER_EVENT_HANGUP)
return 0;
DCHECK(events & ALOOPER_EVENT_INPUT);
MessagePumpForUI* pump = reinterpret_cast<MessagePumpForUI*>(data);
pump->OnNonDelayedLooperCallback();
return 1; // continue listening for events
}
STACK_ALIGN int DelayedLooperCallback(int fd, int events, void* data) {
if (events & ALOOPER_EVENT_HANGUP)
return 0;
DCHECK(events & ALOOPER_EVENT_INPUT);
MessagePumpForUI* pump = reinterpret_cast<MessagePumpForUI*>(data);
pump->OnDelayedLooperCallback();
return 1; // continue listening for events
}
// A bit added to the |non_delayed_fd_| to keep it signaled when we yield to
// native work below.
constexpr uint64_t kTryNativeWorkBeforeIdleBit = uint64_t(1) << 32;
} // namespace
MessagePumpForUI::MessagePumpForUI()
: env_(base::android::AttachCurrentThread()) {
// The Android native ALooper uses epoll to poll our file descriptors and wake
// us up. We use a simple level-triggered eventfd to signal that non-delayed
// work is available, and a timerfd to signal when delayed work is ready to
// be run.
non_delayed_fd_ = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
CHECK_NE(non_delayed_fd_, -1);
DCHECK_EQ(TimeTicks::GetClock(), TimeTicks::Clock::LINUX_CLOCK_MONOTONIC);
delayed_fd_ = checked_cast<int>(
timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC));
CHECK_NE(delayed_fd_, -1);
looper_ = ALooper_prepare(0);
DCHECK(looper_);
// Add a reference to the looper so it isn't deleted on us.
ALooper_acquire(looper_);
ALooper_addFd(looper_, non_delayed_fd_, 0, ALOOPER_EVENT_INPUT,
&NonDelayedLooperCallback, reinterpret_cast<void*>(this));
ALooper_addFd(looper_, delayed_fd_, 0, ALOOPER_EVENT_INPUT,
&DelayedLooperCallback, reinterpret_cast<void*>(this));
}
MessagePumpForUI::~MessagePumpForUI() {
DCHECK_EQ(ALooper_forThread(), looper_);
ALooper_removeFd(looper_, non_delayed_fd_);
ALooper_removeFd(looper_, delayed_fd_);
ALooper_release(looper_);
looper_ = nullptr;
close(non_delayed_fd_);
close(delayed_fd_);
}
void MessagePumpForUI::OnDelayedLooperCallback() {
// There may be non-Chromium callbacks on the same ALooper which may have left
// a pending exception set, and ALooper does not check for this between
// callbacks. Check here, and if there's already an exception, just skip this
// iteration without clearing the fd. If the exception ends up being non-fatal
// then we'll just get called again on the next polling iteration.
if (base::android::HasException(env_))
return;
// ALooper_pollOnce may call this after Quit() if OnNonDelayedLooperCallback()
// resulted in Quit() in the same round.
if (ShouldQuit())
return;
// Clear the fd.
uint64_t value;
long ret = read(delayed_fd_, &value, sizeof(value));
// TODO(mthiesse): Figure out how it's possible to hit EAGAIN here.
// According to http://man7.org/linux/man-pages/man2/timerfd_create.2.html
// EAGAIN only happens if no timer has expired. Also according to the man page
// poll only returns readable when a timer has expired. So this function will
// only be called when a timer has expired, but reading reveals no timer has
// expired...
// Quit() and ScheduleDelayedWork() are the only other functions that touch
// the timerfd, and they both run on the same thread as this callback, so
// there are no obvious timing or multi-threading related issues.
DPCHECK(ret >= 0 || errno == EAGAIN);
DoDelayedLooperWork();
}
void MessagePumpForUI::DoDelayedLooperWork() {
delayed_scheduled_time_.reset();
Delegate::NextWorkInfo next_work_info = delegate_->DoWork();
if (ShouldQuit())
return;
if (next_work_info.is_immediate()) {
ScheduleWork();
return;
}
DoIdleWork();
if (!next_work_info.delayed_run_time.is_max())
ScheduleDelayedWork(next_work_info);
}
void MessagePumpForUI::OnNonDelayedLooperCallback() {
// There may be non-Chromium callbacks on the same ALooper which may have left
// a pending exception set, and ALooper does not check for this between
// callbacks. Check here, and if there's already an exception, just skip this
// iteration without clearing the fd. If the exception ends up being non-fatal
// then we'll just get called again on the next polling iteration.
if (base::android::HasException(env_))
return;
// ALooper_pollOnce may call this after Quit() if OnDelayedLooperCallback()
// resulted in Quit() in the same round.
if (ShouldQuit())
return;
// We're about to process all the work requested by ScheduleWork().
// MessagePump users are expected to do their best not to invoke
// ScheduleWork() again before DoWork() returns a non-immediate
// NextWorkInfo below. Hence, capturing the file descriptor's value now and
// resetting its contents to 0 should be okay. The value currently stored
// should be greater than 0 since work having been scheduled is the reason
// we're here. See http://man7.org/linux/man-pages/man2/eventfd.2.html
uint64_t value = 0;
long ret = read(non_delayed_fd_, &value, sizeof(value));
DPCHECK(ret >= 0);
DCHECK_GT(value, 0U);
bool do_idle_work = value == kTryNativeWorkBeforeIdleBit;
DoNonDelayedLooperWork(do_idle_work);
}
void MessagePumpForUI::DoNonDelayedLooperWork(bool do_idle_work) {
// Note: We can't skip DoWork() even if |do_idle_work| is true here (i.e. no
// additional ScheduleWork() since yielding to native) as delayed tasks might
// have come in and we need to re-sample |next_work_info|.
// Runs all application tasks scheduled to run.
Delegate::NextWorkInfo next_work_info;
do {
if (ShouldQuit())
return;
next_work_info = delegate_->DoWork();
// If we are prioritizing native, and the next work would normally run
// immediately, skip the next work and let the native work items have a
// chance to run. This is useful when user input is waiting for native to
// have a chance to run.
if (next_work_info.is_immediate() && next_work_info.yield_to_native) {
ScheduleWork();
return;
}
} while (next_work_info.is_immediate());
// Do not resignal |non_delayed_fd_| if we're quitting (this pump doesn't
// allow nesting so needing to resume in an outer loop is not an issue
// either).
if (ShouldQuit())
return;
// Before declaring this loop idle, yield to native work items and arrange to
// be called again (unless we're already in that second call).
if (!do_idle_work) {
ScheduleWorkInternal(/*do_idle_work=*/true);
return;
}
// We yielded to native work items already and they didn't generate a
// ScheduleWork() request so we can declare idleness. It's possible for a
// ScheduleWork() request to come in racily while this method unwinds, this is
// fine and will merely result in it being re-invoked shortly after it
// returns.
// TODO(scheduler-dev): this doesn't account for tasks that don't ever call
// SchedulerWork() but still keep the system non-idle (e.g., the Java Handler
// API). It would be better to add an API to query the presence of native
// tasks instead of relying on yielding once + kTryNativeWorkBeforeIdleBit.
DCHECK(do_idle_work);
if (ShouldQuit())
return;
// At this point, the java looper might not be idle - it's impossible to know
// pre-Android-M, so we may end up doing Idle work while java tasks are still
// queued up. Note that this won't cause us to fail to run java tasks using
// QuitWhenIdle, as the JavaHandlerThread will finish running all currently
// scheduled tasks before it quits. Also note that we can't just add an idle
// callback to the java looper, as that will fire even if application tasks
// are still queued up.
DoIdleWork();
if (!next_work_info.delayed_run_time.is_max()) {
ScheduleDelayedWork(next_work_info);
}
}
void MessagePumpForUI::DoIdleWork() {
if (delegate_->DoIdleWork()) {
// If DoIdleWork() resulted in any work, we're not idle yet. We need to pump
// the loop here because we may in fact be idle after doing idle work
// without any new tasks being queued.
ScheduleWork();
}
}
void MessagePumpForUI::Run(Delegate* delegate) {
CHECK(false) << "Unexpected call to Run()";
}
void MessagePumpForUI::Attach(Delegate* delegate) {
DCHECK(!quit_);
// Since the Looper is controlled by the UI thread or JavaHandlerThread, we
// can't use Run() like we do on other platforms or we would prevent Java
// tasks from running. Instead we create and initialize a run loop here, then
// return control back to the Looper.
SetDelegate(delegate);
run_loop_ = std::make_unique<RunLoop>();
// Since the RunLoop was just created above, BeforeRun should be guaranteed to
// return true (it only returns false if the RunLoop has been Quit already).
if (!run_loop_->BeforeRun())
NOTREACHED();
}
void MessagePumpForUI::Quit() {
if (quit_)
return;
quit_ = true;
int64_t value;
// Clear any pending timer.
read(delayed_fd_, &value, sizeof(value));
// Clear the eventfd.
read(non_delayed_fd_, &value, sizeof(value));
if (run_loop_) {
run_loop_->AfterRun();
run_loop_ = nullptr;
}
if (on_quit_callback_) {
std::move(on_quit_callback_).Run();
}
}
void MessagePumpForUI::ScheduleWork() {
ScheduleWorkInternal(/*do_idle_work=*/false);
}
void MessagePumpForUI::ScheduleWorkInternal(bool do_idle_work) {
// Write (add) |value| to the eventfd. This tells the Looper to wake up and
// call our callback, allowing us to run tasks. This also allows us to detect,
// when we clear the fd, whether additional work was scheduled after we
// finished performing work, but before we cleared the fd, as we'll read back
// >=2 instead of 1 in that case. See the eventfd man pages
// (http://man7.org/linux/man-pages/man2/eventfd.2.html) for details on how
// the read and write APIs for this file descriptor work, specifically without
// EFD_SEMAPHORE.
// Note: Calls with |do_idle_work| set to true may race with potential calls
// where the parameter is false. This is fine as write() is adding |value|,
// not overwriting the existing value, and as such racing calls would merely
// have their values added together. Since idle work is only executed when the
// value read equals kTryNativeWorkBeforeIdleBit, a race would prevent idle
// work from being run and trigger another call to this method with
// |do_idle_work| set to true.
uint64_t value = do_idle_work ? kTryNativeWorkBeforeIdleBit : 1;
long ret = write(non_delayed_fd_, &value, sizeof(value));
DPCHECK(ret >= 0);
}
void MessagePumpForUI::ScheduleDelayedWork(
const Delegate::NextWorkInfo& next_work_info) {
if (ShouldQuit())
return;
if (delayed_scheduled_time_ &&
*delayed_scheduled_time_ == next_work_info.delayed_run_time) {
return;
}
DCHECK(!next_work_info.is_immediate());
delayed_scheduled_time_ = next_work_info.delayed_run_time;
int64_t nanos =
next_work_info.delayed_run_time.since_origin().InNanoseconds();
struct itimerspec ts;
ts.it_interval.tv_sec = 0; // Don't repeat.
ts.it_interval.tv_nsec = 0;
ts.it_value.tv_sec =
static_cast<time_t>(nanos / TimeTicks::kNanosecondsPerSecond);
ts.it_value.tv_nsec = nanos % TimeTicks::kNanosecondsPerSecond;
long ret = timerfd_settime(delayed_fd_, TFD_TIMER_ABSTIME, &ts, nullptr);
DPCHECK(ret >= 0);
}
void MessagePumpForUI::QuitWhenIdle(base::OnceClosure callback) {
DCHECK(!on_quit_callback_);
DCHECK(run_loop_);
on_quit_callback_ = std::move(callback);
run_loop_->QuitWhenIdle();
// Pump the loop in case we're already idle.
ScheduleWork();
}
MessagePump::Delegate* MessagePumpForUI::SetDelegate(Delegate* delegate) {
return std::exchange(delegate_, delegate);
}
bool MessagePumpForUI::SetQuit(bool quit) {
return std::exchange(quit_, quit);
}
} // namespace base
|