Bug 1794012 - Add a static main thread capability for the thread safety analysis, r=xpcom-reviewers,kmag

This acts like the `EventTargetCapability` type, but is global and specific to
the main thread.

Differential Revision: https://phabricator.services.mozilla.com/D158872
This commit is contained in:
Nika Layzell 2022-10-11 23:06:08 +00:00
Родитель f2e24180f6
Коммит 66987911fd
2 изменённых файлов: 20 добавлений и 2 удалений

Просмотреть файл

@ -48,6 +48,10 @@ namespace mozilla {
// uint32_t mMediaCount MOZ_GUARDED_BY(mTargetCapability) = 0;
// EventTargetCapability<nsIEventTarget> mTargetCapability;
// };
//
// NOTE: If you need a thread-safety capability for specifically the main
// thread, the static `mozilla::sMainThreadCapability` capability exists, and
// can be asserted using `AssertIsOnMainThread()`.
template <typename T>
class MOZ_CAPABILITY EventTargetCapability final {

Просмотреть файл

@ -7,6 +7,7 @@
#ifndef MainThreadUtils_h_
#define MainThreadUtils_h_
#include "mozilla/ThreadSafety.h"
#include "nscore.h"
class nsIThread;
@ -27,10 +28,23 @@ bool NS_IsMainThread();
namespace mozilla {
/**
* A dummy static capability for the thread safety analysis which can be
* required by functions and members using `MOZ_REQUIRE(sMainThreadCapability)`
* and `MOZ_GUARDED_BY(sMainThreadCapability)` and asserted using
* `AssertIsOnMainThread()`.
*
* If you want a thread-safety-analysis capability for a non-main thread,
* consider using the `EventTargetCapability` type.
*/
class MOZ_CAPABILITY MainThreadCapability final {};
constexpr MainThreadCapability sMainThreadCapability;
# ifdef DEBUG
void AssertIsOnMainThread();
void AssertIsOnMainThread() MOZ_ASSERT_CAPABILITY(sMainThreadCapability);
# else
inline void AssertIsOnMainThread() {}
inline void AssertIsOnMainThread()
MOZ_ASSERT_CAPABILITY(sMainThreadCapability) {}
# endif
} // namespace mozilla