summaryrefslogtreecommitdiffstats
path: root/widget/android/AndroidUiThread.cpp
blob: dc8d56b163600eaec85832dade24612813829bb9 (plain)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "base/message_loop.h"
#include "mozilla/Atomics.h"
#include "mozilla/EventQueue.h"
#include "mozilla/java/GeckoThreadWrappers.h"
#include "mozilla/LinkedList.h"
#include "mozilla/Monitor.h"
#include "mozilla/Mutex.h"
#include "mozilla/RefPtr.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/ThreadEventQueue.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/UniquePtr.h"
#include "GeckoProfiler.h"
#include "nsThread.h"
#include "nsThreadManager.h"
#include "nsThreadUtils.h"

#include <android/api-level.h>
#include <pthread.h>

using namespace mozilla;

namespace {

class AndroidUiThread;
class AndroidUiTask;

StaticAutoPtr<LinkedList<AndroidUiTask> > sTaskQueue;
StaticAutoPtr<mozilla::Mutex> sTaskQueueLock;
StaticRefPtr<AndroidUiThread> sThread;
static bool sThreadDestroyed;
static MessageLoop* sMessageLoop;
static Atomic<Monitor*> sMessageLoopAccessMonitor;

void EnqueueTask(already_AddRefed<nsIRunnable> aTask, int aDelayMs);

/*
 * The AndroidUiThread is derived from nsThread so that nsIRunnable objects that
 * get dispatched may be intercepted. Only nsIRunnable objects that need to be
 * synchronously executed are passed into the nsThread to be queued. All other
 * nsIRunnable object are immediately dispatched to the Android UI thread.
 * AndroidUiThread is derived from nsThread instead of being an nsIEventTarget
 * wrapper that contains an nsThread object because if nsIRunnable objects with
 * a delay were dispatch directly to an nsThread object, such as obtained from
 * nsThreadManager::GetCurrentThread(), the nsIRunnable could get stuck in the
 * nsThread nsIRunnable queue. This is due to the fact that Android controls the
 * event loop in the Android UI thread and has no knowledge of when the nsThread
 * needs to be drained.
 */

class AndroidUiThread : public nsThread {
 public:
  NS_INLINE_DECL_REFCOUNTING_INHERITED(AndroidUiThread, nsThread)
  AndroidUiThread()
      : nsThread(
            MakeNotNull<ThreadEventQueue*>(MakeUnique<mozilla::EventQueue>()),
            nsThread::NOT_MAIN_THREAD, {.stackSize = 0}) {}

  nsresult Dispatch(already_AddRefed<nsIRunnable> aEvent,
                    uint32_t aFlags) override;
  nsresult DelayedDispatch(already_AddRefed<nsIRunnable> aEvent,
                           uint32_t aDelayMs) override;

 private:
  ~AndroidUiThread() {}
};

NS_IMETHODIMP
AndroidUiThread::Dispatch(already_AddRefed<nsIRunnable> aEvent,
                          uint32_t aFlags) {
  EnqueueTask(std::move(aEvent), 0);
  return NS_OK;
}

NS_IMETHODIMP
AndroidUiThread::DelayedDispatch(already_AddRefed<nsIRunnable> aEvent,
                                 uint32_t aDelayMs) {
  EnqueueTask(std::move(aEvent), aDelayMs);
  return NS_OK;
}

static void PumpEvents() { NS_ProcessPendingEvents(sThread.get()); }

class ThreadObserver : public nsIThreadObserver {
 public:
  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSITHREADOBSERVER

  ThreadObserver() {}

 private:
  virtual ~ThreadObserver() {}
};

NS_IMPL_ISUPPORTS(ThreadObserver, nsIThreadObserver)

NS_IMETHODIMP
ThreadObserver::OnDispatchedEvent() {
  EnqueueTask(NS_NewRunnableFunction("PumpEvents", &PumpEvents), 0);
  return NS_OK;
}

NS_IMETHODIMP
ThreadObserver::OnProcessNextEvent(nsIThreadInternal* thread, bool mayWait) {
  return NS_OK;
}

NS_IMETHODIMP
ThreadObserver::AfterProcessNextEvent(nsIThreadInternal* thread,
                                      bool eventWasProcessed) {
  return NS_OK;
}

class AndroidUiTask : public LinkedListElement<AndroidUiTask> {
  using TimeStamp = mozilla::TimeStamp;
  using TimeDuration = mozilla::TimeDuration;

