nsThreadUtils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsThreadUtils.h"
  6. #include "mozilla/Attributes.h"
  7. #include "mozilla/Likely.h"
  8. #include "mozilla/TimeStamp.h"
  9. #include "LeakRefPtr.h"
  10. #ifdef MOZILLA_INTERNAL_API
  11. # include "nsThreadManager.h"
  12. #else
  13. # include "nsXPCOMCIDInternal.h"
  14. # include "nsIThreadManager.h"
  15. # include "nsServiceManagerUtils.h"
  16. #endif
  17. #ifdef XP_WIN
  18. #include <windows.h>
  19. #endif
  20. using namespace mozilla;
  21. #ifndef XPCOM_GLUE_AVOID_NSPR
  22. NS_IMPL_ISUPPORTS(IdlePeriod, nsIIdlePeriod)
  23. NS_IMETHODIMP
  24. IdlePeriod::GetIdlePeriodHint(TimeStamp* aIdleDeadline)
  25. {
  26. *aIdleDeadline = TimeStamp();
  27. return NS_OK;
  28. }
  29. NS_IMPL_ISUPPORTS(Runnable, nsIRunnable)
  30. NS_IMETHODIMP
  31. Runnable::Run()
  32. {
  33. // Do nothing
  34. return NS_OK;
  35. }
  36. NS_IMPL_ISUPPORTS_INHERITED(CancelableRunnable, Runnable,
  37. nsICancelableRunnable)
  38. nsresult
  39. CancelableRunnable::Cancel()
  40. {
  41. // Do nothing
  42. return NS_OK;
  43. }
  44. NS_IMPL_ISUPPORTS_INHERITED(IncrementalRunnable, CancelableRunnable,
  45. nsIIncrementalRunnable)
  46. void
  47. IncrementalRunnable::SetDeadline(TimeStamp aDeadline)
  48. {
  49. // Do nothing
  50. }
  51. #endif // XPCOM_GLUE_AVOID_NSPR
  52. //-----------------------------------------------------------------------------
  53. nsresult
  54. NS_NewThread(nsIThread** aResult, nsIRunnable* aEvent, uint32_t aStackSize)
  55. {
  56. nsCOMPtr<nsIThread> thread;
  57. #ifdef MOZILLA_INTERNAL_API
  58. nsresult rv =
  59. nsThreadManager::get().nsThreadManager::NewThread(0, aStackSize,
  60. getter_AddRefs(thread));
  61. #else
  62. nsresult rv;
  63. nsCOMPtr<nsIThreadManager> mgr =
  64. do_GetService(NS_THREADMANAGER_CONTRACTID, &rv);
  65. if (NS_WARN_IF(NS_FAILED(rv))) {
  66. return rv;
  67. }
  68. rv = mgr->NewThread(0, aStackSize, getter_AddRefs(thread));
  69. #endif
  70. if (NS_WARN_IF(NS_FAILED(rv))) {
  71. return rv;
  72. }
  73. if (aEvent) {
  74. rv = thread->Dispatch(aEvent, NS_DISPATCH_NORMAL);
  75. if (NS_WARN_IF(NS_FAILED(rv))) {
  76. return rv;
  77. }
  78. }
  79. *aResult = nullptr;
  80. thread.swap(*aResult);
  81. return NS_OK;
  82. }
  83. nsresult
  84. NS_GetCurrentThread(nsIThread** aResult)
  85. {
  86. #ifdef MOZILLA_INTERNAL_API
  87. return nsThreadManager::get().nsThreadManager::GetCurrentThread(aResult);
  88. #else
  89. nsresult rv;
  90. nsCOMPtr<nsIThreadManager> mgr =
  91. do_GetService(NS_THREADMANAGER_CONTRACTID, &rv);
  92. if (NS_WARN_IF(NS_FAILED(rv))) {
  93. return rv;
  94. }
  95. return mgr->GetCurrentThread(aResult);
  96. #endif
  97. }
  98. nsresult
  99. NS_GetMainThread(nsIThread** aResult)
  100. {
  101. #ifdef MOZILLA_INTERNAL_API
  102. return nsThreadManager::get().nsThreadManager::GetMainThread(aResult);
  103. #else
  104. nsresult rv;
  105. nsCOMPtr<nsIThreadManager> mgr =
  106. do_GetService(NS_THREADMANAGER_CONTRACTID, &rv);
  107. if (NS_WARN_IF(NS_FAILED(rv))) {
  108. return rv;
  109. }
  110. return mgr->GetMainThread(aResult);
  111. #endif
  112. }
  113. #ifndef MOZILLA_INTERNAL_API
  114. bool
  115. NS_IsMainThread()
  116. {
  117. bool result = false;
  118. nsCOMPtr<nsIThreadManager> mgr =
  119. do_GetService(NS_THREADMANAGER_CONTRACTID);
  120. if (mgr) {
  121. mgr->GetIsMainThread(&result);
  122. }
  123. return bool(result);
  124. }
  125. #endif
  126. nsresult
  127. NS_DispatchToCurrentThread(already_AddRefed<nsIRunnable>&& aEvent)
  128. {
  129. nsresult rv;
  130. nsCOMPtr<nsIRunnable> event(aEvent);
  131. #ifdef MOZILLA_INTERNAL_API
  132. nsIThread* thread = NS_GetCurrentThread();
  133. if (!thread) {
  134. return NS_ERROR_UNEXPECTED;
  135. }
  136. #else
  137. nsCOMPtr<nsIThread> thread;
  138. rv = NS_GetCurrentThread(getter_AddRefs(thread));
  139. if (NS_WARN_IF(NS_FAILED(rv))) {
  140. return rv;
  141. }
  142. #endif
  143. // To keep us from leaking the runnable if dispatch method fails,
  144. // we grab the reference on failures and release it.
  145. nsIRunnable* temp = event.get();
  146. rv = thread->Dispatch(event.forget(), NS_DISPATCH_NORMAL);
  147. if (NS_WARN_IF(NS_FAILED(rv))) {
  148. // Dispatch() leaked the reference to the event, but due to caller's
  149. // assumptions, we shouldn't leak here. And given we are on the same
  150. // thread as the dispatch target, it's mostly safe to do it here.
  151. NS_RELEASE(temp);
  152. }
  153. return rv;
  154. }
  155. // It is common to call NS_DispatchToCurrentThread with a newly
  156. // allocated runnable with a refcount of zero. To keep us from leaking
  157. // the runnable if the dispatch method fails, we take a death grip.
  158. nsresult
  159. NS_DispatchToCurrentThread(nsIRunnable* aEvent)
  160. {
  161. nsCOMPtr<nsIRunnable> event(aEvent);
  162. return NS_DispatchToCurrentThread(event.forget());
  163. }
  164. nsresult
  165. NS_DispatchToMainThread(already_AddRefed<nsIRunnable>&& aEvent, uint32_t aDispatchFlags)
  166. {
  167. LeakRefPtr<nsIRunnable> event(Move(aEvent));
  168. nsCOMPtr<nsIThread> thread;
  169. nsresult rv = NS_GetMainThread(getter_AddRefs(thread));
  170. if (NS_WARN_IF(NS_FAILED(rv))) {
  171. NS_ASSERTION(false, "Failed NS_DispatchToMainThread() in shutdown; leaking");
  172. // NOTE: if you stop leaking here, adjust Promise::MaybeReportRejected(),
  173. // which assumes a leak here, or split into leaks and no-leaks versions
  174. return rv;
  175. }
  176. return thread->Dispatch(event.take(), aDispatchFlags);
  177. }
  178. // In the case of failure with a newly allocated runnable with a
  179. // refcount of zero, we intentionally leak the runnable, because it is
  180. // likely that the runnable is being dispatched to the main thread
  181. // because it owns main thread only objects, so it is not safe to
  182. // release them here.
  183. nsresult
  184. NS_DispatchToMainThread(nsIRunnable* aEvent, uint32_t aDispatchFlags)
  185. {
  186. nsCOMPtr<nsIRunnable> event(aEvent);
  187. return NS_DispatchToMainThread(event.forget(), aDispatchFlags);
  188. }
  189. nsresult
  190. NS_DelayedDispatchToCurrentThread(already_AddRefed<nsIRunnable>&& aEvent, uint32_t aDelayMs)
  191. {
  192. nsCOMPtr<nsIRunnable> event(aEvent);
  193. #ifdef MOZILLA_INTERNAL_API
  194. nsIThread* thread = NS_GetCurrentThread();
  195. if (!thread) {
  196. return NS_ERROR_UNEXPECTED;
  197. }
  198. #else
  199. nsresult rv;
  200. nsCOMPtr<nsIThread> thread;
  201. rv = NS_GetCurrentThread(getter_AddRefs(thread));
  202. if (NS_WARN_IF(NS_FAILED(rv))) {
  203. return rv;
  204. }
  205. #endif
  206. return thread->DelayedDispatch(event.forget(), aDelayMs);
  207. }
  208. nsresult
  209. NS_IdleDispatchToCurrentThread(already_AddRefed<nsIRunnable>&& aEvent)
  210. {
  211. nsresult rv;
  212. nsCOMPtr<nsIRunnable> event(aEvent);
  213. #ifdef MOZILLA_INTERNAL_API
  214. nsIThread* thread = NS_GetCurrentThread();
  215. if (!thread) {
  216. return NS_ERROR_UNEXPECTED;
  217. }
  218. #else
  219. nsCOMPtr<nsIThread> thread;
  220. rv = NS_GetCurrentThread(getter_AddRefs(thread));
  221. if (NS_WARN_IF(NS_FAILED(rv))) {
  222. return rv;
  223. }
  224. #endif
  225. // To keep us from leaking the runnable if dispatch method fails,
  226. // we grab the reference on failures and release it.
  227. nsIRunnable* temp = event.get();
  228. rv = thread->IdleDispatch(event.forget());
  229. if (NS_WARN_IF(NS_FAILED(rv))) {
  230. // Dispatch() leaked the reference to the event, but due to caller's
  231. // assumptions, we shouldn't leak here. And given we are on the same
  232. // thread as the dispatch target, it's mostly safe to do it here.
  233. NS_RELEASE(temp);
  234. }
  235. return rv;
  236. }
  237. #ifndef XPCOM_GLUE_AVOID_NSPR
  238. nsresult
  239. NS_ProcessPendingEvents(nsIThread* aThread, PRIntervalTime aTimeout)
  240. {
  241. nsresult rv = NS_OK;
  242. #ifdef MOZILLA_INTERNAL_API
  243. if (!aThread) {
  244. aThread = NS_GetCurrentThread();
  245. if (NS_WARN_IF(!aThread)) {
  246. return NS_ERROR_UNEXPECTED;
  247. }
  248. }
  249. #else
  250. nsCOMPtr<nsIThread> current;
  251. if (!aThread) {
  252. rv = NS_GetCurrentThread(getter_AddRefs(current));
  253. if (NS_WARN_IF(NS_FAILED(rv))) {
  254. return rv;
  255. }
  256. aThread = current.get();
  257. }
  258. #endif
  259. PRIntervalTime start = PR_IntervalNow();
  260. for (;;) {
  261. bool processedEvent;
  262. rv = aThread->ProcessNextEvent(false, &processedEvent);
  263. if (NS_FAILED(rv) || !processedEvent) {
  264. break;
  265. }
  266. if (PR_IntervalNow() - start > aTimeout) {
  267. break;
  268. }
  269. }
  270. return rv;
  271. }
  272. #endif // XPCOM_GLUE_AVOID_NSPR
  273. inline bool
  274. hasPendingEvents(nsIThread* aThread)
  275. {
  276. bool val;
  277. return NS_SUCCEEDED(aThread->HasPendingEvents(&val)) && val;
  278. }
  279. bool
  280. NS_HasPendingEvents(nsIThread* aThread)
  281. {
  282. if (!aThread) {
  283. #ifndef MOZILLA_INTERNAL_API
  284. nsCOMPtr<nsIThread> current;
  285. NS_GetCurrentThread(getter_AddRefs(current));
  286. return hasPendingEvents(current);
  287. #else
  288. aThread = NS_GetCurrentThread();
  289. if (NS_WARN_IF(!aThread)) {
  290. return false;
  291. }
  292. #endif
  293. }
  294. return hasPendingEvents(aThread);
  295. }
  296. bool
  297. NS_ProcessNextEvent(nsIThread* aThread, bool aMayWait)
  298. {
  299. #ifdef MOZILLA_INTERNAL_API
  300. if (!aThread) {
  301. aThread = NS_GetCurrentThread();
  302. if (NS_WARN_IF(!aThread)) {
  303. return false;
  304. }
  305. }
  306. #else
  307. nsCOMPtr<nsIThread> current;
  308. if (!aThread) {
  309. NS_GetCurrentThread(getter_AddRefs(current));
  310. if (NS_WARN_IF(!current)) {
  311. return false;
  312. }
  313. aThread = current.get();
  314. }
  315. #endif
  316. bool val;
  317. return NS_SUCCEEDED(aThread->ProcessNextEvent(aMayWait, &val)) && val;
  318. }
  319. #ifndef XPCOM_GLUE_AVOID_NSPR
  320. namespace {
  321. class nsNameThreadRunnable final : public nsIRunnable
  322. {
  323. ~nsNameThreadRunnable() {}
  324. public:
  325. explicit nsNameThreadRunnable(const nsACString& aName) : mName(aName) {}
  326. NS_DECL_THREADSAFE_ISUPPORTS
  327. NS_DECL_NSIRUNNABLE
  328. protected:
  329. const nsCString mName;
  330. };
  331. NS_IMPL_ISUPPORTS(nsNameThreadRunnable, nsIRunnable)
  332. NS_IMETHODIMP
  333. nsNameThreadRunnable::Run()
  334. {
  335. PR_SetCurrentThreadName(mName.BeginReading());
  336. return NS_OK;
  337. }
  338. } // namespace
  339. void
  340. NS_SetThreadName(nsIThread* aThread, const nsACString& aName)
  341. {
  342. if (!aThread) {
  343. return;
  344. }
  345. aThread->Dispatch(new nsNameThreadRunnable(aName),
  346. nsIEventTarget::DISPATCH_NORMAL);
  347. }
  348. #else // !XPCOM_GLUE_AVOID_NSPR
  349. void
  350. NS_SetThreadName(nsIThread* aThread, const nsACString& aName)
  351. {
  352. // No NSPR, no love.
  353. }
  354. #endif
  355. #ifdef MOZILLA_INTERNAL_API
  356. nsIThread*
  357. NS_GetCurrentThread()
  358. {
  359. return nsThreadManager::get().GetCurrentThread();
  360. }
  361. #endif
  362. // nsThreadPoolNaming
  363. void
  364. nsThreadPoolNaming::SetThreadPoolName(const nsACString& aPoolName,
  365. nsIThread* aThread)
  366. {
  367. nsCString name(aPoolName);
  368. name.AppendLiteral(" #");
  369. name.AppendInt(++mCounter, 10); // The counter is declared as volatile
  370. if (aThread) {
  371. // Set on the target thread
  372. NS_SetThreadName(aThread, name);
  373. } else {
  374. // Set on the current thread
  375. #ifndef XPCOM_GLUE_AVOID_NSPR
  376. PR_SetCurrentThreadName(name.BeginReading());
  377. #endif
  378. }
  379. }
  380. // nsAutoLowPriorityIO
  381. nsAutoLowPriorityIO::nsAutoLowPriorityIO()
  382. {
  383. #if defined(XP_WIN)
  384. lowIOPrioritySet = SetThreadPriority(GetCurrentThread(),
  385. THREAD_MODE_BACKGROUND_BEGIN);
  386. #else
  387. lowIOPrioritySet = false;
  388. #endif
  389. }
  390. nsAutoLowPriorityIO::~nsAutoLowPriorityIO()
  391. {
  392. #if defined(XP_WIN)
  393. if (MOZ_LIKELY(lowIOPrioritySet)) {
  394. // On Windows the old thread priority is automatically restored
  395. SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_END);
  396. }
  397. #endif
  398. }