Mercurial > projects > dwt2
comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/internal/Lock.d @ 22:4642ab680468
some work on dwt-win for tango/phobos
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 20 Mar 2009 12:52:28 +0100 |
parents | 52184e4b815c |
children | 0ecb2b338560 |
comparison
equal
deleted
inserted
replaced
21:9b96950f2c3c | 22:4642ab680468 |
---|---|
11 * Frank Benoit <benoit@tionex.de> | 11 * Frank Benoit <benoit@tionex.de> |
12 *******************************************************************************/ | 12 *******************************************************************************/ |
13 module org.eclipse.swt.internal.Lock; | 13 module org.eclipse.swt.internal.Lock; |
14 | 14 |
15 import java.lang.Thread; | 15 import java.lang.Thread; |
16 import tango.core.sync.Mutex; | 16 version(Tango){ |
17 import tango.core.sync.Condition; | 17 import tango.core.sync.Mutex; |
18 import tango.core.sync.Condition; | |
19 } else { // Phobos | |
20 } | |
18 | 21 |
19 /** | 22 /** |
20 * Instance of this represent a recursive monitor. | 23 * Instance of this represent a recursive monitor. |
21 */ | 24 */ |
22 public class Lock { | 25 public class Lock { |
23 int count, waitCount; | 26 int count, waitCount; |
24 Thread owner; | 27 Thread owner; |
25 Mutex mutex; | 28 version(Tango){ |
26 Condition cond; | 29 Mutex mutex; |
30 Condition cond; | |
31 } else { // Phobos | |
32 } | |
33 | |
27 | 34 |
28 public this() { | 35 public this() { |
29 mutex = new Mutex; | 36 version(Tango){ |
30 cond = new Condition(mutex); | 37 mutex = new Mutex; |
38 cond = new Condition(mutex); | |
39 } else { // Phobos | |
40 } | |
31 } | 41 } |
32 /** | 42 /** |
33 * Locks the monitor and returns the lock count. If | 43 * Locks the monitor and returns the lock count. If |
34 * the lock is owned by another thread, wait until | 44 * the lock is owned by another thread, wait until |
35 * the lock is released. | 45 * the lock is released. |
36 * | 46 * |
37 * @return the lock count | 47 * @return the lock count |
38 */ | 48 */ |
39 public int lock() { | 49 public int lock() { |
40 synchronized (mutex) { | 50 version(Tango){ |
41 Thread current = Thread.currentThread(); | 51 synchronized (mutex) { |
42 if (owner !is current) { | 52 Thread current = Thread.currentThread(); |
43 waitCount++; | 53 if (owner !is current) { |
44 while (count > 0) { | 54 waitCount++; |
45 try { | 55 while (count > 0) { |
46 cond.wait(); | 56 try { |
47 } catch (SyncException e) { | 57 cond.wait(); |
48 /* Wait forever, just like synchronized blocks */ | 58 } catch (SyncException e) { |
59 /* Wait forever, just like synchronized blocks */ | |
60 } | |
61 } | |
62 --waitCount; | |
63 owner = current; | |
64 } | |
65 return ++count; | |
66 } | |
67 } else { // Phobos | |
68 implMissing( __FILE__, __LINE__ ); | |
69 return 0; | |
70 } | |
71 } | |
72 | |
73 /** | |
74 * Unlocks the monitor. If the current thread is not | |
75 * the monitor owner, do nothing. | |
76 */ | |
77 public void unlock() { | |
78 version(Tango){ | |
79 synchronized (mutex) { | |
80 Thread current = Thread.currentThread(); | |
81 if (owner is current) { | |
82 if (--count is 0) { | |
83 owner = null; | |
84 if (waitCount > 0) cond.notifyAll(); | |
85 } | |
49 } | 86 } |
50 } | 87 } |
51 --waitCount; | 88 } else { // Phobos |
52 owner = current; | 89 implMissing( __FILE__, __LINE__ ); |
53 } | |
54 return ++count; | |
55 } | |
56 } | |
57 | |
58 /** | |
59 * Unlocks the monitor. If the current thread is not | |
60 * the monitor owner, do nothing. | |
61 */ | |
62 public void unlock() { | |
63 synchronized (mutex) { | |
64 Thread current = Thread.currentThread(); | |
65 if (owner is current) { | |
66 if (--count is 0) { | |
67 owner = null; | |
68 if (waitCount > 0) cond.notifyAll(); | |
69 } | |
70 } | 90 } |
71 } | 91 } |
72 } | 92 } |
73 } |