summaryrefslogtreecommitdiffstats
path: root/dom/workers/WorkerRef.h
blob: f1dc4deb1b117e2fdb9ade5d001fb41391bc71d6 (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
/* -*- 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/. */

#ifndef mozilla_dom_workers_WorkerRef_h
#define mozilla_dom_workers_WorkerRef_h

#include "mozilla/dom/WorkerStatus.h"
#include "mozilla/MoveOnlyFunction.h"
#include "mozilla/RefPtr.h"
#include "nsISupports.h"
#include "nsTString.h"

#ifdef DEBUG
#  include "mozilla/Mutex.h"
#endif

namespace mozilla::dom {

/*
 * If you want to play with a DOM Worker, you must know that it can go away
 * at any time if nothing prevents its shutting down. This documentation helps
 * to understand how to play with DOM Workers correctly.
 *
 * There are several reasons why a DOM Worker could go away. Here is the
 * complete list:
 *
 * a. GC/CC - If the DOM Worker thread is idle and the Worker object is garbage
 *    collected, it goes away.
 * b. The worker script can call self.close()
 * c. The Worker object calls worker.terminate()
 * d. Firefox is shutting down.
 *
 * When a DOM Worker goes away, it does several steps. See more in
 * WorkerStatus.h. The DOM Worker thread will basically stop scheduling
 * WorkerRunnables, and eventually WorkerControlRunnables. But if there is
 * something preventing the shutting down, it will always possible to dispatch
 * WorkerControlRunnables.  Of course, at some point, the worker _must_ be
 * released, otherwise firefox will leak it and the browser shutdown will hang.
 *
 * WeakWorkerRef is a refcounted, NON thread-safe object.
 *
 * From this object, you can obtain a WorkerPrivate, calling
 * WeakWorkerRef::GetPrivate(). It returns nullptr if the worker is shutting
 * down or if it is already gone away.
 *
 * If you want to know when a DOM Worker starts the shutting down procedure,
 * pass a callback to the mozilla::dom::WeakWorkerRef::Create() method.
 * Your function will be called. Note that _after_ the callback,
 * WeakWorkerRef::GetPrivate() will return nullptr.
 *
 * How to keep a DOM Worker alive?
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * If you need to keep the worker alive, you must use StrongWorkerRef.
 * You can have this refcounted, NON thread-safe object, calling
 * mozilla::dom::StrongWorkerRef::Create(WorkerPrivate* aWorkerPrivate);
 *
 * If you have a StrongWorkerRef:
 * a. the DOM Worker is kept alive.
 * b. you can have access to the WorkerPrivate, calling: Private().
 * c. WorkerControlRunnable can be dispatched.
 *
 * Note that the DOM Worker shutdown can start at any time, but having a
 * StrongWorkerRef prevents the full shutdown. Also with StrongWorkerRef, you
 * can pass a callback when calling mozilla::dom::StrongWorkerRef::Create().
 *
 * When the DOM Worker shutdown starts, WorkerRunnable cannot be dispatched
 * anymore. At this point, you should dispatch WorkerControlRunnable just to
 * release resources.
 *
 * How to have a thread-safe DOM Worker reference?
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * Sometimes you need to play with threads and you need a thread-safe worker
 * reference. ThreadSafeWorkerRef is what you want.
 *
 * Just because this object can be sent to different threads, we don't allow the
 * setting of a callback. It would be confusing.
 *
 * ThreadSafeWorkerRef can be destroyed in any thread. Internally it keeps a
 * reference to its StrongWorkerRef creator and this ref will be dropped on the
 * correct thread when the ThreadSafeWorkerRef is deleted.
 *
 * IPC WorkerRef
 * ~~~~~~~~~~~~~
 *
 * IPDL protocols require a correct shutdown sequence. Because of this, they
 * need a special configuration:
 * 1. they need to be informed when the Worker starts the shutting down
 * 2. they don't want to prevent the shutdown
 * 3. but at the same time, they need to block the shutdown until the WorkerRef
 *    is not longer alive.
 *
 * Point 1 is a standard feature of WorkerRef; point 2 is similar to
 * WeakWorkerRef; point 3 is similar to StrongWorkerRef.
 *
 * You can create a special IPC WorkerRef using this static method:
 * mozilla::dom::IPCWorkerRef::Create(WorkerPrivate* aWorkerPrivate,
 *                                    const char* * aName);
 */

class WorkerPrivate;
class StrongWorkerRef;
class ThreadSafeWorkerRef;

#ifdef DEBUG  // In debug mode, provide a way for clients to annotate WorkerRefs
#  define SET_WORKERREF_DEBUG_STATUS(workerref, str) \
    ((workerref)->DebugSetWorkerRefStatus(str))
#  define GET_WORKERREF_DEBUG_STATUS(workerref) \
    ((workerref)->DebugGetWorkerRefStatus())
#else
#  define SET_WORKERREF_DEBUG_STATUS(workerref, str) (void())
#  define GET_WORKERREF_DEBUG_STATUS(workerref) (EmptyCString())
#endif

class WorkerRef {
  friend class WorkerPrivate;

 public:
  NS_INLINE_DECL_REFCOUNTING(WorkerRef)

#ifdef DEBUG
  mutable Mutex mDebugMutex;
  nsCString mDebugStatus MOZ_GUARDED_BY(mDebugMutex);

  void DebugSetWorkerRefStatus(const nsCString& aStatus) {
    MutexAutoLock lock(mDebugMutex);
    mDebugStatus = aStatus;
  }

  const nsCString DebugGetWorkerRefStatus() const {
    MutexAutoLock lock(mDebugMutex);
    return mDebugStatus;
  }
#endif

 protected:
  WorkerRef(WorkerPrivate* aWorkerPrivate, const char* aName,
            bool aIsPreventingShutdown);
  virtual ~WorkerRef();

  virtual void Notify();

  bool HoldWorker(WorkerStatus aStatus);
  void ReleaseWorker();

  bool IsPreventingShutdown() const { return mIsPreventingShutdown; }

  const char* Name() const { return mName; }

  WorkerPrivate* mWorkerPrivate;

  MoveOnlyFunction<void()> mCallback;
  const char* const mName;
  const bool mIsPreventingShutdown;

  // True if this WorkerRef has been added to a WorkerPrivate.
  bool mHolding;
};

class WeakWorkerRef final : public WorkerRef {
 public:
  static already_AddRefed<WeakWorkerRef> Create(
      WorkerPrivate* aWorkerPrivate,
      MoveOnlyFunction<void()>&& aCallback = nullptr);

  WorkerPrivate* GetPrivate() const;

  // This can be called on any thread. It's racy and, in general, the wrong
  // choice.
  WorkerPrivate* GetUnsafePrivate() const;

 private:
  friend class ThreadSafeWeakWorkerRef;

  explicit WeakWorkerRef(WorkerPrivate* aWorkerPrivate);
  ~WeakWorkerRef();

  void Notify() override;
};

class StrongWorkerRef final : public WorkerRef {
 public:
  static already_AddRefed<StrongWorkerRef> Create(
      WorkerPrivate* aWorkerPrivate, const char* aName,
      MoveOnlyFunction<void()>&& aCallback = nullptr);

  // This function creates a StrongWorkerRef even when in the Canceling state of
  // the worker's lifecycle. It's intended to be used by system code, e.g. code
  // that needs to perform IPC.
  //
  // This method should only be used in cases where the StrongWorkerRef will be
  // used for an extremely bounded duration that cannot be impacted by content.
  // For example, IPCStreams use this type of ref in order to immediately
  // migrate to an actor on another thread. Whether the IPCStream ever actually
  // is streamed does not matter; the ref will be dropped once the new actor is
  // created. For this reason, this method does not take a callback. It's
  // expected and required that callers will drop the reference when they are
  // done.
  static already_AddRefed<StrongWorkerRef> CreateForcibly(
      WorkerPrivate* aWorkerPrivate, const char* aName);

  WorkerPrivate* Private() const;

 private:
  friend class WeakWorkerRef;
  friend class ThreadSafeWorkerRef;

  static already_AddRefed<StrongWorkerRef> CreateImpl(
      WorkerPrivate* aWorkerPrivate, const char* aName,
      WorkerStatus aFailStatus);

  StrongWorkerRef(WorkerPrivate* aWorkerPrivate, const char* aName);
  ~StrongWorkerRef();
};

class ThreadSafeWorkerRef final {
 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ThreadSafeWorkerRef)

  explicit ThreadSafeWorkerRef(StrongWorkerRef* aRef);

  WorkerPrivate* Private() const;

#ifdef DEBUG
  RefPtr<StrongWorkerRef>& Ref() { return mRef; }
#endif

 private:
  friend class StrongWorkerRef;

  ~ThreadSafeWorkerRef();

  RefPtr<StrongWorkerRef> mRef;
};

class IPCWorkerRef final : public WorkerRef {
 public:
  static already_AddRefed<IPCWorkerRef> Create(
      WorkerPrivate* aWorkerPrivate, const char* aName,
      MoveOnlyFunction<void()>&& aCallback = nullptr);

  WorkerPrivate* Private() const;

  void SetActorCount(uint32_t aCount);

 private:
  IPCWorkerRef(WorkerPrivate* aWorkerPrivate, const char* aName);
  ~IPCWorkerRef();

  // The count of background actors which binding with this IPCWorkerRef.
  uint32_t mActorCount;
};

// Template class to keep an Actor pointer, as a raw pointer, in a ref-counted
// way when passed to lambdas.
template <class ActorPtr>
class IPCWorkerRefHelper final {
 public:
  NS_INLINE_DECL_REFCOUNTING(IPCWorkerRefHelper);

  explicit IPCWorkerRefHelper(ActorPtr* aActor) : mActor(aActor) {}

  ActorPtr* Actor() const { return mActor; }

 private:
  ~IPCWorkerRefHelper() = default;

  // Raw pointer
  ActorPtr* mActor;
};

}  // namespace mozilla::dom

#endif /* mozilla_dom_workers_WorkerRef_h */