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.ReadWriteMutex;
|
|
9 private import tango.core.sync.Mutex;
|
|
10 private import tango.core.Thread;
|
|
11 private import tango.text.convert.Integer;
|
|
12 debug (readwritemutex)
|
|
13 {
|
|
14 private import tango.util.log.Log;
|
|
15 private import tango.util.log.ConsoleAppender;
|
|
16 private import tango.util.log.DateLayout;
|
|
17 }
|
|
18
|
|
19
|
|
20 /**
|
|
21 * Example program for the tango.core.sync.ReadWriteMutex module.
|
|
22 */
|
|
23 void main(char[][] args)
|
|
24 {
|
|
25 const uint ReaderThreads = 100;
|
|
26 const uint WriterThreads = 20;
|
|
27 const uint LoopsPerReader = 10000;
|
|
28 const uint LoopsPerWriter = 1000;
|
|
29 const uint CounterIncrement = 3;
|
|
30
|
|
31 debug (readwritemutex)
|
|
32 {
|
|
33 Logger log = Log.getLogger("readwritemutex");
|
|
34
|
|
35 log.addAppender(new ConsoleAppender(new DateLayout()));
|
|
36
|
|
37 log.info("ReadWriteMutex test");
|
|
38 }
|
|
39
|
|
40 ReadWriteMutex rwlock = new ReadWriteMutex();
|
|
41 Mutex mutex = new Mutex();
|
|
42 uint readCount = 0;
|
|
43 uint passed = 0;
|
|
44 uint failed = 0;
|
|
45
|
|
46 void mutexReaderThread()
|
|
47 {
|
|
48 debug (readwritemutex)
|
|
49 {
|
|
50 Logger log = Log.getLogger("readwritemutex." ~ Thread.getThis().name());
|
|
51
|
|
52 log.trace("Starting reader thread");
|
|
53 }
|
|
54
|
|
55 for (uint i = 0; i < LoopsPerReader; ++i)
|
|
56 {
|
|
57 // All the reader threads acquire the mutex for reading and when they are
|
|
58 // all done
|
|
59 synchronized (rwlock.reader)
|
|
60 {
|
|
61 for (uint j = 0; j < CounterIncrement; ++j)
|
|
62 {
|
|
63 synchronized (mutex)
|
|
64 {
|
|
65 ++readCount;
|
|
66 }
|
|
67 }
|
|
68 }
|
|
69 }
|
|
70 }
|
|
71
|
|
72 void mutexWriterThread()
|
|
73 {
|
|
74 debug (readwritemutex)
|
|
75 {
|
|
76 Logger log = Log.getLogger("readwritemutex." ~ Thread.getThis().name());
|
|
77
|
|
78 log.trace("Starting writer thread");
|
|
79 }
|
|
80
|
|
81 for (uint i = 0; i < LoopsPerWriter; ++i)
|
|
82 {
|
|
83 synchronized (rwlock.writer)
|
|
84 {
|
|
85 synchronized (mutex)
|
|
86 {
|
|
87 if (readCount % 3 == 0)
|
|
88 {
|
|
89 ++passed;
|
|
90 }
|
|
91 }
|
|
92 }
|
|
93 }
|
|
94 }
|
|
95
|
|
96 ThreadGroup group = new ThreadGroup();
|
|
97 Thread thread;
|
|
98 char[10] tmp;
|
|
99
|
|
100 for (uint i = 0; i < ReaderThreads; ++i)
|
|
101 {
|
|
102 thread = new Thread(&mutexReaderThread);
|
|
103 thread.name = "reader-" ~ format(tmp, i);
|
|
104
|
|
105 debug (readwritemutex)
|
|
106 log.trace("Created reader thread " ~ thread.name);
|
|
107 thread.start();
|
|
108
|
|
109 group.add(thread);
|
|
110 }
|
|
111
|
|
112 for (uint i = 0; i < WriterThreads; ++i)
|
|
113 {
|
|
114 thread = new Thread(&mutexWriterThread);
|
|
115 thread.name = "writer-" ~ format(tmp, i);
|
|
116
|
|
117 debug (readwritemutex)
|
|
118 log.trace("Created writer thread " ~ thread.name);
|
|
119 thread.start();
|
|
120
|
|
121 group.add(thread);
|
|
122 }
|
|
123
|
|
124 debug (readwritemutex)
|
|
125 log.trace("Waiting for threads to finish");
|
|
126 group.joinAll();
|
|
127
|
|
128 if (passed == WriterThreads * LoopsPerWriter)
|
|
129 {
|
|
130 debug (readwritemutex)
|
|
131 log.info("The ReadWriteMutex test was successful");
|
|
132 }
|
|
133 else
|
|
134 {
|
|
135 debug (readwritemutex)
|
|
136 {
|
|
137 log.error("The ReadWriteMutex is not working properly: the counter has an incorrect value");
|
|
138 assert(false);
|
|
139 }
|
|
140 else
|
|
141 {
|
|
142 assert(false, "The ReadWriteMutex is not working properly: the counter has an incorrect value");
|
|
143 }
|
|
144 }
|
|
145 }
|