Mercurial > projects > dwt2
annotate org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/internal/Lock.d @ 51:c01d033c633a
[swt lin]
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 27 Mar 2009 19:58:06 +0100 |
parents | 65761bc28ab2 |
children | fb3aa8075988 |
rev | line source |
---|---|
25 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2005 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.internal.Lock; | |
14 | |
15 import java.lang.all; | |
26 | 16 import java.lang.Thread; |
25 | 17 |
26 | 18 version(Tango){ |
19 import tango.core.sync.Mutex; | |
20 import tango.core.sync.Condition; | |
21 } else { // Phobos | |
22 } | |
25 | 23 |
24 /** | |
25 * Instance of this represent a recursive monitor. | |
26 */ | |
27 public class Lock { | |
28 int count, waitCount; | |
29 Thread owner; | |
26 | 30 version(Tango){ |
31 Mutex mutex; | |
32 Condition cond; | |
33 } else { // Phobos | |
34 } | |
25 | 35 |
36 public this() { | |
51 | 37 version(Tango){ |
38 mutex = new Mutex; | |
39 cond = new Condition(mutex); | |
40 } else { // Phobos | |
41 implMissing( __FILE__, __LINE__ ); | |
42 } | |
25 | 43 } |
44 /** | |
45 * Locks the monitor and returns the lock count. If | |
46 * the lock is owned by another thread, wait until | |
47 * the lock is released. | |
48 * | |
49 * @return the lock count | |
50 */ | |
51 public int lock() { | |
26 | 52 version(Tango){ |
53 synchronized (mutex) { | |
54 Thread current = Thread.currentThread(); | |
55 if (owner !is current) { | |
56 waitCount++; | |
57 while (count > 0) { | |
58 try { | |
59 cond.wait(); | |
60 } catch (SyncException e) { | |
61 /* Wait forever, just like synchronized blocks */ | |
62 } | |
25 | 63 } |
26 | 64 --waitCount; |
65 owner = current; | |
25 | 66 } |
26 | 67 return ++count; |
25 | 68 } |
26 | 69 } else { // Phobos |
70 implMissing( __FILE__, __LINE__ ); | |
51 | 71 return 0; |
25 | 72 } |
73 } | |
74 | |
75 /** | |
76 * Unlocks the monitor. If the current thread is not | |
77 * the monitor owner, do nothing. | |
78 */ | |
79 public void unlock() { | |
26 | 80 version(Tango){ |
81 synchronized (mutex) { | |
47
65761bc28ab2
swt linux again compilable for d1.
Frank Benoit <benoit@tionex.de>
parents:
26
diff
changeset
|
82 Thread current = Thread.currentThread(); |
26 | 83 if (owner is current) { |
84 if (--count is 0) { | |
85 owner = null; | |
86 if (waitCount > 0) cond.notifyAll(); | |
87 } | |
25 | 88 } |
89 } | |
26 | 90 } else { // Phobos |
91 implMissing( __FILE__, __LINE__ ); | |
25 | 92 } |
93 } | |
94 } |