 public:
  explicit AndroidUiTask(already_AddRefed<nsIRunnable> aTask)
      : mTask(aTask),
        mRunTime()  // Null timestamp representing no delay.
  {}

  AndroidUiTask(already_AddRefed<nsIRunnable> aTask, int aDelayMs)
      : mTask(aTask),
        mRunTime(TimeStamp::Now() + TimeDuration::FromMilliseconds(aDelayMs)) {}

  bool IsEarlierThan(const AndroidUiTask& aOther) const {
    if (mRunTime) {
      return aOther.mRunTime ? mRunTime < aOther.mRunTime : false;
    }
    // In the case of no delay, we're earlier if aOther has a delay.
    // Otherwise, we're not earlier, to maintain task order.
    return !!aOther.mRunTime;
  }

  int64_t MillisecondsToRunTime() const {
    if (mRunTime) {
      return int64_t((mRunTime - TimeStamp::Now()).ToMilliseconds());
    }
    return 0;
  }

  already_AddRefed<nsIRunnable> TakeTask() { return mTask.forget(); }

 private:
  nsCOMPtr<nsIRunnable> mTask;
  const TimeStamp mRunTime;
};

class CreateOnUiThread : public Runnable {
 public:
  CreateOnUiThread() : Runnable("CreateOnUiThread") {}

  NS_IMETHOD Run() override {
    MOZ_ASSERT(!sThreadDestroyed);
    MOZ_ASSERT(sMessageLoopAccessMonitor);
    MonitorAutoLock lock(*sMessageLoopAccessMonitor);
    sThread = new AndroidUiThread();
    sThread->InitCurrentThread();
    sThread->SetObserver(new ThreadObserver());
    RegisterThreadWithProfiler();
    sMessageLoop =
        new MessageLoop(MessageLoop::TYPE_MOZILLA_ANDROID_UI, sThread.get());
    lock.NotifyAll();
    return NS_OK;
  }

 private:
  static void RegisterThreadWithProfiler() {
#if defined(MOZ_GECKO_PROFILER)
    // We don't use the PROFILER_REGISTER_THREAD macro here because by this
    // point the Android UI thread is already quite a ways into its stack;
    // the profiler's sampler thread will ignore a lot of frames if we do not
    // provide a better value for the stack top. We'll manually obtain that
    // info via pthreads.

    // Fallback address if any pthread calls fail
    char fallback;
    char* stackTop = &fallback;

    auto regOnExit = MakeScopeExit(
        [&stackTop]() { profiler_register_thread("AndroidUI", stackTop); });

    // Bionic does not properly support pthread_attr_getstack for the UI thread
    // until Lollipop (API 21).
#  if __ANDROID_API__ >= __ANDROID_API_L__
    pthread_attr_t attrs;
    if (pthread_getattr_np(pthread_self(), &attrs)) {
      return;
    }

    void* stackBase;
    size_t stackSize;
    if (pthread_attr_getstack(&attrs, &stackBase, &stackSize)) {
      return;
    }

    stackTop = static_cast<char*>(stackBase) + stackSize - 1;
#  endif  // __ANDROID_API__ >= __ANDROID_API_L__
#endif    // defined(MOZ_GECKO_PROFILER)
  }
};

class DestroyOnUiThread : public Runnable {
 public:
  DestroyOnUiThread() : Runnable("DestroyOnUiThread"), mDestroyed(false) {}

  NS_IMETHOD Run() override {
    MOZ_ASSERT(!sThreadDestroyed);
    MOZ_ASSERT(sMessageLoopAccessMonitor);
    MOZ_ASSERT(sTaskQueue);
    MonitorAutoLock lock(*sMessageLoopAccessMonitor);
    sThreadDestroyed = true;

    {
      // Flush the queue
      MutexAutoLock lock(*sTaskQueueLock);
      while (AndroidUiTask* task = sTaskQueue->getFirst()) {
        delete task;
      }
    }

    delete sMessageLoop;
    sMessageLoop = nullptr;
    MOZ_ASSERT(sThread);
    PROFILER_UNREGISTER_THREAD();
    nsThreadManager::get().UnregisterCurrentThread(*sThread);
    sThread = nullptr;
    mDestroyed = true;
    lock.NotifyAll();
    return NS_OK;
  }

