Bug 575870 - Enable the firefox button on xp themed, classic, and aero basic. r=dao...
[mozilla-central.git] / xpcom / threads / nsThreadPool.cpp
blob969493efc0db2bfdf4577acfcc16eab22cb5c6ed
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
14 * License.
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.
22 * Contributor(s):
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"
43 #include "nsThread.h"
44 #include "nsMemory.h"
45 #include "nsAutoPtr.h"
46 #include "nsAutoLock.h"
47 #include "prinrval.h"
48 #include "prlog.h"
50 #ifdef PR_LOGGING
51 static PRLogModuleInfo *sLog = PR_NewLogModule("nsThreadPool");
52 #endif
53 #define LOG(args) PR_LOG(sLog, PR_LOG_DEBUG, args)
55 // DESIGN:
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,
68 NS_THREADPOOL_CID)
69 NS_IMPL_QUERY_INTERFACE3_CI(nsThreadPool, nsIThreadPool, nsIEventTarget,
70 nsIRunnable)
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)
77 , mIdleCount(0)
78 , mShutdown(PR_FALSE)
82 nsThreadPool::~nsThreadPool()
84 Shutdown();
87 nsresult
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(),
97 mThreadLimit));
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));
108 if (!spawnThread)
109 return NS_OK;
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);
120 } else {
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));
125 if (killThread) {
126 thread->Shutdown();
127 } else {
128 thread->Dispatch(this, NS_DISPATCH_NORMAL);
131 return NS_OK;
134 void
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));
147 if (doomed) {
148 doomed->Shutdown();
149 } else {
150 NS_WARNING("failed to construct proxy to main thread");
154 NS_IMETHODIMP
155 nsThreadPool::Run()
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;
173 if (listener) {
174 listener->OnThreadCreated();
177 do {
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
186 if (mShutdown) {
187 exitThread = PR_TRUE;
188 } else {
189 if (wasIdle) {
190 // if too many idle threads or idle for too long, then bail.
191 if (mIdleCount > mIdleThreadLimit || (now - idleSince) >= timeout)
192 exitThread = PR_TRUE;
193 } else {
194 // if would be too many idle threads...
195 if (mIdleCount == mIdleThreadLimit) {
196 exitThread = PR_TRUE;
197 } else {
198 ++mIdleCount;
199 idleSince = now;
200 wasIdle = PR_TRUE;
205 if (exitThread) {
206 if (wasIdle)
207 --mIdleCount;
208 shutdownThreadOnExit = mThreads.RemoveObject(current);
209 } else {
210 PRIntervalTime delta = timeout - (now - idleSince);
211 LOG(("THRD-P(%p) waiting [%d]\n", this, delta));
212 mon.Wait(delta);
214 } else if (wasIdle) {
215 wasIdle = PR_FALSE;
216 --mIdleCount;
219 if (event) {
220 LOG(("THRD-P(%p) running [%p]\n", this, event.get()));
221 event->Run();
223 } while (!exitThread);
225 if (listener) {
226 listener->OnThreadShuttingDown();
229 if (shutdownThreadOnExit) {
230 ShutdownThread(current);
233 LOG(("THRD-P(%p) leave\n", this));
234 return NS_OK;
237 NS_IMETHODIMP
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);
251 PutEvent(wrapper);
253 while (wrapper->IsPending())
254 NS_ProcessNextEvent(thread);
255 } else {
256 NS_ASSERTION(flags == NS_DISPATCH_NORMAL, "unexpected dispatch flags");
257 PutEvent(event);
259 return NS_OK;
262 NS_IMETHODIMP
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");
269 *result = PR_FALSE;
270 return NS_OK;
273 NS_IMETHODIMP
274 nsThreadPool::Shutdown()
276 nsCOMArray<nsIThread> threads;
277 nsCOMPtr<nsIThreadPoolListener> listener;
279 nsAutoMonitor mon(mEvents.Monitor());
280 mShutdown = PR_TRUE;
281 mon.NotifyAll();
283 threads.AppendObjects(mThreads);
284 mThreads.Clear();
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();
298 return NS_OK;
301 NS_IMETHODIMP
302 nsThreadPool::GetThreadLimit(PRUint32 *value)
304 *value = mThreadLimit;
305 return NS_OK;
308 NS_IMETHODIMP
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
316 return NS_OK;
319 NS_IMETHODIMP
320 nsThreadPool::GetIdleThreadLimit(PRUint32 *value)
322 *value = mIdleThreadLimit;
323 return NS_OK;
326 NS_IMETHODIMP
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
334 return NS_OK;
337 NS_IMETHODIMP
338 nsThreadPool::GetIdleThreadTimeout(PRUint32 *value)
340 *value = mIdleThreadTimeout;
341 return NS_OK;
344 NS_IMETHODIMP
345 nsThreadPool::SetIdleThreadTimeout(PRUint32 value)
347 nsAutoMonitor mon(mEvents.Monitor());
348 mIdleThreadTimeout = value;
349 mon.NotifyAll(); // wake up threads so they observe this change
350 return NS_OK;
353 NS_IMETHODIMP
354 nsThreadPool::GetListener(nsIThreadPoolListener** aListener)
356 nsAutoMonitor mon(mEvents.Monitor());
357 NS_IF_ADDREF(*aListener = mListener);
358 return NS_OK;
361 NS_IMETHODIMP
362 nsThreadPool::SetListener(nsIThreadPoolListener* aListener)
364 nsCOMPtr<nsIThreadPoolListener> swappedListener(aListener);
366 nsAutoMonitor mon(mEvents.Monitor());
367 mListener.swap(swappedListener);
369 return NS_OK;