From 5e630e1a8060732234b291f4f19ab8c4e2d58d0d Mon Sep 17 00:00:00 2001 From: athomas Date: Sat, 28 Jun 2003 03:11:07 +0000 Subject: mutex to provide thread safety git-svn-id: file:///home/mbien/NetBeansProjects/JOGAMP/joal-sync/git-svn/../svn-server-sync/joal/trunk@42 03bf7f67-59de-4072-a415-9a990d468a3f --- src/java/net/java/games/joal/ALC.java | 2 ++ src/java/net/java/games/joal/ALCImpl.java | 52 +++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 17 deletions(-) (limited to 'src/java/net') diff --git a/src/java/net/java/games/joal/ALC.java b/src/java/net/java/games/joal/ALC.java index bb53bc5..2da7f2d 100644 --- a/src/java/net/java/games/joal/ALC.java +++ b/src/java/net/java/games/joal/ALC.java @@ -91,6 +91,8 @@ public interface ALC extends ALCConstants { */ public int alcMakeContextCurrent(Context context); + public void alcFreeCurrentContext(); + /** * This method tells a context to begin processing.
*
diff --git a/src/java/net/java/games/joal/ALCImpl.java b/src/java/net/java/games/joal/ALCImpl.java index d621a0a..6e7ccc5 100644 --- a/src/java/net/java/games/joal/ALCImpl.java +++ b/src/java/net/java/games/joal/ALCImpl.java @@ -37,16 +37,16 @@ import java.util.HashMap; final class ALCImpl implements ALC { private final HashMap contextMap = new HashMap(); - private static Object lock = new Object(); + private static Mutex lock = new Mutex(); ALCImpl() { System.loadLibrary("joal"); - Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { exit(); } })); + } public Device alcOpenDevice(String deviceName) { @@ -84,26 +84,18 @@ final class ALCImpl implements ALC { public int alcMakeContextCurrent(Context context) { int result = 0; - synchronized(lock) { - int pointer = 0; - if (context != null) { - pointer = context.pointer; - } - result = makeContextCurrentNative(pointer); - try { - lock.wait(); - } catch (InterruptedException e) { - result = 0; - } + int pointer = 0; + if (context != null) { + pointer = context.pointer; } + lock.acquire(); + result = makeContextCurrentNative(pointer); return result; } public void alcFreeCurrentContext() { - synchronized(lock) { - makeContextCurrentNative(0); - lock.notifyAll(); - } + makeContextCurrentNative(0); + lock.release(); } private native int makeContextCurrentNative(int pointer); @@ -204,4 +196,30 @@ final class ALCImpl implements ALC { alcCloseDevice(alcDevice); } } + + private static class Mutex { + Thread owner = null; + public synchronized void acquire() { + boolean interrupted = false; + while(owner != null) { + try { + wait(); + } catch (InterruptedException e) { + interrupted = true; + } + } + owner = Thread.currentThread(); + if(interrupted) { + owner.interrupt(); + } + } + + public synchronized void release() { + if(!owner.equals(Thread.currentThread())) { + throw new IllegalMonitorStateException("Not Owner"); + } + owner = null; + notify(); + } + } } -- cgit v1.2.3