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) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/error.hpp"
30 
31 #include "lib/thread.hpp"
32 #include "lib/sync.hpp"
33 
34 #include <atomic>
35 
36 using test::Test;
37 using std::atomic_uint;
38 using std::atomic_bool;
39 using std::this_thread::sleep_for;
40 using std::chrono_literals::operator ""ms;
41 
42 
43 namespace lib {
44 namespace test{
45 
46  namespace { // test subject...
47 
49  class SyncOnBool
50  : public Sync<NonrecursiveLock_Waitable>
51  {
52  atomic_uint sum_{0}, input_{0};
53  atomic_bool got_new_data_{false};
54 
55  public:
56  void getIt()
57  {
58  Lock await{this, [&]{ return bool(got_new_data_); }};
59  sum_ += input_;
60  }
61 
62  void provide (uint val)
63  {
64  Lock sync{this};
65  input_ = val;
66  got_new_data_ = true;
67  sync.notify_all();
68  }
69 
71  uint result () { return sum_; }
72  };
73  }//(End) test subject.
74 
75 
76 
77 
78 
79 
80 
81  /************************************************************************/
90  class SyncWaiting_test : public Test
91  {
92 
93  virtual void
94  run (Arg)
95  {
96  SyncOnBool token;
97 
98  ThreadJoinable ping ("SyncWaiting ping", [&]{ token.getIt(); });
99  ThreadJoinable pong ("SyncWaiting pong", [&]{ token.getIt(); });
100 
101  CHECK (ping);
102  CHECK (pong);
103  CHECK (0 == token.result());
104 
105  sleep_for (100ms); // if the threads don't block correctly, they've missed their chance by now...
106 
107  // kick off the notification cascade...
108  uint val = (rand() % 1000);
109  token.provide (val);
110 
111  // wait for the two Threads to finish their handshake
112  pong.join();
113  ping.join();
114 
115  CHECK (2*val == token.result());
116  }
117  };
118 
119 
120 
122  LAUNCHER (SyncWaiting_test, "unit common");
123 
124 
125 
126 }} // namespace lib::test
Facility for monitor object based locking.
Definition: sync.hpp:217
Variant of the standard case, requiring to wait and join() on the termination of this thread...
Definition: thread.hpp:676
Definition: run.hpp:49
scoped guard to control the actual locking.
Definition: sync.hpp:234
Implementation namespace for support and library code.
Object Monitor based synchronisation.
Abstract Base Class for all testcases.
Definition: run.hpp:62
Simple 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