Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
RecursiveThreadGroupLock.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 */
26package jau.test.util.parallel.locks;
27
28/**
29 * Reentrance capable locking toolkit, supporting multiple threads as owner.
30 * <p>
31 * See use case description at {@link #addOwner(Thread)}.
32 * </p>
33 */
34public interface RecursiveThreadGroupLock extends RecursiveLock {
35 /**
36 * Returns true if the current thread is the original lock owner, ie.
37 * successfully claimed this lock the first time, ie. {@link #getHoldCount()} == 1.
38 */
39 boolean isOriginalOwner();
40
41 /**
42 * Returns true if the passed thread is the original lock owner, ie.
43 * successfully claimed this lock the first time, ie. {@link #getHoldCount()} == 1.
44 */
45 boolean isOriginalOwner(Thread thread);
46
47 /**
48 * Add a thread to the list of additional lock owners, which enables them to recursively claim this lock.
49 * <p>
50 * The caller must hold this lock and be the original lock owner, see {@link #isOriginalOwner()}.
51 * </p>
52 * <p>
53 * If the original owner releases this lock via {@link #unlock()}
54 * all additional lock owners are released as well.
55 * This ensures consistency of spawn off additional lock owner threads and it's release.
56 * </p>
57 * Use case:
58 * <pre>
59 * Thread2 thread2 = new Thread2();
60 *
61 * Thread1 {
62 *
63 * // Claim this lock and become the original lock owner.
64 * lock.lock();
65 *
66 * try {
67 *
68 * // Allow Thread2 to claim the lock, ie. make thread2 an additional lock owner
69 * addOwner(thread2);
70 *
71 * // Start thread2
72 * thread2.start();
73 *
74 * // Wait until thread2 has finished requiring this lock, but keep thread2 running
75 * while(!thread2.waitForResult()) sleep();
76 *
77 * // Optional: Only if sure that this thread doesn't hold the lock anymore,
78 * // otherwise just release the lock via unlock().
79 * removeOwner(thread2);
80 *
81 * } finally {
82 *
83 * // Release this lock and remove all additional lock owners.
84 * // Implicit wait until thread2 gets off the lock.
85 * lock.unlock();
86 *
87 * }
88 *
89 * }.start();
90 * </pre>
91 *
92 * @param t the thread to be added to the list of additional owning threads
93 * @throws RuntimeException if the current thread does not hold the lock.
94 * @throws IllegalArgumentException if the passed thread is the lock owner or already added.
95 *
96 * @see #removeOwner(Thread)
97 * @see #unlock()
98 * @see #lock()
99 */
100 void addOwner(Thread t) throws RuntimeException, IllegalArgumentException;
101
102 /**
103 * Remove a thread from the list of additional lock owner threads.
104 * <p>
105 * The caller must hold this lock and be the original lock owner, see {@link #isOriginalOwner()}.
106 * </p>
107 * <p>
108 * Only use this method if sure that the thread doesn't hold the lock anymore.
109 * </p>
110 *
111 * @param t the thread to be removed from the list of additional owning threads
112 * @throws RuntimeException if the current thread does not hold the lock.
113 * @throws IllegalArgumentException if the passed thread is not added by {@link #addOwner(Thread)}
114 */
115 void removeOwner(Thread t) throws RuntimeException, IllegalArgumentException;
116
117 /**
118 * <p>
119 * Wait's until all additional owners released this lock before releasing it.
120 * </p>
121 *
122 * {@inheritDoc}
123 */
124 @Override
125 void unlock() throws RuntimeException;
126
127 /**
128 * <p>
129 * Wait's until all additional owners released this lock before releasing it.
130 * </p>
131 *
132 * {@inheritDoc}
133 */
134 @Override
135 void unlock(Runnable taskAfterUnlockBeforeNotify);
136
137}
Reentrance capable locking toolkit.
Reentrance capable locking toolkit, supporting multiple threads as owner.
void removeOwner(Thread t)
Remove a thread from the list of additional lock owner threads.
boolean isOriginalOwner(Thread thread)
Returns true if the passed thread is the original lock owner, ie.
boolean isOriginalOwner()
Returns true if the current thread is the original lock owner, ie.
void addOwner(Thread t)
Add a thread to the list of additional lock owners, which enables them to recursively claim this lock...