comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/internal/Lock.d @ 113:fb3aa8075988

D2 support for the linux port.
author Jacob Carlborg <doob@me.com>
date Wed, 06 Apr 2011 21:57:23 +0200
parents 0ecb2b338560
children
comparison
equal deleted inserted replaced
112:9f4c18c268b2 113:fb3aa8075988
15 import java.lang.all; 15 import java.lang.all;
16 import java.lang.Thread; 16 import java.lang.Thread;
17 version(Tango){ 17 version(Tango){
18 import tango.core.sync.Mutex; 18 import tango.core.sync.Mutex;
19 import tango.core.sync.Condition; 19 import tango.core.sync.Condition;
20 } else { // Phobos 20 } else {
21 import core.sync.mutex;
22 import core.sync.condition;
21 } 23 }
22 24
23 /** 25 /**
24 * Instance of this represent a recursive monitor. 26 * Instance of this represent a recursive monitor.
25 */ 27 */
26 public class Lock { 28 public class Lock {
27 int count, waitCount; 29 int count, waitCount;
28 Thread owner; 30 Thread owner;
29 version(Tango){ 31 Mutex mutex;
30 Mutex mutex; 32 Condition cond;
31 Condition cond;
32 } else { // Phobos
33 }
34
35 33
36 public this() { 34 public this() {
37 version(Tango){ 35 mutex = new Mutex;
38 mutex = new Mutex; 36 cond = new Condition(mutex);
39 cond = new Condition(mutex);
40 } else { // Phobos
41 }
42 } 37 }
43 /** 38 /**
44 * Locks the monitor and returns the lock count. If 39 * Locks the monitor and returns the lock count. If
45 * the lock is owned by another thread, wait until 40 * the lock is owned by another thread, wait until
46 * the lock is released. 41 * the lock is released.
47 * 42 *
48 * @return the lock count 43 * @return the lock count
49 */ 44 */
50 public int lock() { 45 public int lock() {
51 version(Tango){ 46 synchronized (mutex) {
52 synchronized (mutex) { 47 Thread current = Thread.currentThread();
53 Thread current = Thread.currentThread(); 48 if (owner !is current) {
54 if (owner !is current) { 49 waitCount++;
55 waitCount++; 50 while (count > 0) {
56 while (count > 0) { 51 try {
57 try { 52 cond.wait();
58 cond.wait(); 53 } catch (SyncException e) {
59 } catch (SyncException e) { 54 /* Wait forever, just like synchronized blocks */
60 /* Wait forever, just like synchronized blocks */
61 }
62 } 55 }
63 --waitCount;
64 owner = current;
65 } 56 }
66 return ++count; 57 --waitCount;
58 owner = current;
67 } 59 }
68 } else { // Phobos 60 return ++count;
69 implMissing( __FILE__, __LINE__ );
70 return 0;
71 } 61 }
72 } 62 }
73 63
74 /** 64 /**
75 * Unlocks the monitor. If the current thread is not 65 * Unlocks the monitor. If the current thread is not
76 * the monitor owner, do nothing. 66 * the monitor owner, do nothing.
77 */ 67 */
78 public void unlock() { 68 public void unlock() {
79 version(Tango){ 69 synchronized (mutex) {
80 synchronized (mutex) { 70 Thread current = Thread.currentThread();
81 Thread current = Thread.currentThread(); 71 if (owner is current) {
82 if (owner is current) { 72 if (--count is 0) {
83 if (--count is 0) { 73 owner = null;
84 owner = null; 74 if (waitCount > 0) cond.notifyAll();
85 if (waitCount > 0) cond.notifyAll();
86 }
87 } 75 }
88 } 76 }
89 } else { // Phobos
90 implMissing( __FILE__, __LINE__ );
91 } 77 }
92 } 78 }
93 } 79 }