1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/threads/nsThreadPool.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,407 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "nsIClassInfoImpl.h" 1.11 +#include "nsThreadPool.h" 1.12 +#include "nsThreadManager.h" 1.13 +#include "nsThread.h" 1.14 +#include "nsMemory.h" 1.15 +#include "nsAutoPtr.h" 1.16 +#include "prinrval.h" 1.17 +#include "prlog.h" 1.18 + 1.19 +using namespace mozilla; 1.20 + 1.21 +#ifdef PR_LOGGING 1.22 +static PRLogModuleInfo * 1.23 +GetThreadPoolLog() 1.24 +{ 1.25 + static PRLogModuleInfo *sLog; 1.26 + if (!sLog) 1.27 + sLog = PR_NewLogModule("nsThreadPool"); 1.28 + return sLog; 1.29 +} 1.30 +#endif 1.31 +#ifdef LOG 1.32 +#undef LOG 1.33 +#endif 1.34 +#define LOG(args) PR_LOG(GetThreadPoolLog(), PR_LOG_DEBUG, args) 1.35 + 1.36 +// DESIGN: 1.37 +// o Allocate anonymous threads. 1.38 +// o Use nsThreadPool::Run as the main routine for each thread. 1.39 +// o Each thread waits on the event queue's monitor, checking for 1.40 +// pending events and rescheduling itself as an idle thread. 1.41 + 1.42 +#define DEFAULT_THREAD_LIMIT 4 1.43 +#define DEFAULT_IDLE_THREAD_LIMIT 1 1.44 +#define DEFAULT_IDLE_THREAD_TIMEOUT PR_SecondsToInterval(60) 1.45 + 1.46 +NS_IMPL_ADDREF(nsThreadPool) 1.47 +NS_IMPL_RELEASE(nsThreadPool) 1.48 +NS_IMPL_CLASSINFO(nsThreadPool, nullptr, nsIClassInfo::THREADSAFE, 1.49 + NS_THREADPOOL_CID) 1.50 +NS_IMPL_QUERY_INTERFACE_CI(nsThreadPool, nsIThreadPool, nsIEventTarget, 1.51 + nsIRunnable) 1.52 +NS_IMPL_CI_INTERFACE_GETTER(nsThreadPool, nsIThreadPool, nsIEventTarget) 1.53 + 1.54 +nsThreadPool::nsThreadPool() 1.55 + : mThreadLimit(DEFAULT_THREAD_LIMIT) 1.56 + , mIdleThreadLimit(DEFAULT_IDLE_THREAD_LIMIT) 1.57 + , mIdleThreadTimeout(DEFAULT_IDLE_THREAD_TIMEOUT) 1.58 + , mIdleCount(0) 1.59 + , mStackSize(nsIThreadManager::DEFAULT_STACK_SIZE) 1.60 + , mShutdown(false) 1.61 +{ 1.62 +} 1.63 + 1.64 +nsThreadPool::~nsThreadPool() 1.65 +{ 1.66 + // Threads keep a reference to the nsThreadPool until they return from Run() 1.67 + // after removing themselves from mThreads. 1.68 + MOZ_ASSERT(mThreads.IsEmpty()); 1.69 +} 1.70 + 1.71 +nsresult 1.72 +nsThreadPool::PutEvent(nsIRunnable *event) 1.73 +{ 1.74 + // Avoid spawning a new thread while holding the event queue lock... 1.75 + 1.76 + bool spawnThread = false; 1.77 + uint32_t stackSize = 0; 1.78 + { 1.79 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.80 + 1.81 + LOG(("THRD-P(%p) put [%d %d %d]\n", this, mIdleCount, mThreads.Count(), 1.82 + mThreadLimit)); 1.83 + MOZ_ASSERT(mIdleCount <= (uint32_t) mThreads.Count(), "oops"); 1.84 + 1.85 + // Make sure we have a thread to service this event. 1.86 + if (mIdleCount == 0 && mThreads.Count() < (int32_t) mThreadLimit) 1.87 + spawnThread = true; 1.88 + 1.89 + mEvents.PutEvent(event); 1.90 + stackSize = mStackSize; 1.91 + } 1.92 + 1.93 + LOG(("THRD-P(%p) put [spawn=%d]\n", this, spawnThread)); 1.94 + if (!spawnThread) 1.95 + return NS_OK; 1.96 + 1.97 + nsCOMPtr<nsIThread> thread; 1.98 + nsThreadManager::get()->NewThread(0, 1.99 + stackSize, 1.100 + getter_AddRefs(thread)); 1.101 + if (NS_WARN_IF(!thread)) 1.102 + return NS_ERROR_UNEXPECTED; 1.103 + 1.104 + bool killThread = false; 1.105 + { 1.106 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.107 + if (mThreads.Count() < (int32_t) mThreadLimit) { 1.108 + mThreads.AppendObject(thread); 1.109 + } else { 1.110 + killThread = true; // okay, we don't need this thread anymore 1.111 + } 1.112 + } 1.113 + LOG(("THRD-P(%p) put [%p kill=%d]\n", this, thread.get(), killThread)); 1.114 + if (killThread) { 1.115 + // Pending events are processed on the current thread during 1.116 + // nsIThread::Shutdown() execution, so if nsThreadPool::Dispatch() is called 1.117 + // under caller's lock then deadlock could occur. This happens e.g. in case 1.118 + // of nsStreamCopier. To prevent this situation, dispatch a shutdown event 1.119 + // to the current thread instead of calling nsIThread::Shutdown() directly. 1.120 + 1.121 + nsRefPtr<nsIRunnable> r = NS_NewRunnableMethod(thread, 1.122 + &nsIThread::Shutdown); 1.123 + NS_DispatchToCurrentThread(r); 1.124 + } else { 1.125 + thread->Dispatch(this, NS_DISPATCH_NORMAL); 1.126 + } 1.127 + 1.128 + return NS_OK; 1.129 +} 1.130 + 1.131 +void 1.132 +nsThreadPool::ShutdownThread(nsIThread *thread) 1.133 +{ 1.134 + LOG(("THRD-P(%p) shutdown async [%p]\n", this, thread)); 1.135 + 1.136 + // This method is responsible for calling Shutdown on |thread|. This must be 1.137 + // done from some other thread, so we use the main thread of the application. 1.138 + 1.139 + MOZ_ASSERT(!NS_IsMainThread(), "wrong thread"); 1.140 + 1.141 + nsRefPtr<nsIRunnable> r = NS_NewRunnableMethod(thread, &nsIThread::Shutdown); 1.142 + NS_DispatchToMainThread(r); 1.143 +} 1.144 + 1.145 +NS_IMETHODIMP 1.146 +nsThreadPool::Run() 1.147 +{ 1.148 + LOG(("THRD-P(%p) enter\n", this)); 1.149 + 1.150 + mThreadNaming.SetThreadPoolName(mName); 1.151 + 1.152 + nsCOMPtr<nsIThread> current; 1.153 + nsThreadManager::get()->GetCurrentThread(getter_AddRefs(current)); 1.154 + 1.155 + bool shutdownThreadOnExit = false; 1.156 + bool exitThread = false; 1.157 + bool wasIdle = false; 1.158 + PRIntervalTime idleSince; 1.159 + 1.160 + nsCOMPtr<nsIThreadPoolListener> listener; 1.161 + { 1.162 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.163 + listener = mListener; 1.164 + } 1.165 + 1.166 + if (listener) { 1.167 + listener->OnThreadCreated(); 1.168 + } 1.169 + 1.170 + do { 1.171 + nsCOMPtr<nsIRunnable> event; 1.172 + { 1.173 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.174 + if (!mEvents.GetPendingEvent(getter_AddRefs(event))) { 1.175 + PRIntervalTime now = PR_IntervalNow(); 1.176 + PRIntervalTime timeout = PR_MillisecondsToInterval(mIdleThreadTimeout); 1.177 + 1.178 + // If we are shutting down, then don't keep any idle threads 1.179 + if (mShutdown) { 1.180 + exitThread = true; 1.181 + } else { 1.182 + if (wasIdle) { 1.183 + // if too many idle threads or idle for too long, then bail. 1.184 + if (mIdleCount > mIdleThreadLimit || (now - idleSince) >= timeout) 1.185 + exitThread = true; 1.186 + } else { 1.187 + // if would be too many idle threads... 1.188 + if (mIdleCount == mIdleThreadLimit) { 1.189 + exitThread = true; 1.190 + } else { 1.191 + ++mIdleCount; 1.192 + idleSince = now; 1.193 + wasIdle = true; 1.194 + } 1.195 + } 1.196 + } 1.197 + 1.198 + if (exitThread) { 1.199 + if (wasIdle) 1.200 + --mIdleCount; 1.201 + shutdownThreadOnExit = mThreads.RemoveObject(current); 1.202 + } else { 1.203 + PRIntervalTime delta = timeout - (now - idleSince); 1.204 + LOG(("THRD-P(%p) waiting [%d]\n", this, delta)); 1.205 + mon.Wait(delta); 1.206 + } 1.207 + } else if (wasIdle) { 1.208 + wasIdle = false; 1.209 + --mIdleCount; 1.210 + } 1.211 + } 1.212 + if (event) { 1.213 + LOG(("THRD-P(%p) running [%p]\n", this, event.get())); 1.214 + event->Run(); 1.215 + } 1.216 + } while (!exitThread); 1.217 + 1.218 + if (listener) { 1.219 + listener->OnThreadShuttingDown(); 1.220 + } 1.221 + 1.222 + if (shutdownThreadOnExit) { 1.223 + ShutdownThread(current); 1.224 + } 1.225 + 1.226 + LOG(("THRD-P(%p) leave\n", this)); 1.227 + return NS_OK; 1.228 +} 1.229 + 1.230 +NS_IMETHODIMP 1.231 +nsThreadPool::Dispatch(nsIRunnable *event, uint32_t flags) 1.232 +{ 1.233 + LOG(("THRD-P(%p) dispatch [%p %x]\n", this, event, flags)); 1.234 + 1.235 + if (NS_WARN_IF(mShutdown)) 1.236 + return NS_ERROR_NOT_AVAILABLE; 1.237 + 1.238 + if (flags & DISPATCH_SYNC) { 1.239 + nsCOMPtr<nsIThread> thread; 1.240 + nsThreadManager::get()->GetCurrentThread(getter_AddRefs(thread)); 1.241 + if (NS_WARN_IF(!thread)) 1.242 + return NS_ERROR_NOT_AVAILABLE; 1.243 + 1.244 + nsRefPtr<nsThreadSyncDispatch> wrapper = 1.245 + new nsThreadSyncDispatch(thread, event); 1.246 + PutEvent(wrapper); 1.247 + 1.248 + while (wrapper->IsPending()) 1.249 + NS_ProcessNextEvent(thread); 1.250 + } else { 1.251 + NS_ASSERTION(flags == NS_DISPATCH_NORMAL, "unexpected dispatch flags"); 1.252 + PutEvent(event); 1.253 + } 1.254 + return NS_OK; 1.255 +} 1.256 + 1.257 +NS_IMETHODIMP 1.258 +nsThreadPool::IsOnCurrentThread(bool *result) 1.259 +{ 1.260 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.261 + nsIThread* thread = NS_GetCurrentThread(); 1.262 + for (uint32_t i = 0; i < static_cast<uint32_t>(mThreads.Count()); ++i) { 1.263 + if (mThreads[i] == thread) { 1.264 + *result = true; 1.265 + return NS_OK; 1.266 + } 1.267 + } 1.268 + *result = false; 1.269 + return NS_OK; 1.270 +} 1.271 + 1.272 +NS_IMETHODIMP 1.273 +nsThreadPool::Shutdown() 1.274 +{ 1.275 + nsCOMArray<nsIThread> threads; 1.276 + nsCOMPtr<nsIThreadPoolListener> listener; 1.277 + { 1.278 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.279 + mShutdown = true; 1.280 + mon.NotifyAll(); 1.281 + 1.282 + threads.AppendObjects(mThreads); 1.283 + mThreads.Clear(); 1.284 + 1.285 + // Swap in a null listener so that we release the listener at the end of 1.286 + // this method. The listener will be kept alive as long as the other threads 1.287 + // that were created when it was set. 1.288 + mListener.swap(listener); 1.289 + } 1.290 + 1.291 + // It's important that we shutdown the threads while outside the event queue 1.292 + // monitor. Otherwise, we could end up dead-locking. 1.293 + 1.294 + for (int32_t i = 0; i < threads.Count(); ++i) 1.295 + threads[i]->Shutdown(); 1.296 + 1.297 + return NS_OK; 1.298 +} 1.299 + 1.300 +NS_IMETHODIMP 1.301 +nsThreadPool::GetThreadLimit(uint32_t *value) 1.302 +{ 1.303 + *value = mThreadLimit; 1.304 + return NS_OK; 1.305 +} 1.306 + 1.307 +NS_IMETHODIMP 1.308 +nsThreadPool::SetThreadLimit(uint32_t value) 1.309 +{ 1.310 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.311 + mThreadLimit = value; 1.312 + if (mIdleThreadLimit > mThreadLimit) 1.313 + mIdleThreadLimit = mThreadLimit; 1.314 + 1.315 + if (static_cast<uint32_t>(mThreads.Count()) > mThreadLimit) { 1.316 + mon.NotifyAll(); // wake up threads so they observe this change 1.317 + } 1.318 + return NS_OK; 1.319 +} 1.320 + 1.321 +NS_IMETHODIMP 1.322 +nsThreadPool::GetIdleThreadLimit(uint32_t *value) 1.323 +{ 1.324 + *value = mIdleThreadLimit; 1.325 + return NS_OK; 1.326 +} 1.327 + 1.328 +NS_IMETHODIMP 1.329 +nsThreadPool::SetIdleThreadLimit(uint32_t value) 1.330 +{ 1.331 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.332 + mIdleThreadLimit = value; 1.333 + if (mIdleThreadLimit > mThreadLimit) 1.334 + mIdleThreadLimit = mThreadLimit; 1.335 + 1.336 + // Do we need to kill some idle threads? 1.337 + if (mIdleCount > mIdleThreadLimit) { 1.338 + mon.NotifyAll(); // wake up threads so they observe this change 1.339 + } 1.340 + return NS_OK; 1.341 +} 1.342 + 1.343 +NS_IMETHODIMP 1.344 +nsThreadPool::GetIdleThreadTimeout(uint32_t *value) 1.345 +{ 1.346 + *value = mIdleThreadTimeout; 1.347 + return NS_OK; 1.348 +} 1.349 + 1.350 +NS_IMETHODIMP 1.351 +nsThreadPool::SetIdleThreadTimeout(uint32_t value) 1.352 +{ 1.353 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.354 + uint32_t oldTimeout = mIdleThreadTimeout; 1.355 + mIdleThreadTimeout = value; 1.356 + 1.357 + // Do we need to notify any idle threads that their sleep time has shortened? 1.358 + if (mIdleThreadTimeout < oldTimeout && mIdleCount > 0) { 1.359 + mon.NotifyAll(); // wake up threads so they observe this change 1.360 + } 1.361 + return NS_OK; 1.362 +} 1.363 + 1.364 +NS_IMETHODIMP 1.365 +nsThreadPool::GetThreadStackSize(uint32_t* value) 1.366 +{ 1.367 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.368 + *value = mStackSize; 1.369 + return NS_OK; 1.370 +} 1.371 + 1.372 +NS_IMETHODIMP 1.373 +nsThreadPool::SetThreadStackSize(uint32_t value) 1.374 +{ 1.375 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.376 + mStackSize = value; 1.377 + return NS_OK; 1.378 +} 1.379 + 1.380 +NS_IMETHODIMP 1.381 +nsThreadPool::GetListener(nsIThreadPoolListener** aListener) 1.382 +{ 1.383 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.384 + NS_IF_ADDREF(*aListener = mListener); 1.385 + return NS_OK; 1.386 +} 1.387 + 1.388 +NS_IMETHODIMP 1.389 +nsThreadPool::SetListener(nsIThreadPoolListener* aListener) 1.390 +{ 1.391 + nsCOMPtr<nsIThreadPoolListener> swappedListener(aListener); 1.392 + { 1.393 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.394 + mListener.swap(swappedListener); 1.395 + } 1.396 + return NS_OK; 1.397 +} 1.398 + 1.399 +NS_IMETHODIMP 1.400 +nsThreadPool::SetName(const nsACString& aName) 1.401 +{ 1.402 + { 1.403 + ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor()); 1.404 + if (mThreads.Count()) 1.405 + return NS_ERROR_NOT_AVAILABLE; 1.406 + } 1.407 + 1.408 + mName = aName; 1.409 + return NS_OK; 1.410 +}