michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef _NSPSMBACKGROUNDTHREAD_H_ michael@0: #define _NSPSMBACKGROUNDTHREAD_H_ michael@0: michael@0: #include "nspr.h" michael@0: #include "nscore.h" michael@0: #include "mozilla/CondVar.h" michael@0: #include "mozilla/Mutex.h" michael@0: #include "nsNSSComponent.h" michael@0: michael@0: class nsPSMBackgroundThread michael@0: { michael@0: protected: michael@0: static void nsThreadRunner(void *arg); michael@0: virtual void Run(void) = 0; michael@0: michael@0: // used to join the thread michael@0: PRThread *mThreadHandle; michael@0: michael@0: // Shared mutex used for condition variables, michael@0: // and to protect access to mExitState. michael@0: // Derived classes may use it to protect additional michael@0: // resources. michael@0: mozilla::Mutex mMutex; michael@0: michael@0: // Used to signal the thread's Run loop when a job is added michael@0: // and/or exit is requested. michael@0: mozilla::CondVar mCond; michael@0: michael@0: bool exitRequested(::mozilla::MutexAutoLock const & proofOfLock) const; michael@0: bool exitRequestedNoLock() const { return mExitState != ePSMThreadRunning; } michael@0: nsresult postStoppedEventToMainThread(::mozilla::MutexAutoLock const & proofOfLock); michael@0: michael@0: private: michael@0: enum { michael@0: ePSMThreadRunning = 0, michael@0: ePSMThreadStopRequested = 1, michael@0: ePSMThreadStopped = 2 michael@0: } mExitState; michael@0: michael@0: // The thread's name. michael@0: nsCString mName; michael@0: michael@0: public: michael@0: nsPSMBackgroundThread(); michael@0: virtual ~nsPSMBackgroundThread(); michael@0: michael@0: nsresult startThread(const nsCSubstring & name); michael@0: void requestExit(); michael@0: }; michael@0: michael@0: michael@0: #endif