aboutsummaryrefslogtreecommitdiffstats
path: root/src/newt
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2011-02-26 21:43:20 +0100
committerSven Gothel <[email protected]>2011-02-26 21:43:20 +0100
commit76444dce2b678a7f6769564abac4f8a73f414609 (patch)
tree09358c0a48715c69e7e8f511cf3d9be729177509 /src/newt
parent77546f8968779fbdcfe58f89c6924803642889c7 (diff)
Clean/Fix: Threading Code
- Remove unsafe double checked locking - Annotate safe double checked locking (volatile) - use 'static final' if possible
Diffstat (limited to 'src/newt')
-rw-r--r--src/newt/classes/com/jogamp/newt/NewtVersion.java2
-rw-r--r--src/newt/classes/com/jogamp/newt/util/MainThread.java37
-rw-r--r--src/newt/classes/jogamp/newt/DefaultEDTUtil.java45
3 files changed, 43 insertions, 41 deletions
diff --git a/src/newt/classes/com/jogamp/newt/NewtVersion.java b/src/newt/classes/com/jogamp/newt/NewtVersion.java
index a38dca34c..961ffdf6a 100644
--- a/src/newt/classes/com/jogamp/newt/NewtVersion.java
+++ b/src/newt/classes/com/jogamp/newt/NewtVersion.java
@@ -43,7 +43,7 @@ public class NewtVersion extends JogampVersion {
}
public static NewtVersion getInstance() {
- if(null == jogampCommonVersionInfo) {
+ if(null == jogampCommonVersionInfo) { // volatile: ok
synchronized(NewtVersion.class) {
if( null == jogampCommonVersionInfo ) {
final String packageName = "com.jogamp.newt";
diff --git a/src/newt/classes/com/jogamp/newt/util/MainThread.java b/src/newt/classes/com/jogamp/newt/util/MainThread.java
index 9f66fefe6..8bb725b99 100644
--- a/src/newt/classes/com/jogamp/newt/util/MainThread.java
+++ b/src/newt/classes/com/jogamp/newt/util/MainThread.java
@@ -91,25 +91,25 @@ import jogamp.newt.awt.AWTEDTUtil;
* Which starts 4 threads, each with a window and OpenGL rendering.<br>
*/
public class MainThread implements EDTUtil {
- private static AccessControlContext localACC = AccessController.getContext();
+ private static final AccessControlContext localACC = AccessController.getContext();
public static final boolean MAIN_THREAD_CRITERIA = ( !NativeWindowFactory.isAWTAvailable() &&
NativeWindowFactory.TYPE_MACOSX.equals(NativeWindowFactory.getNativeWindowType(false))
) || Debug.getBooleanProperty("newt.MainThread.force", true, localACC);
protected static final boolean DEBUG = Debug.debug("MainThread");
- private static MainThread singletonMainThread = new MainThread(); // one singleton MainThread
+ private static final MainThread singletonMainThread = new MainThread(); // one singleton MainThread
private static boolean isExit=false;
private static volatile boolean isRunning=false;
- private static Object taskWorkerLock=new Object();
+ private static final Object taskWorkerLock=new Object();
private static boolean shouldStop;
private static ArrayList tasks;
private static Thread mainThread;
private static Timer pumpMessagesTimer=null;
private static TimerTask pumpMessagesTimerTask=null;
- private static Map/*<Display, Runnable>*/ pumpMessageDisplayMap = new HashMap();
+ private static final Map/*<Display, Runnable>*/ pumpMessageDisplayMap = new HashMap();
private static boolean useMainThread = false;
@@ -124,6 +124,7 @@ public class MainThread implements EDTUtil {
this.mainClassArgs=mainClassArgs;
}
+ @Override
public void run() {
if ( useMainThread ) {
// we have to start first to provide the service ..
@@ -205,7 +206,7 @@ public class MainThread implements EDTUtil {
}
}
- public static final MainThread getSingleton() {
+ public static MainThread getSingleton() {
return singletonMainThread;
}
@@ -219,24 +220,20 @@ public class MainThread implements EDTUtil {
if ( useMainThread ) {
return; // error ?
}
- if(null == pumpMessagesTimer) {
- synchronized (MainThread.class) {
- if(null == pumpMessagesTimer) {
- pumpMessagesTimer = new Timer();
- pumpMessagesTimerTask = new TimerTask() {
- public void run() {
- synchronized(pumpMessageDisplayMap) {
- for(Iterator i = pumpMessageDisplayMap.values().iterator(); i.hasNext(); ) {
- ((Runnable) i.next()).run();
- }
+ synchronized (pumpMessageDisplayMap) {
+ if(null == pumpMessagesTimer) {
+ pumpMessagesTimer = new Timer();
+ pumpMessagesTimerTask = new TimerTask() {
+ public void run() {
+ synchronized(pumpMessageDisplayMap) {
+ for(Iterator i = pumpMessageDisplayMap.values().iterator(); i.hasNext(); ) {
+ ((Runnable) i.next()).run();
}
}
- };
- pumpMessagesTimer.scheduleAtFixedRate(pumpMessagesTimerTask, 0, defaultEDTPollGranularity);
- }
+ }
+ };
+ pumpMessagesTimer.scheduleAtFixedRate(pumpMessagesTimerTask, 0, defaultEDTPollGranularity);
}
- }
- synchronized(pumpMessageDisplayMap) {
pumpMessageDisplayMap.put(dpy, pumpMessage);
}
}
diff --git a/src/newt/classes/jogamp/newt/DefaultEDTUtil.java b/src/newt/classes/jogamp/newt/DefaultEDTUtil.java
index 016906581..3b14f30fc 100644
--- a/src/newt/classes/jogamp/newt/DefaultEDTUtil.java
+++ b/src/newt/classes/jogamp/newt/DefaultEDTUtil.java
@@ -54,7 +54,7 @@ public class DefaultEDTUtil implements EDTUtil {
public DefaultEDTUtil(ThreadGroup tg, String name, Runnable dispatchMessages) {
this.threadGroup = tg;
- this.name=new String(Thread.currentThread().getName()+"-"+name+"-EDT-");
+ this.name=Thread.currentThread().getName()+"-"+name+"-EDT-";
this.dispatchMessages=dispatchMessages;
this.edt = new EventDispatchThread(threadGroup, name);
this.edt.setDaemon(true); // don't stop JVM from shutdown ..
@@ -113,7 +113,7 @@ public class DefaultEDTUtil implements EDTUtil {
invokeImpl(wait, task, false);
}
- private final void invokeImpl(boolean wait, Runnable task, boolean stop) {
+ private void invokeImpl(boolean wait, Runnable task, boolean stop) {
if(task == null) {
throw new RuntimeException("Null Runnable");
}
@@ -192,30 +192,33 @@ public class DefaultEDTUtil implements EDTUtil {
}
final public void waitUntilIdle() {
- if(edt.isRunning() && edt != Thread.currentThread()) {
- synchronized(edt.tasks) {
- while(edt.isRunning() && edt.tasks.size()>0) {
- try {
- edt.tasks.notifyAll();
- edt.tasks.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
+ final EventDispatchThread _edt;
+ synchronized(edtLock) {
+ _edt = edt;
+ }
+ if(!_edt.isRunning() || _edt == Thread.currentThread()) {
+ return;
+ }
+ synchronized(_edt.tasks) {
+ while(_edt.isRunning() && _edt.tasks.size()>0) {
+ try {
+ _edt.tasks.notifyAll();
+ _edt.tasks.wait();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
}
}
}
}
final public void waitUntilStopped() {
- if(edt.isRunning() && edt != Thread.currentThread() ) {
- synchronized(edtLock) {
- if(edt.isRunning() && edt != Thread.currentThread() ) {
- while(edt.isRunning()) {
- try {
- edtLock.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
+ synchronized(edtLock) {
+ if(edt.isRunning() && edt != Thread.currentThread() ) {
+ while(edt.isRunning()) {
+ try {
+ edtLock.wait();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
}
}
}
@@ -235,6 +238,7 @@ public class DefaultEDTUtil implements EDTUtil {
return isRunning;
}
+ @Override
final public void start() throws IllegalThreadStateException {
isRunning = true;
super.start();
@@ -244,6 +248,7 @@ public class DefaultEDTUtil implements EDTUtil {
* Utilizing locking only on tasks and its execution,
* not for event dispatching.
*/
+ @Override
final public void run() {
if(DEBUG) {
System.err.println(getName()+": EDT run() START "+ getName());