Lines 39-44
Link Here
|
39 |
import org.eclipse.rse.core.model.IHost; |
39 |
import org.eclipse.rse.core.model.IHost; |
40 |
import org.eclipse.rse.core.model.IRSEPersistableContainer; |
40 |
import org.eclipse.rse.core.model.IRSEPersistableContainer; |
41 |
import org.eclipse.rse.core.model.RSEModelObject; |
41 |
import org.eclipse.rse.core.model.RSEModelObject; |
|
|
42 |
import org.eclipse.rse.services.Mutex; |
42 |
import org.eclipse.rse.services.clientserver.messages.SystemMessageException; |
43 |
import org.eclipse.rse.services.clientserver.messages.SystemMessageException; |
43 |
|
44 |
|
44 |
/** |
45 |
/** |
Lines 423-481
Link Here
|
423 |
* number of releases exceeds the initial count. |
424 |
* number of releases exceeds the initial count. |
424 |
*/ |
425 |
*/ |
425 |
// TODO it may be possible to replace this class with the one in the 5.0 JRE when we move to that as a base |
426 |
// TODO it may be possible to replace this class with the one in the 5.0 JRE when we move to that as a base |
426 |
private class Semaphore { |
427 |
// private class Semaphore { |
427 |
private int count = 1; |
428 |
// private int count = 1; |
428 |
/** |
429 |
// /** |
429 |
* Create a semaphore with the specified acquire count. |
430 |
// * Create a semaphore with the specified acquire count. |
430 |
* @param count |
431 |
// * @param count |
431 |
*/ |
432 |
// */ |
432 |
Semaphore(int count) { |
433 |
// Semaphore(int count) { |
433 |
this.count = count; |
434 |
// this.count = count; |
434 |
} |
435 |
// } |
435 |
/** |
436 |
// /** |
436 |
* Acquire the semaphore. If the semaphore has already been acquired "count" times |
437 |
// * Acquire the semaphore. If the semaphore has already been acquired "count" times |
437 |
* then this waits for the timeout period. This method reports false if the timeout |
438 |
// * then this waits for the timeout period. This method reports false if the timeout |
438 |
* expires before the semaphore becomes available. |
439 |
// * expires before the semaphore becomes available. |
439 |
* @param timeout the time in milliseconds to wait for the semaphore. |
440 |
// * @param timeout the time in milliseconds to wait for the semaphore. |
440 |
* @return true if the semaphore was acquired within the timeout period, false otherwise. |
441 |
// * @return true if the semaphore was acquired within the timeout period, false otherwise. |
441 |
*/ |
442 |
// */ |
442 |
synchronized boolean acquire(long timeout) { |
443 |
// synchronized boolean acquire(long timeout) { |
443 |
long started = System.currentTimeMillis(); |
444 |
// long started = System.currentTimeMillis(); |
444 |
boolean expired = false; |
445 |
// boolean expired = false; |
445 |
boolean acquired = false; |
446 |
// boolean acquired = false; |
446 |
while (count <= 0) { |
447 |
// while (count <= 0) { |
447 |
try { |
448 |
// try { |
448 |
wait(1000); // wait one second |
449 |
// wait(1000); // wait one second |
449 |
} catch (InterruptedException e) { |
450 |
// } catch (InterruptedException e) { |
450 |
// do nothing |
451 |
// // do nothing |
451 |
} |
452 |
// } |
452 |
long now = System.currentTimeMillis(); |
453 |
// long now = System.currentTimeMillis(); |
453 |
long elapsed = now - started; |
454 |
// long elapsed = now - started; |
454 |
expired = elapsed > timeout; |
455 |
// expired = elapsed > timeout; |
455 |
if (expired) break; |
456 |
// if (expired) break; |
456 |
} |
457 |
// } |
457 |
if (count > 0) { |
458 |
// if (count > 0) { |
458 |
--count; |
459 |
// --count; |
459 |
acquired = true; |
460 |
// acquired = true; |
460 |
} |
461 |
// } |
461 |
return acquired; |
462 |
// return acquired; |
462 |
} |
463 |
// } |
463 |
/** |
464 |
// /** |
464 |
* Release the semaphore. This makes the semaphore available to be acquired. |
465 |
// * Release the semaphore. This makes the semaphore available to be acquired. |
465 |
*/ |
466 |
// */ |
466 |
synchronized void release() { |
467 |
// synchronized void release() { |
467 |
count++; |
468 |
// count++; |
468 |
notifyAll(); |
469 |
// notifyAll(); |
469 |
} |
470 |
// } |
470 |
} |
471 |
// } |
471 |
|
472 |
|
472 |
/** |
473 |
/** |
473 |
* A SafeRunner makes sure that instances of UnsafeRunnableWithProgress will run one |
474 |
* A SafeRunner makes sure that instances of UnsafeRunnableWithProgress will run one |
474 |
* at a time. A timeout value is specified. If the runnable cannot be started within |
475 |
* at a time. A timeout value is specified. If the runnable cannot be started within |
475 |
* the timeout value it is not run and a TimeoutException is thrown. |
476 |
* the timeout value it is not run and a TimeoutException is thrown. |
|
|
477 |
* <p> |
478 |
* A SafeRunner keeps track of the thread that is running. If that thread |
479 |
* reenters the SafeRunner, then it is allowed to continue execution. |
476 |
*/ |
480 |
*/ |
477 |
private class SafeRunner { |
481 |
private class SafeRunner { |
478 |
private Semaphore semaphore = new Semaphore(1); |
482 |
// private Semaphore semaphore = new Semaphore(1); |
|
|
483 |
private Mutex semaphore = new Mutex(); |
484 |
private Thread semaphoreOwner = null; |
479 |
/** |
485 |
/** |
480 |
* Run a runnable. If one is already running in this runner then this one will wait up to |
486 |
* Run a runnable. If one is already running in this runner then this one will wait up to |
481 |
* the specified timeout period. If the timeout expires an exception is thrown. |
487 |
* the specified timeout period. If the timeout expires an exception is thrown. |
Lines 485-498
Link Here
|
485 |
* @throws TimeoutException if the timeout expires before the runner becomes unblocked. |
491 |
* @throws TimeoutException if the timeout expires before the runner becomes unblocked. |
486 |
*/ |
492 |
*/ |
487 |
void run(UnsafeRunnableWithProgress runnable, long timeout, IProgressMonitor monitor) throws Exception { |
493 |
void run(UnsafeRunnableWithProgress runnable, long timeout, IProgressMonitor monitor) throws Exception { |
488 |
if (semaphore.acquire(timeout)) { |
494 |
if (semaphoreOwner != Thread.currentThread()) { |
489 |
try { |
495 |
// if (semaphore.acquire(timeout)) { |
490 |
runnable.run(monitor); |
496 |
if (semaphore.waitForLock(monitor, timeout)) { |
491 |
} finally { |
497 |
semaphoreOwner = Thread.currentThread(); |
492 |
semaphore.release(); |
498 |
try { |
|
|
499 |
if (monitor.isCanceled()) { |
500 |
throw new OperationCanceledException(); |
501 |
} |
502 |
runnable.run(monitor); |
503 |
} finally { |
504 |
semaphore.release(); |
505 |
semaphoreOwner = null; |
506 |
} |
507 |
} else { |
508 |
throw new TimeoutException(timeout); |
493 |
} |
509 |
} |
494 |
} else { |
510 |
} else { |
495 |
throw new TimeoutException(timeout); |
511 |
runnable.run(monitor); |
496 |
} |
512 |
} |
497 |
} |
513 |
} |
498 |
} |
514 |
} |