Bug 1910362 - Create new Nimbus helper r=aaronmt,ohorvath
[gecko.git] / xpcom / threads / MainThreadUtils.h
blob152805eb66cef8f243728376ff2786adca25fdc6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef MainThreadUtils_h_
8 #define MainThreadUtils_h_
10 #include "mozilla/Assertions.h"
11 #include "mozilla/ThreadSafety.h"
12 #include "nscore.h"
14 class nsIThread;
16 /**
17 * Get a reference to the main thread.
19 * @param aResult
20 * The resulting nsIThread object.
22 extern nsresult NS_GetMainThread(nsIThread** aResult);
24 #ifdef MOZILLA_INTERNAL_API
25 bool NS_IsMainThreadTLSInitialized();
26 extern "C" {
27 bool NS_IsMainThread();
30 namespace mozilla {
32 /**
33 * A dummy static capability for the thread safety analysis which can be
34 * required by functions and members using `MOZ_REQUIRE(sMainThreadCapability)`
35 * and `MOZ_GUARDED_BY(sMainThreadCapability)` and asserted using
36 * `AssertIsOnMainThread()`.
38 * If you want a thread-safety-analysis capability for a non-main thread,
39 * consider using the `EventTargetCapability` type.
41 class MOZ_CAPABILITY("main thread") MainThreadCapability final {};
42 constexpr MainThreadCapability sMainThreadCapability;
44 # ifdef DEBUG
45 void AssertIsOnMainThread() MOZ_ASSERT_CAPABILITY(sMainThreadCapability);
46 # else
47 inline void AssertIsOnMainThread()
48 MOZ_ASSERT_CAPABILITY(sMainThreadCapability) {}
49 # endif
51 inline void ReleaseAssertIsOnMainThread()
52 MOZ_ASSERT_CAPABILITY(sMainThreadCapability) {
53 MOZ_RELEASE_ASSERT(NS_IsMainThread());
56 } // namespace mozilla
58 #endif
60 #endif // MainThreadUtils_h_