diff options
Diffstat (limited to 'src')
5 files changed, 186 insertions, 23 deletions
diff --git a/src/java/com/jogamp/common/util/locks/SingletonInstance.java b/src/java/com/jogamp/common/util/locks/SingletonInstance.java index 5f2718b..476c269 100644 --- a/src/java/com/jogamp/common/util/locks/SingletonInstance.java +++ b/src/java/com/jogamp/common/util/locks/SingletonInstance.java @@ -95,19 +95,19 @@ public abstract class SingletonInstance implements Lock { locked = tryLockImpl(); if(locked) { if(DEBUG) { - System.err.println("SLOCK "+System.currentTimeMillis()+" +++ "+getName()+" - Locked "); + System.err.println(infoPrefix()+" +++ "+getName()+" - Locked "); } return true; } if(DEBUG && 0==i) { - System.err.println("SLOCK "+System.currentTimeMillis()+" ??? "+getName()+" - Wait for lock"); + System.err.println(infoPrefix()+" III "+getName()+" - Wait for lock"); } Thread.sleep(poll_ms); maxwait -= poll_ms; i++; } while ( 0 < maxwait ) ; } catch ( InterruptedException ie ) { - throw new RuntimeException("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - couldn't get lock", ie); + throw new RuntimeException(infoPrefix()+" EEE "+getName()+" - couldn't get lock", ie); } return false; } @@ -118,8 +118,7 @@ public abstract class SingletonInstance implements Lock { if(locked) { locked = !unlockImpl(); if(DEBUG) { - System.err.println("SLOCK "+System.currentTimeMillis()+" --- "+getName()+" - Unlock " - + ( locked ? "failed" : "ok" ) ); + System.err.println(infoPrefix()+" --- "+getName()+" - Unlock "+ ( locked ? "failed" : "ok" ) ); } } } @@ -130,6 +129,10 @@ public abstract class SingletonInstance implements Lock { return locked; } + protected String infoPrefix() { + return "SLOCK [T "+Thread.currentThread().getName()+" @ "+System.currentTimeMillis()+" ms"; + } + private final long poll_ms; private boolean locked = false; } diff --git a/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java b/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java index c8a05ed..a76f261 100644 --- a/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java +++ b/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java @@ -80,7 +80,7 @@ public class SingletonInstanceFileLock extends SingletonInstance { @Override public void run() { if(isLocked()) { - System.err.println("SLOCK "+System.currentTimeMillis()+" XXX "+getName()+" - Unlock @ JVM Shutdown"); + System.err.println(infoPrefix()+" XXX "+getName()+" - Unlock @ JVM Shutdown"); } unlock(); } @@ -97,7 +97,7 @@ public class SingletonInstanceFileLock extends SingletonInstance { return true; } } catch (Exception e) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - Unable to create and/or lock file"); + System.err.println(infoPrefix()+" III "+getName()+" - Unable to create and/or lock file"); e.printStackTrace(); } return false; @@ -119,7 +119,7 @@ public class SingletonInstanceFileLock extends SingletonInstance { } return true; } catch (Exception e) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - Unable to remove lock file"); + System.err.println(infoPrefix()+" EEE "+getName()+" - Unable to remove lock file"); e.printStackTrace(); } finally { fileLock = null; diff --git a/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java b/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java index eab687d..dac6e72 100644 --- a/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java +++ b/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java @@ -66,7 +66,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance { } catch (UnknownHostException e) { } } if(null == ilh) { - throw new RuntimeException("Could not determine local InetAddress"); + throw new RuntimeException(infoPrefix()+" EEE Could not determine local InetAddress"); } fullName = ilh.toString()+":"+portNumber; @@ -126,13 +126,11 @@ public class SingletonInstanceServerSocket extends SingletonInstance { private Object syncOnStartStop = new Object(); private ServerSocket serverSocket = null; - private Thread serverThread = null; + private Thread serverThread = null; // allowing kill() to force-stop last server-thread public Server(InetAddress localInetAddress, int portNumber) { this.localInetAddress = localInetAddress; this.portNumber = portNumber; - this.serverThread = new Thread(this); - this.serverThread.setDaemon(true); // be a daemon, don't keep the JVM running } public final InetAddress getLocalInetAddress() { return localInetAddress; } @@ -142,6 +140,8 @@ public class SingletonInstanceServerSocket extends SingletonInstance { if(alive) return true; synchronized (syncOnStartStop) { + serverThread = new Thread(this); + serverThread.setDaemon(true); // be a daemon, don't keep the JVM running serverThread.start(); try { syncOnStartStop.wait(); @@ -169,12 +169,10 @@ public class SingletonInstanceServerSocket extends SingletonInstance { } } if(alive) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - Unable to remove lock: ServerThread still alive ?"); + System.err.println(infoPrefix()+" EEE "+getName()+" - Unable to remove lock: ServerThread still alive ?"); kill(); alive = false; } - serverThread = new Thread(this); - serverThread.setDaemon(true); // be a daemon, don't keep the JVM running return true; } @@ -185,11 +183,13 @@ public class SingletonInstanceServerSocket extends SingletonInstance { @SuppressWarnings("deprecation") public final void kill() { if(alive) { - System.err.println("SLOCK "+System.currentTimeMillis()+" XXX "+getName()+" - Kill @ JVM Shutdown"); + System.err.println(infoPrefix()+" XXX "+getName()+" - Kill @ JVM Shutdown"); + } + if(null != serverThread) { + try { + serverThread.stop(); + } catch(Throwable t) { } } - try { - serverThread.stop(); - } catch(Throwable t) { } if(null != serverSocket) { try { final ServerSocket ss = serverSocket; @@ -216,7 +216,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance { public void run() { { final Thread currentThread = Thread.currentThread(); - currentThread.setName(currentThread.getName() + " - SingletonInstanceServerSocket: "+getName()); + currentThread.setName(currentThread.getName() + " - SISock: "+getName()); if(DEBUG) { System.err.println(currentThread.getName()+" - started"); } @@ -227,7 +227,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance { serverSocket = new ServerSocket(portNumber, 1, localInetAddress); alive = true; } catch (IOException e) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - Unable to install ServerSocket: "+e.getMessage()); + System.err.println(infoPrefix()+" III - Unable to install ServerSocket: "+e.getMessage()); shallQuit = true; } finally { syncOnStartStop.notifyAll(); @@ -239,7 +239,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance { final Socket clientSocket = serverSocket.accept(); clientSocket.close(); } catch (IOException ioe) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - Exception during accept: " + ioe.getMessage()); + System.err.println(infoPrefix()+" EEE - Exception during accept: " + ioe.getMessage()); } } @@ -249,7 +249,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance { serverSocket.close(); } } catch (IOException e) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - Exception during close: " + e.getMessage()); + System.err.println(infoPrefix()+" EEE - Exception during close: " + e.getMessage()); } finally { serverSocket = null; alive = false; diff --git a/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket00.java b/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket00.java new file mode 100644 index 0000000..bb6a55c --- /dev/null +++ b/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket00.java @@ -0,0 +1,101 @@ +/** + * Copyright 2012 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 junit.framework.Assert; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestSingletonServerSocket00 { + // public static final String SINGLE_INSTANCE_LOCK_FILE = "UITestCase.lock"; + public static final int SINGLE_INSTANCE_LOCK_PORT = 59999; + public static final long SINGLE_INSTANCE_LOCK_TO = 3*60*1000; // wait up to 3 min + public static final long SINGLE_INSTANCE_LOCK_POLL = 100; // poll every 100ms + private static volatile SingletonInstance singletonInstance; + + @BeforeClass + public static void oneTimeSetUp() { + // one-time initialization code + singletonInstance = SingletonInstance.createServerSocket(SINGLE_INSTANCE_LOCK_POLL, SINGLE_INSTANCE_LOCK_PORT); + } + + @Test + public void testLockUnlock() { + Assert.assertTrue("Could not lock single instance: "+singletonInstance.getName(), singletonInstance.tryLock(SINGLE_INSTANCE_LOCK_TO)); + System.gc(); // force cleanup + singletonInstance.unlock(); + } + + @Test + public void test2ndInstanceLockTimeout() { + Assert.assertTrue("Could not lock single instance: "+singletonInstance.getName(), singletonInstance.tryLock(SINGLE_INSTANCE_LOCK_TO)); + SingletonInstance instanceTwo = SingletonInstance.createServerSocket(SINGLE_INSTANCE_LOCK_POLL, SINGLE_INSTANCE_LOCK_PORT); + Assert.assertFalse("Could lock 2nd instance: "+instanceTwo.getName(), instanceTwo.tryLock(1000)); // 10x + System.gc(); // force cleanup + singletonInstance.unlock(); + } + + private Thread startLockUnlockOffThread(int i) { + final Thread t = new Thread(new Runnable() { + public void run() { + SingletonInstance myLock = SingletonInstance.createServerSocket(10, SINGLE_INSTANCE_LOCK_PORT); + System.err.println(Thread.currentThread().getName()+" LOCK try .."); + Assert.assertTrue(Thread.currentThread().getName()+" - Could not lock instance: "+myLock.getName(), myLock.tryLock(1000)); + System.err.println(Thread.currentThread().getName()+" LOCK ON"); + try { + Thread.sleep(300); + } catch (InterruptedException e) { } + myLock.unlock(); + System.err.println(Thread.currentThread().getName()+" LOCK OFF"); + } + }, "LockUnlock #"+i); + t.start(); + return t; + } + + @Test + public void testOffthreadLockUnlock() throws InterruptedException { + Assert.assertTrue("Could not lock single instance: "+singletonInstance.getName(), singletonInstance.tryLock(SINGLE_INSTANCE_LOCK_TO)); + Thread t1 = startLockUnlockOffThread(1); + Thread t2 = startLockUnlockOffThread(2); + Thread.sleep(300); + System.gc(); // force cleanup + singletonInstance.unlock(); + while(t1.isAlive() || t2.isAlive()) { + Thread.sleep(100); + } + } + + public static void main(String args[]) throws IOException, InterruptedException { + String tstname = TestSingletonServerSocket00.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} diff --git a/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket01.java b/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket01.java new file mode 100644 index 0000000..659e69e --- /dev/null +++ b/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket01.java @@ -0,0 +1,59 @@ +/** + * Copyright 2012 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 junit.framework.Assert; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestSingletonServerSocket01 { + // public static final String SINGLE_INSTANCE_LOCK_FILE = "UITestCase.lock"; + public static final int SINGLE_INSTANCE_LOCK_PORT = 59999; + public static final long SINGLE_INSTANCE_LOCK_TO = 3*60*1000; // wait up to 3 min + public static final long SINGLE_INSTANCE_LOCK_POLL = 1000; // poll every 1s + private static volatile SingletonInstance singletonInstance; + + @BeforeClass + public static void oneTimeSetUp() { + // one-time initialization code + singletonInstance = SingletonInstance.createServerSocket(SINGLE_INSTANCE_LOCK_POLL, SINGLE_INSTANCE_LOCK_PORT); + } + + @Test + public void testJVMShutdown() { + Assert.assertTrue("Could not lock single instance: "+singletonInstance.getName(), singletonInstance.tryLock(SINGLE_INSTANCE_LOCK_TO)); + } + + public static void main(String args[]) throws IOException, InterruptedException { + String tstname = TestSingletonServerSocket01.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} |