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:
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "jsapi-tests/tests.h"
9 #include "threading/ConditionVariable.h"
10 #include "threading/Thread.h"
11 #include "vm/MutexIDs.h"
14 js::Mutex mutex MOZ_UNANNOTATED
;
15 js::ConditionVariable condition
;
17 js::Thread testThread
;
19 explicit TestState(bool createThread
= true)
20 : mutex(js::mutexid::TestMutex
), flag(false) {
22 MOZ_RELEASE_ASSERT(testThread
.init(setFlag
, this));
26 static void setFlag(TestState
* state
) {
27 js::UniqueLock
<js::Mutex
> lock(state
->mutex
);
29 state
->condition
.notify_one();
32 void join() { testThread
.join(); }
35 BEGIN_TEST(testThreadingConditionVariable
) {
36 auto state
= mozilla::MakeUnique
<TestState
>();
38 js::UniqueLock
<js::Mutex
> lock(state
->mutex
);
39 while (!state
->flag
) {
40 state
->condition
.wait(lock
);
49 END_TEST(testThreadingConditionVariable
)
51 BEGIN_TEST(testThreadingConditionVariablePredicate
) {
52 auto state
= mozilla::MakeUnique
<TestState
>();
54 js::UniqueLock
<js::Mutex
> lock(state
->mutex
);
55 state
->condition
.wait(lock
, [&state
]() { return state
->flag
; });
63 END_TEST(testThreadingConditionVariablePredicate
)
65 BEGIN_TEST(testThreadingConditionVariableUntilOkay
) {
66 auto state
= mozilla::MakeUnique
<TestState
>();
68 js::UniqueLock
<js::Mutex
> lock(state
->mutex
);
69 while (!state
->flag
) {
71 mozilla::TimeStamp::Now() + mozilla::TimeDuration::FromSeconds(600);
72 js::CVStatus res
= state
->condition
.wait_until(lock
, to
);
73 CHECK(res
== js::CVStatus::NoTimeout
);
82 END_TEST(testThreadingConditionVariableUntilOkay
)
84 BEGIN_TEST(testThreadingConditionVariableUntilTimeout
) {
85 auto state
= mozilla::MakeUnique
<TestState
>(false);
87 js::UniqueLock
<js::Mutex
> lock(state
->mutex
);
88 while (!state
->flag
) {
89 auto to
= mozilla::TimeStamp::Now() +
90 mozilla::TimeDuration::FromMilliseconds(10);
91 js::CVStatus res
= state
->condition
.wait_until(lock
, to
);
92 if (res
== js::CVStatus::Timeout
) {
99 // Timeout in the past should return with timeout immediately.
101 js::UniqueLock
<js::Mutex
> lock(state
->mutex
);
103 mozilla::TimeStamp::Now() - mozilla::TimeDuration::FromMilliseconds(10);
104 js::CVStatus res
= state
->condition
.wait_until(lock
, to
);
105 CHECK(res
== js::CVStatus::Timeout
);
110 END_TEST(testThreadingConditionVariableUntilTimeout
)
112 BEGIN_TEST(testThreadingConditionVariableUntilOkayPredicate
) {
113 auto state
= mozilla::MakeUnique
<TestState
>();
115 js::UniqueLock
<js::Mutex
> lock(state
->mutex
);
117 mozilla::TimeStamp::Now() + mozilla::TimeDuration::FromSeconds(600);
118 bool res
= state
->condition
.wait_until(lock
, to
,
119 [&state
]() { return state
->flag
; });
128 END_TEST(testThreadingConditionVariableUntilOkayPredicate
)
130 BEGIN_TEST(testThreadingConditionVariableUntilTimeoutPredicate
) {
131 auto state
= mozilla::MakeUnique
<TestState
>(false);
133 js::UniqueLock
<js::Mutex
> lock(state
->mutex
);
135 mozilla::TimeStamp::Now() + mozilla::TimeDuration::FromMilliseconds(10);
136 bool res
= state
->condition
.wait_until(lock
, to
,
137 [&state
]() { return state
->flag
; });
144 END_TEST(testThreadingConditionVariableUntilTimeoutPredicate
)
146 BEGIN_TEST(testThreadingConditionVariableForOkay
) {
147 auto state
= mozilla::MakeUnique
<TestState
>();
149 js::UniqueLock
<js::Mutex
> lock(state
->mutex
);
150 while (!state
->flag
) {
151 auto duration
= mozilla::TimeDuration::FromSeconds(600);
152 js::CVStatus res
= state
->condition
.wait_for(lock
, duration
);
153 CHECK(res
== js::CVStatus::NoTimeout
);
162 END_TEST(testThreadingConditionVariableForOkay
)
164 BEGIN_TEST(testThreadingConditionVariableForTimeout
) {
165 auto state
= mozilla::MakeUnique
<TestState
>(false);
167 js::UniqueLock
<js::Mutex
> lock(state
->mutex
);
168 while (!state
->flag
) {
169 auto duration
= mozilla::TimeDuration::FromMilliseconds(10);
170 js::CVStatus res
= state
->condition
.wait_for(lock
, duration
);
171 if (res
== js::CVStatus::Timeout
) {
178 // Timeout in the past should return with timeout immediately.
180 js::UniqueLock
<js::Mutex
> lock(state
->mutex
);
181 auto duration
= mozilla::TimeDuration::FromMilliseconds(-10);
182 js::CVStatus res
= state
->condition
.wait_for(lock
, duration
);
183 CHECK(res
== js::CVStatus::Timeout
);
188 END_TEST(testThreadingConditionVariableForTimeout
)
190 BEGIN_TEST(testThreadingConditionVariableForOkayPredicate
) {
191 auto state
= mozilla::MakeUnique
<TestState
>();
193 js::UniqueLock
<js::Mutex
> lock(state
->mutex
);
194 auto duration
= mozilla::TimeDuration::FromSeconds(600);
195 bool res
= state
->condition
.wait_for(lock
, duration
,
196 [&state
]() { return state
->flag
; });
205 END_TEST(testThreadingConditionVariableForOkayPredicate
)
207 BEGIN_TEST(testThreadingConditionVariableForTimeoutPredicate
) {
208 auto state
= mozilla::MakeUnique
<TestState
>(false);
210 js::UniqueLock
<js::Mutex
> lock(state
->mutex
);
211 auto duration
= mozilla::TimeDuration::FromMilliseconds(10);
212 bool res
= state
->condition
.wait_for(lock
, duration
,
213 [&state
]() { return state
->flag
; });
220 END_TEST(testThreadingConditionVariableForTimeoutPredicate
)