From 4374d11a3a204ab608402ee24a8cee14eff923ef Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sun, 11 Dec 2011 04:10:34 +0100 Subject: RecursiveThreadGroupLock: New recursive lock interface and impl, allowing 'spawn off' process to become the lock owner. To avoid complicated synchronization via synchronized, wait and notify between one thread and a 'spawn' off thread which temporarly requires the hold lock, RecursiveThreadGroupLock allows to add and remove other threads to become owners of the lock as if they were the original holder. This simplifies some rare locking use cases, eg. in JOGL's GLProfile initialization sequence where a SharedResourceRunner thread is taking over initialization of shared resources. --- .../util/locks/TestRecursiveThreadGroupLock01.java | 327 +++++++++++++++++++++ 1 file changed, 327 insertions(+) create mode 100644 src/junit/com/jogamp/common/util/locks/TestRecursiveThreadGroupLock01.java (limited to 'src/junit/com') diff --git a/src/junit/com/jogamp/common/util/locks/TestRecursiveThreadGroupLock01.java b/src/junit/com/jogamp/common/util/locks/TestRecursiveThreadGroupLock01.java new file mode 100644 index 0000000..2eecce1 --- /dev/null +++ b/src/junit/com/jogamp/common/util/locks/TestRecursiveThreadGroupLock01.java @@ -0,0 +1,327 @@ +/** + * 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.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.jogamp.common.os.Platform; +import com.jogamp.junit.util.JunitTracer; + +public class TestRecursiveThreadGroupLock01 extends JunitTracer { + + public enum YieldMode { + NONE(0), YIELD(1), SLEEP(2); + + public final int id; + + YieldMode(int id){ + this.id = id; + } + } + + static void yield(YieldMode mode) { + switch(mode) { + case YIELD: + Thread.yield(); + break; + case SLEEP: + try { + Thread.sleep(10); + } catch (InterruptedException ie) { + ie.printStackTrace(); + } + break; + default: + break; + } + + } + + static class LockedObject { + static final boolean DEBUG = false; + + private RecursiveThreadGroupLock locker; // post + private volatile int slaveCounter; + + public LockedObject() { + locker = LockFactory.createRecursiveThreadGroupLock(); + slaveCounter = 0; + } + + public final void masterAction(String tab, String name, Thread[] slaves, int loops, 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"); + } + // Implicit waits until all slaves got off the lock + locker.unlock(); + } + } + + public final void slaveAction(String tab, String name, int loops, int mark, 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 && slaveCounterloops); + Assert.assertTrue(loops*loops>mark); + } + + /** slave constructor */ + public LockedObjectRunner1(String tab, String name, LockedObject lo, int loops, int mark, 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 (InterruptedException e) { + e.printStackTrace(); + } + } + } + + } + + public void run() { + synchronized(this) { + started = true; + for(int i=0; !shouldStop && i