Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / xpcom / glue / nsThreadUtils.cpp
blob1bc0a60f6b7d0d1e03a88e3d2359ebdbf079832e
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 #include "nsThreadUtils.h"
41 #ifdef MOZILLA_INTERNAL_API
42 # include "nsThreadManager.h"
43 #else
44 # include "nsXPCOMCIDInternal.h"
45 # include "nsIThreadManager.h"
46 # include "nsServiceManagerUtils.h"
47 #endif
49 #ifndef XPCOM_GLUE_AVOID_NSPR
51 NS_IMPL_THREADSAFE_ISUPPORTS1(nsRunnable, nsIRunnable)
53 NS_IMETHODIMP
54 nsRunnable::Run()
56 // Do nothing
57 return NS_OK;
60 #endif // XPCOM_GLUE_AVOID_NSPR
62 //-----------------------------------------------------------------------------
64 NS_METHOD
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));
71 #else
72 nsresult rv;
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));
78 #endif
79 NS_ENSURE_SUCCESS(rv, rv);
81 if (event) {
82 rv = thread->Dispatch(event, NS_DISPATCH_NORMAL);
83 NS_ENSURE_SUCCESS(rv, rv);
86 *result = nsnull;
87 thread.swap(*result);
88 return NS_OK;
91 NS_METHOD
92 NS_GetCurrentThread(nsIThread **result)
94 #ifdef MOZILLA_INTERNAL_API
95 return nsThreadManager::get()->nsThreadManager::GetCurrentThread(result);
96 #else
97 nsresult rv;
98 nsCOMPtr<nsIThreadManager> mgr =
99 do_GetService(NS_THREADMANAGER_CONTRACTID, &rv);
100 NS_ENSURE_SUCCESS(rv, rv);
101 return mgr->GetCurrentThread(result);
102 #endif
105 NS_METHOD
106 NS_GetMainThread(nsIThread **result)
108 #ifdef MOZILLA_INTERNAL_API
109 return nsThreadManager::get()->nsThreadManager::GetMainThread(result);
110 #else
111 nsresult rv;
112 nsCOMPtr<nsIThreadManager> mgr =
113 do_GetService(NS_THREADMANAGER_CONTRACTID, &rv);
114 NS_ENSURE_SUCCESS(rv, rv);
115 return mgr->GetMainThread(result);
116 #endif
119 NS_METHOD_(PRBool)
120 NS_IsMainThread()
122 PRBool result = PR_FALSE;
123 #ifdef MOZILLA_INTERNAL_API
124 nsThreadManager::get()->nsThreadManager::GetIsMainThread(&result);
125 #else
126 nsCOMPtr<nsIThreadManager> mgr =
127 do_GetService(NS_THREADMANAGER_CONTRACTID);
128 if (mgr)
129 mgr->GetIsMainThread(&result);
130 #endif
131 return result;
134 NS_METHOD
135 NS_DispatchToCurrentThread(nsIRunnable *event)
137 #ifdef MOZILLA_INTERNAL_API
138 nsIThread *thread = NS_GetCurrentThread();
139 if (!thread) { return NS_ERROR_UNEXPECTED; }
140 #else
141 nsCOMPtr<nsIThread> thread;
142 nsresult rv = NS_GetCurrentThread(getter_AddRefs(thread));
143 NS_ENSURE_SUCCESS(rv, rv);
144 #endif
145 return thread->Dispatch(event, NS_DISPATCH_NORMAL);
148 NS_METHOD
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
158 NS_METHOD
159 NS_ProcessPendingEvents(nsIThread *thread, PRIntervalTime timeout)
161 nsresult rv = NS_OK;
163 #ifdef MOZILLA_INTERNAL_API
164 if (!thread) {
165 thread = NS_GetCurrentThread();
166 NS_ENSURE_STATE(thread);
168 #else
169 nsCOMPtr<nsIThread> current;
170 if (!thread) {
171 rv = NS_GetCurrentThread(getter_AddRefs(current));
172 NS_ENSURE_SUCCESS(rv, rv);
173 thread = current.get();
175 #endif
177 PRIntervalTime start = PR_IntervalNow();
178 for (;;) {
179 PRBool processedEvent;
180 rv = thread->ProcessNextEvent(PR_FALSE, &processedEvent);
181 if (NS_FAILED(rv) || !processedEvent)
182 break;
183 if (PR_IntervalNow() - start > timeout)
184 break;
186 return rv;
188 #endif // XPCOM_GLUE_AVOID_NSPR
190 PRBool
191 NS_HasPendingEvents(nsIThread *thread)
193 #ifdef MOZILLA_INTERNAL_API
194 if (!thread) {
195 thread = NS_GetCurrentThread();
196 NS_ENSURE_TRUE(thread, PR_FALSE);
198 #else
199 nsCOMPtr<nsIThread> current;
200 if (!thread) {
201 NS_GetCurrentThread(getter_AddRefs(current));
202 NS_ENSURE_TRUE(current, PR_FALSE);
203 thread = current.get();
205 #endif
206 PRBool val;
207 return NS_SUCCEEDED(thread->HasPendingEvents(&val)) && val;
210 PRBool
211 NS_ProcessNextEvent(nsIThread *thread, PRBool mayWait)
213 #ifdef MOZILLA_INTERNAL_API
214 if (!thread) {
215 thread = NS_GetCurrentThread();
216 NS_ENSURE_TRUE(thread, PR_FALSE);
218 #else
219 nsCOMPtr<nsIThread> current;
220 if (!thread) {
221 NS_GetCurrentThread(getter_AddRefs(current));
222 NS_ENSURE_TRUE(current, PR_FALSE);
223 thread = current.get();
225 #endif
226 PRBool val;
227 return NS_SUCCEEDED(thread->ProcessNextEvent(mayWait, &val)) && val;
230 #ifdef MOZILLA_INTERNAL_API
231 nsIThread *
232 NS_GetCurrentThread() {
233 return nsThreadManager::get()->GetCurrentThread();
235 #endif