comparison dwtx/core/internal/jobs/JobManager.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 c3583c6ec027
comparison
equal deleted inserted replaced
163:e5dd0081ccba 167:862b05e0334a
16 import dwtx.dwtxhelper.Collection; 16 import dwtx.dwtxhelper.Collection;
17 import tango.io.Stdout; 17 import tango.io.Stdout;
18 import tango.text.convert.Format; 18 import tango.text.convert.Format;
19 import tango.time.WallClock; 19 import tango.time.WallClock;
20 import tango.time.Time; 20 import tango.time.Time;
21 import tango.core.Thread; 21 import dwtx.dwtxhelper.JThread;
22 import tango.text.convert.Format; 22 import tango.text.convert.Format;
23 23
24 //don't use ICU because this is used for debugging only (see bug 135785) 24 //don't use ICU because this is used for debugging only (see bug 135785)
25 // import java.text.DateFormat; 25 // import java.text.DateFormat;
26 // import java.text.FieldPosition; 26 // import java.text.FieldPosition;
191 time.time.span.seconds, 191 time.time.span.seconds,
192 time.time.span.millis )); 192 time.time.span.millis ));
193 msgBuf.append('-'); 193 msgBuf.append('-');
194 } 194 }
195 msgBuf.append('['); 195 msgBuf.append('[');
196 msgBuf.append(Thread.getThis().toString()); 196 msgBuf.append(JThread.currentThread().toString());
197 msgBuf.append(']'); 197 msgBuf.append(']');
198 msgBuf.append(msg); 198 msgBuf.append(msg);
199 Stdout.formatln( "{}", msgBuf.toString()); 199 Stdout.formatln( "{}", msgBuf.toString());
200 } 200 }
201 201
464 464
465 /* (non-Javadoc) 465 /* (non-Javadoc)
466 * @see dwtx.core.runtime.jobs.IJobManager#currentJob() 466 * @see dwtx.core.runtime.jobs.IJobManager#currentJob()
467 */ 467 */
468 public Job currentJob() { 468 public Job currentJob() {
469 Thread current = Thread.getThis(); 469 JThread current = JThread.currentThread();
470 if (cast(Worker)current ) 470 if (cast(Worker)current )
471 return (cast(Worker) current).currentJob(); 471 return (cast(Worker) current).currentJob();
472 synchronized (lock) { 472 synchronized (lock) {
473 for (Iterator it = running.iterator(); it.hasNext();) { 473 for (Iterator it = running.iterator(); it.hasNext();) {
474 Job job = cast(Job) it.next(); 474 Job job = cast(Job) it.next();
548 for (int i = 0; i < toCancel.length; i++) { 548 for (int i = 0; i < toCancel.length; i++) {
549 cancel(cast(InternalJob)toCancel[i]); // cancel jobs outside sync block to avoid deadlock 549 cancel(cast(InternalJob)toCancel[i]); // cancel jobs outside sync block to avoid deadlock
550 } 550 }
551 551
552 for (int waitAttempts = 0; waitAttempts < 3; waitAttempts++) { 552 for (int waitAttempts = 0; waitAttempts < 3; waitAttempts++) {
553 Thread.yield(); 553 JThread.yield();
554 synchronized (lock) { 554 synchronized (lock) {
555 if (running.isEmpty()) 555 if (running.isEmpty())
556 break; 556 break;
557 } 557 }
558 if (DEBUG_SHUTDOWN) { 558 if (DEBUG_SHUTDOWN) {
566 JobManager.debug_(Format("\tJob: {}", printJobName(stillRunning[j]))); //$NON-NLS-1$ 566 JobManager.debug_(Format("\tJob: {}", printJobName(stillRunning[j]))); //$NON-NLS-1$
567 } 567 }
568 } 568 }
569 } 569 }
570 try { 570 try {
571 Thread.sleep(0.100); 571 JThread.sleep(100);
572 } catch (InterruptedException e) { 572 } catch (InterruptedException e) {
573 //ignore 573 //ignore
574 } 574 }
575 Thread.yield(); 575 JThread.yield();
576 } 576 }
577 577
578 synchronized (lock) { // retrieve list of the jobs that are still running 578 synchronized (lock) { // retrieve list of the jobs that are still running
579 toCancel = arraycast!(Job)( running.toArray()); 579 toCancel = arraycast!(Job)( running.toArray());
580 } 580 }
771 return; 771 return;
772 //don't join a waiting or sleeping job when suspended (deadlock risk) 772 //don't join a waiting or sleeping job when suspended (deadlock risk)
773 if (suspended && state !is Job.RUNNING) 773 if (suspended && state !is Job.RUNNING)
774 return; 774 return;
775 //it's an error for a job to join itself 775 //it's an error for a job to join itself
776 if (state is Job.RUNNING && job.getThread_package() is Thread.getThis()) 776 if (state is Job.RUNNING && job.getThread_package() is JThread.currentThread())
777 throw new IllegalStateException("Job attempted to join itself"); //$NON-NLS-1$ 777 throw new IllegalStateException("Job attempted to join itself"); //$NON-NLS-1$
778 //the semaphore will be released when the job is done 778 //the semaphore will be released when the job is done
779 barrier = new Semaphore(null); 779 barrier = new Semaphore(null);
780 listener = new class(barrier) JobChangeAdapter { 780 listener = new class(barrier) JobChangeAdapter {
781 Semaphore barrier_; 781 Semaphore barrier_;
875 if (reportedWorkDone < actualWorkDone) { 875 if (reportedWorkDone < actualWorkDone) {
876 monitor.worked(actualWorkDone - reportedWorkDone); 876 monitor.worked(actualWorkDone - reportedWorkDone);
877 reportedWorkDone = actualWorkDone; 877 reportedWorkDone = actualWorkDone;
878 monitor.subTask(NLS.bind(JobMessages.jobs_waitFamSub, Integer.toString(jobsLeft))); 878 monitor.subTask(NLS.bind(JobMessages.jobs_waitFamSub, Integer.toString(jobsLeft)));
879 } 879 }
880 implMissing(__FILE__, __LINE__ ); 880
881 // DWT 881 if (JThread.interrupted())
882 // if (Thread.interrupted()) 882 throw new InterruptedException();
883 // throw new InterruptedException();
884 if (monitor.isCanceled()) 883 if (monitor.isCanceled())
885 throw new OperationCanceledException(); 884 throw new OperationCanceledException();
886 //notify hook to service pending syncExecs before falling asleep 885 //notify hook to service pending syncExecs before falling asleep
887 lockManager.aboutToWait(null); 886 lockManager.aboutToWait(null);
888 Thread.sleep(0.100); 887 JThread.sleep(100);
889 } 888 }
890 } finally { 889 } finally {
891 lockManager.aboutToRelease(); 890 lockManager.aboutToRelease();
892 removeJobChangeListener(listener); 891 removeJobChangeListener(listener);
893 reportUnblocked(monitor); 892 reportUnblocked(monitor);
1298 } 1297 }
1299 1298
1300 /* non-Javadoc) 1299 /* non-Javadoc)
1301 * @see dwtx.core.runtime.jobs.IJobManager#transferRule() 1300 * @see dwtx.core.runtime.jobs.IJobManager#transferRule()
1302 */ 1301 */
1303 public void transferRule(ISchedulingRule rule, Thread destinationThread) { 1302 public void transferRule(ISchedulingRule rule, JThread destinationThread) {
1304 implicitJobs.transfer(rule, destinationThread); 1303 implicitJobs.transfer(rule, destinationThread);
1305 } 1304 }
1306 1305
1307 /** 1306 /**
1308 * Validates that the given scheduling rule obeys the constraints of 1307 * Validates that the given scheduling rule obeys the constraints of