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_QUERY_INTERFACE3_CI(nsThreadPool
, nsIThreadPool
, nsIEventTarget
,
69 NS_IMPL_CI_INTERFACE_GETTER2(nsThreadPool
, nsIThreadPool
, nsIEventTarget
)
71 nsThreadPool::nsThreadPool()
72 : mThreadLimit(DEFAULT_THREAD_LIMIT
)
73 , mIdleThreadLimit(DEFAULT_IDLE_THREAD_LIMIT
)
74 , mIdleThreadTimeout(DEFAULT_IDLE_THREAD_TIMEOUT
)
80 nsThreadPool::~nsThreadPool()
86 nsThreadPool::PutEvent(nsIRunnable
*event
)
88 // Avoid spawning a new thread while holding the event queue lock...
90 PRBool spawnThread
= PR_FALSE
;
92 nsAutoMonitor
mon(mEvents
.Monitor());
94 LOG(("THRD-P(%p) put [%d %d %d]\n", this, mIdleCount
, mThreads
.Count(),
96 NS_ASSERTION(mIdleCount
<= (PRUint32
) mThreads
.Count(), "oops");
98 // Make sure we have a thread to service this event.
99 if (mIdleCount
== 0 && mThreads
.Count() < (PRInt32
) mThreadLimit
)
100 spawnThread
= PR_TRUE
;
102 mEvents
.PutEvent(event
);
105 LOG(("THRD-P(%p) put [spawn=%d]\n", this, spawnThread
));
109 nsCOMPtr
<nsIThread
> thread
;
110 nsThreadManager::get()->NewThread(0, getter_AddRefs(thread
));
111 NS_ENSURE_STATE(thread
);
113 PRBool killThread
= PR_FALSE
;
115 nsAutoMonitor
mon(mEvents
.Monitor());
116 if (mThreads
.Count() < (PRInt32
) mThreadLimit
) {
117 mThreads
.AppendObject(thread
);
119 killThread
= PR_TRUE
; // okay, we don't need this thread anymore
122 LOG(("THRD-P(%p) put [%p kill=%d]\n", this, thread
.get(), killThread
));
126 thread
->Dispatch(this, NS_DISPATCH_NORMAL
);
133 nsThreadPool::ShutdownThread(nsIThread
*thread
)
135 LOG(("THRD-P(%p) shutdown async [%p]\n", this, thread
));
137 // This method is responsible for calling Shutdown on |thread|. This must be
138 // done from some other thread, so we use the main thread of the application.
140 NS_ASSERTION(!NS_IsMainThread(), "wrong thread");
142 nsCOMPtr
<nsIThread
> doomed
;
143 NS_GetProxyForObject(NS_PROXY_TO_MAIN_THREAD
, NS_GET_IID(nsIThread
), thread
,
144 NS_PROXY_ASYNC
, getter_AddRefs(doomed
));
148 NS_WARNING("failed to construct proxy to main thread");
155 LOG(("THRD-P(%p) enter\n", this));
157 nsCOMPtr
<nsIThread
> current
;
158 nsThreadManager::get()->GetCurrentThread(getter_AddRefs(current
));
160 PRBool shutdownThreadOnExit
= PR_FALSE
;
161 PRBool exitThread
= PR_FALSE
;
162 PRBool wasIdle
= PR_FALSE
;
163 PRIntervalTime idleSince
;
165 nsCOMPtr
<nsIThreadPoolListener
> listener
;
167 nsAutoMonitor
mon(mEvents
.Monitor());
168 listener
= mListener
;
172 listener
->OnThreadCreated();
176 nsCOMPtr
<nsIRunnable
> event
;
178 nsAutoMonitor
mon(mEvents
.Monitor());
179 if (!mEvents
.GetPendingEvent(getter_AddRefs(event
))) {
180 PRIntervalTime now
= PR_IntervalNow();
181 PRIntervalTime timeout
= PR_MillisecondsToInterval(mIdleThreadTimeout
);
183 // If we are shutting down, then don't keep any idle threads
185 exitThread
= PR_TRUE
;
188 // if too many idle threads or idle for too long, then bail.
189 if (mIdleCount
> mIdleThreadLimit
|| (now
- idleSince
) >= timeout
)
190 exitThread
= PR_TRUE
;
192 // if would be too many idle threads...
193 if (mIdleCount
== mIdleThreadLimit
) {
194 exitThread
= PR_TRUE
;
206 shutdownThreadOnExit
= mThreads
.RemoveObject(current
);
208 PRIntervalTime delta
= timeout
- (now
- idleSince
);
209 LOG(("THRD-P(%p) waiting [%d]\n", this, delta
));
212 } else if (wasIdle
) {
218 LOG(("THRD-P(%p) running [%p]\n", this, event
.get()));
221 } while (!exitThread
);
224 listener
->OnThreadShuttingDown();
227 if (shutdownThreadOnExit
) {
228 ShutdownThread(current
);
231 LOG(("THRD-P(%p) leave\n", this));
236 nsThreadPool::Dispatch(nsIRunnable
*event
, PRUint32 flags
)
238 LOG(("THRD-P(%p) dispatch [%p %x]\n", this, event
, flags
));
240 NS_ENSURE_STATE(!mShutdown
);
242 if (flags
& DISPATCH_SYNC
) {
243 nsCOMPtr
<nsIThread
> thread
;
244 nsThreadManager::get()->GetCurrentThread(getter_AddRefs(thread
));
245 NS_ENSURE_STATE(thread
);
247 nsRefPtr
<nsThreadSyncDispatch
> wrapper
=
248 new nsThreadSyncDispatch(thread
, event
);
251 while (wrapper
->IsPending())
252 NS_ProcessNextEvent(thread
);
254 NS_ASSERTION(flags
== NS_DISPATCH_NORMAL
, "unexpected dispatch flags");
261 nsThreadPool::IsOnCurrentThread(PRBool
*result
)
263 // No one should be calling this method. If this assertion gets hit, then we
264 // need to think carefully about what this method should be returning.
265 NS_NOTREACHED("implement me");
272 nsThreadPool::Shutdown()
274 nsCOMArray
<nsIThread
> threads
;
275 nsCOMPtr
<nsIThreadPoolListener
> listener
;
277 nsAutoMonitor
mon(mEvents
.Monitor());
281 threads
.AppendObjects(mThreads
);
284 // Swap in a null listener so that we release the listener at the end of
285 // this method. The listener will be kept alive as long as the other threads
286 // that were created when it was set.
287 mListener
.swap(listener
);
290 // It's important that we shutdown the threads while outside the event queue
291 // monitor. Otherwise, we could end up dead-locking.
293 for (PRInt32 i
= 0; i
< threads
.Count(); ++i
)
294 threads
[i
]->Shutdown();
300 nsThreadPool::GetThreadLimit(PRUint32
*value
)
302 *value
= mThreadLimit
;
307 nsThreadPool::SetThreadLimit(PRUint32 value
)
309 nsAutoMonitor
mon(mEvents
.Monitor());
310 mThreadLimit
= value
;
311 if (mIdleThreadLimit
> mThreadLimit
)
312 mIdleThreadLimit
= mThreadLimit
;
313 mon
.NotifyAll(); // wake up threads so they observe this change
318 nsThreadPool::GetIdleThreadLimit(PRUint32
*value
)
320 *value
= mIdleThreadLimit
;
325 nsThreadPool::SetIdleThreadLimit(PRUint32 value
)
327 nsAutoMonitor
mon(mEvents
.Monitor());
328 mIdleThreadLimit
= value
;
329 if (mIdleThreadLimit
> mThreadLimit
)
330 mIdleThreadLimit
= mThreadLimit
;
331 mon
.NotifyAll(); // wake up threads so they observe this change
336 nsThreadPool::GetIdleThreadTimeout(PRUint32
*value
)
338 *value
= mIdleThreadTimeout
;
343 nsThreadPool::SetIdleThreadTimeout(PRUint32 value
)
345 nsAutoMonitor
mon(mEvents
.Monitor());
346 mIdleThreadTimeout
= value
;
347 mon
.NotifyAll(); // wake up threads so they observe this change
352 nsThreadPool::GetListener(nsIThreadPoolListener
** aListener
)
354 nsAutoMonitor
mon(mEvents
.Monitor());
355 NS_IF_ADDREF(*aListener
= mListener
);
360 nsThreadPool::SetListener(nsIThreadPoolListener
* aListener
)
362 nsCOMPtr
<nsIThreadPoolListener
> swappedListener(aListener
);
364 nsAutoMonitor
mon(mEvents
.Monitor());
365 mListener
.swap(swappedListener
);