  void WaitForDestruction() {
    MOZ_ASSERT(sMessageLoopAccessMonitor);
    MonitorAutoLock lock(*sMessageLoopAccessMonitor);
    while (!mDestroyed) {
      lock.Wait();
    }
  }

 private:
  bool mDestroyed;
};

void EnqueueTask(already_AddRefed<nsIRunnable> aTask, int aDelayMs) {
  if (sThreadDestroyed) {
    return;
  }

  // add the new task into the sTaskQueue, sorted with
  // the earliest task first in the queue
  AndroidUiTask* newTask =
      (aDelayMs ? new AndroidUiTask(std::move(aTask), aDelayMs)
                : new AndroidUiTask(std::move(aTask)));

  bool headOfList = false;
  {
    MOZ_ASSERT(sTaskQueue);
    MOZ_ASSERT(sTaskQueueLock);
    MutexAutoLock lock(*sTaskQueueLock);

    AndroidUiTask* task = sTaskQueue->getFirst();

    while (task) {
      if (newTask->IsEarlierThan(*task)) {
        task->setPrevious(newTask);
        break;
      }
      task = task->getNext();
    }

    if (!newTask->isInList()) {
      sTaskQueue->insertBack(newTask);
    }
    headOfList = !newTask->getPrevious();
  }

  if (headOfList) {
    // if we're inserting it at the head of the queue, notify Java because
    // we need to get a callback at an earlier time than the last scheduled
    // callback
    java::GeckoThread::RequestUiThreadCallback(int64_t(aDelayMs));
  }
}

}  // namespace

namespace mozilla {

void CreateAndroidUiThread() {
  MOZ_ASSERT(!sThread);
  MOZ_ASSERT(!sMessageLoopAccessMonitor);
  sTaskQueue = new LinkedList<AndroidUiTask>();
  sTaskQueueLock = new Mutex("AndroidUiThreadTaskQueueLock");
  sMessageLoopAccessMonitor =
      new Monitor("AndroidUiThreadMessageLoopAccessMonitor");
  sThreadDestroyed = false;
  RefPtr<CreateOnUiThread> runnable = new CreateOnUiThread;
  EnqueueTask(do_AddRef(runnable), 0);
}

void DestroyAndroidUiThread() {
  MOZ_ASSERT(sThread);
  RefPtr<DestroyOnUiThread> runnable = new DestroyOnUiThread;
  EnqueueTask(do_AddRef(runnable), 0);
  runnable->WaitForDestruction();
  delete sMessageLoopAccessMonitor;
  sMessageLoopAccessMonitor = nullptr;
}

MessageLoop* GetAndroidUiThreadMessageLoop() {
  if (!sMessageLoopAccessMonitor) {
    return nullptr;
  }

  MonitorAutoLock lock(*sMessageLoopAccessMonitor);
  while (!sMessageLoop) {
    lock.Wait();
  }

  return sMessageLoop;
}

RefPtr<nsThread> GetAndroidUiThread() {
  if (!sMessageLoopAccessMonitor) {
    return nullptr;
  }

  MonitorAutoLock lock(*sMessageLoopAccessMonitor);
  while (!sThread) {
    lock.Wait();
  }

  return sThread;
}

int64_t RunAndroidUiTasks() {
  MutexAutoLock lock(*sTaskQueueLock);

  if (sThreadDestroyed) {
    return -1;
  }

  while (!sTaskQueue->isEmpty()) {
    AndroidUiTask* task = sTaskQueue->getFirst();
    const int64_t timeLeft = task->MillisecondsToRunTime();
    if (timeLeft > 0) {
      // this task (and therefore all remaining tasks)
      // have not yet reached their runtime. return the
      // time left until we should be called again
      return timeLeft;
    }

    // Retrieve task before unlocking/running.
    nsCOMPtr<nsIRunnable> runnable(task->TakeTask());
    // LinkedListElements auto remove from list upon destruction
    delete task;

    // Unlock to allow posting new tasks reentrantly.
    MutexAutoUnlock unlock(*sTaskQueueLock);
    runnable->Run();
    if (sThreadDestroyed) {
      return -1;
    }
  }
  return -1;
}

}  // namespace mozilla