summaryrefslogtreecommitdiffstats
path: root/gfx/layers/apz/util/APZThreadUtils.cpp
blob: 63db9768347180ea032c782c38be45788a836f4b (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "APZThreadUtils.h"

#include "mozilla/ClearOnShutdown.h"
#include "mozilla/StaticMutex.h"

#include "nsISerialEventTarget.h"
#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"

namespace mozilla {
namespace layers {

static bool sThreadAssertionsEnabled = true;
static StaticRefPtr<nsISerialEventTarget> sControllerThread;
static StaticMutex sControllerThreadMutex;

/*static*/
void APZThreadUtils::SetThreadAssertionsEnabled(bool aEnabled) {
  StaticMutexAutoLock lock(sControllerThreadMutex);
  sThreadAssertionsEnabled = aEnabled;
}

/*static*/
bool APZThreadUtils::GetThreadAssertionsEnabled() {
  StaticMutexAutoLock lock(sControllerThreadMutex);
  return sThreadAssertionsEnabled;
}

/*static*/
void APZThreadUtils::SetControllerThread(nsISerialEventTarget* aThread) {
  // We must either be setting the initial controller thread, or removing it,
  // or re-using an existing controller thread.
  StaticMutexAutoLock lock(sControllerThreadMutex);
  MOZ_ASSERT(!sControllerThread || !aThread || sControllerThread == aThread);
  if (aThread != sControllerThread) {
    // This can only happen once, on startup.
    sControllerThread = aThread;
    ClearOnShutdown(&sControllerThread);
  }
}

/*static*/
void APZThreadUtils::AssertOnControllerThread() {
#if DEBUG
  if (!GetThreadAssertionsEnabled()) {
    return;
  }
  StaticMutexAutoLock lock(sControllerThreadMutex);
  MOZ_ASSERT(sControllerThread && sControllerThread->IsOnCurrentThread());
#endif
}

/*static*/
void APZThreadUtils::RunOnControllerThread(RefPtr<Runnable>&& aTask) {
  RefPtr<nsISerialEventTarget> thread;
  {
    StaticMutexAutoLock lock(sControllerThreadMutex);
    thread = sControllerThread;
  }
  RefPtr<Runnable> task = std::move(aTask);

  if (!thread) {
    // Could happen on startup or if Shutdown() got called.
    NS_WARNING("Dropping task posted to controller thread");
    return;
  }

  if (thread->IsOnCurrentThread()) {
    task->Run();
  } else {
    thread->Dispatch(task.forget());
  }
}

/*static*/
bool APZThreadUtils::IsControllerThread() {
  StaticMutexAutoLock lock(sControllerThreadMutex);
  return sControllerThread && sControllerThread->IsOnCurrentThread();
}

/*static*/
bool APZThreadUtils::IsControllerThreadAlive() {
  StaticMutexAutoLock lock(sControllerThreadMutex);
  return !!sControllerThread;
}

/*static*/
void APZThreadUtils::DelayedDispatch(already_AddRefed<Runnable> aRunnable,
                                     int aDelayMs) {
  MOZ_ASSERT(!XRE_IsContentProcess(),
             "ContentProcessController should only be used remotely.");
  RefPtr<nsISerialEventTarget> thread;
  {
    StaticMutexAutoLock lock(sControllerThreadMutex);
    thread = sControllerThread;
  }
  if (!thread) {
    // Could happen on startup
    NS_WARNING("Dropping task posted to controller thread");
    return;
  }
  if (aDelayMs) {
    thread->DelayedDispatch(std::move(aRunnable), aDelayMs);
  } else {
    thread->Dispatch(std::move(aRunnable));
  }
}

NS_IMPL_ISUPPORTS(GenericNamedTimerCallbackBase, nsITimerCallback, nsINamed)

}  // namespace layers
}  // namespace mozilla