forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsThreadPool.cpp
368 lines (316 loc) · 10.1 KB
/
nsThreadPool.cpp
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla code.
*
* The Initial Developer of the Original Code is Google Inc.
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Darin Fisher <darin@meer.net>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsIProxyObjectManager.h"
#include "nsIClassInfoImpl.h"
#include "nsThreadPool.h"
#include "nsThreadManager.h"
#include "nsThread.h"
#include "nsMemory.h"
#include "nsAutoPtr.h"
#include "nsAutoLock.h"
#include "prinrval.h"
#include "prlog.h"
#ifdef PR_LOGGING
static PRLogModuleInfo *sLog = PR_NewLogModule("nsThreadPool");
#endif
#define LOG(args) PR_LOG(sLog, PR_LOG_DEBUG, args)
// 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_THREADSAFE_ADDREF(nsThreadPool)
NS_IMPL_THREADSAFE_RELEASE(nsThreadPool)
NS_IMPL_QUERY_INTERFACE3_CI(nsThreadPool, nsIThreadPool, nsIEventTarget,
nsIRunnable)
NS_IMPL_CI_INTERFACE_GETTER2(nsThreadPool, nsIThreadPool, nsIEventTarget)
nsThreadPool::nsThreadPool()
: mThreadLimit(DEFAULT_THREAD_LIMIT)
, mIdleThreadLimit(DEFAULT_IDLE_THREAD_LIMIT)
, mIdleThreadTimeout(DEFAULT_IDLE_THREAD_TIMEOUT)
, mIdleCount(0)
, mShutdown(PR_FALSE)
{
}
nsThreadPool::~nsThreadPool()
{
Shutdown();
}
nsresult
nsThreadPool::PutEvent(nsIRunnable *event)
{
// Avoid spawning a new thread while holding the event queue lock...
PRBool spawnThread = PR_FALSE;
{
nsAutoMonitor mon(mEvents.Monitor());
LOG(("THRD-P(%p) put [%d %d %d]\n", this, mIdleCount, mThreads.Count(),
mThreadLimit));
NS_ASSERTION(mIdleCount <= (PRUint32) mThreads.Count(), "oops");
// Make sure we have a thread to service this event.
if (mIdleCount == 0 && mThreads.Count() < (PRInt32) mThreadLimit)
spawnThread = PR_TRUE;
mEvents.PutEvent(event);
}
LOG(("THRD-P(%p) put [spawn=%d]\n", this, spawnThread));
if (!spawnThread)
return NS_OK;
nsCOMPtr<nsIThread> thread;
nsThreadManager::get()->NewThread(0, getter_AddRefs(thread));
NS_ENSURE_STATE(thread);
PRBool killThread = PR_FALSE;
{
nsAutoMonitor mon(mEvents.Monitor());
if (mThreads.Count() < (PRInt32) mThreadLimit) {
mThreads.AppendObject(thread);
} else {
killThread = PR_TRUE; // okay, we don't need this thread anymore
}
}
LOG(("THRD-P(%p) put [%p kill=%d]\n", this, thread.get(), killThread));
if (killThread) {
thread->Shutdown();
} else {
thread->Dispatch(this, NS_DISPATCH_NORMAL);
}
return NS_OK;
}
void
nsThreadPool::ShutdownThread(nsIThread *thread)
{
LOG(("THRD-P(%p) shutdown async [%p]\n", this, thread));
// This method is responsible for calling Shutdown on |thread|. This must be
// done from some other thread, so we use the main thread of the application.
NS_ASSERTION(!NS_IsMainThread(), "wrong thread");
nsCOMPtr<nsIThread> doomed;
NS_GetProxyForObject(NS_PROXY_TO_MAIN_THREAD, NS_GET_IID(nsIThread), thread,
NS_PROXY_ASYNC, getter_AddRefs(doomed));
if (doomed) {
doomed->Shutdown();
} else {
NS_WARNING("failed to construct proxy to main thread");
}
}
NS_IMETHODIMP
nsThreadPool::Run()
{
LOG(("THRD-P(%p) enter\n", this));
nsCOMPtr<nsIThread> current;
nsThreadManager::get()->GetCurrentThread(getter_AddRefs(current));
PRBool shutdownThreadOnExit = PR_FALSE;
PRBool exitThread = PR_FALSE;
PRBool wasIdle = PR_FALSE;
PRIntervalTime idleSince;
nsCOMPtr<nsIThreadPoolListener> listener;
{
nsAutoMonitor mon(mEvents.Monitor());
listener = mListener;
}
if (listener) {
listener->OnThreadCreated();
}
do {
nsCOMPtr<nsIRunnable> event;
{
nsAutoMonitor mon(mEvents.Monitor());
if (!mEvents.GetPendingEvent(getter_AddRefs(event))) {
PRIntervalTime now = PR_IntervalNow();
PRIntervalTime timeout = PR_MillisecondsToInterval(mIdleThreadTimeout);
// If we are shutting down, then don't keep any idle threads
if (mShutdown) {
exitThread = PR_TRUE;
} else {
if (wasIdle) {
// if too many idle threads or idle for too long, then bail.
if (mIdleCount > mIdleThreadLimit || (now - idleSince) >= timeout)
exitThread = PR_TRUE;
} else {
// if would be too many idle threads...
if (mIdleCount == mIdleThreadLimit) {
exitThread = PR_TRUE;
} else {
++mIdleCount;
idleSince = now;
wasIdle = PR_TRUE;
}
}
}
if (exitThread) {
if (wasIdle)
--mIdleCount;
shutdownThreadOnExit = mThreads.RemoveObject(current);
} else {
PRIntervalTime delta = timeout - (now - idleSince);
LOG(("THRD-P(%p) waiting [%d]\n", this, delta));
mon.Wait(delta);
}
} else if (wasIdle) {
wasIdle = PR_FALSE;
--mIdleCount;
}
}
if (event) {
LOG(("THRD-P(%p) running [%p]\n", this, event.get()));
event->Run();
}
} while (!exitThread);
if (listener) {
listener->OnThreadShuttingDown();
}
if (shutdownThreadOnExit) {
ShutdownThread(current);
}
LOG(("THRD-P(%p) leave\n", this));
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::Dispatch(nsIRunnable *event, PRUint32 flags)
{
LOG(("THRD-P(%p) dispatch [%p %x]\n", this, event, flags));
NS_ENSURE_STATE(!mShutdown);
if (flags & DISPATCH_SYNC) {
nsCOMPtr<nsIThread> thread;
nsThreadManager::get()->GetCurrentThread(getter_AddRefs(thread));
NS_ENSURE_STATE(thread);
nsRefPtr<nsThreadSyncDispatch> wrapper =
new nsThreadSyncDispatch(thread, event);
PutEvent(wrapper);
while (wrapper->IsPending())
NS_ProcessNextEvent(thread);
} else {
NS_ASSERTION(flags == NS_DISPATCH_NORMAL, "unexpected dispatch flags");
PutEvent(event);
}
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::IsOnCurrentThread(PRBool *result)
{
// No one should be calling this method. If this assertion gets hit, then we
// need to think carefully about what this method should be returning.
NS_NOTREACHED("implement me");
*result = PR_FALSE;
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::Shutdown()
{
nsCOMArray<nsIThread> threads;
nsCOMPtr<nsIThreadPoolListener> listener;
{
nsAutoMonitor mon(mEvents.Monitor());
mShutdown = PR_TRUE;
mon.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);
}
// It's important that we shutdown the threads while outside the event queue
// monitor. Otherwise, we could end up dead-locking.
for (PRInt32 i = 0; i < threads.Count(); ++i)
threads[i]->Shutdown();
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::GetThreadLimit(PRUint32 *value)
{
*value = mThreadLimit;
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::SetThreadLimit(PRUint32 value)
{
nsAutoMonitor mon(mEvents.Monitor());
mThreadLimit = value;
if (mIdleThreadLimit > mThreadLimit)
mIdleThreadLimit = mThreadLimit;
mon.NotifyAll(); // wake up threads so they observe this change
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::GetIdleThreadLimit(PRUint32 *value)
{
*value = mIdleThreadLimit;
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::SetIdleThreadLimit(PRUint32 value)
{
nsAutoMonitor mon(mEvents.Monitor());
mIdleThreadLimit = value;
if (mIdleThreadLimit > mThreadLimit)
mIdleThreadLimit = mThreadLimit;
mon.NotifyAll(); // wake up threads so they observe this change
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::GetIdleThreadTimeout(PRUint32 *value)
{
*value = mIdleThreadTimeout;
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::SetIdleThreadTimeout(PRUint32 value)
{
nsAutoMonitor mon(mEvents.Monitor());
mIdleThreadTimeout = value;
mon.NotifyAll(); // wake up threads so they observe this change
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::GetListener(nsIThreadPoolListener** aListener)
{
nsAutoMonitor mon(mEvents.Monitor());
NS_IF_ADDREF(*aListener = mListener);
return NS_OK;
}
NS_IMETHODIMP
nsThreadPool::SetListener(nsIThreadPoolListener* aListener)
{
nsCOMPtr<nsIThreadPoolListener> swappedListener(aListener);
{
nsAutoMonitor mon(mEvents.Monitor());
mListener.swap(swappedListener);
}
return NS_OK;
}