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 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla code.
18 * The Initial Developer of the Original Code is Google Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2006
20 * the Initial Developer. All Rights Reserved.
23 * Darin Fisher <darin@meer.net>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsIProxyObjectManager.h"
40 #include "nsIClassInfoImpl.h"
41 #include "nsThreadPool.h"
42 #include "nsThreadManager.h"
45 #include "nsAutoPtr.h"
46 #include "nsAutoLock.h"
51 static PRLogModuleInfo
*sLog
= PR_NewLogModule("nsThreadPool");
53 #define LOG(args) PR_LOG(sLog, PR_LOG_DEBUG, args)
56 // o Allocate anonymous threads.
57 // o Use nsThreadPool::Run as the main routine for each thread.
58 // o Each thread waits on the event queue's monitor, checking for
59 // pending events and rescheduling itself as an idle thread.
61 #define DEFAULT_THREAD_LIMIT 4
62 #define DEFAULT_IDLE_THREAD_LIMIT 1
63 #define DEFAULT_IDLE_THREAD_TIMEOUT PR_SecondsToInterval(60)
65 NS_IMPL_THREADSAFE_ADDREF(nsThreadPool
)
66 NS_IMPL_THREADSAFE_RELEASE(nsThreadPool
)
67 NS_IMPL_CLASSINFO(nsThreadPool
, NULL
, nsIClassInfo::THREADSAFE
,
69 NS_IMPL_QUERY_INTERFACE3_CI(nsThreadPool
, nsIThreadPool
, nsIEventTarget
,
71 NS_IMPL_CI_INTERFACE_GETTER2(nsThreadPool
, nsIThreadPool
, nsIEventTarget
)
73 nsThreadPool::nsThreadPool()
74 : mThreadLimit(DEFAULT_THREAD_LIMIT
)
75 , mIdleThreadLimit(DEFAULT_IDLE_THREAD_LIMIT
)
76 , mIdleThreadTimeout(DEFAULT_IDLE_THREAD_TIMEOUT
)
82 nsThreadPool::~nsThreadPool()
88 nsThreadPool::PutEvent(nsIRunnable
*event
)
90 // Avoid spawning a new thread while holding the event queue lock...
92 PRBool spawnThread
= PR_FALSE
;
94 nsAutoMonitor
mon(mEvents
.Monitor());
96 LOG(("THRD-P(%p) put [%d %d %d]\n", this, mIdleCount
, mThreads
.Count(),
98 NS_ASSERTION(mIdleCount
<= (PRUint32
) mThreads
.Count(), "oops");
100 // Make sure we have a thread to service this event.
101 if (mIdleCount
== 0 && mThreads
.Count() < (PRInt32
) mThreadLimit
)
102 spawnThread
= PR_TRUE
;
104 mEvents
.PutEvent(event
);
107 LOG(("THRD-P(%p) put [spawn=%d]\n", this, spawnThread
));
111 nsCOMPtr
<nsIThread
> thread
;
112 nsThreadManager::get()->NewThread(0, getter_AddRefs(thread
));
113 NS_ENSURE_STATE(thread
);
115 PRBool killThread
= PR_FALSE
;
117 nsAutoMonitor
mon(mEvents
.Monitor());
118 if (mThreads
.Count() < (PRInt32
) mThreadLimit
) {
119 mThreads
.AppendObject(thread
);
121 killThread
= PR_TRUE
; // okay, we don't need this thread anymore
124 LOG(("THRD-P(%p) put [%p kill=%d]\n", this, thread
.get(), killThread
));
128 thread
->Dispatch(this, NS_DISPATCH_NORMAL
);
135 nsThreadPool::ShutdownThread(nsIThread
*thread
)
137 LOG(("THRD-P(%p) shutdown async [%p]\n", this, thread
));
139 // This method is responsible for calling Shutdown on |thread|. This must be
140 // done from some other thread, so we use the main thread of the application.
142 NS_ASSERTION(!NS_IsMainThread(), "wrong thread");
144 nsCOMPtr
<nsIThread
> doomed
;
145 NS_GetProxyForObject(NS_PROXY_TO_MAIN_THREAD
, NS_GET_IID(nsIThread
), thread
,
146 NS_PROXY_ASYNC
, getter_AddRefs(doomed
));
150 NS_WARNING("failed to construct proxy to main thread");
157 LOG(("THRD-P(%p) enter\n", this));
159 nsCOMPtr
<nsIThread
> current
;
160 nsThreadManager::get()->GetCurrentThread(getter_AddRefs(current
));
162 PRBool shutdownThreadOnExit
= PR_FALSE
;
163 PRBool exitThread
= PR_FALSE
;
164 PRBool wasIdle
= PR_FALSE
;
165 PRIntervalTime idleSince
;
167 nsCOMPtr
<nsIThreadPoolListener
> listener
;
169 nsAutoMonitor
mon(mEvents
.Monitor());
170 listener
= mListener
;
174 listener
->OnThreadCreated();
178 nsCOMPtr
<nsIRunnable
> event
;
180 nsAutoMonitor
mon(mEvents
.Monitor());
181 if (!mEvents
.GetPendingEvent(getter_AddRefs(event
))) {
182 PRIntervalTime now
= PR_IntervalNow();
183 PRIntervalTime timeout
= PR_MillisecondsToInterval(mIdleThreadTimeout
);
185 // If we are shutting down, then don't keep any idle threads
187 exitThread
= PR_TRUE
;
190 // if too many idle threads or idle for too long, then bail.
191 if (mIdleCount
> mIdleThreadLimit
|| (now
- idleSince
) >= timeout
)
192 exitThread
= PR_TRUE
;
194 // if would be too many idle threads...
195 if (mIdleCount
== mIdleThreadLimit
) {
196 exitThread
= PR_TRUE
;
208 shutdownThreadOnExit
= mThreads
.RemoveObject(current
);
210 PRIntervalTime delta
= timeout
- (now
- idleSince
);
211 LOG(("THRD-P(%p) waiting [%d]\n", this, delta
));
214 } else if (wasIdle
) {
220 LOG(("THRD-P(%p) running [%p]\n", this, event
.get()));
223 } while (!exitThread
);
226 listener
->OnThreadShuttingDown();
229 if (shutdownThreadOnExit
) {
230 ShutdownThread(current
);
233 LOG(("THRD-P(%p) leave\n", this));
238 nsThreadPool::Dispatch(nsIRunnable
*event
, PRUint32 flags
)
240 LOG(("THRD-P(%p) dispatch [%p %x]\n", this, event
, flags
));
242 NS_ENSURE_STATE(!mShutdown
);
244 if (flags
& DISPATCH_SYNC
) {
245 nsCOMPtr
<nsIThread
> thread
;
246 nsThreadManager::get()->GetCurrentThread(getter_AddRefs(thread
));
247 NS_ENSURE_STATE(thread
);
249 nsRefPtr
<nsThreadSyncDispatch
> wrapper
=
250 new nsThreadSyncDispatch(thread
, event
);
253 while (wrapper
->IsPending())
254 NS_ProcessNextEvent(thread
);
256 NS_ASSERTION(flags
== NS_DISPATCH_NORMAL
, "unexpected dispatch flags");
263 nsThreadPool::IsOnCurrentThread(PRBool
*result
)
265 // No one should be calling this method. If this assertion gets hit, then we
266 // need to think carefully about what this method should be returning.
267 NS_NOTREACHED("implement me");
274 nsThreadPool::Shutdown()
276 nsCOMArray
<nsIThread
> threads
;
277 nsCOMPtr
<nsIThreadPoolListener
> listener
;
279 nsAutoMonitor
mon(mEvents
.Monitor());
283 threads
.AppendObjects(mThreads
);
286 // Swap in a null listener so that we release the listener at the end of
287 // this method. The listener will be kept alive as long as the other threads
288 // that were created when it was set.
289 mListener
.swap(listener
);
292 // It's important that we shutdown the threads while outside the event queue
293 // monitor. Otherwise, we could end up dead-locking.
295 for (PRInt32 i
= 0; i
< threads
.Count(); ++i
)
296 threads
[i
]->Shutdown();
302 nsThreadPool::GetThreadLimit(PRUint32
*value
)
304 *value
= mThreadLimit
;
309 nsThreadPool::SetThreadLimit(PRUint32 value
)
311 nsAutoMonitor
mon(mEvents
.Monitor());
312 mThreadLimit
= value
;
313 if (mIdleThreadLimit
> mThreadLimit
)
314 mIdleThreadLimit
= mThreadLimit
;
315 mon
.NotifyAll(); // wake up threads so they observe this change
320 nsThreadPool::GetIdleThreadLimit(PRUint32
*value
)
322 *value
= mIdleThreadLimit
;
327 nsThreadPool::SetIdleThreadLimit(PRUint32 value
)
329 nsAutoMonitor
mon(mEvents
.Monitor());
330 mIdleThreadLimit
= value
;
331 if (mIdleThreadLimit
> mThreadLimit
)
332 mIdleThreadLimit
= mThreadLimit
;
333 mon
.NotifyAll(); // wake up threads so they observe this change
338 nsThreadPool::GetIdleThreadTimeout(PRUint32
*value
)
340 *value
= mIdleThreadTimeout
;
345 nsThreadPool::SetIdleThreadTimeout(PRUint32 value
)
347 nsAutoMonitor
mon(mEvents
.Monitor());
348 mIdleThreadTimeout
= value
;
349 mon
.NotifyAll(); // wake up threads so they observe this change
354 nsThreadPool::GetListener(nsIThreadPoolListener
** aListener
)
356 nsAutoMonitor
mon(mEvents
.Monitor());
357 NS_IF_ADDREF(*aListener
= mListener
);
362 nsThreadPool::SetListener(nsIThreadPoolListener
* aListener
)
364 nsCOMPtr
<nsIThreadPoolListener
> swappedListener(aListener
);
366 nsAutoMonitor
mon(mEvents
.Monitor());
367 mListener
.swap(swappedListener
);