Skip to content

Commit

Permalink
RecursiveThreadGroupLock: New recursive lock interface and impl, allo…
Browse files Browse the repository at this point in the history
…wing 'spawn off' process to become the lock owner.

To avoid complicated synchronization via synchronized, wait and notify between one thread
and a 'spawn' off thread which temporarly requires the hold lock,
RecursiveThreadGroupLock allows to add and remove other threads to become owners of the lock
as if they were the original holder.

This simplifies some rare locking use cases, eg. in JOGL's GLProfile initialization sequence
where a SharedResourceRunner thread is taking over initialization of shared resources.
  • Loading branch information
sgothel committed Dec 11, 2011
1 parent 8dc2ad6 commit 4374d11
Show file tree
Hide file tree
Showing 5 changed files with 686 additions and 2 deletions.
3 changes: 2 additions & 1 deletion make/scripts/runtest.sh
Expand Up @@ -55,6 +55,7 @@ function onetest() {
#onetest com.jogamp.common.util.TestVersionInfo 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.TestIteratorIndexCORE 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.locks.TestRecursiveLock01 2>&1 | tee -a $LOG
onetest com.jogamp.common.util.locks.TestRecursiveThreadGroupLock01 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.TestArrayHashSet01 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.IntIntHashMapTest 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.IntObjectHashMapTest 2>&1 | tee -a $LOG
Expand All @@ -64,7 +65,7 @@ function onetest() {
#onetest com.jogamp.common.nio.TestPointerBufferEndian 2>&1 | tee -a $LOG
#onetest com.jogamp.common.nio.TestStructAccessorEndian 2>&1 | tee -a $LOG
#onetest com.jogamp.gluegen.test.junit.generation.Test1p1JavaEmitter 2>&1 | tee -a $LOG
onetest com.jogamp.gluegen.test.junit.generation.Test1p2ProcAddressEmitter 2>&1 | tee -a $LOG
#onetest com.jogamp.gluegen.test.junit.generation.Test1p2ProcAddressEmitter 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.TestPlatform01 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.TestRunnableTask01 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.TestIOUtil01 2>&1 | tee -a $LOG
Expand Down
10 changes: 9 additions & 1 deletion src/java/com/jogamp/common/util/locks/LockFactory.java
Expand Up @@ -30,11 +30,12 @@
import jogamp.common.util.locks.RecursiveLockImpl01CompleteFair;
import jogamp.common.util.locks.RecursiveLockImpl01Unfairish;
import jogamp.common.util.locks.RecursiveLockImplJava5;
import jogamp.common.util.locks.RecursiveThreadGroupLockImpl01Unfairish;

public class LockFactory {

public enum ImplType {
Int01(0), Java5(1);
Int01(0), Java5(1), Int02ThreadGroup(2);

public final int id;

Expand All @@ -48,12 +49,19 @@ public static RecursiveLock createRecursiveLock() {
return new RecursiveLockImpl01Unfairish();
}

/** default is ImplType.Int02ThreadGroup, unfair'ish (fastest w/ least deviation) */
public static RecursiveThreadGroupLock createRecursiveThreadGroupLock() {
return new RecursiveThreadGroupLockImpl01Unfairish();
}

public static RecursiveLock createRecursiveLock(ImplType t, boolean fair) {
switch(t) {
case Int01:
return fair ? new RecursiveLockImpl01CompleteFair() : new RecursiveLockImpl01Unfairish();
case Java5:
return new RecursiveLockImplJava5(fair);
case Int02ThreadGroup:
return new RecursiveThreadGroupLockImpl01Unfairish();
}
throw new InternalError("XXX");
}
Expand Down
137 changes: 137 additions & 0 deletions src/java/com/jogamp/common/util/locks/RecursiveThreadGroupLock.java
@@ -0,0 +1,137 @@
/**
* Copyright 2011 JogAmp Community. All rights reserved.
*
* 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
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* 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 com.jogamp.common.util.locks;

/**
* Reentrance capable locking toolkit, supporting multiple threads as owner.
* <p>
* See use case description at {@link #addOwner(Thread)}.
* </p>
*/
public interface RecursiveThreadGroupLock extends RecursiveLock {
/**
* Returns true if the current thread is the original lock owner, ie.
* successfully claimed this lock the first time, ie. {@link #getHoldCount()} == 1.
*/
boolean isOriginalOwner();

/**
* Returns true if the passed thread is the original lock owner, ie.
* successfully claimed this lock the first time, ie. {@link #getHoldCount()} == 1.
*/
boolean isOriginalOwner(Thread thread);

/**
* Add a thread to the list of additional lock owners, which enables them to recursively claim this lock.
* <p>
* The caller must hold this lock and be the original lock owner, see {@link #isOriginalOwner()}.
* </p>
* <p>
* If the original owner releases this lock via {@link #unlock()}
* all additional lock owners are released as well.
* This ensures consistency of spawn off additional lock owner threads and it's release.
* </p>
* Use case:
* <pre>
* Thread2 thread2 = new Thread2();
*
* Thread1 {
*
* // Claim this lock and become the original lock owner.
* lock.lock();
*
* try {
*
* // Allow Thread2 to claim the lock, ie. make thread2 an additional lock owner
* addOwner(thread2);
*
* // Start thread2
* thread2.start();
*
* // Wait until thread2 has finished requiring this lock, but keep thread2 running
* while(!thread2.waitForResult()) sleep();
*
* // Optional: Only if sure that this thread doesn't hold the lock anymore,
* // otherwise just release the lock via unlock().
* removeOwner(thread2);
*
* } finally {
*
* // Release this lock and remove all additional lock owners.
* // Implicit wait until thread2 gets off the lock.
* lock.unlock();
*
* }
*
* }.start();
* </pre>
*
* @param t the thread to be added to the list of additional owning threads
* @throws RuntimeException if the current thread does not hold the lock.
* @throws IllegalArgumentException if the passed thread is the lock owner or already added.
*
* @see #removeOwner(Thread)
* @see #unlock()
* @see #lock()
*/
void addOwner(Thread t) throws RuntimeException, IllegalArgumentException;

/**
* Remove a thread from the list of additional lock owner threads.
* <p>
* The caller must hold this lock and be the original lock owner, see {@link #isOriginalOwner()}.
* </p>
* <p>
* Only use this method if sure that the thread doesn't hold the lock anymore.
* </p>
*
* @param t the thread to be removed from the list of additional owning threads
* @throws RuntimeException if the current thread does not hold the lock.
* @throws IllegalArgumentException if the passed thread is not added by {@link #addOwner(Thread)}
*/
void removeOwner(Thread t) throws RuntimeException, IllegalArgumentException;

/**
* <p>
* Wait's until all additional owners released this lock before releasing it.
* </p>
*
* {@inheritDoc}
*/
void unlock() throws RuntimeException;

/**
* <p>
* Wait's until all additional owners released this lock before releasing it.
* </p>
*
* {@inheritDoc}
*/
void unlock(Runnable taskAfterUnlockBeforeNotify);

}

0 comments on commit 4374d11

Please sign in to comment.