comparison dwtx/core/internal/jobs/OrderedLock.d @ 167:862b05e0334a

Add a wrapper for Thread
author Frank Benoit <benoit@tionex.de>
date Tue, 09 Sep 2008 15:59:16 +0200
parents 9d0585bcb7aa
children
comparison
equal deleted inserted replaced
163:e5dd0081ccba 167:862b05e0334a
11 * Frank Benoit <benoit@tionex.de> 11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/ 12 *******************************************************************************/
13 module dwtx.core.internal.jobs.OrderedLock; 13 module dwtx.core.internal.jobs.OrderedLock;
14 14
15 import tango.text.convert.Format; 15 import tango.text.convert.Format;
16 import tango.core.Thread; 16 import dwtx.dwtxhelper.JThread;
17 import tango.io.Stdout; 17 import tango.io.Stdout;
18 import dwt.dwthelper.utils; 18 import dwt.dwthelper.utils;
19 19
20 import dwtx.core.runtime.Assert; 20 import dwtx.core.runtime.Assert;
21 import dwtx.core.runtime.jobs.ILock; 21 import dwtx.core.runtime.jobs.ILock;
51 */ 51 */
52 private static int nextLockNumber = 0; 52 private static int nextLockNumber = 0;
53 /** 53 /**
54 * The thread of the operation that currently owns the lock. 54 * The thread of the operation that currently owns the lock.
55 */ 55 */
56 private /+volatile+/ Thread currentOperationThread; 56 private /+volatile+/ JThread currentOperationThread;
57 /** 57 /**
58 * Records the number of successive acquires in the same 58 * Records the number of successive acquires in the same
59 * thread. The lock is released only when the depth 59 * thread. The lock is released only when the depth
60 * reaches zero. 60 * reaches zero.
61 */ 61 */
114 return attempt(); 114 return attempt();
115 Semaphore semaphore = createSemaphore(); 115 Semaphore semaphore = createSemaphore();
116 if (semaphore is null) 116 if (semaphore is null)
117 return true; 117 return true;
118 if (DEBUG) 118 if (DEBUG)
119 Stdout.formatln("[{}] Operation waiting to be executed... ", Thread.getThis(), this); //$NON-NLS-1$ //$NON-NLS-2$ 119 Stdout.formatln("[{}] Operation waiting to be executed... ", JThread.currentThread(), this); //$NON-NLS-1$ //$NON-NLS-2$
120 success = doAcquire(semaphore, delay); 120 success = doAcquire(semaphore, delay);
121 manager.resumeSuspendedLocks(Thread.getThis()); 121 manager.resumeSuspendedLocks(JThread.currentThread());
122 if (DEBUG && success) 122 if (DEBUG && success)
123 Stdout.formatln("[{}] Operation started... ", Thread.getThis(), this); //$NON-NLS-1$ //$NON-NLS-2$ 123 Stdout.formatln("[{}] Operation started... ", JThread.currentThread(), this); //$NON-NLS-1$ //$NON-NLS-2$
124 else if (DEBUG) 124 else if (DEBUG)
125 Stdout.formatln("[{}] Operation timed out... ", Thread.getThis(), this); //$NON-NLS-1$ //$NON-NLS-2$ 125 Stdout.formatln("[{}] Operation timed out... ", JThread.currentThread(), this); //$NON-NLS-1$ //$NON-NLS-2$
126 return success; 126 return success;
127 } 127 }
128 128
129 /** 129 /**
130 * Attempts to acquire the lock. Returns false if the lock is not available and 130 * Attempts to acquire the lock. Returns false if the lock is not available and
131 * true if the lock has been successfully acquired. 131 * true if the lock has been successfully acquired.
132 */ 132 */
133 private synchronized bool attempt() { 133 private synchronized bool attempt() {
134 //return true if we already own the lock 134 //return true if we already own the lock
135 //also, if nobody is waiting, grant the lock immediately 135 //also, if nobody is waiting, grant the lock immediately
136 if ((currentOperationThread is Thread.getThis()) || (currentOperationThread is null && operations.isEmpty())) { 136 if ((currentOperationThread is JThread.currentThread()) || (currentOperationThread is null && operations.isEmpty())) {
137 depth++; 137 depth++;
138 setCurrentOperationThread(Thread.getThis()); 138 setCurrentOperationThread(JThread.currentThread());
139 return true; 139 return true;
140 } 140 }
141 return false; 141 return false;
142 } 142 }
143 143
152 * Returns null if acquired and a Semaphore object otherwise. If a 152 * Returns null if acquired and a Semaphore object otherwise. If a
153 * waiting semaphore already exists for this thread, it will be returned, 153 * waiting semaphore already exists for this thread, it will be returned,
154 * otherwise a new semaphore will be created, enqueued, and returned. 154 * otherwise a new semaphore will be created, enqueued, and returned.
155 */ 155 */
156 private synchronized Semaphore createSemaphore() { 156 private synchronized Semaphore createSemaphore() {
157 return attempt() ? null : enqueue(new Semaphore(Thread.getThis())); 157 return attempt() ? null : enqueue(new Semaphore(JThread.currentThread()));
158 } 158 }
159 159
160 /** 160 /**
161 * Attempts to acquire this lock. Callers will block until this lock comes available to 161 * Attempts to acquire this lock. Callers will block until this lock comes available to
162 * them, or until the specified delay has elapsed. 162 * them, or until the specified delay has elapsed.
177 //It might have been removed from the queue while servicing syncExecs 177 //It might have been removed from the queue while servicing syncExecs
178 //This is will return our existing semaphore if it is still in the queue 178 //This is will return our existing semaphore if it is still in the queue
179 semaphore = createSemaphore(); 179 semaphore = createSemaphore();
180 if (semaphore is null) 180 if (semaphore is null)
181 return true; 181 return true;
182 manager.addLockWaitThread(Thread.getThis(), this); 182 manager.addLockWaitThread(JThread.currentThread(), this);
183 try { 183 try {
184 success = semaphore.acquire(delay); 184 success = semaphore.acquire(delay);
185 } catch (InterruptedException e) { 185 } catch (InterruptedException e) {
186 if (DEBUG) 186 if (DEBUG)
187 Stdout.formatln(Format("[{}] Operation interrupted while waiting... :-|", Thread.getThis())); //$NON-NLS-1$ //$NON-NLS-2$ 187 Stdout.formatln(Format("[{}] Operation interrupted while waiting... :-|", JThread.currentThread())); //$NON-NLS-1$ //$NON-NLS-2$
188 throw e; 188 throw e;
189 } 189 }
190 if (success) { 190 if (success) {
191 depth++; 191 depth++;
192 updateCurrentOperation(); 192 updateCurrentOperation();
193 } else { 193 } else {
194 removeFromQueue(semaphore); 194 removeFromQueue(semaphore);
195 manager.removeLockWaitThread(Thread.getThis(), this); 195 manager.removeLockWaitThread(JThread.currentThread(), this);
196 } 196 }
197 return success; 197 return success;
198 } 198 }
199 199
200 /** 200 /**
276 276
277 /** 277 /**
278 * If newThread is null, release this lock from its previous owner. 278 * If newThread is null, release this lock from its previous owner.
279 * If newThread is not null, grant this lock to newThread. 279 * If newThread is not null, grant this lock to newThread.
280 */ 280 */
281 private void setCurrentOperationThread(Thread newThread) { 281 private void setCurrentOperationThread(JThread newThread) {
282 if ((currentOperationThread !is null) && (newThread is null)) 282 if ((currentOperationThread !is null) && (newThread is null))
283 manager.removeLockThread(currentOperationThread, this); 283 manager.removeLockThread(currentOperationThread, this);
284 this.currentOperationThread = newThread; 284 this.currentOperationThread = newThread;
285 if (currentOperationThread !is null) 285 if (currentOperationThread !is null)
286 manager.addLockThread(currentOperationThread, this); 286 manager.addLockThread(currentOperationThread, this);
311 * This lock has just been granted to a new thread (the thread waited for it). 311 * This lock has just been granted to a new thread (the thread waited for it).
312 * Remove the request from the queue and update both the graph and the lock. 312 * Remove the request from the queue and update both the graph and the lock.
313 */ 313 */
314 private synchronized void updateCurrentOperation() { 314 private synchronized void updateCurrentOperation() {
315 operations.dequeue(); 315 operations.dequeue();
316 setCurrentOperationThread(Thread.getThis()); 316 setCurrentOperationThread(JThread.currentThread());
317 } 317 }
318 } 318 }