29package com.jogamp.common.util.locks;
33import jogamp.common.util.locks.SingletonInstanceFileLock;
34import jogamp.common.util.locks.SingletonInstanceServerSocket;
38 protected static final boolean DEBUG =
true;
41 return new SingletonInstanceFileLock(poll_ms, lockFileBasename);
45 return new SingletonInstanceFileLock(poll_ms, lockFile);
62 return new SingletonInstanceServerSocket(poll_ms, portNumber);
66 this.poll_ms = Math.max(10, poll_ms);
75 public synchronized void lock() throws RuntimeException {
82 }
catch (
final RuntimeException ie ) {
83 throw new RuntimeException(ie);
88 public synchronized boolean tryLock(
long maxwait)
throws RuntimeException {
92 final long t0 = System.currentTimeMillis();
96 final long t1 = System.currentTimeMillis();
100 final long t2 = System.currentTimeMillis();
101 System.err.println(
infoPrefix(t2)+
" +++ "+
getName()+
" - Locked within "+(t2-t0)+
" ms, "+(i+1)+
" attempts");
105 if(
DEBUG && 0==i ) {
106 System.err.println(
infoPrefix(System.currentTimeMillis())+
" III "+
getName()+
" - Wait for lock");
108 Thread.sleep(poll_ms);
109 maxwait -= System.currentTimeMillis()-t1;
111 }
while ( 0 < maxwait ) ;
112 }
catch (
final InterruptedException ie ) {
113 final long t2 = System.currentTimeMillis();
114 throw new RuntimeException(
infoPrefix(t2)+
" EEE (1) "+
getName()+
" - couldn't get lock within "+(t2-t0)+
" ms, "+i+
" attempts", ie);
117 final long t2 = System.currentTimeMillis();
118 System.err.println(
infoPrefix(t2)+
" +++ EEE (2) "+
getName()+
" - couldn't get lock within "+(t2-t0)+
" ms, "+i+
" attempts");
125 public void unlock() throws RuntimeException {
126 final long t0 = System.currentTimeMillis();
130 final long t2 = System.currentTimeMillis();
131 System.err.println(
infoPrefix(t2)+
" --- "+
getName()+
" - Unlock "+ ( locked ?
"failed" :
"ok" ) +
" within "+(t2-t0)+
" ms");
143 return "SLOCK [T "+Thread.currentThread().getName()+
" @ "+currentMillis+
" ms";
146 return infoPrefix(System.currentTimeMillis());
149 private final long poll_ms;
150 private boolean locked =
false;
static SingletonInstance createFileLock(final long poll_ms, final File lockFile)
abstract boolean tryLockImpl()
abstract String getName()
synchronized boolean tryLock(long maxwait)
Blocking until the lock is acquired by this Thread or maxwait in ms is reached.
static final boolean DEBUG
SingletonInstance(final long poll_ms)
final long getPollPeriod()
abstract boolean unlockImpl()
static SingletonInstance createFileLock(final long poll_ms, final String lockFileBasename)
String infoPrefix(final long currentMillis)
static SingletonInstance createServerSocket(final long poll_ms, final int portNumber)
A user shall use ephemeral ports:
synchronized boolean isLocked()
Query if locked.
void unlock()
Release the lock.
synchronized void lock()
Blocking until the lock is acquired by this Thread or TIMEOUT is reached.
Specifying a thread blocking lock implementation.
static final long TIMEOUT
The TIMEOUT for lock() in ms, defaults to DEFAULT_TIMEOUT.