Lumiera  0.pre.03
»edit your freedom«
sync-classlock-test.cpp
Go to the documentation of this file.
1 /*
2  SyncClasslock(Test) - validate the type-based Monitor locking
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/sync-classlock.hpp"
22 #include "lib/thread.hpp"
23 
24 using test::Test;
25 //using vault::ThreadJoinable; //////////////////WIP
26 
27 namespace lib {
28 namespace test {
29 
30  namespace { // Parameters for multithreaded contention test
31 
32  const uint NUM_THREADS = 20;
33  const uint NUM_LOOP = 1000;
34 
35  }
36 
37 
38 
39 
40  /**********************************************************************/
51  class SyncClasslock_test : public Test
52  {
53 
54  virtual void
55  run (Arg)
56  {
57  seedRand();
58  auto gen = buildCappedSubSequence(defaultGen);
59  int contended = 0;
60 
62 
63  // Start a bunch of threads with random access pattern
64  Threads threads{NUM_THREADS,
65  [&](Threads::ElementHolder& storage)
66  {
67  storage.create<ThreadJoinable<>> ("Sync-ClassLock stress test"
68  ,[&]{
69  for (uint i=0; i<NUM_LOOP; ++i)
70  {
71  uint delay = gen.i(10);
72  usleep (delay);
73  {
74  ClassLock<void> guard;
75  ++contended;
76  }
77  }
78  });
79  }
80  };
81 
82  for (auto& thread : threads)
83  thread.join(); // block until thread terminates // @suppress("Return value not evaluated")
84 
85  CHECK (contended == NUM_THREADS * NUM_LOOP,
86  "ALARM: Lock failed, concurrent modification "
87  "during counter increment. Delta == %d"
88  ,NUM_THREADS * NUM_LOOP - contended);
89  }
90 
91  };
92 
93 
94 
96  LAUNCHER (SyncClasslock_test, "unit common");
97 
98 
99 
100 }} // namespace lib::test
A fixed collection of non-copyable polymorphic objects.
Definition: run.hpp:40
Implementation namespace for support and library code.
Managing a collection of non-copyable polymorphic objects in compact storage.
Abstract Base Class for all testcases.
Definition: run.hpp:53
const uint NUM_LOOP
number of loop iterations per thread
const uint NUM_THREADS
number of contending threads to create
Simplistic test class runner.
void seedRand()
draw a new random seed from a common nucleus, and re-seed the default-Gen.
Definition: suite.cpp:211
lib::Result< RES > join()
put the caller into a blocking wait until this thread has terminated
Definition: thread.hpp:685
Convenience front-end to simplify and codify basic thread handling.
A special implementation of lib::Sync, where the storage of the object monitor is associated directly...
A synchronisation protection guard employing a lock scoped to the parameter type as a whole...
Random defaultGen
a global default RandomSequencer for mundane purposes
Definition: random.cpp:70