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
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.
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 #include "nsThreadUtils.h"
41 #ifdef MOZILLA_INTERNAL_API
42 # include "nsThreadManager.h"
44 # include "nsXPCOMCIDInternal.h"
45 # include "nsIThreadManager.h"
46 # include "nsServiceManagerUtils.h"
53 #ifndef XPCOM_GLUE_AVOID_NSPR
55 NS_IMPL_THREADSAFE_ISUPPORTS1(nsRunnable
, nsIRunnable
)
64 #endif // XPCOM_GLUE_AVOID_NSPR
66 //-----------------------------------------------------------------------------
69 NS_NewThread(nsIThread
**result
, nsIRunnable
*event
)
71 nsCOMPtr
<nsIThread
> thread
;
72 #ifdef MOZILLA_INTERNAL_API
73 nsresult rv
= nsThreadManager::get()->
74 nsThreadManager::NewThread(0, getter_AddRefs(thread
));
77 nsCOMPtr
<nsIThreadManager
> mgr
=
78 do_GetService(NS_THREADMANAGER_CONTRACTID
, &rv
);
79 NS_ENSURE_SUCCESS(rv
, rv
);
81 rv
= mgr
->NewThread(0, getter_AddRefs(thread
));
83 NS_ENSURE_SUCCESS(rv
, rv
);
86 rv
= thread
->Dispatch(event
, NS_DISPATCH_NORMAL
);
87 NS_ENSURE_SUCCESS(rv
, rv
);
96 NS_GetCurrentThread(nsIThread
**result
)
98 #ifdef MOZILLA_INTERNAL_API
99 return nsThreadManager::get()->nsThreadManager::GetCurrentThread(result
);
102 nsCOMPtr
<nsIThreadManager
> mgr
=
103 do_GetService(NS_THREADMANAGER_CONTRACTID
, &rv
);
104 NS_ENSURE_SUCCESS(rv
, rv
);
105 return mgr
->GetCurrentThread(result
);
110 NS_GetMainThread(nsIThread
**result
)
112 #ifdef MOZILLA_INTERNAL_API
113 return nsThreadManager::get()->nsThreadManager::GetMainThread(result
);
116 nsCOMPtr
<nsIThreadManager
> mgr
=
117 do_GetService(NS_THREADMANAGER_CONTRACTID
, &rv
);
118 NS_ENSURE_SUCCESS(rv
, rv
);
119 return mgr
->GetMainThread(result
);
123 #ifndef MOZILLA_INTERNAL_API
124 bool NS_IsMainThread()
126 PRBool result
= PR_FALSE
;
127 nsCOMPtr
<nsIThreadManager
> mgr
=
128 do_GetService(NS_THREADMANAGER_CONTRACTID
);
130 mgr
->GetIsMainThread(&result
);
133 #elif defined(XP_WIN)
134 extern DWORD gTLSIsMainThreadIndex
;
138 return !!TlsGetValue(gTLSIsMainThreadIndex
);
140 #elif !defined(NS_TLS)
141 bool NS_IsMainThread()
143 PRBool result
= PR_FALSE
;
144 nsThreadManager::get()->nsThreadManager::GetIsMainThread(&result
);
147 #elif !defined(MOZ_ENABLE_LIBXUL)
148 bool NS_IsMainThread()
150 return gTLSIsMainThread
;
155 NS_DispatchToCurrentThread(nsIRunnable
*event
)
157 #ifdef MOZILLA_INTERNAL_API
158 nsIThread
*thread
= NS_GetCurrentThread();
159 if (!thread
) { return NS_ERROR_UNEXPECTED
; }
161 nsCOMPtr
<nsIThread
> thread
;
162 nsresult rv
= NS_GetCurrentThread(getter_AddRefs(thread
));
163 NS_ENSURE_SUCCESS(rv
, rv
);
165 return thread
->Dispatch(event
, NS_DISPATCH_NORMAL
);
169 NS_DispatchToMainThread(nsIRunnable
*event
, PRUint32 dispatchFlags
)
171 nsCOMPtr
<nsIThread
> thread
;
172 nsresult rv
= NS_GetMainThread(getter_AddRefs(thread
));
173 NS_ENSURE_SUCCESS(rv
, rv
);
174 return thread
->Dispatch(event
, dispatchFlags
);
177 #ifndef XPCOM_GLUE_AVOID_NSPR
179 NS_ProcessPendingEvents(nsIThread
*thread
, PRIntervalTime timeout
)
183 #ifdef MOZILLA_INTERNAL_API
185 thread
= NS_GetCurrentThread();
186 NS_ENSURE_STATE(thread
);
189 nsCOMPtr
<nsIThread
> current
;
191 rv
= NS_GetCurrentThread(getter_AddRefs(current
));
192 NS_ENSURE_SUCCESS(rv
, rv
);
193 thread
= current
.get();
197 PRIntervalTime start
= PR_IntervalNow();
199 PRBool processedEvent
;
200 rv
= thread
->ProcessNextEvent(PR_FALSE
, &processedEvent
);
201 if (NS_FAILED(rv
) || !processedEvent
)
203 if (PR_IntervalNow() - start
> timeout
)
208 #endif // XPCOM_GLUE_AVOID_NSPR
211 hasPendingEvents(nsIThread
*thread
)
214 return NS_SUCCEEDED(thread
->HasPendingEvents(&val
)) && val
;
218 NS_HasPendingEvents(nsIThread
*thread
)
221 #ifndef MOZILLA_INTERNAL_API
222 nsCOMPtr
<nsIThread
> current
;
223 NS_GetCurrentThread(getter_AddRefs(current
));
224 return hasPendingEvents(current
);
226 thread
= NS_GetCurrentThread();
227 NS_ENSURE_TRUE(thread
, PR_FALSE
);
230 return hasPendingEvents(thread
);
234 NS_ProcessNextEvent(nsIThread
*thread
, PRBool mayWait
)
236 #ifdef MOZILLA_INTERNAL_API
238 thread
= NS_GetCurrentThread();
239 NS_ENSURE_TRUE(thread
, PR_FALSE
);
242 nsCOMPtr
<nsIThread
> current
;
244 NS_GetCurrentThread(getter_AddRefs(current
));
245 NS_ENSURE_TRUE(current
, PR_FALSE
);
246 thread
= current
.get();
250 return NS_SUCCEEDED(thread
->ProcessNextEvent(mayWait
, &val
)) && val
;
253 #ifdef MOZILLA_INTERNAL_API
255 NS_GetCurrentThread() {
256 return nsThreadManager::get()->GetCurrentThread();