comparison 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
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2003, 2006 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.core.internal.jobs.Semaphore;
14
15 import java.lang.JThread;
16 import tango.core.sync.Mutex;
17 import tango.core.sync.Condition;
18 import java.lang.all;
19 import tango.text.convert.Format;
20
21 public class Semaphore {
22 protected long notifications;
23 protected JThread runnable;
24
25 private Mutex mutex;
26 private Condition condition;
27
28 public this(JThread runnable) {
29 mutex = new Mutex;
30 condition = new Condition(mutex);
31 this.runnable = runnable;
32 notifications = 0;
33 }
34
35 /**
36 * Attempts to acquire this semaphore. Returns true if it was successfully acquired,
37 * and false otherwise.
38 */
39 public bool acquire(long delay) {
40 synchronized(mutex){
41 implMissing( __FILE__, __LINE__ );
42 // SWT
43 // if (Thread.interrupted())
44 // throw new InterruptedException();
45 long start = System.currentTimeMillis();
46 long timeLeft = delay;
47 while (true) {
48 if (notifications > 0) {
49 notifications--;
50 return true;
51 }
52 if (timeLeft <= 0)
53 return false;
54 condition.wait(timeLeft/1000.0f);
55 timeLeft = start + delay - System.currentTimeMillis();
56 }
57 }
58 }
59
60 public override int opEquals(Object obj) {
61 return (runnable is (cast(Semaphore) obj).runnable);
62 }
63
64 public override hash_t toHash() {
65 return runnable is null ? 0 : (cast(Object)runnable).toHash();
66 }
67
68 public void release() {
69 synchronized( mutex ){
70 notifications++;
71 condition.notifyAll();
72 }
73 }
74
75 // for debug only
76 public String toString() {
77 return Format("Semaphore({})", cast(Object) runnable ); //$NON-NLS-1$ //$NON-NLS-2$
78 }
79 }