Bug 1688832: part 5) Add `static` `AccessibleCaretManager::GetSelection`, `::GetFrame...
[gecko.git] / xpcom / threads / RWLock.cpp
blobffdd78ceb39020ea15775eeb360a4b8b6ce5a1c4
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 #include "mozilla/RWLock.h"
9 #ifdef XP_WIN
10 # include <windows.h>
12 static_assert(sizeof(SRWLOCK) <= sizeof(void*), "SRWLOCK is too big!");
14 # define NativeHandle(m) (reinterpret_cast<SRWLOCK*>(&m))
15 #else
16 # define NativeHandle(m) (&m)
17 #endif
19 namespace mozilla {
21 RWLock::RWLock(const char* aName)
22 : BlockingResourceBase(aName, eMutex)
23 #ifdef DEBUG
25 mOwningThread(nullptr)
26 #endif
28 #ifdef XP_WIN
29 InitializeSRWLock(NativeHandle(mRWLock));
30 #else
31 MOZ_RELEASE_ASSERT(pthread_rwlock_init(NativeHandle(mRWLock), nullptr) == 0,
32 "pthread_rwlock_init failed");
33 #endif
36 #ifdef DEBUG
37 bool RWLock::LockedForWritingByCurrentThread() {
38 return mOwningThread == PR_GetCurrentThread();
40 #endif
42 #ifndef XP_WIN
43 RWLock::~RWLock() {
44 MOZ_RELEASE_ASSERT(pthread_rwlock_destroy(NativeHandle(mRWLock)) == 0,
45 "pthread_rwlock_destroy failed");
47 #endif
49 void RWLock::ReadLockInternal() {
50 #ifdef XP_WIN
51 AcquireSRWLockShared(NativeHandle(mRWLock));
52 #else
53 MOZ_RELEASE_ASSERT(pthread_rwlock_rdlock(NativeHandle(mRWLock)) == 0,
54 "pthread_rwlock_rdlock failed");
55 #endif
58 void RWLock::ReadUnlockInternal() {
59 #ifdef XP_WIN
60 ReleaseSRWLockShared(NativeHandle(mRWLock));
61 #else
62 MOZ_RELEASE_ASSERT(pthread_rwlock_unlock(NativeHandle(mRWLock)) == 0,
63 "pthread_rwlock_unlock failed");
64 #endif
67 void RWLock::WriteLockInternal() {
68 #ifdef XP_WIN
69 AcquireSRWLockExclusive(NativeHandle(mRWLock));
70 #else
71 MOZ_RELEASE_ASSERT(pthread_rwlock_wrlock(NativeHandle(mRWLock)) == 0,
72 "pthread_rwlock_wrlock failed");
73 #endif
76 void RWLock::WriteUnlockInternal() {
77 #ifdef XP_WIN
78 ReleaseSRWLockExclusive(NativeHandle(mRWLock));
79 #else
80 MOZ_RELEASE_ASSERT(pthread_rwlock_unlock(NativeHandle(mRWLock)) == 0,
81 "pthread_rwlock_unlock failed");
82 #endif
85 } // namespace mozilla
87 #undef NativeHandle