Mercurial > projects > dwt2
diff org.eclipse.core.jobs/src/org/eclipse/core/internal/jobs/Semaphore.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children | 6f068362a363 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/org.eclipse.core.jobs/src/org/eclipse/core/internal/jobs/Semaphore.d Sat Mar 14 18:23:29 2009 +0100 @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2003, 2006 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Port to the D programming language: + * Frank Benoit <benoit@tionex.de> + *******************************************************************************/ +module org.eclipse.core.internal.jobs.Semaphore; + +import java.lang.JThread; +import tango.core.sync.Mutex; +import tango.core.sync.Condition; +import java.lang.all; +import tango.text.convert.Format; + +public class Semaphore { + protected long notifications; + protected JThread runnable; + + private Mutex mutex; + private Condition condition; + + public this(JThread runnable) { + mutex = new Mutex; + condition = new Condition(mutex); + this.runnable = runnable; + notifications = 0; + } + + /** + * Attempts to acquire this semaphore. Returns true if it was successfully acquired, + * and false otherwise. + */ + public bool acquire(long delay) { + synchronized(mutex){ + implMissing( __FILE__, __LINE__ ); +// SWT +// if (Thread.interrupted()) +// throw new InterruptedException(); + long start = System.currentTimeMillis(); + long timeLeft = delay; + while (true) { + if (notifications > 0) { + notifications--; + return true; + } + if (timeLeft <= 0) + return false; + condition.wait(timeLeft/1000.0f); + timeLeft = start + delay - System.currentTimeMillis(); + } + } + } + + public override int opEquals(Object obj) { + return (runnable is (cast(Semaphore) obj).runnable); + } + + public override hash_t toHash() { + return runnable is null ? 0 : (cast(Object)runnable).toHash(); + } + + public void release() { + synchronized( mutex ){ + notifications++; + condition.notifyAll(); + } + } + + // for debug only + public String toString() { + return Format("Semaphore({})", cast(Object) runnable ); //$NON-NLS-1$ //$NON-NLS-2$ + } +}