1 /******************************************************************************* 2 * Copyright (c) 2000, 2007 IBM Corporation and others. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * IBM Corporation - initial API and implementation 10 * Port to the D programming language: 11 * Frank Benoit <benoit@tionex.de> 12 *******************************************************************************/ 13 module org.eclipse.swt.widgets.RunnableLock; 14 15 import java.lang.all; 16 17 import java.lang.Thread; 18 version(Tango){ 19 import tango.core.sync.Condition; 20 import tango.core.sync.Mutex; 21 } else { // Phobos 22 import java.nonstandard.sync.condition; 23 import java.nonstandard.sync.mutex; 24 } 25 26 /** 27 * Instances of this class are used to ensure that an 28 * application cannot interfere with the locking mechanism 29 * used to implement asynchronous and synchronous communication 30 * between widgets and background threads. 31 */ 32 33 class RunnableLock : Mutex { 34 Runnable runnable; 35 Thread thread; 36 Exception throwable; 37 38 Condition cond; 39 bool syncExec; 40 41 this (Runnable runnable, bool syncExec) { 42 this.runnable = runnable; 43 this.cond = new Condition(this); 44 this.syncExec = syncExec; 45 } 46 47 ~this () { 48 // Release handle of Condition. 49 // If not released here, the handle will not be released until the next GC works. 50 destroy(this.cond); 51 } 52 53 bool done () { 54 return runnable is null || throwable !is null; 55 } 56 57 void run () { 58 if (runnable !is null) runnable.run (); 59 runnable = null; 60 } 61 62 void notifyAll(){ 63 cond.notifyAll(); 64 } 65 void wait(){ 66 cond.wait(); 67 } 68 69 }