From 4aa7541635e1fd8ae6a41f1d32d85828a18f7028 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 21 Sep 2012 12:15:46 +0200 Subject: SingletonInstanceServerSocket: Add kill @ JVM Shutdown _and_ if normal unlock fails ; Added unit tests. --- .../util/locks/SingletonInstanceServerSocket.java | 47 ++++++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) (limited to 'src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java') diff --git a/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java b/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java index 14a6aaf..eab687d 100644 --- a/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java +++ b/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java @@ -70,7 +70,13 @@ public class SingletonInstanceServerSocket extends SingletonInstance { } fullName = ilh.toString()+":"+portNumber; - singletonServer = new Server(ilh, portNumber); + singletonServer = new Server(ilh, portNumber); + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + singletonServer.kill(); + } + }); } public final InetAddress getLocalInetAddress() { @@ -120,23 +126,23 @@ public class SingletonInstanceServerSocket extends SingletonInstance { private Object syncOnStartStop = new Object(); private ServerSocket serverSocket = null; + private Thread serverThread = null; 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; } public final int getPortNumber() { return portNumber; } - public boolean start() { + public final boolean start() { if(alive) return true; - - Thread t = new Thread(this); - t.setDaemon(true); // be a daemon, don't keep the JVM running - + synchronized (syncOnStartStop) { - t.start(); + serverThread.start(); try { syncOnStartStop.wait(); } catch (InterruptedException ie) { @@ -164,8 +170,33 @@ public class SingletonInstanceServerSocket extends SingletonInstance { } if(alive) { System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - Unable to remove lock: ServerThread still alive ?"); + kill(); + alive = false; } - return !alive; + serverThread = new Thread(this); + serverThread.setDaemon(true); // be a daemon, don't keep the JVM running + return true; + } + + /** + * Brutally kill server thread and close socket regardless. + * This is out last chance for JVM shutdown. + */ + @SuppressWarnings("deprecation") + public final void kill() { + if(alive) { + System.err.println("SLOCK "+System.currentTimeMillis()+" XXX "+getName()+" - Kill @ JVM Shutdown"); + } + try { + serverThread.stop(); + } catch(Throwable t) { } + if(null != serverSocket) { + try { + final ServerSocket ss = serverSocket; + serverSocket = null; + ss.close(); + } catch (Throwable t) { } + } } public final boolean isRunning() { return alive; } -- cgit v1.2.3