2014-06-30 15:39:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
#ifndef nsThreadManager_h__
|
|
|
|
#define nsThreadManager_h__
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
#include "mozilla/Mutex.h"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsIThreadManager.h"
|
|
|
|
#include "nsThread.h"
|
|
|
|
|
|
|
|
class nsIRunnable;
|
|
|
|
|
|
|
|
class nsThreadManager : public nsIThreadManager {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSITHREADMANAGER
|
|
|
|
|
2017-09-20 21:10:20 +00:00
|
|
|
static nsThreadManager& get();
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2018-01-28 07:41:36 +00:00
|
|
|
static void InitializeShutdownObserver();
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
// Shutdown all threads. This function should only be called on the main
|
|
|
|
// thread of the application process.
|
|
|
|
void Shutdown();
|
|
|
|
|
|
|
|
// Called by nsThread to inform the ThreadManager it exists. This method
|
|
|
|
// must be called when the given thread is the current thread.
|
2016-06-10 06:04:49 +00:00
|
|
|
void RegisterCurrentThread(nsThread& aThread);
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
// Called by nsThread to inform the ThreadManager it is going away. This
|
|
|
|
// method must be called when the given thread is the current thread.
|
2018-07-27 22:26:08 +00:00
|
|
|
void UnregisterCurrentThread(nsThread& aThread);
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2008-02-21 12:47:26 +00:00
|
|
|
// Returns the current thread. Returns null if OOM or if ThreadManager isn't
|
2018-01-19 15:41:22 +00:00
|
|
|
// initialized. Creates the nsThread if one does not exist yet.
|
2014-05-27 07:15:35 +00:00
|
|
|
nsThread* GetCurrentThread();
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2018-01-19 15:41:22 +00:00
|
|
|
// Returns true iff the currently running thread has an nsThread associated
|
|
|
|
// with it (ie; whether this is a thread that we can dispatch runnables to).
|
|
|
|
bool IsNSThread() const;
|
|
|
|
|
2017-07-28 21:56:49 +00:00
|
|
|
// CreateCurrentThread sets up an nsThread for the current thread. It uses the
|
|
|
|
// event queue and main thread flags passed in. It should only be called once
|
|
|
|
// for the current thread. After it returns, GetCurrentThread() will return
|
|
|
|
// the thread that was created. GetCurrentThread() will also create a thread
|
|
|
|
// (lazily), but it doesn't allow the queue or main-thread attributes to be
|
|
|
|
// specified.
|
|
|
|
nsThread* CreateCurrentThread(mozilla::SynchronizedEventQueue* aQueue,
|
|
|
|
nsThread::MainThreadFlag aMainThread);
|
|
|
|
|
2013-04-19 11:54:18 +00:00
|
|
|
// Returns the maximal number of threads that have been in existence
|
|
|
|
// simultaneously during the execution of the thread manager.
|
|
|
|
uint32_t GetHighestNumberOfThreads();
|
|
|
|
|
2006-05-10 18:05:38 +00:00
|
|
|
// This needs to be public in order to support static instantiation of this
|
|
|
|
// class with older compilers (e.g., egcs-2.91.66).
|
2014-05-27 07:15:35 +00:00
|
|
|
~nsThreadManager() {}
|
Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:
Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.
Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.
There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.
The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.
One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.
MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 02:42:13 +00:00
|
|
|
|
2017-03-21 07:44:12 +00:00
|
|
|
void EnableMainThreadEventPrioritization();
|
2017-07-28 07:14:54 +00:00
|
|
|
void FlushInputEventPrioritization();
|
|
|
|
void SuspendInputEventPrioritization();
|
|
|
|
void ResumeInputEventPrioritization();
|
2006-05-10 18:05:38 +00:00
|
|
|
|
2019-01-10 17:07:34 +00:00
|
|
|
static bool MainThreadHasPendingHighPriorityEvents();
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
private:
|
|
|
|
nsThreadManager()
|
2012-07-30 14:20:58 +00:00
|
|
|
: mCurThreadIndex(0), mMainPRThread(nullptr), mInitialized(false) {}
|
2013-04-19 11:54:18 +00:00
|
|
|
|
2018-01-28 07:41:36 +00:00
|
|
|
nsresult SpinEventLoopUntilInternal(nsINestedEventLoopCondition* aCondition,
|
|
|
|
bool aCheckingShutdown);
|
|
|
|
|
2018-07-26 23:36:16 +00:00
|
|
|
static void ReleaseThread(void* aData);
|
|
|
|
|
2015-03-23 20:49:09 +00:00
|
|
|
unsigned mCurThreadIndex; // thread-local-storage index
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<nsThread> mMainThread;
|
2016-06-10 06:04:49 +00:00
|
|
|
PRThread* mMainPRThread;
|
2018-07-21 14:27:32 +00:00
|
|
|
mozilla::Atomic<bool, mozilla::SequentiallyConsistent,
|
|
|
|
mozilla::recordreplay::Behavior::DontPreserve>
|
|
|
|
mInitialized;
|
2006-05-10 17:30:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NS_THREADMANAGER_CID \
|
|
|
|
{ /* 7a4204c6-e45a-4c37-8ebb-6709a22c917c */ \
|
|
|
|
0x7a4204c6, 0xe45a, 0x4c37, { \
|
|
|
|
0x8e, 0xbb, 0x67, 0x09, 0xa2, 0x2c, 0x91, 0x7c \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // nsThreadManager_h__
|