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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
/* -*- 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 "nsThreadPool.h"
#include "nsCOMArray.h"
#include "ThreadDelay.h"
#include "nsThreadManager.h"
#include "nsThread.h"
#include "nsThreadUtils.h"
#include "prinrval.h"
#include "mozilla/Logging.h"
#include "mozilla/ProfilerLabels.h"
#include "mozilla/ProfilerRunnable.h"
#include "mozilla/SchedulerGroup.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "nsThreadSyncDispatch.h"
#include <mutex>
using namespace mozilla;
static LazyLogModule sThreadPoolLog("nsThreadPool");
#ifdef LOG
# undef LOG
#endif
#define LOG(args) MOZ_LOG(sThreadPoolLog, mozilla::LogLevel::Debug, args)
static MOZ_THREAD_LOCAL(nsThreadPool*) gCurrentThreadPool;
void nsThreadPool::InitTLS() { gCurrentThreadPool.infallibleInit(); }
// DESIGN:
// o Allocate anonymous threads.
// o Use nsThreadPool::Run as the main routine for each thread.
// o Each thread waits on the event queue's monitor, checking for
// pending events and rescheduling itself as an idle thread.
#define DEFAULT_THREAD_LIMIT 4
#define DEFAULT_IDLE_THREAD_LIMIT 1
#define DEFAULT_IDLE_THREAD_TIMEOUT PR_SecondsToInterval(60)
NS_IMPL_ISUPPORTS_INHERITED(nsThreadPool, Runnable, nsIThreadPool,
nsIEventTarget)
nsThreadPool* nsThreadPool::GetCurrentThreadPool() {
return gCurrentThreadPool.get();
}
nsThreadPool::nsThreadPool()
: Runnable("nsThreadPool"),
mMutex("[nsThreadPool.mMutex]"),
mEventsAvailable(mMutex, "[nsThreadPool.mEventsAvailable]"),
mThreadLimit(DEFAULT_THREAD_LIMIT),
mIdleThreadLimit(DEFAULT_IDLE_THREAD_LIMIT),
mIdleThreadTimeout(DEFAULT_IDLE_THREAD_TIMEOUT),
mIdleCount(0),
mQoSPriority(nsIThread::QOS_PRIORITY_NORMAL),
mStackSize(nsIThreadManager::DEFAULT_STACK_SIZE),
mShutdown(false),
mRegressiveMaxIdleTime(false),
mIsAPoolThreadFree(true) {
LOG(("THRD-P(%p) constructor!!!\n", this));
}
nsThreadPool::~nsThreadPool() {
// Threads keep a reference to the nsThreadPool until they return from Run()
// after removing themselves from mThreads.
MOZ_ASSERT(mThreads.IsEmpty());
}
nsresult nsThreadPool::PutEvent(nsIRunnable* aEvent) {
nsCOMPtr<nsIRunnable> event(aEvent);
return PutEvent(event.forget(), 0);
}
nsresult nsThreadPool::PutEvent(already_AddRefed<nsIRunnable> aEvent,
uint32_t aFlags) {
// Avoid spawning a new thread while holding the event queue lock...
bool spawnThread = false;
uint32_t stackSize = 0;
nsCString name;
{
MutexAutoLock lock(mMutex);
if (NS_WARN_IF(mShutdown)) {
return NS_ERROR_NOT_AVAILABLE;
}
LOG(("THRD-P(%p) put [%d %d %d]\n", this, mIdleCount, mThreads.Count(),
mThreadLimit));
MOZ_ASSERT(mIdleCount <= (uint32_t)mThreads.Count(), "oops");
// Make sure we have a thread to service this event.
if (mThreads.Count() < (int32_t)mThreadLimit &&
!(aFlags & NS_DISPATCH_AT_END) &&
// Spawn a new thread if we don't have enough idle threads to serve
// pending events immediately.
mEvents.Count(lock) >= mIdleCount) {
spawnThread = true;
}
nsCOMPtr<nsIRunnable> event(aEvent);
LogRunnable::LogDispatch(event);
mEvents.PutEvent(event.forget(), EventQueuePriority::Normal, lock);
mEventsAvailable.Notify();
stackSize = mStackSize;
name = mName;
}
auto delay = MakeScopeExit([&]() {
// Delay to encourage the receiving task to run before we do work.
DelayForChaosMode(ChaosFeature::TaskDispatching, 1000);
});
LOG(("THRD-P(%p) put [spawn=%d]\n", this, spawnThread));
if (!spawnThread) {
return NS_OK;
}
nsCOMPtr<nsIThread> thread;
nsresult rv = NS_NewNamedThread(
mThreadNaming.GetNextThreadName(name), getter_AddRefs(thread), nullptr,
{.stackSize = stackSize, .blockDispatch = true});
if (NS_WARN_IF(NS_FAILED(rv))) {
return NS_ERROR_UNEXPECTED;
}
bool killThread = false;
{
MutexAutoLock lock(mMutex);
if (mShutdown) {
killThread = true;
} else if (mThreads.Count() < (int32_t)mThreadLimit) {
mThreads.AppendObject(thread);
if (mThreads.Count() >= (int32_t)mThreadLimit) {
mIsAPoolThreadFree = false;
}
} else {
// Someone else may have also been starting a thread
killThread = true; // okay, we don't need this thread anymore
}
}
LOG(("THRD-P(%p) put [%p kill=%d]\n", this, thread.get(), killThread));
if (killThread) {
// We never dispatched any events to the thread, so we can shut it down
// asynchronously without worrying about anything.
ShutdownThread(thread);
} else {
thread->Dispatch(this, NS_DISPATCH_IGNORE_BLOCK_DISPATCH);
}
return NS_OK;
}
void nsThreadPool::ShutdownThread(nsIThread* aThread) {
LOG(("THRD-P(%p) shutdown async [%p]\n", this, aThread));
// This is either called by a threadpool thread that is out of work, or
// a thread that attempted to create a threadpool thread and raced in
// such a way that the newly created thread is no longer necessary.
// In the first case, we must go to another thread to shut aThread down
// (because it is the current thread). In the second case, we cannot
// synchronously shut down the current thread (because then Dispatch() would
// spin the event loop, and that could blow up the world), and asynchronous
// shutdown requires this thread have an event loop (and it may not, see bug
// 10204784). The simplest way to cover all cases is to asynchronously
// shutdown aThread from the main thread.
SchedulerGroup::Dispatch(
TaskCategory::Other,
NewRunnableMethod("nsIThread::AsyncShutdown", aThread,
&nsIThread::AsyncShutdown));
}
NS_IMETHODIMP
nsThreadPool::SetQoSForThreads(nsIThread::QoSPriority aPriority) {
MutexAutoLock lock(mMutex);
mQoSPriority = aPriority;
// We don't notify threads here to observe the change, because we don't want
// to create spurious wakeups during idle. Rather, we want threads to simply
// observe the change on their own if they wake up to do some task.
return NS_OK;
}
// This event 'runs' for the lifetime of the worker thread. The actual
// eventqueue is mEvents, and is shared by all the worker threads. This
// means that the set of threads together define the delay seen by a new
// event sent to the pool.
//
// To model the delay experienced by the pool, we can have each thread in
// the pool report 0 if it's idle OR if the pool is below the threadlimit;
// or otherwise the current event's queuing delay plus current running
// time.
//
// To reconstruct the delays for the pool, the profiler can look at all the
// threads that are part of a pool (pools have defined naming patterns that
// can be user to connect them). If all threads have delays at time X,
// that means that all threads saturated at that point and any event
// dispatched to the pool would get a delay.
//
// The delay experienced by an event dispatched when all pool threads are
// busy is based on the calculations shown in platform.cpp. Run that
// algorithm for each thread in the pool, and the delay at time X is the
// longest value for time X of any of the threads, OR the time from X until
// any one of the threads reports 0 (i.e. it's not busy), whichever is
// shorter.
// In order to record this when the profiler samples threads in the pool,
// each thread must (effectively) override GetRunnningEventDelay, by
// resetting the mLastEventDelay/Start values in the nsThread when we start
// to run an event (or when we run out of events to run). Note that handling
// the shutdown of a thread may be a little tricky.
NS_IMETHODIMP
nsThreadPool::Run() {
nsCOMPtr<nsIThread> current;
nsThreadManager::get().GetCurrentThread(getter_AddRefs(current));
bool shutdownThreadOnExit = false;
bool exitThread = false;
bool wasIdle = false;
TimeStamp idleSince;
nsIThread::QoSPriority threadPriority = nsIThread::QOS_PRIORITY_NORMAL;
// This thread is an nsThread created below with NS_NewNamedThread()
static_cast<nsThread*>(current.get())
->SetPoolThreadFreePtr(&mIsAPoolThreadFree);
nsCOMPtr<nsIThreadPoolListener> listener;
{
MutexAutoLock lock(mMutex);
listener = mListener;
LOG(("THRD-P(%p) enter %s\n", this, mName.BeginReading()));
// Go ahead and check for thread priority. If priority is normal, do nothing
// because threads are created with default priority.
if (threadPriority != mQoSPriority) {
current->SetThreadQoS(threadPriority);
threadPriority = mQoSPriority;
}
}
if (listener) {
listener->OnThreadCreated();
}
MOZ_ASSERT(!gCurrentThreadPool.get());
gCurrentThreadPool.set(this);
do {
nsCOMPtr<nsIRunnable> event;
TimeDuration delay;
{
MutexAutoLock lock(mMutex);
// Before getting the next event, we can adjust priority as needed.
if (threadPriority != mQoSPriority) {
current->SetThreadQoS(threadPriority);
threadPriority = mQoSPriority;
}
event = mEvents.GetEvent(lock, &delay);
if (!event) {
TimeStamp now = TimeStamp::Now();
uint32_t idleTimeoutDivider =
(mIdleCount && mRegressiveMaxIdleTime) ? mIdleCount : 1;
TimeDuration timeout = TimeDuration::FromMilliseconds(
static_cast<double>(mIdleThreadTimeout) / idleTimeoutDivider);
// If we are shutting down, then don't keep any idle threads.
if (mShutdown) {
exitThread = true;
} else {
if (wasIdle) {
// if too many idle threads or idle for too long, then bail.
if (mIdleCount > mIdleThreadLimit ||
(mIdleThreadTimeout != UINT32_MAX &&
(now - idleSince) >= timeout)) {
exitThread = true;
}
} else {
// if would be too many idle threads...
if (mIdleCount == mIdleThreadLimit) {
exitThread = true;
} else {
++mIdleCount;
idleSince = now;
wasIdle = true;
}
}
}
if (exitThread) {
if (wasIdle) {
--mIdleCount;
}
shutdownThreadOnExit = mThreads.RemoveObject(current);
// keep track if there are threads available to start
mIsAPoolThreadFree = (mThreads.Count() < (int32_t)mThreadLimit);
} else {
current->SetRunningEventDelay(TimeDuration(), TimeStamp());
AUTO_PROFILER_LABEL("nsThreadPool::Run::Wait", IDLE);
TimeDuration delta = timeout - (now - idleSince);
LOG(("THRD-P(%p) %s waiting [%f]\n", this, mName.BeginReading(),
delta.ToMilliseconds()));
mEventsAvailable.Wait(delta);
LOG(("THRD-P(%p) done waiting\n", this));
}
} else if (wasIdle) {
wasIdle = false;
--mIdleCount;
}
}
if (event) {
if (MOZ_LOG_TEST(sThreadPoolLog, mozilla::LogLevel::Debug)) {
MutexAutoLock lock(mMutex);
LOG(("THRD-P(%p) %s running [%p]\n", this, mName.BeginReading(),
event.get()));
}
// Delay event processing to encourage whoever dispatched this event
// to run.
DelayForChaosMode(ChaosFeature::TaskRunning, 1000);
if (profiler_thread_is_being_profiled(
ThreadProfilingFeatures::Sampling)) {
// We'll handle the case of unstarted threads available
// when we sample.
current->SetRunningEventDelay(delay, TimeStamp::Now());
}
LogRunnable::Run log(event);
AUTO_PROFILE_FOLLOWING_RUNNABLE(event);
event->Run();
// To cover the event's destructor code in the LogRunnable span
event = nullptr;
}
} while (!exitThread);
if (listener) {
listener->OnThreadShuttingDown();
}
MOZ_ASSERT(gCurrentThreadPool.get() == this);
gCurrentThreadPool.set(nullptr);
if (shutdownThreadOnExit) {
ShutdownThread(current);
}
LOG(("THRD-P(%p) leave\n", this));
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::DispatchFromScript(nsIRunnable* aEvent, uint32_t aFlags) {
nsCOMPtr<nsIRunnable> event(aEvent);
return Dispatch(event.forget(), aFlags);
}
NS_IMETHODIMP
nsThreadPool::Dispatch(already_AddRefed<nsIRunnable> aEvent, uint32_t aFlags) {
LOG(("THRD-P(%p) dispatch [%p %x]\n", this, /* XXX aEvent*/ nullptr, aFlags));
if (NS_WARN_IF(mShutdown)) {
return NS_ERROR_NOT_AVAILABLE;
}
NS_ASSERTION(aFlags == NS_DISPATCH_NORMAL || aFlags == NS_DISPATCH_AT_END,
"unexpected dispatch flags");
PutEvent(std::move(aEvent), aFlags);
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsThreadPool::RegisterShutdownTask(nsITargetShutdownTask*) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsThreadPool::UnregisterShutdownTask(nsITargetShutdownTask*) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP_(bool)
nsThreadPool::IsOnCurrentThreadInfallible() {
return gCurrentThreadPool.get() == this;
}
NS_IMETHODIMP
nsThreadPool::IsOnCurrentThread(bool* aResult) {
MutexAutoLock lock(mMutex);
if (NS_WARN_IF(mShutdown)) {
return NS_ERROR_NOT_AVAILABLE;
}
*aResult = IsOnCurrentThreadInfallible();
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::Shutdown() { return ShutdownWithTimeout(-1); }
NS_IMETHODIMP
nsThreadPool::ShutdownWithTimeout(int32_t aTimeoutMs) {
nsCOMArray<nsIThread> threads;
nsCOMPtr<nsIThreadPoolListener> listener;
{
MutexAutoLock lock(mMutex);
if (mShutdown) {
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
}
mShutdown = true;
mEventsAvailable.NotifyAll();
threads.AppendObjects(mThreads);
mThreads.Clear();
// Swap in a null listener so that we release the listener at the end of
// this method. The listener will be kept alive as long as the other threads
// that were created when it was set.
mListener.swap(listener);
}
nsTArray<nsCOMPtr<nsIThreadShutdown>> contexts;
for (int32_t i = 0; i < threads.Count(); ++i) {
nsCOMPtr<nsIThreadShutdown> context;
if (NS_SUCCEEDED(threads[i]->BeginShutdown(getter_AddRefs(context)))) {
contexts.AppendElement(std::move(context));
}
}
// Start a timer which will stop waiting & leak the thread, forcing
// onCompletion to be called when it expires.
nsCOMPtr<nsITimer> timer;
if (aTimeoutMs >= 0) {
NS_NewTimerWithCallback(
getter_AddRefs(timer),
[&](nsITimer*) {
for (auto& context : contexts) {
context->StopWaitingAndLeakThread();
}
},
aTimeoutMs, nsITimer::TYPE_ONE_SHOT,
"nsThreadPool::ShutdownWithTimeout");
}
// Start a counter and register a callback to decrement outstandingThreads
// when the threads finish exiting. We'll spin an event loop until
// outstandingThreads reaches 0.
uint32_t outstandingThreads = contexts.Length();
RefPtr onCompletion = NS_NewCancelableRunnableFunction(
"nsThreadPool thread completion", [&] { --outstandingThreads; });
for (auto& context : contexts) {
context->OnCompletion(onCompletion);
}
mozilla::SpinEventLoopUntil("nsThreadPool::ShutdownWithTimeout"_ns,
[&] { return outstandingThreads == 0; });
if (timer) {
timer->Cancel();
}
onCompletion->Cancel();
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::GetThreadLimit(uint32_t* aValue) {
MutexAutoLock lock(mMutex);
*aValue = mThreadLimit;
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::SetThreadLimit(uint32_t aValue) {
MutexAutoLock lock(mMutex);
LOG(("THRD-P(%p) thread limit [%u]\n", this, aValue));
mThreadLimit = aValue;
if (mIdleThreadLimit > mThreadLimit) {
mIdleThreadLimit = mThreadLimit;
}
if (static_cast<uint32_t>(mThreads.Count()) > mThreadLimit) {
mEventsAvailable
.NotifyAll(); // wake up threads so they observe this change
}
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::GetIdleThreadLimit(uint32_t* aValue) {
MutexAutoLock lock(mMutex);
*aValue = mIdleThreadLimit;
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::SetIdleThreadLimit(uint32_t aValue) {
MutexAutoLock lock(mMutex);
LOG(("THRD-P(%p) idle thread limit [%u]\n", this, aValue));
mIdleThreadLimit = aValue;
if (mIdleThreadLimit > mThreadLimit) {
mIdleThreadLimit = mThreadLimit;
}
// Do we need to kill some idle threads?
if (mIdleCount > mIdleThreadLimit) {
mEventsAvailable
.NotifyAll(); // wake up threads so they observe this change
}
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::GetIdleThreadTimeout(uint32_t* aValue) {
MutexAutoLock lock(mMutex);
*aValue = mIdleThreadTimeout;
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::SetIdleThreadTimeout(uint32_t aValue) {
MutexAutoLock lock(mMutex);
uint32_t oldTimeout = mIdleThreadTimeout;
mIdleThreadTimeout = aValue;
// Do we need to notify any idle threads that their sleep time has shortened?
if (mIdleThreadTimeout < oldTimeout && mIdleCount > 0) {
mEventsAvailable
.NotifyAll(); // wake up threads so they observe this change
}
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::GetIdleThreadTimeoutRegressive(bool* aValue) {
MutexAutoLock lock(mMutex);
*aValue = mRegressiveMaxIdleTime;
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::SetIdleThreadTimeoutRegressive(bool aValue) {
MutexAutoLock lock(mMutex);
bool oldRegressive = mRegressiveMaxIdleTime;
mRegressiveMaxIdleTime = aValue;
// Would setting regressive timeout effect idle threads?
if (mRegressiveMaxIdleTime > oldRegressive && mIdleCount > 1) {
mEventsAvailable
.NotifyAll(); // wake up threads so they observe this change
}
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::GetThreadStackSize(uint32_t* aValue) {
MutexAutoLock lock(mMutex);
*aValue = mStackSize;
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::SetThreadStackSize(uint32_t aValue) {
MutexAutoLock lock(mMutex);
mStackSize = aValue;
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::GetListener(nsIThreadPoolListener** aListener) {
MutexAutoLock lock(mMutex);
NS_IF_ADDREF(*aListener = mListener);
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::SetListener(nsIThreadPoolListener* aListener) {
nsCOMPtr<nsIThreadPoolListener> swappedListener(aListener);
{
MutexAutoLock lock(mMutex);
mListener.swap(swappedListener);
}
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::SetName(const nsACString& aName) {
MutexAutoLock lock(mMutex);
if (mThreads.Count()) {
return NS_ERROR_NOT_AVAILABLE;
}
mName = aName;
return NS_OK;
}
|