aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/jogamp/common/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/java/jogamp/common/util')
-rw-r--r--src/java/jogamp/common/util/locks/LockDebugUtil.java4
-rw-r--r--src/java/jogamp/common/util/locks/RecursiveLockImpl01CompleteFair.java54
-rw-r--r--src/java/jogamp/common/util/locks/RecursiveLockImpl01Unfairish.java54
-rw-r--r--src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java6
-rw-r--r--src/java/jogamp/common/util/locks/RecursiveThreadGroupLockImpl01Unfairish.java66
-rw-r--r--src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java14
-rw-r--r--src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java76
7 files changed, 137 insertions, 137 deletions
diff --git a/src/java/jogamp/common/util/locks/LockDebugUtil.java b/src/java/jogamp/common/util/locks/LockDebugUtil.java
index 480b143..ee0a8e8 100644
--- a/src/java/jogamp/common/util/locks/LockDebugUtil.java
+++ b/src/java/jogamp/common/util/locks/LockDebugUtil.java
@@ -50,7 +50,7 @@ public class LockDebugUtil {
dummy = new ArrayList<Throwable>(0);
}
}
-
+
public static List<Throwable> getRecursiveLockTrace() {
if(Lock.DEBUG) {
ArrayList<Throwable> ls = tlsLockedStacks.get();
@@ -63,7 +63,7 @@ public class LockDebugUtil {
return dummy;
}
}
-
+
public static void dumpRecursiveLockTrace(PrintStream out) {
if(Lock.DEBUG) {
List<Throwable> ls = getRecursiveLockTrace();
diff --git a/src/java/jogamp/common/util/locks/RecursiveLockImpl01CompleteFair.java b/src/java/jogamp/common/util/locks/RecursiveLockImpl01CompleteFair.java
index 986a7fc..f49e406 100644
--- a/src/java/jogamp/common/util/locks/RecursiveLockImpl01CompleteFair.java
+++ b/src/java/jogamp/common/util/locks/RecursiveLockImpl01CompleteFair.java
@@ -36,7 +36,7 @@ import com.jogamp.common.util.locks.RecursiveLock;
/**
* Reentrance locking toolkit, impl a complete fair FIFO scheduler
- *
+ *
* <p>
* Sync object extends {@link AbstractOwnableSynchronizer}, hence monitoring is possible.</p>
*/
@@ -50,7 +50,7 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock {
final Thread thread;
boolean signaledByUnlock; // if true, it's also removed from queue
}
-
+
@SuppressWarnings("serial")
private static class Sync extends AbstractOwnableSynchronizer {
private Sync() {
@@ -68,7 +68,7 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock {
ls.remove(lockedStack);
} else {
ls.add(s);
- }
+ }
lockedStack = s;
}
/** lock count by same thread */
@@ -79,7 +79,7 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock {
private Throwable lockedStack = null;
}
private Sync sync = new Sync();
-
+
public RecursiveLockImpl01CompleteFair() {
}
@@ -119,7 +119,7 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock {
public final boolean isLockedByOtherThread() {
synchronized(sync) {
final Thread o = sync.getOwner();
- return null != o && Thread.currentThread() != o ;
+ return null != o && Thread.currentThread() != o ;
}
}
@@ -175,9 +175,9 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock {
}
return true;
}
-
+
if ( sync.getOwner() != null || ( 0<timeout && 0<sync.queue.size() ) ) {
-
+
if ( 0 >= timeout ) {
// locked by other thread and no waiting requested
if(TRACE_LOCK) {
@@ -185,7 +185,7 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock {
}
return false;
}
-
+
// enqueue at the start
WaitingThread wCur = new WaitingThread(cur);
sync.queue.add(0, wCur);
@@ -198,24 +198,24 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock {
if( !wCur.signaledByUnlock ) {
sync.queue.remove(wCur); // O(n)
throw e; // propagate interruption not send by unlock
- } else if( cur != sync.getOwner() ) {
+ } else if( cur != sync.getOwner() ) {
// Issued by unlock, but still locked by other thread
//
timeout -= System.currentTimeMillis() - t0;
-
+
if(TRACE_LOCK) {
System.err.println("+++ LOCK 1 "+toString()+", cur "+threadName(cur)+", left "+timeout+" ms, signaled: "+wCur.signaledByUnlock);
}
-
+
if(0 < timeout) {
// not timed out, re-enque - lock was 'stolen'
wCur.signaledByUnlock = false;
sync.queue.add(sync.queue.size(), wCur);
}
- } // else: Issued by unlock, owning lock .. expected!
+ } // else: Issued by unlock, owning lock .. expected!
}
} while ( cur != sync.getOwner() && 0 < timeout ) ;
-
+
if( 0 >= timeout && cur != sync.getOwner() ) {
// timed out
if(!wCur.signaledByUnlock) {
@@ -226,7 +226,7 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock {
}
return false;
}
-
+
++sync.holdCount;
if(TRACE_LOCK) {
System.err.println("+++ LOCK X1 "+toString()+", cur "+threadName(cur)+", left "+timeout+" ms");
@@ -237,7 +237,7 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock {
System.err.println("+++ LOCK X0 "+toString()+", cur "+threadName(cur));
}
}
-
+
sync.setOwner(cur);
if(DEBUG) {
sync.setLockedStack(new Throwable("Previously locked by "+toString()));
@@ -245,7 +245,7 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock {
return true;
}
}
-
+
@Override
public final void unlock() {
@@ -259,33 +259,33 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock {
synchronized(sync) {
validateLocked();
final Thread cur = Thread.currentThread();
-
+
--sync.holdCount;
-
+
if (sync.holdCount > 0) {
if(TRACE_LOCK) {
System.err.println("--- LOCK XR "+toString()+", cur "+threadName(cur));
}
return;
}
-
+
if(DEBUG) {
sync.setLockedStack(null);
}
if(null!=taskAfterUnlockBeforeNotify) {
taskAfterUnlockBeforeNotify.run();
}
-
+
if(sync.queue.size() > 0) {
// fair, wakeup the oldest one ..
// final WaitingThread oldest = queue.removeLast();
final WaitingThread oldest = sync.queue.remove(sync.queue.size()-1);
- sync.setOwner(oldest.thread);
-
+ sync.setOwner(oldest.thread);
+
if(TRACE_LOCK) {
System.err.println("--- LOCK X1 "+toString()+", cur "+threadName(cur)+", signal: "+threadName(oldest.thread));
}
-
+
oldest.signaledByUnlock = true;
oldest.thread.interrupt(); // Propagate SecurityException if it happens
} else {
@@ -293,24 +293,24 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock {
if(TRACE_LOCK) {
System.err.println("--- LOCK X0 "+toString()+", cur "+threadName(cur)+", signal any");
}
- sync.notify();
+ sync.notify();
}
}
}
-
+
@Override
public final int getQueueLength() {
synchronized(sync) {
return sync.queue.size();
}
}
-
+
@Override
public String toString() {
return syncName()+"[count "+sync.holdCount+
", qsz "+sync.queue.size()+", owner "+threadName(sync.getOwner())+"]";
}
-
+
private final String syncName() {
return "<"+Integer.toHexString(this.hashCode())+", "+Integer.toHexString(sync.hashCode())+">";
}
diff --git a/src/java/jogamp/common/util/locks/RecursiveLockImpl01Unfairish.java b/src/java/jogamp/common/util/locks/RecursiveLockImpl01Unfairish.java
index e8fecb1..8c9f720 100644
--- a/src/java/jogamp/common/util/locks/RecursiveLockImpl01Unfairish.java
+++ b/src/java/jogamp/common/util/locks/RecursiveLockImpl01Unfairish.java
@@ -37,9 +37,9 @@ import com.jogamp.common.util.locks.RecursiveLock;
* Reentrance locking toolkit, impl a non-complete fair FIFO scheduler.
* <p>
* Fair scheduling is not guaranteed due to the usage of {@link Object#notify()},
- * however new lock-applicants will wait if queue is not empty for {@link #lock()}
+ * however new lock-applicants will wait if queue is not empty for {@link #lock()}
* and {@link #tryLock(long) tryLock}(timeout>0).</p>
- *
+ *
* <p>
* Sync object extends {@link AbstractOwnableSynchronizer}, hence monitoring is possible.</p>
*/
@@ -49,19 +49,19 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock {
Thread getOwner();
boolean isOwner(Thread t);
void setOwner(Thread t);
-
+
Throwable getLockedStack();
void setLockedStack(Throwable s);
-
+
int getHoldCount();
void incrHoldCount(Thread t);
void decrHoldCount(Thread t);
-
+
int getQSz();
void incrQSz();
void decrQSz();
}
-
+
@SuppressWarnings("serial")
/* package */ static class SingleThreadSync extends AbstractOwnableSynchronizer implements Sync {
/* package */ SingleThreadSync() {
@@ -90,7 +90,7 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock {
ls.remove(lockedStack);
} else {
ls.add(s);
- }
+ }
lockedStack = s;
}
@Override
@@ -99,14 +99,14 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock {
public void incrHoldCount(Thread t) { holdCount++; }
@Override
public void decrHoldCount(Thread t) { holdCount--; }
-
+
@Override
public final int getQSz() { return qsz; }
@Override
public final void incrQSz() { qsz++; }
@Override
public final void decrQSz() { qsz--; }
-
+
/** lock count by same thread */
private int holdCount = 0;
/** queue size of waiting threads */
@@ -114,13 +114,13 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock {
/** stack trace of the lock, only used if DEBUG */
private Throwable lockedStack = null;
}
-
+
protected final Sync sync;
-
+
public RecursiveLockImpl01Unfairish(Sync sync) {
this.sync = sync;
}
-
+
public RecursiveLockImpl01Unfairish() {
this(new SingleThreadSync());
}
@@ -217,9 +217,9 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock {
}
return true;
}
-
+
if ( sync.getOwner() != null || ( 0<timeout && 0<sync.getQSz() ) ) {
-
+
if ( 0 >= timeout ) {
// locked by other thread and no waiting requested
if(TRACE_LOCK) {
@@ -227,7 +227,7 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock {
}
return false;
}
-
+
sync.incrQSz();
do {
final long t0 = System.currentTimeMillis();
@@ -235,7 +235,7 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock {
timeout -= System.currentTimeMillis() - t0;
} while (null != sync.getOwner() && 0 < timeout) ;
sync.decrQSz();
-
+
if( 0 >= timeout && sync.getOwner() != null ) {
// timed out
if(TRACE_LOCK) {
@@ -243,24 +243,24 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock {
}
return false;
}
-
+
if(TRACE_LOCK) {
System.err.println("+++ LOCK X1 "+toString()+", cur "+threadName(cur)+", left "+timeout+" ms");
}
} else if(TRACE_LOCK) {
System.err.println("+++ LOCK X0 "+toString()+", cur "+threadName(cur));
}
-
+
sync.setOwner(cur);
sync.incrHoldCount(cur);
-
+
if(DEBUG) {
sync.setLockedStack(new Throwable("Previously locked by "+toString()));
}
return true;
}
}
-
+
@Override
public final void unlock() {
@@ -274,16 +274,16 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock {
synchronized(sync) {
validateLocked();
final Thread cur = Thread.currentThread();
-
+
sync.decrHoldCount(cur);
-
+
if (sync.getHoldCount() > 0) {
if(TRACE_LOCK) {
System.err.println("--- LOCK XR "+toString()+", cur "+threadName(cur));
}
return;
}
-
+
sync.setOwner(null);
if(DEBUG) {
sync.setLockedStack(null);
@@ -291,27 +291,27 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock {
if(null!=taskAfterUnlockBeforeNotify) {
taskAfterUnlockBeforeNotify.run();
}
-
+
if(TRACE_LOCK) {
System.err.println("--- LOCK X0 "+toString()+", cur "+threadName(cur)+", signal any");
}
sync.notify();
}
}
-
+
@Override
public final int getQueueLength() {
synchronized(sync) {
return sync.getQSz();
}
}
-
+
@Override
public String toString() {
return syncName()+"[count "+sync.getHoldCount()+
", qsz "+sync.getQSz()+", owner "+threadName(sync.getOwner())+"]";
}
-
+
/* package */ final String syncName() {
return "<"+Integer.toHexString(this.hashCode())+", "+Integer.toHexString(sync.hashCode())+">";
}
diff --git a/src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java b/src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java
index d9bc3df..f3dfa42 100644
--- a/src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java
+++ b/src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java
@@ -9,11 +9,11 @@ public class RecursiveLockImplJava5 implements RecursiveLock {
volatile Thread owner = null;
ReentrantLock lock;
-
+
public RecursiveLockImplJava5(boolean fair) {
lock = new ReentrantLock(fair);
}
-
+
@Override
public void lock() {
try {
@@ -49,7 +49,7 @@ public class RecursiveLockImplJava5 implements RecursiveLock {
}
lock.unlock();
}
-
+
@Override
public boolean isLocked() {
return lock.isLocked();
diff --git a/src/java/jogamp/common/util/locks/RecursiveThreadGroupLockImpl01Unfairish.java b/src/java/jogamp/common/util/locks/RecursiveThreadGroupLockImpl01Unfairish.java
index 6c43580..7a386d6 100644
--- a/src/java/jogamp/common/util/locks/RecursiveThreadGroupLockImpl01Unfairish.java
+++ b/src/java/jogamp/common/util/locks/RecursiveThreadGroupLockImpl01Unfairish.java
@@ -31,12 +31,12 @@ import java.util.Arrays;
import com.jogamp.common.util.locks.RecursiveThreadGroupLock;
-public class RecursiveThreadGroupLockImpl01Unfairish
- extends RecursiveLockImpl01Unfairish
- implements RecursiveThreadGroupLock
+public class RecursiveThreadGroupLockImpl01Unfairish
+ extends RecursiveLockImpl01Unfairish
+ implements RecursiveThreadGroupLock
{
/* package */ @SuppressWarnings("serial")
- static class ThreadGroupSync extends SingleThreadSync {
+ static class ThreadGroupSync extends SingleThreadSync {
/* package */ ThreadGroupSync() {
super();
threadNum = 0;
@@ -44,23 +44,23 @@ public class RecursiveThreadGroupLockImpl01Unfairish
holdCountAdditionOwner = 0;
}
@Override
- public final void incrHoldCount(Thread t) {
- super.incrHoldCount(t);
- if(!isOriginalOwner(t)) {
- holdCountAdditionOwner++;
- }
+ public final void incrHoldCount(Thread t) {
+ super.incrHoldCount(t);
+ if(!isOriginalOwner(t)) {
+ holdCountAdditionOwner++;
+ }
}
@Override
- public final void decrHoldCount(Thread t) {
- super.decrHoldCount(t);
- if(!isOriginalOwner(t)) {
- holdCountAdditionOwner--;
- }
+ public final void decrHoldCount(Thread t) {
+ super.decrHoldCount(t);
+ if(!isOriginalOwner(t)) {
+ holdCountAdditionOwner--;
+ }
}
public final int getAdditionalOwnerHoldCount() {
return holdCountAdditionOwner;
}
-
+
public final boolean isOriginalOwner(Thread t) {
return super.isOwner(t);
}
@@ -76,11 +76,11 @@ public class RecursiveThreadGroupLockImpl01Unfairish
}
return false;
}
-
+
public final int getAddOwnerCount() {
return threadNum;
}
- public final void addOwner(Thread t) throws IllegalArgumentException {
+ public final void addOwner(Thread t) throws IllegalArgumentException {
if(null == threads) {
if(threadNum>0) {
throw new InternalError("XXX");
@@ -98,15 +98,15 @@ public class RecursiveThreadGroupLockImpl01Unfairish
threads[threadNum] = t;
threadNum++;
}
-
+
public final void removeAllOwners() {
for(int i=threadNum-1; 0<=i; i--) {
threads[i]=null;
}
- threadNum=0;
+ threadNum=0;
}
-
- public final void removeOwner(Thread t) throws IllegalArgumentException {
+
+ public final void removeOwner(Thread t) throws IllegalArgumentException {
for (int i = 0 ; i < threadNum ; i++) {
if (threads[i] == t) {
threadNum--;
@@ -117,7 +117,7 @@ public class RecursiveThreadGroupLockImpl01Unfairish
}
throw new IllegalArgumentException("Not an owner: "+t);
}
-
+
String addOwnerToString() {
StringBuilder sb = new StringBuilder();
for(int i=0; i<threadNum; i++) {
@@ -126,19 +126,19 @@ public class RecursiveThreadGroupLockImpl01Unfairish
}
sb.append(threads[i].getName());
}
- return sb.toString();
+ return sb.toString();
}
-
+
// lock count by addition owner threads
- private int holdCountAdditionOwner;
+ private int holdCountAdditionOwner;
private Thread[] threads;
private int threadNum;
}
-
+
public RecursiveThreadGroupLockImpl01Unfairish() {
super(new ThreadGroupSync());
}
-
+
@Override
public final boolean isOriginalOwner() {
return isOriginalOwner(Thread.currentThread());
@@ -164,13 +164,13 @@ public class RecursiveThreadGroupLockImpl01Unfairish
}
tgSync.addOwner(t);
}
-
+
@Override
public final void unlock(Runnable taskAfterUnlockBeforeNotify) {
synchronized(sync) {
final Thread cur = Thread.currentThread();
final ThreadGroupSync tgSync = (ThreadGroupSync)sync;
-
+
if( tgSync.getAddOwnerCount()>0 ) {
if(TRACE_LOCK) {
System.err.println("--- LOCK XR (tg) "+toString()+", cur "+threadName(cur)+" -> owner...");
@@ -193,23 +193,23 @@ public class RecursiveThreadGroupLockImpl01Unfairish
final Thread originalOwner = tgSync.getOwner();
if(originalOwner.getState() == Thread.State.WAITING) {
originalOwner.interrupt();
- }
+ }
}
}
if(TRACE_LOCK) {
System.err.println("++ unlock(X): currentThread "+cur.getName()+", lock: "+this.toString());
System.err.println("--- LOCK X0 (tg) "+toString()+", cur "+threadName(cur)+" -> unlock!");
}
- super.unlock(taskAfterUnlockBeforeNotify);
+ super.unlock(taskAfterUnlockBeforeNotify);
}
}
-
+
@Override
public final void removeOwner(Thread t) throws RuntimeException, IllegalArgumentException {
validateLocked();
((ThreadGroupSync)sync).removeOwner(t);
}
-
+
@Override
public String toString() {
final ThreadGroupSync tgSync = (ThreadGroupSync)sync;
diff --git a/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java b/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java
index 42d125a..a3d3ac9 100644
--- a/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java
+++ b/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java
@@ -3,14 +3,14 @@
*
* 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
@@ -20,12 +20,12 @@
* 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 jogamp.common.util.locks;
import java.io.File;
@@ -73,7 +73,7 @@ public class SingletonInstanceFileLock extends SingletonInstance {
@Override
public final String getName() { return file.getPath(); }
-
+
private void setupFileCleanup() {
file.deleteOnExit();
Runtime.getRuntime().addShutdownHook(new Thread() {
@@ -84,7 +84,7 @@ public class SingletonInstanceFileLock extends SingletonInstance {
}
unlock();
}
- });
+ });
}
@Override
diff --git a/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java b/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java
index c4f9564..a1ca2ff 100644
--- a/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java
+++ b/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java
@@ -3,14 +3,14 @@
*
* 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
@@ -20,12 +20,12 @@
* 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 jogamp.common.util.locks;
import java.io.IOException;
@@ -39,11 +39,11 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
private final Server singletonServer;
private final String fullName;
-
+
public SingletonInstanceServerSocket(long poll_ms, int portNumber) {
super(poll_ms);
-
- // Gather the local InetAddress, loopback is prioritized
+
+ // Gather the local InetAddress, loopback is prioritized
InetAddress ilh = null;
try {
ilh = InetAddress.getByName(null); // loopback
@@ -56,10 +56,10 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
}
if(null == ilh) {
try {
- ilh = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 } );
+ ilh = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 } );
if(null!=ilh && !ilh.isLoopbackAddress()) { ilh = null; }
} catch (UnknownHostException e) { }
- }
+ }
if(null == ilh) {
try {
ilh = InetAddress.getLocalHost();
@@ -68,15 +68,15 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
if(null == ilh) {
throw new RuntimeException(infoPrefix()+" EEE Could not determine local InetAddress");
}
-
+
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() {
@@ -89,13 +89,13 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
@Override
public final String getName() { return fullName; }
-
+
@Override
protected boolean tryLockImpl() {
if( singletonServer.isRunning() ) {
return false; // same JVM .. server socket already installed !
}
-
+
// check if other JVM's locked the server socket ..
Socket clientSocket = singletonServer.connect();
if(null != clientSocket) {
@@ -104,11 +104,11 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
} catch (IOException e) { }
return false;
}
-
+
if( !singletonServer.start() ) {
return false;
}
-
+
return true;
}
@@ -119,26 +119,26 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
public class Server implements Runnable {
private final InetAddress localInetAddress;
- private final int portNumber;
-
- private volatile boolean shallQuit = false;
+ private final int portNumber;
+
+ private volatile boolean shallQuit = false;
private volatile boolean alive = false;
-
+
private Object syncOnStartStop = new Object();
private ServerSocket serverSocket = 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;
}
-
+
public final InetAddress getLocalInetAddress() { return localInetAddress; }
public final int getPortNumber() { return portNumber; }
-
+
public final boolean start() {
if(alive) return true;
-
+
synchronized (syncOnStartStop) {
serverThread = new Thread(this);
serverThread.setDaemon(true); // be a daemon, don't keep the JVM running
@@ -147,7 +147,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
syncOnStartStop.wait();
} catch (InterruptedException ie) {
ie.printStackTrace();
- }
+ }
}
boolean ok = isBound();
if(!ok) {
@@ -155,10 +155,10 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
}
return ok;
}
-
+
public final boolean shutdown() {
if(!alive) return true;
-
+
synchronized (syncOnStartStop) {
shallQuit = true;
connect();
@@ -166,7 +166,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
syncOnStartStop.wait();
} catch (InterruptedException ie) {
ie.printStackTrace();
- }
+ }
}
if(alive) {
System.err.println(infoPrefix()+" EEE "+getName()+" - Unable to remove lock: ServerThread still alive ?");
@@ -175,7 +175,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
return true;
}
- /**
+ /**
* Brutally kill server thread and close socket regardless.
* This is out last chance for JVM shutdown.
*/
@@ -196,11 +196,11 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
serverSocket = null;
ss.close();
} catch (Throwable t) { }
- }
+ }
}
-
+
public final boolean isRunning() { return alive; }
-
+
public final boolean isBound() {
return alive && null != serverSocket && serverSocket.isBound() ;
}
@@ -211,14 +211,14 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
} catch (Exception e) { }
return null;
}
-
+
@Override
public void run() {
{
final Thread currentThread = Thread.currentThread();
currentThread.setName(currentThread.getName() + " - SISock: "+getName());
if(DEBUG) {
- System.err.println(currentThread.getName()+" - started");
+ System.err.println(currentThread.getName()+" - started");
}
}
alive = false;
@@ -231,10 +231,10 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
System.err.println(infoPrefix()+" III - Unable to install ServerSocket: "+e.getMessage());
shallQuit = true;
} finally {
- syncOnStartStop.notifyAll();
+ syncOnStartStop.notifyAll();
}
}
-
+
while (!shallQuit) {
try {
final Socket clientSocket = serverSocket.accept();
@@ -243,7 +243,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance {
System.err.println(infoPrefix()+" EEE - Exception during accept: " + ioe.getMessage());
}
}
-
+
synchronized (syncOnStartStop) {
try {
if(null != serverSocket) {