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