Backed out changeset fa432b23baa5. (Backing out bug 454781 to investigate mochitest...
[wine-gecko.git] / xpcom / threads / nsThreadPool.cpp
blob398a4a3883cf8227a383acc7911b8e1744b8f0d7
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_QUERY_INTERFACE3_CI(nsThreadPool, nsIThreadPool, nsIEventTarget,
68 nsIRunnable)
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)
75 , mIdleCount(0)
76 , mShutdown(PR_FALSE)
80 nsThreadPool::~nsThreadPool()
82 Shutdown();
85 nsresult
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(),
95 mThreadLimit));
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));
106 if (!spawnThread)
107 return NS_OK;
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);
118 } else {
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));
123 if (killThread) {
124 thread->Shutdown();
125 } else {
126 thread->Dispatch(this, NS_DISPATCH_NORMAL);
129 return NS_OK;
132 void
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));
145 if (doomed) {
146 doomed->Shutdown();
147 } else {
148 NS_WARNING("failed to construct proxy to main thread");
152 NS_IMETHODIMP
153 nsThreadPool::Run()
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;
171 if (listener) {
172 listener->OnThreadCreated();
175 do {
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
184 if (mShutdown) {
185 exitThread = PR_TRUE;
186 } else {
187 if (wasIdle) {
188 // if too many idle threads or idle for too long, then bail.
189 if (mIdleCount > mIdleThreadLimit || (now - idleSince) >= timeout)
190 exitThread = PR_TRUE;
191 } else {
192 // if would be too many idle threads...
193 if (mIdleCount == mIdleThreadLimit) {
194 exitThread = PR_TRUE;
195 } else {
196 ++mIdleCount;
197 idleSince = now;
198 wasIdle = PR_TRUE;
203 if (exitThread) {
204 if (wasIdle)
205 --mIdleCount;
206 shutdownThreadOnExit = mThreads.RemoveObject(current);
207 } else {
208 PRIntervalTime delta = timeout - (now - idleSince);
209 LOG(("THRD-P(%p) waiting [%d]\n", this, delta));
210 mon.Wait(delta);
212 } else if (wasIdle) {
213 wasIdle = PR_FALSE;
214 --mIdleCount;
217 if (event) {
218 LOG(("THRD-P(%p) running [%p]\n", this, event.get()));
219 event->Run();
221 } while (!exitThread);
223 if (listener) {
224 listener->OnThreadShuttingDown();
227 if (shutdownThreadOnExit) {
228 ShutdownThread(current);
231 LOG(("THRD-P(%p) leave\n", this));
232 return NS_OK;
235 NS_IMETHODIMP
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);
249 PutEvent(wrapper);
251 while (wrapper->IsPending())
252 NS_ProcessNextEvent(thread);
253 } else {
254 NS_ASSERTION(flags == NS_DISPATCH_NORMAL, "unexpected dispatch flags");
255 PutEvent(event);
257 return NS_OK;
260 NS_IMETHODIMP
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");
267 *result = PR_FALSE;
268 return NS_OK;
271 NS_IMETHODIMP
272 nsThreadPool::Shutdown()
274 nsCOMArray<nsIThread> threads;
275 nsCOMPtr<nsIThreadPoolListener> listener;
277 nsAutoMonitor mon(mEvents.Monitor());
278 mShutdown = PR_TRUE;
279 mon.NotifyAll();
281 threads.AppendObjects(mThreads);
282 mThreads.Clear();
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();
296 return NS_OK;
299 NS_IMETHODIMP
300 nsThreadPool::GetThreadLimit(PRUint32 *value)
302 *value = mThreadLimit;
303 return NS_OK;
306 NS_IMETHODIMP
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
314 return NS_OK;
317 NS_IMETHODIMP
318 nsThreadPool::GetIdleThreadLimit(PRUint32 *value)
320 *value = mIdleThreadLimit;
321 return NS_OK;
324 NS_IMETHODIMP
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
332 return NS_OK;
335 NS_IMETHODIMP
336 nsThreadPool::GetIdleThreadTimeout(PRUint32 *value)
338 *value = mIdleThreadTimeout;
339 return NS_OK;
342 NS_IMETHODIMP
343 nsThreadPool::SetIdleThreadTimeout(PRUint32 value)
345 nsAutoMonitor mon(mEvents.Monitor());
346 mIdleThreadTimeout = value;
347 mon.NotifyAll(); // wake up threads so they observe this change
348 return NS_OK;
351 NS_IMETHODIMP
352 nsThreadPool::GetListener(nsIThreadPoolListener** aListener)
354 nsAutoMonitor mon(mEvents.Monitor());
355 NS_IF_ADDREF(*aListener = mListener);
356 return NS_OK;
359 NS_IMETHODIMP
360 nsThreadPool::SetListener(nsIThreadPoolListener* aListener)
362 nsCOMPtr<nsIThreadPoolListener> swappedListener(aListener);
364 nsAutoMonitor mon(mEvents.Monitor());
365 mListener.swap(swappedListener);
367 return NS_OK;