Mercurial > projects > dwt-mac
comparison dwt/internal/Lock.d @ 58:6d9ec9ccdcdd
Updated Accessible to 3.514
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Tue, 09 Dec 2008 21:35:30 +0100 |
parents | d8635bb48c7c |
children |
comparison
equal
deleted
inserted
replaced
57:4444d15131d5 | 58:6d9ec9ccdcdd |
---|---|
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 IBM Corporation and others. | 2 * Copyright (c) 2000, 2005 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * | |
11 * Port to the D programming language: | 10 * Port to the D programming language: |
12 * Frank Benoit <benoit@tionex.de> | 11 * Frank Benoit <benoit@tionex.de> |
13 *******************************************************************************/ | 12 *******************************************************************************/ |
14 module dwt.internal.Lock; | 13 module dwt.internal.Lock; |
15 | 14 |
16 import tango.core.Thread; | 15 import tango.core.Thread; |
17 import tango.core.sync.Mutex; | 16 import tango.core.sync.Mutex; |
18 import tango.core.sync.Condition; | 17 import tango.core.sync.Condition; |
19 import tango.core.Exception; | |
20 | |
21 import dwt.dwthelper.utils; | |
22 | 18 |
23 /** | 19 /** |
24 * Instances of this represent a recursive monitor. Note that this | 20 * Instance of this represent a recursive monitor. |
25 * is an empty implementation which does not actually perform locking. | |
26 */ | 21 */ |
27 public class Lock | 22 public class Lock { |
28 { | 23 int count, waitCount; |
24 Thread owner; | |
29 Mutex mutex; | 25 Mutex mutex; |
30 Condition cond; | 26 Condition cond; |
31 | 27 |
32 public this () | 28 public this() { |
33 { | |
34 mutex = new Mutex; | 29 mutex = new Mutex; |
35 cond = new Condition(mutex); | 30 cond = new Condition(mutex); |
36 } | 31 } |
32 /** | |
33 * Locks the monitor and returns the lock count. If | |
34 * the lock is owned by another thread, wait until | |
35 * the lock is released. | |
36 * | |
37 * @return the lock count | |
38 */ | |
39 public int lock() { | |
40 synchronized (mutex) { | |
41 Thread current = Thread.getThis(); | |
42 if (owner !is current) { | |
43 waitCount++; | |
44 while (count > 0) { | |
45 try { | |
46 cond.wait(); | |
47 } catch (SyncException e) { | |
48 /* Wait forever, just like synchronized blocks */ | |
49 } | |
50 } | |
51 --waitCount; | |
52 owner = current; | |
53 } | |
54 return ++count; | |
55 } | |
56 } | |
37 | 57 |
38 /** | 58 /** |
39 * Locks the monitor and returns the lock count. If | 59 * Unlocks the monitor. If the current thread is not |
40 * the lock is owned by another thread, wait until | 60 * the monitor owner, do nothing. |
41 * the lock is released. | 61 */ |
42 * | 62 public void unlock() { |
43 * @return the lock count | 63 synchronized (mutex) { |
44 */ | 64 Thread current = Thread.getThis(); |
45 public int lock () | 65 if (owner is current) { |
46 { | 66 if (--count is 0) { |
47 synchronized (mutex) | 67 owner = null; |
48 { | 68 if (waitCount > 0) cond.notifyAll(); |
49 Thread current = Thread.getThis(); | |
50 if (owner !is current) | |
51 { | |
52 waitCount++; | |
53 while (count > 0) | |
54 { | |
55 try | |
56 { | |
57 cond.wait(); | |
58 } | |
59 catch (SyncException e) | |
60 { | |
61 } | |
62 } | |
63 --waitCount; | |
64 owner = current; | |
65 } | |
66 return ++count; | |
67 } | |
68 } | |
69 | |
70 /** | |
71 * Unlocks the monitor. If the current thread is not | |
72 * the monitor owner, do nothing. | |
73 */ | |
74 public void unlock () | |
75 { | |
76 synchronized (mutex) | |
77 { | |
78 Thread current = Thread.getThis(); | |
79 if (owner is current) | |
80 { | |
81 if (--count is 0) | |
82 { | |
83 owner = null; | |
84 if (waitCount > 0) | |
85 cond.notifyAll(); | |
86 } | |
87 } | 69 } |
88 } | 70 } |
89 } | 71 } |
90 } | 72 } |
73 } |