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) 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/sync-classlock.hpp"
31 #include "lib/thread.hpp"
32 
33 using test::Test;
34 //using vault::ThreadJoinable; //////////////////WIP
35 
36 namespace lib {
37 namespace test {
38 
39  namespace { // Parameters for multithreaded contention test
40 
41  const uint NUM_THREADS = 20;
42  const uint NUM_LOOP = 1000;
43 
44  }
45 
46 
47 
48 
49  /**********************************************************************/
60  class SyncClasslock_test : public Test
61  {
62 
63  virtual void
64  run (Arg)
65  {
66  int contended = 0;
67 
69 
70  // Start a bunch of threads with random access pattern
71  Threads threads{NUM_THREADS,
72  [&](Threads::ElementHolder& storage)
73  {
74  storage.create<ThreadJoinable<>> ("Sync-ClassLock stress test"
75  ,[&]{
76  for (uint i=0; i<NUM_LOOP; ++i)
77  {
78  uint delay = rand() % 10;
79  usleep (delay);
80  {
81  ClassLock<void> guard;
82  ++contended;
83  }
84  }
85  });
86  }
87  };
88 
89  for (auto& thread : threads)
90  thread.join(); // block until thread terminates
91 
92  CHECK (contended == NUM_THREADS * NUM_LOOP,
93  "ALARM: Lock failed, concurrent modification "
94  "during counter increment. Delta == %d"
95  ,NUM_THREADS * NUM_LOOP - contended);
96  }
97 
98  };
99 
100 
101 
103  LAUNCHER (SyncClasslock_test, "unit common");
104 
105 
106 
107 }} // namespace lib::test
A fixed collection of non-copyable polymorphic objects.
Definition: run.hpp:49
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:62
const uint NUM_LOOP
number of loop iterations per thread
const uint NUM_THREADS
number of contending threads to create
Simple test class runner.
lib::Result< RES > join()
put the caller into a blocking wait until this thread has terminated
Definition: thread.hpp:693
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...