Lines Matching refs:cxt
88 TestContext *cxt = static_cast<TestContext *>(v); in CheckSumG0G1() local
89 ABSL_RAW_CHECK(cxt->g0 == -cxt->g1, "Error in CheckSumG0G1"); in CheckSumG0G1()
93 static void TestMu(TestContext *cxt, int c) { in TestMu() argument
94 for (int i = 0; i != cxt->iterations; i++) { in TestMu()
95 absl::MutexLock l(&cxt->mu); in TestMu()
96 int a = cxt->g0 + 1; in TestMu()
97 cxt->g0 = a; in TestMu()
98 cxt->g1--; in TestMu()
102 static void TestTry(TestContext *cxt, int c) { in TestTry() argument
103 for (int i = 0; i != cxt->iterations; i++) { in TestTry()
106 } while (!cxt->mu.TryLock()); in TestTry()
107 int a = cxt->g0 + 1; in TestTry()
108 cxt->g0 = a; in TestTry()
109 cxt->g1--; in TestTry()
110 cxt->mu.Unlock(); in TestTry()
114 static void TestR20ms(TestContext *cxt, int c) { in TestR20ms() argument
115 for (int i = 0; i != cxt->iterations; i++) { in TestR20ms()
116 absl::ReaderMutexLock l(&cxt->mu); in TestR20ms()
118 cxt->mu.AssertReaderHeld(); in TestR20ms()
122 static void TestRW(TestContext *cxt, int c) { in TestRW() argument
124 for (int i = 0; i != cxt->iterations; i++) { in TestRW()
125 absl::WriterMutexLock l(&cxt->mu); in TestRW()
126 cxt->g0++; in TestRW()
127 cxt->g1--; in TestRW()
128 cxt->mu.AssertHeld(); in TestRW()
129 cxt->mu.AssertReaderHeld(); in TestRW()
132 for (int i = 0; i != cxt->iterations; i++) { in TestRW()
133 absl::ReaderMutexLock l(&cxt->mu); in TestRW()
134 ABSL_RAW_CHECK(cxt->g0 == -cxt->g1, "Error in TestRW"); in TestRW()
135 cxt->mu.AssertReaderHeld(); in TestRW()
142 TestContext *cxt; member
147 TestContext *cxt = this->cxt; in MyTurn() local
148 return cxt->g0 == this->target || cxt->g0 == cxt->iterations; in MyTurn()
151 static void TestAwait(TestContext *cxt, int c) { in TestAwait() argument
154 mc.cxt = cxt; in TestAwait()
155 absl::MutexLock l(&cxt->mu); in TestAwait()
156 cxt->mu.AssertHeld(); in TestAwait()
157 while (cxt->g0 < cxt->iterations) { in TestAwait()
158 cxt->mu.Await(absl::Condition(&mc, &MyContext::MyTurn)); in TestAwait()
160 cxt->mu.AssertHeld(); in TestAwait()
161 if (cxt->g0 < cxt->iterations) { in TestAwait()
162 int a = cxt->g0 + 1; in TestAwait()
163 cxt->g0 = a; in TestAwait()
164 mc.target += cxt->threads; in TestAwait()
169 static void TestSignalAll(TestContext *cxt, int c) { in TestSignalAll() argument
171 absl::MutexLock l(&cxt->mu); in TestSignalAll()
172 cxt->mu.AssertHeld(); in TestSignalAll()
173 while (cxt->g0 < cxt->iterations) { in TestSignalAll()
174 while (cxt->g0 != target && cxt->g0 != cxt->iterations) { in TestSignalAll()
175 cxt->cv.Wait(&cxt->mu); in TestSignalAll()
177 if (cxt->g0 < cxt->iterations) { in TestSignalAll()
178 int a = cxt->g0 + 1; in TestSignalAll()
179 cxt->g0 = a; in TestSignalAll()
180 cxt->cv.SignalAll(); in TestSignalAll()
181 target += cxt->threads; in TestSignalAll()
186 static void TestSignal(TestContext *cxt, int c) { in TestSignal() argument
187 ABSL_RAW_CHECK(cxt->threads == 2, "TestSignal should use 2 threads"); in TestSignal()
189 absl::MutexLock l(&cxt->mu); in TestSignal()
190 cxt->mu.AssertHeld(); in TestSignal()
191 while (cxt->g0 < cxt->iterations) { in TestSignal()
192 while (cxt->g0 != target && cxt->g0 != cxt->iterations) { in TestSignal()
193 cxt->cv.Wait(&cxt->mu); in TestSignal()
195 if (cxt->g0 < cxt->iterations) { in TestSignal()
196 int a = cxt->g0 + 1; in TestSignal()
197 cxt->g0 = a; in TestSignal()
198 cxt->cv.Signal(); in TestSignal()
199 target += cxt->threads; in TestSignal()
204 static void TestCVTimeout(TestContext *cxt, int c) { in TestCVTimeout() argument
206 absl::MutexLock l(&cxt->mu); in TestCVTimeout()
207 cxt->mu.AssertHeld(); in TestCVTimeout()
208 while (cxt->g0 < cxt->iterations) { in TestCVTimeout()
209 while (cxt->g0 != target && cxt->g0 != cxt->iterations) { in TestCVTimeout()
210 cxt->cv.WaitWithTimeout(&cxt->mu, absl::Seconds(100)); in TestCVTimeout()
212 if (cxt->g0 < cxt->iterations) { in TestCVTimeout()
213 int a = cxt->g0 + 1; in TestCVTimeout()
214 cxt->g0 = a; in TestCVTimeout()
215 cxt->cv.SignalAll(); in TestCVTimeout()
216 target += cxt->threads; in TestCVTimeout()
221 static bool G0GE2(TestContext *cxt) { return cxt->g0 >= 2; } in G0GE2() argument
223 static void TestTime(TestContext *cxt, int c, bool use_cv) { in TestTime() argument
224 ABSL_RAW_CHECK(cxt->iterations == 1, "TestTime should only use 1 iteration"); in TestTime()
225 ABSL_RAW_CHECK(cxt->threads > 2, "TestTime should use more than 2 threads"); in TestTime()
228 absl::Condition g0ge2(G0GE2, cxt); in TestTime()
230 absl::MutexLock l(&cxt->mu); in TestTime()
234 cxt->cv.WaitWithTimeout(&cxt->mu, absl::Seconds(1)); in TestTime()
236 ABSL_RAW_CHECK(!cxt->mu.AwaitWithTimeout(false_cond, absl::Seconds(1)), in TestTime()
243 ABSL_RAW_CHECK(cxt->g0 == 1, "TestTime failed"); in TestTime()
247 cxt->cv.WaitWithTimeout(&cxt->mu, absl::Seconds(1)); in TestTime()
249 ABSL_RAW_CHECK(!cxt->mu.AwaitWithTimeout(false_cond, absl::Seconds(1)), in TestTime()
256 cxt->g0++; in TestTime()
258 cxt->cv.Signal(); in TestTime()
263 cxt->cv.WaitWithTimeout(&cxt->mu, absl::Seconds(4)); in TestTime()
265 ABSL_RAW_CHECK(!cxt->mu.AwaitWithTimeout(false_cond, absl::Seconds(4)), in TestTime()
272 ABSL_RAW_CHECK(cxt->g0 >= 3, "TestTime failed"); in TestTime()
276 cxt->cv.WaitWithTimeout(&cxt->mu, absl::Seconds(1)); in TestTime()
278 ABSL_RAW_CHECK(!cxt->mu.AwaitWithTimeout(false_cond, absl::Seconds(1)), in TestTime()
286 cxt->cv.SignalAll(); in TestTime()
291 cxt->cv.WaitWithTimeout(&cxt->mu, absl::Seconds(1)); in TestTime()
293 ABSL_RAW_CHECK(!cxt->mu.AwaitWithTimeout(false_cond, absl::Seconds(1)), in TestTime()
299 ABSL_RAW_CHECK(cxt->g0 == cxt->threads, "TestTime failed"); in TestTime()
302 absl::MutexLock l(&cxt->mu); in TestTime()
305 cxt->cv.WaitWithTimeout(&cxt->mu, absl::Milliseconds(500)); in TestTime()
308 !cxt->mu.AwaitWithTimeout(false_cond, absl::Milliseconds(500)), in TestTime()
315 cxt->g0++; in TestTime()
317 absl::MutexLock l(&cxt->mu); in TestTime()
319 while (cxt->g0 < 2) { in TestTime()
320 cxt->cv.WaitWithTimeout(&cxt->mu, absl::Seconds(100)); in TestTime()
323 ABSL_RAW_CHECK(cxt->mu.AwaitWithTimeout(g0ge2, absl::Seconds(100)), in TestTime()
326 cxt->g0++; in TestTime()
328 absl::MutexLock l(&cxt->mu); in TestTime()
330 while (cxt->g0 < 2) { in TestTime()
331 cxt->cv.Wait(&cxt->mu); in TestTime()
334 cxt->mu.Await(g0ge2); in TestTime()
336 cxt->g0++; in TestTime()
340 static void TestMuTime(TestContext *cxt, int c) { TestTime(cxt, c, false); } in TestMuTime() argument
342 static void TestCVTime(TestContext *cxt, int c) { TestTime(cxt, c, true); } in TestCVTime() argument
356 static int RunTestCommon(TestContext *cxt, void (*test)(TestContext *cxt, int), in RunTestCommon() argument
362 cxt->g0 = 0; in RunTestCommon()
363 cxt->g1 = 0; in RunTestCommon()
364 cxt->iterations = iterations; in RunTestCommon()
365 cxt->threads = threads; in RunTestCommon()
370 std::bind(test, cxt, std::placeholders::_1)))); in RunTestCommon()
377 return cxt->g0; in RunTestCommon()
381 static int RunTest(void (*test)(TestContext *cxt, int), int threads, in RunTest() argument
383 TestContext cxt; in RunTest() local
384 return RunTestCommon(&cxt, test, threads, iterations, operations); in RunTest()
392 static int RunTestWithInvariantDebugging(void (*test)(TestContext *cxt, int), in RunTestWithInvariantDebugging() argument
398 TestContext cxt; in RunTestWithInvariantDebugging() local
399 cxt.mu.EnableInvariantDebugging(invariant, &cxt); in RunTestWithInvariantDebugging()
400 int ret = RunTestCommon(&cxt, test, threads, iterations, operations); in RunTestWithInvariantDebugging()