mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
6f8bd8b8e5
We want to ensure that nsThread's use of nsEventQueue uses locking done in nsThread instead of nsEventQueue, for efficiency's sake: we only need to lock once in nsThread, rather than the current situation of locking in nsThread and additionally in nsEventQueue. With the current structure of nsEventQueue, that would mean that nsThread should be using a Monitor internally, rather than a Mutex. Which would be well and good, except that DOM workers use nsThread's mutex to protect their own, internal CondVar. Switching nsThread to use a Monitor would mean that either: - DOM workers drop their internal CondVar in favor of nsThread's Monitor-owned CondVar. This change seems unlikely to work out well, because now the Monitor-owned CondVar is performing double duty: tracking availability of events in nsThread's event queue and additionally whatever DOM workers were using a CondVar for. Having a single CondVar track two things in such a fashion is for Experts Only. - DOM workers grow their own Mutex to protect their own CondVar. Adding a mutex like this would change locking in subtle ways and seems unlikely to lead to success. Using a Monitor in nsThread is therefore untenable, and we would like to retain the current Mutex that lives in nsThread. Therefore, we need to have nsEventQueue manage its own condition variable and push the required (Mutex) locking to the client of nsEventQueue. This scheme also seems more fitting: external clients merely need synchronized access to the event queue; the details of managing notifications about events in the event queue should be left up to the event queue itself. Doing so also forces us to merge nsEventQueueBase and nsEventQueue: there's no way to have nsEventQueueBase require an externally-defined Mutex and then have nsEventQueue subclass nsEventQueueBase and provide its own Mutex to the superclass. C++ initialization rules (and the way things like CondVar are constructed) simply forbid it. But that's OK, because we want a world where nsEventQueue is externally locked anyway, so there's no reason to have separate classes here. One casualty of this work is removing ChaosMode support from nsEventQueue. nsEventQueue had support to delay placing events into the queue, theoretically giving other threads the chance to put events there first. Unfortunately, since the thread would have been holding a lock (as is evident from the MutexAutoLock& parameter required), sleeping in PutEvent accomplishes nothing but delaying the thread from getting useful work done. We should support this, but it's complicated to figure out how to reasonably support this right now. A wrinkle in this overall pleasant refactoring is that nsThreadPool's threads wait for limited amounts of time for new events to be placed in the event queue, so that they can shut themselves down if no new events are appearing. Setting limits on the number of threads also needs to be able to wake up all threads, so threads can shut themselves down if necessary. Unfortunately, with the transition to nsEventQueue managing its own condition variable, there's no way for nsThreadPool to perform these functions, since there's no Monitor to wait on. Therefore, we add a private API for accessing the condition variable and performing the tasks nsThreadPool needs. Prior to all the previous patches, placing items in an nsThread's event queue required three lock/unlock pairs: one for nsThread's Mutex, one to enter nsEventQueue's ReentrantMonitor, and one to exit nsEventQueue's ReentrantMonitor. The upshot of all this work is that we now only require one lock/unlock pair in nsThread itself, as things should be.
68 lines
2.0 KiB
C++
68 lines
2.0 KiB
C++
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#ifndef nsThreadPool_h__
|
|
#define nsThreadPool_h__
|
|
|
|
#include "nsIThreadPool.h"
|
|
#include "nsIThread.h"
|
|
#include "nsIRunnable.h"
|
|
#include "nsEventQueue.h"
|
|
#include "nsCOMArray.h"
|
|
#include "nsCOMPtr.h"
|
|
#include "nsThreadUtils.h"
|
|
#include "mozilla/Attributes.h"
|
|
#include "mozilla/AlreadyAddRefed.h"
|
|
#include "mozilla/Mutex.h"
|
|
#include "mozilla/Monitor.h"
|
|
|
|
class nsThreadPool final
|
|
: public nsIThreadPool
|
|
, public nsIRunnable
|
|
{
|
|
public:
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
NS_DECL_NSIEVENTTARGET
|
|
NS_DECL_NSITHREADPOOL
|
|
NS_DECL_NSIRUNNABLE
|
|
// missing from NS_DECL_NSIEVENTTARGET because MSVC
|
|
nsresult Dispatch(nsIRunnable* aEvent, uint32_t aFlags) {
|
|
return Dispatch(nsCOMPtr<nsIRunnable>(aEvent).forget(), aFlags);
|
|
}
|
|
|
|
nsThreadPool();
|
|
|
|
private:
|
|
~nsThreadPool();
|
|
|
|
void ShutdownThread(nsIThread* aThread);
|
|
nsresult PutEvent(nsIRunnable* aEvent);
|
|
nsresult PutEvent(already_AddRefed<nsIRunnable>&& aEvent);
|
|
|
|
nsCOMArray<nsIThread> mThreads;
|
|
mozilla::Mutex mMutex;
|
|
nsEventQueue mEvents;
|
|
uint32_t mThreadLimit;
|
|
uint32_t mIdleThreadLimit;
|
|
uint32_t mIdleThreadTimeout;
|
|
uint32_t mIdleCount;
|
|
uint32_t mStackSize;
|
|
nsCOMPtr<nsIThreadPoolListener> mListener;
|
|
bool mShutdown;
|
|
nsCString mName;
|
|
nsThreadPoolNaming mThreadNaming;
|
|
};
|
|
|
|
#define NS_THREADPOOL_CID \
|
|
{ /* 547ec2a8-315e-4ec4-888e-6e4264fe90eb */ \
|
|
0x547ec2a8, \
|
|
0x315e, \
|
|
0x4ec4, \
|
|
{0x88, 0x8e, 0x6e, 0x42, 0x64, 0xfe, 0x90, 0xeb} \
|
|
}
|
|
|
|
#endif // nsThreadPool_h__
|