|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #ifndef _NSPSMBACKGROUNDTHREAD_H_ |
|
6 #define _NSPSMBACKGROUNDTHREAD_H_ |
|
7 |
|
8 #include "nspr.h" |
|
9 #include "nscore.h" |
|
10 #include "mozilla/CondVar.h" |
|
11 #include "mozilla/Mutex.h" |
|
12 #include "nsNSSComponent.h" |
|
13 |
|
14 class nsPSMBackgroundThread |
|
15 { |
|
16 protected: |
|
17 static void nsThreadRunner(void *arg); |
|
18 virtual void Run(void) = 0; |
|
19 |
|
20 // used to join the thread |
|
21 PRThread *mThreadHandle; |
|
22 |
|
23 // Shared mutex used for condition variables, |
|
24 // and to protect access to mExitState. |
|
25 // Derived classes may use it to protect additional |
|
26 // resources. |
|
27 mozilla::Mutex mMutex; |
|
28 |
|
29 // Used to signal the thread's Run loop when a job is added |
|
30 // and/or exit is requested. |
|
31 mozilla::CondVar mCond; |
|
32 |
|
33 bool exitRequested(::mozilla::MutexAutoLock const & proofOfLock) const; |
|
34 bool exitRequestedNoLock() const { return mExitState != ePSMThreadRunning; } |
|
35 nsresult postStoppedEventToMainThread(::mozilla::MutexAutoLock const & proofOfLock); |
|
36 |
|
37 private: |
|
38 enum { |
|
39 ePSMThreadRunning = 0, |
|
40 ePSMThreadStopRequested = 1, |
|
41 ePSMThreadStopped = 2 |
|
42 } mExitState; |
|
43 |
|
44 // The thread's name. |
|
45 nsCString mName; |
|
46 |
|
47 public: |
|
48 nsPSMBackgroundThread(); |
|
49 virtual ~nsPSMBackgroundThread(); |
|
50 |
|
51 nsresult startThread(const nsCSubstring & name); |
|
52 void requestExit(); |
|
53 }; |
|
54 |
|
55 |
|
56 #endif |