1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsThreadUtils.h"
#include "mozilla/Atomics.h"
#include "mozilla/gtest/MozAssertions.h"
#include "mozilla/RWLock.h"
#include "mozilla/SyncRunnable.h"
#include "nsIThread.h"
#include "gtest/gtest.h"
using mozilla::AutoReadLock;
using mozilla::AutoTryReadLock;
using mozilla::AutoTryWriteLock;
using mozilla::AutoWriteLock;
using mozilla::RWLock;
static const size_t sNumThreads = 4;
static const size_t sOuterIterations = 100;
static const size_t sInnerIterations = 100;
static const size_t sWriteLockIteration = 10;
// Based on example code from _Programming with POSIX Threads_. Not an actual
// test of correctness, but more of a "does this work at all" sort of test.
class RWLockRunnable : public mozilla::Runnable {
public:
RWLockRunnable(RWLock* aRWLock, mozilla::Atomic<size_t>* aSharedData)
: mozilla::Runnable("RWLockRunnable"),
mRWLock(aRWLock),
mSharedData(aSharedData) {}
NS_DECL_NSIRUNNABLE
private:
~RWLockRunnable() = default;
RWLock* mRWLock;
mozilla::Atomic<size_t>* mSharedData;
};
NS_IMETHODIMP
RWLockRunnable::Run() {
for (size_t i = 0; i < sOuterIterations; ++i) {
if (i % sWriteLockIteration == 0) {
mozilla::AutoWriteLock lock(*mRWLock);
++(*mSharedData);
} else {
mozilla::AutoReadLock lock(*mRWLock);
// Loop and try to force other threads to run, but check that our
// shared data isn't being modified by them.
size_t initialValue = *mSharedData;
for (size_t j = 0; j < sInnerIterations; ++j) {
EXPECT_EQ(initialValue, *mSharedData);
// This is a magic yield call.
PR_Sleep(PR_INTERVAL_NO_WAIT);
}
}
}
return NS_OK;
}
TEST(RWLock, SmokeTest)
{
nsCOMPtr<nsIThread> threads[sNumThreads];
RWLock rwlock MOZ_UNANNOTATED("test lock");
mozilla::Atomic<size_t> data(0);
for (size_t i = 0; i < sNumThreads; ++i) {
nsCOMPtr<nsIRunnable> event = new RWLockRunnable(&rwlock, &data);
NS_NewNamedThread("RWLockTester", getter_AddRefs(threads[i]), event);
}
// Wait for all the threads to finish.
for (size_t i = 0; i < sNumThreads; ++i) {
nsresult rv = threads[i]->Shutdown();
EXPECT_NS_SUCCEEDED(rv);
}
EXPECT_EQ(data, (sOuterIterations / sWriteLockIteration) * sNumThreads);
}
template <typename Function>
static std::invoke_result_t<Function> RunOnBackgroundThread(
Function&& aFunction) {
using Result = std::invoke_result_t<Function>;
nsCOMPtr<nsISerialEventTarget> thread;
MOZ_ALWAYS_SUCCEEDS(NS_CreateBackgroundTaskQueue(
"TestRWLock Background Thread", getter_AddRefs(thread)));
mozilla::Maybe<Result> tryResult;
RefPtr<nsIRunnable> runnable =
NS_NewRunnableFunction(__func__, [&] { tryResult.emplace(aFunction()); });
MOZ_ALWAYS_SUCCEEDS(
mozilla::SyncRunnable::DispatchToThread(thread.get(), runnable));
return *tryResult;
}
TEST(RWLock, AutoTryReadLock)
{
RWLock l1 MOZ_UNANNOTATED("autotryreadlock");
{
AutoTryReadLock autol1(l1);
EXPECT_TRUE(autol1);
AutoTryReadLock autol2(l1);
EXPECT_TRUE(autol2);
EXPECT_TRUE(RunOnBackgroundThread([&] {
AutoTryReadLock lock(l1);
return !!lock;
}));
EXPECT_TRUE(autol1);
EXPECT_TRUE(autol2);
{
RWLock l2 MOZ_UNANNOTATED("autotryreadlock2");
AutoTryReadLock autol3(l2);
EXPECT_TRUE(autol3);
}
EXPECT_TRUE(autol1);
EXPECT_TRUE(autol2);
}
{
AutoWriteLock autol4(l1);
MOZ_ASSERT(l1.LockedForWritingByCurrentThread());
AutoTryReadLock autol5(l1);
EXPECT_FALSE(autol5);
EXPECT_FALSE(RunOnBackgroundThread([&] {
AutoTryReadLock lock(l1);
return !!lock;
}));
}
AutoTryReadLock autol6(l1);
EXPECT_TRUE(autol6);
EXPECT_TRUE(RunOnBackgroundThread([&] {
AutoTryReadLock lock(l1);
return !!lock;
}));
}
TEST(RWLock, AutoTryWriteLock)
{
RWLock l1 MOZ_UNANNOTATED("autotrywritelock");
{
AutoTryWriteLock autol1(l1);
EXPECT_TRUE(autol1);
AutoTryReadLock autol2(l1);
EXPECT_FALSE(autol2);
EXPECT_FALSE(RunOnBackgroundThread([&] {
AutoTryWriteLock lock(l1);
return !!lock;
}));
EXPECT_TRUE(autol1);
EXPECT_FALSE(autol2);
{
RWLock l2 MOZ_UNANNOTATED("autotrywritelock2");
AutoTryWriteLock autol3(l2);
EXPECT_TRUE(autol3);
}
EXPECT_TRUE(autol1);
EXPECT_FALSE(autol2);
}
{
AutoReadLock autol4(l1);
AutoTryWriteLock autol5(l1);
EXPECT_FALSE(autol5);
EXPECT_FALSE(RunOnBackgroundThread([&] {
AutoTryWriteLock lock(l1);
return !!lock;
}));
}
{
AutoWriteLock autol6(l1);
MOZ_ASSERT(l1.LockedForWritingByCurrentThread());
AutoTryWriteLock autol7(l1);
EXPECT_FALSE(autol7);
EXPECT_FALSE(RunOnBackgroundThread([&] {
AutoTryWriteLock lock(l1);
return !!lock;
}));
}
AutoTryWriteLock autol8(l1);
EXPECT_TRUE(autol8);
}
|