Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
RecursiveThreadGroupLockImpl01Unfairish.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) 2010 Gothel Software e.K.
5 * Copyright (c) 2010 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 */
26package jau.test.util.parallel.locks.impl;
27
28import java.util.Arrays;
29
30import jau.test.util.parallel.locks.RecursiveThreadGroupLock;
31
35{
36 /* package */ @SuppressWarnings("serial")
37 static class ThreadGroupSync extends SingleThreadSync {
38 /* package */ ThreadGroupSync() {
39 super();
40 threadNum = 0;
41 threads = null;
42 holdCountAdditionOwner = 0;
43 waitingOrigOwner = null;
44 }
45 @Override
46 public final void incrHoldCount(final Thread t) {
47 super.incrHoldCount(t);
48 if(!isOriginalOwner(t)) {
49 holdCountAdditionOwner++;
50 }
51 }
52 @Override
53 public final void decrHoldCount(final Thread t) {
54 super.decrHoldCount(t);
55 if(!isOriginalOwner(t)) {
56 holdCountAdditionOwner--;
57 }
58 }
59 public final int getAdditionalOwnerHoldCount() {
60 return holdCountAdditionOwner;
61 }
62
63 public final boolean isOriginalOwner(final Thread t) {
64 return super.isOwner(t);
65 }
66 public final void setWaitingOrigOwner(final Thread origOwner) {
67 waitingOrigOwner = origOwner;
68 }
69 public final Thread getWaitingOrigOwner() {
70 return waitingOrigOwner;
71 }
72 @Override
73 public final boolean isOwner(final Thread t) {
74 if(getExclusiveOwnerThread()==t) {
75 return true;
76 }
77 for(int i=threadNum-1; 0<=i; i--) {
78 if(threads[i]==t) {
79 return true;
80 }
81 }
82 return false;
83 }
84
85 public final int getAddOwnerCount() {
86 return threadNum;
87 }
88 public final void addOwner(final Thread t) throws IllegalArgumentException {
89 if(null == threads) {
90 if(threadNum>0) {
91 throw new InternalError("XXX");
92 }
93 threads = new Thread[4];
94 }
95 for(int i=threadNum-1; 0<=i; i--) {
96 if(threads[i]==t) {
97 throw new IllegalArgumentException("Thread already added: "+t);
98 }
99 }
100 if (threadNum == threads.length) {
101 threads = Arrays.copyOf(threads, threadNum * 2);
102 }
103 threads[threadNum] = t;
104 threadNum++;
105 }
106
107 public final void removeAllOwners() {
108 for(int i=threadNum-1; 0<=i; i--) {
109 threads[i]=null;
110 }
111 threadNum=0;
112 }
113
114 public final void removeOwner(final Thread t) throws IllegalArgumentException {
115 for (int i = 0 ; i < threadNum ; i++) {
116 if (threads[i] == t) {
117 threadNum--;
118 System.arraycopy(threads, i + 1, threads, i, threadNum - i);
119 threads[threadNum] = null; // cleanup 'dead' [or duplicate] reference for GC
120 return;
121 }
122 }
123 throw new IllegalArgumentException("Not an owner: "+t);
124 }
125
126 String addOwnerToString() {
127 final StringBuilder sb = new StringBuilder();
128 for(int i=0; i<threadNum; i++) {
129 if(i>0) {
130 sb.append(", ");
131 }
132 sb.append(threads[i].getName());
133 }
134 return sb.toString();
135 }
136
137 // lock count by addition owner threads
138 private int holdCountAdditionOwner;
139 private Thread[] threads;
140 private int threadNum;
141 private Thread waitingOrigOwner;
142 }
143
145 super(new ThreadGroupSync());
146 }
147
148 @Override
149 public final boolean isOriginalOwner() {
150 return isOriginalOwner(Thread.currentThread());
151 }
152
153 @Override
154 public final boolean isOriginalOwner(final Thread thread) {
155 synchronized(sync) {
156 return ((ThreadGroupSync)sync).isOriginalOwner(thread) ;
157 }
158 }
159
160 @Override
161 public final void addOwner(final Thread t) throws RuntimeException, IllegalArgumentException {
163 final Thread cur = Thread.currentThread();
164 final ThreadGroupSync tgSync = (ThreadGroupSync)sync;
165 if(!tgSync.isOriginalOwner(cur)) {
166 throw new IllegalArgumentException("Current thread is not the original owner: orig-owner: "+tgSync.getOwner()+", current "+cur+": "+toString());
167 }
168 if(tgSync.isOriginalOwner(t)) {
169 throw new IllegalArgumentException("Passed thread is original owner: "+t+", "+toString());
170 }
171 tgSync.addOwner(t);
172 }
173
174 @Override
175 public final void unlock(final Runnable taskAfterUnlockBeforeNotify) {
176 synchronized(sync) {
177 final Thread cur = Thread.currentThread();
178 final ThreadGroupSync tgSync = (ThreadGroupSync)sync;
179
180 if( tgSync.getAddOwnerCount()>0 ) {
181 if(TRACE_LOCK) {
182 System.err.println("--- LOCK XR (tg) "+toString()+", cur "+threadName(cur)+" -> owner...");
183 }
184 if( tgSync.isOriginalOwner(cur) ) {
185 // original locking owner thread
186 if( tgSync.getHoldCount() - tgSync.getAdditionalOwnerHoldCount() == 1 ) {
187 // release orig. lock
188 tgSync.setWaitingOrigOwner(cur);
189 try {
190 while ( tgSync.getAdditionalOwnerHoldCount() > 0 ) {
191 try {
192 sync.wait();
193 } catch (final InterruptedException e) {
194 // regular wake up!
195 }
196 }
197 } finally {
198 tgSync.setWaitingOrigOwner(null);
199 Thread.interrupted(); // clear slipped interrupt
200 }
201 tgSync.removeAllOwners();
202 }
203 } else if( tgSync.getAdditionalOwnerHoldCount() == 1 ) {
204 // last additional owner thread wakes up original owner if waiting in unlock(..)
205 final Thread originalOwner = tgSync.getWaitingOrigOwner();
206 if( null != originalOwner ) {
207 originalOwner.interrupt();
208 }
209 }
210 }
211 if(TRACE_LOCK) {
212 System.err.println("++ unlock(X): currentThread "+cur.getName()+", lock: "+this.toString());
213 System.err.println("--- LOCK X0 (tg) "+toString()+", cur "+threadName(cur)+" -> unlock!");
214 }
215 super.unlock(taskAfterUnlockBeforeNotify);
216 }
217 }
218
219 @Override
220 public final void removeOwner(final Thread t) throws RuntimeException, IllegalArgumentException {
222 ((ThreadGroupSync)sync).removeOwner(t);
223 }
224
225 @Override
226 public String toString() {
227 final ThreadGroupSync tgSync = (ThreadGroupSync)sync;
228 final int hc = sync.getHoldCount();
229 final int addHC = tgSync.getAdditionalOwnerHoldCount();
230 return syncName()+"[count "+hc+" [ add. "+addHC+", orig "+(hc-addHC)+
231 "], qsz "+sync.getQSz()+", owner "+threadName(sync.getOwner())+", add.owner "+tgSync.addOwnerToString()+"]";
232 }
233}
Reentrance locking toolkit, impl a non-complete fair FIFO scheduler.
final boolean isOwner(final Thread thread)
Query whether the lock is hold by the given thread.
final boolean isOriginalOwner(final Thread thread)
Returns true if the passed thread is the original lock owner, ie.
final void unlock(final Runnable taskAfterUnlockBeforeNotify)
Execute the Runnable taskAfterUnlockBeforeNotify while holding the exclusive lock.
final boolean isOriginalOwner()
Returns true if the current thread is the original lock owner, ie.
final void addOwner(final Thread t)
Add a thread to the list of additional lock owners, which enables them to recursively claim this lock...
final void removeOwner(final Thread t)
Remove a thread from the list of additional lock owner threads.
static final boolean TRACE_LOCK
Enable via the property jogamp.debug.Lock.TraceLock
Definition: Lock.java:40
Reentrance capable locking toolkit, supporting multiple threads as owner.