Mercurial > projects > dwt-linux
annotate dwt/internal/Lock.d @ 54:8f049b136add
first example working
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 11 Jan 2008 14:31:37 +0100 |
parents | 63c023465156 |
children | 8cec8f536af3 |
rev | line source |
---|---|
0 | 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 *******************************************************************************/ | |
10
63c023465156
moved from org.eclipse.swt to dwt
Frank Benoit <benoit@tionex.de>
parents:
9
diff
changeset
|
11 module dwt.internal.Lock; |
0 | 12 |
13 import tango.core.Thread; | |
14 import tango.core.sync.Mutex; | |
15 import tango.core.sync.Condition; | |
16 | |
17 /** | |
18 * Instance of this represent a recursive monitor. | |
19 */ | |
20 public class Lock { | |
21 int count, waitCount; | |
22 Thread owner; | |
23 Mutex mutex; | |
24 Condition cond; | |
25 | |
54 | 26 public this() { |
0 | 27 mutex = new Mutex; |
28 cond = new Condition(mutex); | |
29 } | |
30 /** | |
31 * Locks the monitor and returns the lock count. If | |
32 * the lock is owned by another thread, wait until | |
33 * the lock is released. | |
34 * | |
35 * @return the lock count | |
36 */ | |
37 public int lock() { | |
38 synchronized (mutex) { | |
39 Thread current = Thread.getThis(); | |
54 | 40 if (owner !is current) { |
0 | 41 waitCount++; |
42 while (count > 0) { | |
43 try { | |
44 cond.wait(); | |
45 } catch (SyncException e) { | |
46 /* Wait forever, just like synchronized blocks */ | |
47 } | |
48 } | |
49 --waitCount; | |
50 owner = current; | |
51 } | |
52 return ++count; | |
53 } | |
54 } | |
55 | |
56 /** | |
57 * Unlocks the monitor. If the current thread is not | |
58 * the monitor owner, do nothing. | |
59 */ | |
60 public void unlock() { | |
61 synchronized (mutex) { | |
62 Thread current = Thread.getThis(); | |
54 | 63 if (owner is current) { |
64 if (--count is 0) { | |
0 | 65 owner = null; |
66 if (waitCount > 0) cond.notifyAll(); | |
67 } | |
68 } | |
69 } | |
70 } | |
71 } |