Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
sync-waiting-test.cpp
Go to the documentation of this file.
1/*
2 SyncWaiting(Test) - check the monitor object based wait/notification
3
4 Copyright (C)
5 2008, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
19#include "lib/test/run.hpp"
20#include "lib/error.hpp"
21
22#include "lib/thread.hpp"
23#include "lib/sync.hpp"
24
25#include <atomic>
26
27using test::Test;
28using std::atomic_uint;
29using std::atomic_bool;
30using std::this_thread::sleep_for;
31using std::chrono_literals::operator ""ms;
32
33
34namespace lib {
35namespace test{
36
37 namespace { // test subject...
38
41 : public Sync<NonrecursiveLock_Waitable>
42 {
43 atomic_uint sum_{0}, input_{0};
44 atomic_bool got_new_data_{false};
45
46 public:
47 void getIt()
48 {
49 Lock await{this, [&]{ return bool(got_new_data_); }};
50 sum_ += input_;
51 }
52
53 void provide (uint val)
54 {
55 Lock sync{this};
56 input_ = val;
57 got_new_data_ = true;
58 sync.notify_all();
59 }
60
62 uint result () { return sum_; }
63 };
64 }//(End) test subject.
65
66
67
68
69
70
71
72 /************************************************************************/
81 class SyncWaiting_test : public Test
82 {
83
84 virtual void
85 run (Arg)
86 {
87 seedRand();
88 SyncOnBool token;
89
90 ThreadJoinable ping ("SyncWaiting ping", [&]{ token.getIt(); });
91 ThreadJoinable pong ("SyncWaiting pong", [&]{ token.getIt(); });
92
93 CHECK (ping);
94 CHECK (pong);
95 CHECK (0 == token.result());
96
97 sleep_for (100ms); // if the threads don't block correctly, they've missed their chance by now...
98
99 // kick off the notification cascade...
100 uint val = rani(1000);
101 token.provide (val);
102
103 // wait for the two Threads to finish their handshake
104 pong.join();
105 ping.join();
106
107 CHECK (2*val == token.result());
108 }
109 };
110
111
112
114 LAUNCHER (SyncWaiting_test, "unit common");
115
116
117
118}} // namespace lib::test
scoped guard to control the actual locking.
Definition sync.hpp:228
void notify_all()
Definition sync.hpp:237
Facility for monitor object based locking.
Definition sync.hpp:210
Variant of the standard case, requiring to wait and join() on the termination of this thread.
Definition thread.hpp:670
demonstrates how to wait on a simple boolean flag
Abstract Base Class for all testcases.
Definition run.hpp:54
void seedRand()
draw a new random seed from a common nucleus, and re-seed the default-Gen.
Definition suite.cpp:211
Lumiera error handling (C++ interface).
unsigned int uint
Definition integral.hpp:29
Implementation namespace for support and library code.
int rani(uint bound=_iBOUND())
Definition random.hpp:135
Test runner and basic definitions for tests.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Object Monitor based synchronisation.
Convenience front-end to simplify and codify basic thread handling.