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"
12 static_assert(sizeof(SRWLOCK
) <= sizeof(void*), "SRWLOCK is too big!");
14 # define NativeHandle(m) (reinterpret_cast<SRWLOCK*>(&m))
16 # define NativeHandle(m) (&m)
21 RWLock::RWLock(const char* aName
)
22 : BlockingResourceBase(aName
, eMutex
)
25 mOwningThread(nullptr)
29 InitializeSRWLock(NativeHandle(mRWLock
));
31 MOZ_RELEASE_ASSERT(pthread_rwlock_init(NativeHandle(mRWLock
), nullptr) == 0,
32 "pthread_rwlock_init failed");
37 bool RWLock::LockedForWritingByCurrentThread() {
38 return mOwningThread
== PR_GetCurrentThread();
44 MOZ_RELEASE_ASSERT(pthread_rwlock_destroy(NativeHandle(mRWLock
)) == 0,
45 "pthread_rwlock_destroy failed");
49 void RWLock::ReadLockInternal() {
51 AcquireSRWLockShared(NativeHandle(mRWLock
));
53 MOZ_RELEASE_ASSERT(pthread_rwlock_rdlock(NativeHandle(mRWLock
)) == 0,
54 "pthread_rwlock_rdlock failed");
58 void RWLock::ReadUnlockInternal() {
60 ReleaseSRWLockShared(NativeHandle(mRWLock
));
62 MOZ_RELEASE_ASSERT(pthread_rwlock_unlock(NativeHandle(mRWLock
)) == 0,
63 "pthread_rwlock_unlock failed");
67 void RWLock::WriteLockInternal() {
69 AcquireSRWLockExclusive(NativeHandle(mRWLock
));
71 MOZ_RELEASE_ASSERT(pthread_rwlock_wrlock(NativeHandle(mRWLock
)) == 0,
72 "pthread_rwlock_wrlock failed");
76 void RWLock::WriteUnlockInternal() {
78 ReleaseSRWLockExclusive(NativeHandle(mRWLock
));
80 MOZ_RELEASE_ASSERT(pthread_rwlock_unlock(NativeHandle(mRWLock
)) == 0,
81 "pthread_rwlock_unlock failed");
85 } // namespace mozilla