1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/**
* Copyright 2010 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/
package com.jogamp.common.util.locks;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import com.jogamp.common.os.Platform;
import com.jogamp.junit.util.SingletonTestCase;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestRecursiveThreadGroupLock01 extends SingletonTestCase {
public enum YieldMode {
NONE(0), YIELD(1), SLEEP(2);
public final int id;
YieldMode(final int id){
this.id = id;
}
}
static void yield(final YieldMode mode) {
switch(mode) {
case YIELD:
Thread.yield();
break;
case SLEEP:
try {
Thread.sleep(10);
} catch (final InterruptedException ie) {
ie.printStackTrace();
}
break;
default:
break;
}
}
static class LockedObject {
static final boolean DEBUG = false;
private final RecursiveThreadGroupLock locker; // post
private volatile int slaveCounter;
public LockedObject() {
locker = LockFactory.createRecursiveThreadGroupLock();
slaveCounter = 0;
}
public final void masterAction(final String tab, final String name, final Thread[] slaves, final int loops, final int mark, final YieldMode yieldMode) {
locker.lock();
if(DEBUG) {
System.err.println(tab+"<"+name+" c "+slaveCounter);
}
Assert.assertTrue(mark>loops);
Assert.assertTrue(loops*loops>mark);
try {
if(slaveCounter<mark) {
for(int i=0; i<slaves.length; i++) {
locker.addOwner(slaves[i]);
slaves[i].start();
}
while(slaveCounter<mark) {
yield(yieldMode);
}
}
} finally {
if(DEBUG) {
System.err.println(tab+" "+name+" c "+slaveCounter+">");
}
// Implicit waits until all slaves got off the lock
locker.unlock();
}
}
public final void slaveAction(final String tab, final String name, int loops, final int mark, final YieldMode yieldMode) {
if(slaveCounter>=mark) {
if(DEBUG) {
System.err.println(tab+"["+name+" c "+slaveCounter+" - NOP]");
}
return;
}
locker.lock();
if(DEBUG) {
System.err.println(tab+"["+name+" c "+slaveCounter);
}
Assert.assertTrue(mark>loops);
Assert.assertTrue(loops*loops>mark);
try {
while(loops>0 && slaveCounter<mark) {
slaveCounter++;
loops--;
}
/**
while(slaveCounter<mark) {
slaveCounter++;
} */
yield(yieldMode);
} finally {
if(DEBUG) {
System.err.println(tab+" "+name+" c "+slaveCounter+"]");
}
locker.unlock();
}
}
public final boolean isLocked() {
return locker.isLocked();
}
}
interface LockedObjectRunner extends Runnable {
void stop();
boolean isStarted();
boolean isStopped();
void waitUntilStopped();
}
class LockedObjectRunner1 implements LockedObjectRunner {
volatile boolean shouldStop;
volatile boolean stopped;
volatile boolean started;
String tab, name;
LockedObject lo;
Thread[] slaves;
int loops;
int mark;
YieldMode yieldMode;
/** master constructor */
public LockedObjectRunner1(final String tab, final String name, final LockedObject lo, final Thread[] slaves, final int loops, final int mark, final YieldMode yieldMode) {
this.tab = tab;
this.name = name;
this.lo = lo;
this.slaves = slaves;
this.loops = loops;
this.mark = mark;
this.shouldStop = false;
this.stopped = false;
this.yieldMode = yieldMode;
Assert.assertTrue(mark>loops);
Assert.assertTrue(loops*loops>mark);
}
/** slave constructor */
public LockedObjectRunner1(final String tab, final String name, final LockedObject lo, final int loops, final int mark, final YieldMode yieldMode) {
this.tab = tab;
this.name = name;
this.lo = lo;
this.slaves = null; // slave
this.loops = loops;
this.mark = mark;
this.shouldStop = false;
this.stopped = false;
this.yieldMode = yieldMode;
Assert.assertTrue(mark>loops);
Assert.assertTrue(loops*loops>mark);
}
public final void stop() {
shouldStop = true;
}
public final boolean isStarted() {
return started;
}
public final boolean isStopped() {
return stopped;
}
public void waitUntilStopped() {
synchronized(this) {
while(!stopped) {
try {
this.wait();
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void run() {
synchronized(this) {
started = true;
for(int i=0; !shouldStop && i<loops; i++) {
if(null != slaves) {
lo.masterAction(tab, name, slaves, loops, mark, yieldMode);
} else {
lo.slaveAction(tab, name, loops, mark, yieldMode);
}
}
stopped = true;
this.notifyAll();
}
}
}
protected long testLockedObjectImpl(final LockFactory.ImplType implType, final boolean fair,
final int slaveThreadNum, final int concurrentThreadNum,
final int loops, final int mark, final YieldMode yieldMode) throws InterruptedException {
final long t0 = System.currentTimeMillis();
final LockedObject lo = new LockedObject();
final LockedObjectRunner[] concurrentRunners = new LockedObjectRunner[concurrentThreadNum];
final LockedObjectRunner[] slaveRunners = new LockedObjectRunner[slaveThreadNum];
final Thread[] concurrentThreads = new Thread[concurrentThreadNum];
final Thread[] slaveThreads = new Thread[slaveThreadNum];
final Thread[] noCoOwnerThreads = new Thread[0];
int i;
for(i=0; i<slaveThreadNum; i++) {
slaveRunners[i] = new LockedObjectRunner1(" ", "s"+i, lo, loops, mark, yieldMode);
final String name = "ActionThread-Slaves-"+i+"_of_"+slaveThreadNum;
slaveThreads[i] = new Thread( slaveRunners[i], name );
}
for(i=0; i<concurrentThreadNum; i++) {
String name;
if(i==0) {
concurrentRunners[i] = new LockedObjectRunner1("", "M0", lo, slaveThreads, loops, mark, yieldMode);
name = "ActionThread-Master-"+i+"_of_"+concurrentThreadNum;
} else {
concurrentRunners[i] = new LockedObjectRunner1(" ", "O"+i, lo, noCoOwnerThreads, loops, mark, yieldMode);
name = "ActionThread-Others-"+i+"_of_"+concurrentThreadNum;
}
concurrentThreads[i] = new Thread( concurrentRunners[i], name );
concurrentThreads[i].start();
if(i==0) {
// master thread w/ slaves shall start first
while(!concurrentRunners[i].isStarted()) {
Thread.sleep(100);
}
}
}
for( i=0; i<slaveThreadNum; i++ ) {
slaveRunners[i].waitUntilStopped();
}
for( i=0; i<concurrentThreadNum; i++ ) {
concurrentRunners[i].waitUntilStopped();
}
Assert.assertEquals(0, lo.locker.getHoldCount());
Assert.assertEquals(false, lo.locker.isLocked());
final long dt = System.currentTimeMillis()-t0;
System.err.println();
final String fair_S = fair ? "fair " : "unfair" ;
System.err.printf("---- TestRecursiveLock01.testLockedObjectThreading: i %5s, %s, threads %2d, loops-outter %6d, loops-inner %6d, yield %5s - dt %6d ms",
implType, fair_S, concurrentThreadNum, loops, mark, yieldMode, dt);
System.err.println();
return dt;
}
@Test
public void testTwoThreadsInGroup() throws InterruptedException {
final LockFactory.ImplType t = LockFactory.ImplType.Int02ThreadGroup;
final boolean fair=true;
final int coOwnerThreadNum=2;
final int threadNum=5;
int loops=1000;
int mark=10000;
final YieldMode yieldMode=YieldMode.YIELD;
if( Platform.getCPUFamily() == Platform.CPUFamily.ARM ) {
loops=5; mark=10;
}
testLockedObjectImpl(t, fair, coOwnerThreadNum, threadNum, loops, mark, yieldMode);
}
public static void main(final String args[]) throws IOException, InterruptedException {
final String tstname = TestRecursiveThreadGroupLock01.class.getName();
org.junit.runner.JUnitCore.main(tstname);
/**
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.err.println("Press enter to continue");
System.err.println(stdin.readLine());
TestRecursiveLock01 t = new TestRecursiveLock01();
t.testLockedObjectThreading5x1000x10000N_Int01_Unfair();
t.testLockedObjectThreading5x1000x10000N_Int01_Fair();
t.testLockedObjectThreading5x1000x10000N_Java5_Fair();
t.testLockedObjectThreading5x1000x10000N_Int01_Unfair();
t.testLockedObjectThreading5x1000x10000N_Java5_Unfair();
*/
}
}
|