1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include
"nsISupports.idl"
9 [ptr] native PRThread
(PRThread
);
10 native ThreadCreationOptions
(nsIThreadManager
::ThreadCreationOptions
);
12 interface nsIEventTarget
;
13 interface nsIRunnable
;
17 #include
"mozilla/Maybe.h"
20 [scriptable
, function
, uuid(039a227d
-0cb7
-44a5
-a8f9
-dbb7071979f2
)]
21 interface nsINestedEventLoopCondition
: nsISupports
24 * Returns true if the current nested event loop should stop spinning.
30 * An interface for creating and locating nsIThread instances.
32 [scriptable
, builtinclass
, uuid(1be89eca
-e2f7
-453b
-8d38
-c11ba247f6f3
)]
33 interface nsIThreadManager
: nsISupports
36 * Default number of bytes reserved for a thread's stack, if no stack size
37 * is specified in newThread().
39 * Defaults can be a little overzealous for many platforms.
41 * On Linux and OS X, for instance, the default thread stack size is whatever
42 * getrlimit(RLIMIT_STACK) returns, which is often set at 8MB. Or, on Linux,
43 * if the stack size is unlimited, we fall back to 2MB. This causes particular
44 * problems on Linux, which allocates 2MB huge VM pages, and will often
45 * immediately allocate them for any stacks which are 2MB or larger.
47 * The default on Windows is 1MB, which is a little more reasonable. But the
48 * vast majority of our threads don't need anywhere near that much space.
50 * ASan, TSan and non-opt builds, however, often need a bit more, so give
51 * them the platform default.
54 #if defined
(MOZ_ASAN
) || defined
(MOZ_TSAN
) ||
!defined
(__OPTIMIZE__
)
55 static constexpr uint32_t DEFAULT_STACK_SIZE
= 0;
57 static constexpr uint32_t DEFAULT_STACK_SIZE
= 256 * 1024;
60 static
const uint32_t kThreadPoolStackSize
= DEFAULT_STACK_SIZE
;
62 struct ThreadCreationOptions
{
63 // The size in bytes to reserve for the thread's stack. A value of `0` means
64 // to use the platform default.
65 uint32_t stackSize
= nsIThreadManager
::DEFAULT_STACK_SIZE
;
67 // If set to `true`, any attempts to dispatch runnables to this thread
68 // without `DISPATCH_IGNORE_BLOCK_DISPATCH` will fail.
70 // This is intended to be used for threads which are expected to generally
71 // only service a single runnable (other than thread lifecycle runnables),
72 // and perform their own event dispatching internaly, such as thread pool
73 // threads or the timer thread.
74 bool blockDispatch
= false
;
76 // (Windows-only) Whether the thread should have a MessageLoop capable of
77 // processing native UI events. Defaults to false.
78 bool isUiThread
= false
;
80 // If set, long task markers will be collected for tasks
81 // longer than longTaskLength ms when profiling is enabled.
82 // See https://www.w3.org/TR/longtasks
83 mozilla
::Maybe<uint32_t
> longTaskLength
;
88 * Create a new thread (a global, user PRThread) with the specified name.
91 * The name of the thread. If it is empty the thread will not be named.
93 * Configuration options for the newly created thread.
96 * The newly created nsIThread object.
98 [noscript
] nsIThread newNamedThread
(in ACString name
, in ThreadCreationOptions options
);
101 * Get the main thread.
103 readonly attribute nsIThread mainThread
;
106 * Get the current thread. If the calling thread does not already have a
107 * nsIThread associated with it, then a new nsIThread will be created and
108 * associated with the current PRThread.
110 readonly attribute nsIThread currentThread
;
113 * This queues a runnable to the main thread. It's a shortcut for JS callers
114 * to be used instead of
115 * .mainThread.dispatch(runnable, Ci.nsIEventTarget.DISPATCH_NORMAL);
117 * .currentThread.dispatch(runnable, Ci.nsIEventTarget.DISPATCH_NORMAL);
118 * C++ callers should instead use NS_DispatchToMainThread.
121 void dispatchToMainThread
(in nsIRunnable event
, [optional] in uint32_t priority
);
124 * Similar to dispatchToMainThread, but wraps the event with extra
125 * runnable that allocates nsAutoMicroTask.
128 void dispatchToMainThreadWithMicroTask
(in nsIRunnable event
, [optional] in uint32_t priority
);
131 * This queues a runnable to the main thread's idle queue.
134 * The event to dispatch.
136 * The time in milliseconds until this event should be moved from the idle
137 * queue to the regular queue if it hasn't been executed by then. If not
138 * passed or a zero value is specified, the event will never be moved to
141 void idleDispatchToMainThread
(in nsIRunnable event
,
142 [optional] in uint32_t timeout
);
145 * A helper method to dispatch a task through nsIDirectTaskDispatcher to the
148 void dispatchDirectTaskToCurrentThread
(in nsIRunnable event
);
151 * Enter a nested event loop on the current thread, waiting on, and
152 * processing events until condition.isDone() returns true.
154 * If condition.isDone() throws, this function will throw as well.
156 * C++ code should not use this function, instead preferring
157 * mozilla::SpinEventLoopUntil.
159 void spinEventLoopUntil
(in ACString aVeryGoodReasonToDoThis
, in nsINestedEventLoopCondition condition
);
162 * Similar to the previous method, but the spinning of the event loop
163 * terminates when the quit application shutting down starts.
165 * C++ code should not use this function, instead preferring
166 * mozilla::SpinEventLoopUntil.
168 void spinEventLoopUntilOrQuit
(in ACString aVeryGoodReasonToDoThis
, in nsINestedEventLoopCondition condition
);
171 * Spin the current thread's event loop until there are no more pending
172 * events. This could be done with spinEventLoopUntil, but that would
173 * require access to the current thread from JavaScript, which we are
176 void spinEventLoopUntilEmpty
();
179 * Return the EventTarget for the main thread.
181 readonly attribute nsIEventTarget mainThreadEventTarget
;