Backed out changeset fa432b23baa5. (Backing out bug 454781 to investigate mochitest...
[wine-gecko.git] / xpcom / threads / nsThread.h
blob69764c3baa9bfd820117ca1456955b8d85223687
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla code.
18 * The Initial Developer of the Original Code is Google Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2006
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Darin Fisher <darin@meer.net>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsThread_h__
40 #define nsThread_h__
42 #include "nsIThreadInternal.h"
43 #include "nsISupportsPriority.h"
44 #include "nsEventQueue.h"
45 #include "nsThreadUtils.h"
46 #include "nsString.h"
47 #include "nsAutoLock.h"
48 #include "nsAutoPtr.h"
50 // A native thread
51 class nsThread : public nsIThreadInternal, public nsISupportsPriority
53 public:
54 NS_DECL_ISUPPORTS
55 NS_DECL_NSIEVENTTARGET
56 NS_DECL_NSITHREAD
57 NS_DECL_NSITHREADINTERNAL
58 NS_DECL_NSISUPPORTSPRIORITY
60 nsThread();
62 // Initialize this as a wrapper for a new PRThread.
63 nsresult Init();
65 // Initialize this as a wrapper for the current PRThread.
66 nsresult InitCurrentThread();
68 // The PRThread corresponding to this thread.
69 PRThread *GetPRThread() { return mThread; }
71 // If this flag is true, then the nsThread was created using
72 // nsIThreadManager::NewThread.
73 PRBool ShutdownRequired() { return mShutdownRequired; }
75 // The global thread observer
76 static nsIThreadObserver* sGlobalObserver;
78 private:
79 friend class nsThreadShutdownEvent;
81 ~nsThread();
83 PRBool ShuttingDown() { return mShutdownContext != nsnull; }
85 PR_STATIC_CALLBACK(void) ThreadFunc(void *arg);
87 // Helper
88 already_AddRefed<nsIThreadObserver> GetObserver() {
89 nsIThreadObserver *obs;
90 nsThread::GetObserver(&obs);
91 return already_AddRefed<nsIThreadObserver>(obs);
94 // Wrappers for event queue methods:
95 PRBool GetEvent(PRBool mayWait, nsIRunnable **event) {
96 return mEvents->GetEvent(mayWait, event);
98 nsresult PutEvent(nsIRunnable *event);
100 // Wrapper for nsEventQueue that supports chaining.
101 class nsChainedEventQueue {
102 public:
103 nsChainedEventQueue(nsIThreadEventFilter *filter = nsnull)
104 : mNext(nsnull), mFilter(filter) {
107 PRBool IsInitialized() {
108 return mQueue.IsInitialized();
111 PRBool GetEvent(PRBool mayWait, nsIRunnable **event) {
112 return mQueue.GetEvent(mayWait, event);
115 PRBool PutEvent(nsIRunnable *event);
117 PRBool HasPendingEvent() {
118 return mQueue.HasPendingEvent();
121 class nsChainedEventQueue *mNext;
122 private:
123 nsCOMPtr<nsIThreadEventFilter> mFilter;
124 nsEventQueue mQueue;
127 // This lock protects access to mObserver, mEvents and mEventsAreDoomed.
128 // All of those fields are only modified on the thread itself (never from
129 // another thread). This means that we can avoid holding the lock while
130 // using mObserver and mEvents on the thread itself. When calling PutEvent
131 // on mEvents, we have to hold the lock to synchronize with PopEventQueue.
132 PRLock *mLock;
134 nsCOMPtr<nsIThreadObserver> mObserver;
136 nsChainedEventQueue *mEvents; // never null
137 nsChainedEventQueue mEventsRoot;
139 PRInt32 mPriority;
140 PRThread *mThread;
141 PRUint32 mRunningEvent; // counter
143 struct nsThreadShutdownContext *mShutdownContext;
145 PRPackedBool mShutdownRequired;
146 PRPackedBool mShutdownPending;
147 // Set to true when events posted to this thread will never run.
148 PRPackedBool mEventsAreDoomed;
151 //-----------------------------------------------------------------------------
153 class nsThreadSyncDispatch : public nsRunnable {
154 public:
155 nsThreadSyncDispatch(nsIThread *origin, nsIRunnable *task)
156 : mOrigin(origin), mSyncTask(task) {
159 PRBool IsPending() {
160 return mSyncTask != nsnull;
163 private:
164 NS_DECL_NSIRUNNABLE
166 nsCOMPtr<nsIThread> mOrigin;
167 nsCOMPtr<nsIRunnable> mSyncTask;
170 #endif // nsThread_h__