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"
49 #ifndef XPCOM_GLUE_AVOID_NSPR
51 NS_IMPL_THREADSAFE_ISUPPORTS1(nsRunnable
, nsIRunnable
)
60 #endif // XPCOM_GLUE_AVOID_NSPR
62 //-----------------------------------------------------------------------------
65 NS_NewThread(nsIThread
**result
, nsIRunnable
*event
)
67 nsCOMPtr
<nsIThread
> thread
;
68 #ifdef MOZILLA_INTERNAL_API
69 nsresult rv
= nsThreadManager::get()->
70 nsThreadManager::NewThread(0, getter_AddRefs(thread
));
73 nsCOMPtr
<nsIThreadManager
> mgr
=
74 do_GetService(NS_THREADMANAGER_CONTRACTID
, &rv
);
75 NS_ENSURE_SUCCESS(rv
, rv
);
77 rv
= mgr
->NewThread(0, getter_AddRefs(thread
));
79 NS_ENSURE_SUCCESS(rv
, rv
);
82 rv
= thread
->Dispatch(event
, NS_DISPATCH_NORMAL
);
83 NS_ENSURE_SUCCESS(rv
, rv
);
92 NS_GetCurrentThread(nsIThread
**result
)
94 #ifdef MOZILLA_INTERNAL_API
95 return nsThreadManager::get()->nsThreadManager::GetCurrentThread(result
);
98 nsCOMPtr
<nsIThreadManager
> mgr
=
99 do_GetService(NS_THREADMANAGER_CONTRACTID
, &rv
);
100 NS_ENSURE_SUCCESS(rv
, rv
);
101 return mgr
->GetCurrentThread(result
);
106 NS_GetMainThread(nsIThread
**result
)
108 #ifdef MOZILLA_INTERNAL_API
109 return nsThreadManager::get()->nsThreadManager::GetMainThread(result
);
112 nsCOMPtr
<nsIThreadManager
> mgr
=
113 do_GetService(NS_THREADMANAGER_CONTRACTID
, &rv
);
114 NS_ENSURE_SUCCESS(rv
, rv
);
115 return mgr
->GetMainThread(result
);
122 PRBool result
= PR_FALSE
;
123 #ifdef MOZILLA_INTERNAL_API
124 nsThreadManager::get()->nsThreadManager::GetIsMainThread(&result
);
126 nsCOMPtr
<nsIThreadManager
> mgr
=
127 do_GetService(NS_THREADMANAGER_CONTRACTID
);
129 mgr
->GetIsMainThread(&result
);
135 NS_DispatchToCurrentThread(nsIRunnable
*event
)
137 #ifdef MOZILLA_INTERNAL_API
138 nsIThread
*thread
= NS_GetCurrentThread();
139 if (!thread
) { return NS_ERROR_UNEXPECTED
; }
141 nsCOMPtr
<nsIThread
> thread
;
142 nsresult rv
= NS_GetCurrentThread(getter_AddRefs(thread
));
143 NS_ENSURE_SUCCESS(rv
, rv
);
145 return thread
->Dispatch(event
, NS_DISPATCH_NORMAL
);
149 NS_DispatchToMainThread(nsIRunnable
*event
, PRUint32 dispatchFlags
)
151 nsCOMPtr
<nsIThread
> thread
;
152 nsresult rv
= NS_GetMainThread(getter_AddRefs(thread
));
153 NS_ENSURE_SUCCESS(rv
, rv
);
154 return thread
->Dispatch(event
, dispatchFlags
);
157 #ifndef XPCOM_GLUE_AVOID_NSPR
159 NS_ProcessPendingEvents(nsIThread
*thread
, PRIntervalTime timeout
)
163 #ifdef MOZILLA_INTERNAL_API
165 thread
= NS_GetCurrentThread();
166 NS_ENSURE_STATE(thread
);
169 nsCOMPtr
<nsIThread
> current
;
171 rv
= NS_GetCurrentThread(getter_AddRefs(current
));
172 NS_ENSURE_SUCCESS(rv
, rv
);
173 thread
= current
.get();
177 PRIntervalTime start
= PR_IntervalNow();
179 PRBool processedEvent
;
180 rv
= thread
->ProcessNextEvent(PR_FALSE
, &processedEvent
);
181 if (NS_FAILED(rv
) || !processedEvent
)
183 if (PR_IntervalNow() - start
> timeout
)
188 #endif // XPCOM_GLUE_AVOID_NSPR
191 NS_HasPendingEvents(nsIThread
*thread
)
193 #ifdef MOZILLA_INTERNAL_API
195 thread
= NS_GetCurrentThread();
196 NS_ENSURE_TRUE(thread
, PR_FALSE
);
199 nsCOMPtr
<nsIThread
> current
;
201 NS_GetCurrentThread(getter_AddRefs(current
));
202 NS_ENSURE_TRUE(current
, PR_FALSE
);
203 thread
= current
.get();
207 return NS_SUCCEEDED(thread
->HasPendingEvents(&val
)) && val
;
211 NS_ProcessNextEvent(nsIThread
*thread
, PRBool mayWait
)
213 #ifdef MOZILLA_INTERNAL_API
215 thread
= NS_GetCurrentThread();
216 NS_ENSURE_TRUE(thread
, PR_FALSE
);
219 nsCOMPtr
<nsIThread
> current
;
221 NS_GetCurrentThread(getter_AddRefs(current
));
222 NS_ENSURE_TRUE(current
, PR_FALSE
);
223 thread
= current
.get();
227 return NS_SUCCEEDED(thread
->ProcessNextEvent(mayWait
, &val
)) && val
;
230 #ifdef MOZILLA_INTERNAL_API
232 NS_GetCurrentThread() {
233 return nsThreadManager::get()->GetCurrentThread();