132
|
1 /*******************************************************************************
|
|
2 copyright: Copyright (c) 2006 Juan Jose Comellas. All rights reserved
|
|
3 license: BSD style: $(LICENSE)
|
|
4 author: Juan Jose Comellas <juanjo@comellas.com.ar>
|
|
5 Converted to use core.sync by Sean Kelly <sean@f4.ca>
|
|
6 *******************************************************************************/
|
|
7
|
|
8 private import tango.core.sync.Barrier;
|
|
9 private import tango.core.sync.Mutex;
|
|
10 private import tango.core.Exception;
|
|
11 private import tango.core.Thread;
|
|
12 private import tango.io.Stdout;
|
|
13 private import tango.text.convert.Integer;
|
|
14 debug (barrier)
|
|
15 {
|
|
16 private import tango.util.log.Log;
|
|
17 private import tango.util.log.ConsoleAppender;
|
|
18 private import tango.util.log.DateLayout;
|
|
19 }
|
|
20
|
|
21
|
|
22 /**
|
|
23 * Example program for the tango.core.sync.Barrier module.
|
|
24 */
|
|
25 void main(char[][] args)
|
|
26 {
|
|
27 const uint MaxThreadCount = 100;
|
|
28 const uint LoopsPerThread = 10000;
|
|
29
|
|
30 debug (barrier)
|
|
31 {
|
|
32 Logger log = Log.getLogger("barrier");
|
|
33
|
|
34 log.addAppender(new ConsoleAppender(new DateLayout()));
|
|
35
|
|
36 log.info("Barrier test");
|
|
37 }
|
|
38
|
|
39 Barrier barrier = new Barrier(MaxThreadCount);
|
|
40 Mutex mutex = new Mutex();
|
|
41 uint count = 0;
|
|
42 uint correctCount = 0;
|
|
43
|
|
44 void barrierTestThread()
|
|
45 {
|
|
46 debug (barrier)
|
|
47 {
|
|
48 Logger log = Log.getLogger("barrier." ~ Thread.getThis().name());
|
|
49
|
|
50 log.trace("Starting thread");
|
|
51 }
|
|
52
|
|
53 try
|
|
54 {
|
|
55 for (uint i; i < LoopsPerThread; ++i)
|
|
56 {
|
|
57 // 'count' is a resource shared by multiple threads, so we must
|
|
58 // acquire the mutex before modifying it.
|
|
59 synchronized (mutex)
|
|
60 {
|
|
61 // debug (barrier)
|
|
62 // log.trace("Acquired mutex");
|
|
63 count++;
|
|
64 // debug (barrier)
|
|
65 // log.trace("Releasing mutex");
|
|
66 }
|
|
67 }
|
|
68
|
|
69 // We wait for all the threads to finish counting.
|
|
70 debug (barrier)
|
|
71 log.trace("Waiting on barrier");
|
|
72 barrier.wait();
|
|
73 debug (barrier)
|
|
74 log.trace("Barrier was opened");
|
|
75
|
|
76 // We make sure that all the threads exited the barrier after
|
|
77 // *all* of them had finished counting.
|
|
78 synchronized (mutex)
|
|
79 {
|
|
80 // debug (barrier)
|
|
81 // log.trace("Acquired mutex");
|
|
82 if (count == MaxThreadCount * LoopsPerThread)
|
|
83 {
|
|
84 ++correctCount;
|
|
85 }
|
|
86 // debug (barrier)
|
|
87 // log.trace("Releasing mutex");
|
|
88 }
|
|
89 }
|
|
90 catch (SyncException e)
|
|
91 {
|
|
92 Stderr.formatln("Sync exception caught in Barrier test thread {0}:\n{1}\n",
|
|
93 Thread.getThis().name, e.toString());
|
|
94 }
|
|
95 catch (Exception e)
|
|
96 {
|
|
97 Stderr.formatln("Unexpected exception caught in Barrier test thread {0}:\n{1}\n",
|
|
98 Thread.getThis().name, e.toString());
|
|
99 }
|
|
100 }
|
|
101
|
|
102 ThreadGroup group = new ThreadGroup();
|
|
103 Thread thread;
|
|
104 char[10] tmp;
|
|
105
|
|
106 for (uint i = 0; i < MaxThreadCount; ++i)
|
|
107 {
|
|
108 thread = new Thread(&barrierTestThread);
|
|
109 thread.name = "thread-" ~ format(tmp, i);
|
|
110
|
|
111 group.add(thread);
|
|
112 debug (barrier)
|
|
113 log.trace("Created thread " ~ thread.name);
|
|
114 thread.start();
|
|
115 }
|
|
116
|
|
117 debug (barrier)
|
|
118 log.trace("Waiting for threads to finish");
|
|
119 group.joinAll();
|
|
120
|
|
121 if (count == MaxThreadCount * LoopsPerThread)
|
|
122 {
|
|
123 debug (barrier)
|
|
124 log.info("The Barrier test was successful");
|
|
125 }
|
|
126 else
|
|
127 {
|
|
128 debug (barrier)
|
|
129 {
|
|
130 log.error("The Barrier is not working properly: the counter has an incorrect value");
|
|
131 assert(false);
|
|
132 }
|
|
133 else
|
|
134 {
|
|
135 assert(false, "The Barrier is not working properly: the counter has an incorrect value");
|
|
136 }
|
|
137 }
|
|
138 }
|