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
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is Mozilla code.
17 : *
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.
21 : *
22 : * Contributor(s):
23 : * Darin Fisher <darin@meer.net>
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef nsThread_h__
40 : #define nsThread_h__
41 :
42 : #include "mozilla/Mutex.h"
43 : #include "nsIThreadInternal.h"
44 : #include "nsISupportsPriority.h"
45 : #include "nsEventQueue.h"
46 : #include "nsThreadUtils.h"
47 : #include "nsString.h"
48 : #include "nsTObserverArray.h"
49 :
50 : // A native thread
51 : class nsThread : public nsIThreadInternal, public nsISupportsPriority
52 : {
53 : public:
54 : NS_DECL_ISUPPORTS
55 : NS_DECL_NSIEVENTTARGET
56 : NS_DECL_NSITHREAD
57 : NS_DECL_NSITHREADINTERNAL
58 : NS_DECL_NSISUPPORTSPRIORITY
59 :
60 : enum MainThreadFlag {
61 : MAIN_THREAD,
62 : NOT_MAIN_THREAD
63 : };
64 :
65 : nsThread(MainThreadFlag aMainThread, PRUint32 aStackSize);
66 :
67 : // Initialize this as a wrapper for a new PRThread.
68 : nsresult Init();
69 :
70 : // Initialize this as a wrapper for the current PRThread.
71 : nsresult InitCurrentThread();
72 :
73 : // The PRThread corresponding to this thread.
74 27262 : PRThread *GetPRThread() { return mThread; }
75 :
76 : // If this flag is true, then the nsThread was created using
77 : // nsIThreadManager::NewThread.
78 3077 : bool ShutdownRequired() { return mShutdownRequired; }
79 :
80 : // Clear the observer list.
81 7525 : void ClearObservers() { mEventObservers.Clear(); }
82 :
83 : private:
84 : friend class nsThreadShutdownEvent;
85 :
86 : ~nsThread();
87 :
88 1034991 : bool ShuttingDown() { return mShutdownContext != nsnull; }
89 :
90 : static void ThreadFunc(void *arg);
91 :
92 : // Helper
93 286512 : already_AddRefed<nsIThreadObserver> GetObserver() {
94 : nsIThreadObserver *obs;
95 286512 : nsThread::GetObserver(&obs);
96 286512 : return already_AddRefed<nsIThreadObserver>(obs);
97 : }
98 :
99 : // Wrappers for event queue methods:
100 6106 : bool GetEvent(bool mayWait, nsIRunnable **event) {
101 6106 : return mEvents->GetEvent(mayWait, event);
102 : }
103 : nsresult PutEvent(nsIRunnable *event);
104 :
105 : // Wrapper for nsEventQueue that supports chaining.
106 7514 : class nsChainedEventQueue {
107 : public:
108 7525 : nsChainedEventQueue(nsIThreadEventFilter *filter = nsnull)
109 7525 : : mNext(nsnull), mFilter(filter) {
110 7525 : }
111 :
112 799503 : bool GetEvent(bool mayWait, nsIRunnable **event) {
113 799503 : return mQueue.GetEvent(mayWait, event);
114 : }
115 :
116 : bool PutEvent(nsIRunnable *event);
117 :
118 6106 : bool HasPendingEvent() {
119 6106 : return mQueue.HasPendingEvent();
120 : }
121 :
122 : class nsChainedEventQueue *mNext;
123 : private:
124 : nsCOMPtr<nsIThreadEventFilter> mFilter;
125 : nsEventQueue mQueue;
126 : };
127 :
128 : // This lock protects access to mObserver, mEvents and mEventsAreDoomed.
129 : // All of those fields are only modified on the thread itself (never from
130 : // another thread). This means that we can avoid holding the lock while
131 : // using mObserver and mEvents on the thread itself. When calling PutEvent
132 : // on mEvents, we have to hold the lock to synchronize with PopEventQueue.
133 : mozilla::Mutex mLock;
134 :
135 : nsCOMPtr<nsIThreadObserver> mObserver;
136 :
137 : // Only accessed on the target thread.
138 : nsAutoTObserverArray<nsCOMPtr<nsIThreadObserver>, 2> mEventObservers;
139 :
140 : nsChainedEventQueue *mEvents; // never null
141 : nsChainedEventQueue mEventsRoot;
142 :
143 : PRInt32 mPriority;
144 : PRThread *mThread;
145 : PRUint32 mRunningEvent; // counter
146 : PRUint32 mStackSize;
147 :
148 : struct nsThreadShutdownContext *mShutdownContext;
149 :
150 : bool mShutdownRequired;
151 : bool mShutdownPending;
152 : // Set to true when events posted to this thread will never run.
153 : bool mEventsAreDoomed;
154 : MainThreadFlag mIsMainThread;
155 : };
156 :
157 : //-----------------------------------------------------------------------------
158 :
159 4 : class nsThreadSyncDispatch : public nsRunnable {
160 : public:
161 1 : nsThreadSyncDispatch(nsIThread *origin, nsIRunnable *task)
162 1 : : mOrigin(origin), mSyncTask(task), mResult(NS_ERROR_NOT_INITIALIZED) {
163 1 : }
164 :
165 2 : bool IsPending() {
166 2 : return mSyncTask != nsnull;
167 : }
168 :
169 1 : nsresult Result() {
170 1 : return mResult;
171 : }
172 :
173 : private:
174 : NS_DECL_NSIRUNNABLE
175 :
176 : nsCOMPtr<nsIThread> mOrigin;
177 : nsCOMPtr<nsIRunnable> mSyncTask;
178 : nsresult mResult;
179 : };
180 :
181 : namespace mozilla {
182 :
183 : /**
184 : * This function causes the main thread to fire a memory pressure event at its
185 : * next available opportunity.
186 : *
187 : * You may call this function from any thread.
188 : */
189 : void ScheduleMemoryPressureEvent();
190 :
191 : } // namespace mozilla
192 :
193 : #endif // nsThread_h__
|