Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
SingletonInstance.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2011 Gothel Software e.K.
5 * Copyright (c) 2011 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27package jau.test.util.parallel.locks;
28
29import java.io.File;
30
31import jau.test.util.parallel.locks.impl.SingletonInstanceFileLock;
32import jau.test.util.parallel.locks.impl.SingletonInstanceServerSocket;
33
34public abstract class SingletonInstance implements Lock {
35
36 protected static final boolean DEBUG = true;
37
38 public static SingletonInstance createFileLock(final long poll_ms, final String lockFileBasename) {
39 return new SingletonInstanceFileLock(poll_ms, lockFileBasename);
40 }
41
42 public static SingletonInstance createFileLock(final long poll_ms, final File lockFile) {
43 return new SingletonInstanceFileLock(poll_ms, lockFile);
44 }
45
46 /**
47 * A user shall use <b>ephemeral ports</b>:
48 * <ul>
49 * <li>IANA suggests 49152 to 65535 as "dynamic and/or private ports".</li>
50 * <li>Many GNU/Linux kernels use 32768 to 61000.</li>
51 * <li>FreeBSD >= 4.6 uses the IANA port range.</li>
52 * <li>FreeBSD < 4.6 and BSD use ports 1024 through 4999.</li>
53 * <li>Microsoft Windows operating systems through Server 2003 use the range 1025 to 5000</li>
54 * <li>Windows Vista, Windows 7, and Server 2008 use the IANA range.</li>
55 * </ul>
56 * @param pollPeriod
57 * @param portNumber to be used for this single instance server socket.
58 */
59 public static SingletonInstance createServerSocket(final long poll_ms, final int portNumber) {
60 return new SingletonInstanceServerSocket(poll_ms, portNumber);
61 }
62
63 protected SingletonInstance(final long poll_ms) {
64 this.poll_ms = Math.max(10, poll_ms);
65 }
66
67 public final long getPollPeriod() { return poll_ms; }
68 public abstract String getName();
69 @Override
70 public final String toString() { return getName(); }
71
72 @Override
73 public synchronized void lock() throws RuntimeException {
74 try {
75 do {
76 if(tryLock(TIMEOUT)) {
77 return;
78 }
79 } while ( true ) ;
80 } catch ( final RuntimeException ie ) {
81 throw new RuntimeException(ie);
82 }
83 }
84
85 @Override
86 public synchronized boolean tryLock(long maxwait) throws RuntimeException {
87 if(locked) {
88 return true;
89 }
90 final long t0 = System.currentTimeMillis();
91 int i=0;
92 try {
93 do {
94 final long t1 = System.currentTimeMillis();
95 locked = tryLockImpl();
96 if(locked) {
97 if( DEBUG ) {
98 final long t2 = System.currentTimeMillis();
99 System.err.println(infoPrefix(t2)+" +++ "+getName()+" - Locked within "+(t2-t0)+" ms, "+(i+1)+" attempts");
100 }
101 return true;
102 }
103 if( DEBUG && 0==i ) {
104 System.err.println(infoPrefix(System.currentTimeMillis())+" III "+getName()+" - Wait for lock");
105 }
106 Thread.sleep(poll_ms);
107 maxwait -= System.currentTimeMillis()-t1;
108 i++;
109 } while ( 0 < maxwait ) ;
110 } catch ( final InterruptedException ie ) {
111 final long t2 = System.currentTimeMillis();
112 throw new RuntimeException(infoPrefix(t2)+" EEE (1) "+getName()+" - couldn't get lock within "+(t2-t0)+" ms, "+i+" attempts", ie);
113 }
114 if( DEBUG ) {
115 final long t2 = System.currentTimeMillis();
116 System.err.println(infoPrefix(t2)+" +++ EEE (2) "+getName()+" - couldn't get lock within "+(t2-t0)+" ms, "+i+" attempts");
117 }
118 return false;
119 }
120 protected abstract boolean tryLockImpl();
121
122 @Override
123 public void unlock() throws RuntimeException {
124 final long t0 = System.currentTimeMillis();
125 if(locked) {
126 locked = !unlockImpl();
127 if( DEBUG ) {
128 final long t2 = System.currentTimeMillis();
129 System.err.println(infoPrefix(t2)+" --- "+getName()+" - Unlock "+ ( locked ? "failed" : "ok" ) + " within "+(t2-t0)+" ms");
130 }
131 }
132 }
133 protected abstract boolean unlockImpl();
134
135 @Override
136 public synchronized boolean isLocked() {
137 return locked;
138 }
139
140 protected String infoPrefix(final long currentMillis) {
141 return "SLOCK [T "+Thread.currentThread().getName()+" @ "+currentMillis+" ms";
142 }
143 protected String infoPrefix() {
144 return infoPrefix(System.currentTimeMillis());
145 }
146
147 private final long poll_ms;
148 private boolean locked = false;
149}
synchronized void lock()
Blocking until the lock is acquired by this Thread or TIMEOUT is reached.
static SingletonInstance createServerSocket(final long poll_ms, final int portNumber)
A user shall use ephemeral ports:
static SingletonInstance createFileLock(final long poll_ms, final String lockFileBasename)
synchronized boolean tryLock(long maxwait)
Blocking until the lock is acquired by this Thread or maxwait in ms is reached.
synchronized boolean isLocked()
Query if locked.
static SingletonInstance createFileLock(final long poll_ms, final File lockFile)
Specifying a thread blocking lock implementation.
Definition: Lock.java:34
static final long TIMEOUT
The TIMEOUT for lock() in ms, defaults to DEFAULT_TIMEOUT.
Definition: Lock.java:52