Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/threads/RWLock.cpp
Line
Count
Source (jump to first uncovered line)
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
RWLock::RWLock(const char* aName)
22
  : BlockingResourceBase(aName, eMutex)
23
#ifdef DEBUG
24
  , mOwningThread(nullptr)
25
#endif
26
3
{
27
#ifdef XP_WIN
28
  InitializeSRWLock(NativeHandle(mRWLock));
29
#else
30
3
  MOZ_RELEASE_ASSERT(pthread_rwlock_init(NativeHandle(mRWLock), nullptr) == 0,
31
3
                     "pthread_rwlock_init failed");
32
3
#endif
33
3
}
34
35
#ifdef DEBUG
36
bool
37
RWLock::LockedForWritingByCurrentThread()
38
{
39
  return mOwningThread == PR_GetCurrentThread();
40
}
41
#endif
42
43
#ifndef XP_WIN
44
RWLock::~RWLock()
45
0
{
46
0
  MOZ_RELEASE_ASSERT(pthread_rwlock_destroy(NativeHandle(mRWLock)) == 0,
47
0
                     "pthread_rwlock_destroy failed");
48
0
}
49
#endif
50
51
void
52
RWLock::ReadLockInternal()
53
0
{
54
#ifdef XP_WIN
55
  AcquireSRWLockShared(NativeHandle(mRWLock));
56
#else
57
0
  MOZ_RELEASE_ASSERT(pthread_rwlock_rdlock(NativeHandle(mRWLock)) == 0,
58
0
                     "pthread_rwlock_rdlock failed");
59
0
#endif
60
0
}
61
62
void
63
RWLock::ReadUnlockInternal()
64
0
{
65
#ifdef XP_WIN
66
  ReleaseSRWLockShared(NativeHandle(mRWLock));
67
#else
68
0
  MOZ_RELEASE_ASSERT(pthread_rwlock_unlock(NativeHandle(mRWLock)) == 0,
69
0
                     "pthread_rwlock_unlock failed");
70
0
#endif
71
0
}
72
73
void
74
RWLock::WriteLockInternal()
75
0
{
76
#ifdef XP_WIN
77
  AcquireSRWLockExclusive(NativeHandle(mRWLock));
78
#else
79
0
  MOZ_RELEASE_ASSERT(pthread_rwlock_wrlock(NativeHandle(mRWLock)) == 0,
80
0
                     "pthread_rwlock_wrlock failed");
81
0
#endif
82
0
}
83
84
void
85
RWLock::WriteUnlockInternal()
86
0
{
87
#ifdef XP_WIN
88
  ReleaseSRWLockExclusive(NativeHandle(mRWLock));
89
#else
90
0
  MOZ_RELEASE_ASSERT(pthread_rwlock_unlock(NativeHandle(mRWLock)) == 0,
91
0
                     "pthread_rwlock_unlock failed");
92
0
#endif
93
0
}
94
95
}
96
97
#undef NativeHandle