Line data Source code
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/. */
6 :
7 : #include "mozilla/RWLock.h"
8 :
9 : #ifdef XP_WIN
10 : #include <windows.h>
11 :
12 : static_assert(sizeof(SRWLOCK) <= sizeof(void*), "SRWLOCK is too big!");
13 :
14 : #define NativeHandle(m) (reinterpret_cast<SRWLOCK*>(&m))
15 : #else
16 : #define NativeHandle(m) (&m)
17 : #endif
18 :
19 : namespace mozilla {
20 :
21 0 : RWLock::RWLock(const char* aName)
22 : : BlockingResourceBase(aName, eMutex)
23 : #ifdef DEBUG
24 0 : , mOwningThread(nullptr)
25 : #endif
26 : {
27 : #ifdef XP_WIN
28 : InitializeSRWLock(NativeHandle(mRWLock));
29 : #else
30 0 : MOZ_RELEASE_ASSERT(pthread_rwlock_init(NativeHandle(mRWLock), nullptr) == 0,
31 : "pthread_rwlock_init failed");
32 : #endif
33 0 : }
34 :
35 : #ifdef DEBUG
36 : bool
37 0 : RWLock::LockedForWritingByCurrentThread()
38 : {
39 0 : return mOwningThread == PR_GetCurrentThread();
40 : }
41 : #endif
42 :
43 : #ifndef XP_WIN
44 0 : RWLock::~RWLock()
45 : {
46 0 : MOZ_RELEASE_ASSERT(pthread_rwlock_destroy(NativeHandle(mRWLock)) == 0,
47 : "pthread_rwlock_destroy failed");
48 0 : }
49 : #endif
50 :
51 : void
52 0 : RWLock::ReadLockInternal()
53 : {
54 : #ifdef XP_WIN
55 : AcquireSRWLockShared(NativeHandle(mRWLock));
56 : #else
57 0 : MOZ_RELEASE_ASSERT(pthread_rwlock_rdlock(NativeHandle(mRWLock)) == 0,
58 : "pthread_rwlock_rdlock failed");
59 : #endif
60 0 : }
61 :
62 : void
63 0 : RWLock::ReadUnlockInternal()
64 : {
65 : #ifdef XP_WIN
66 : ReleaseSRWLockShared(NativeHandle(mRWLock));
67 : #else
68 0 : MOZ_RELEASE_ASSERT(pthread_rwlock_unlock(NativeHandle(mRWLock)) == 0,
69 : "pthread_rwlock_unlock failed");
70 : #endif
71 0 : }
72 :
73 : void
74 0 : RWLock::WriteLockInternal()
75 : {
76 : #ifdef XP_WIN
77 : AcquireSRWLockExclusive(NativeHandle(mRWLock));
78 : #else
79 0 : MOZ_RELEASE_ASSERT(pthread_rwlock_wrlock(NativeHandle(mRWLock)) == 0,
80 : "pthread_rwlock_wrlock failed");
81 : #endif
82 0 : }
83 :
84 : void
85 0 : RWLock::WriteUnlockInternal()
86 : {
87 : #ifdef XP_WIN
88 : ReleaseSRWLockExclusive(NativeHandle(mRWLock));
89 : #else
90 0 : MOZ_RELEASE_ASSERT(pthread_rwlock_unlock(NativeHandle(mRWLock)) == 0,
91 : "pthread_rwlock_unlock failed");
92 : #endif
93 0 : }
94 :
95 : }
96 :
97 : #undef NativeHandle
|