Lumiera  0.pre.03
»edit your freedom«
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 
27 using test::Test;
28 using std::atomic_uint;
29 using std::atomic_bool;
30 using std::this_thread::sleep_for;
31 using std::chrono_literals::operator ""ms;
32 
33 
34 namespace lib {
35 namespace test{
36 
37  namespace { // test subject...
38 
40  class SyncOnBool
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
Facility for monitor object based locking.
Definition: sync.hpp:209
Variant of the standard case, requiring to wait and join() on the termination of this thread...
Definition: thread.hpp:668
Definition: run.hpp:40
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
scoped guard to control the actual locking.
Definition: sync.hpp:226
Implementation namespace for support and library code.
Object Monitor based synchronisation.
Abstract Base Class for all testcases.
Definition: run.hpp:53
Simplistic test class runner.
Convenience front-end to simplify and codify basic thread handling.
Lumiera error handling (C++ interface).
demonstrates how to wait on a simple boolean flag