comparison dwtx/core/internal/jobs/ImplicitJobs.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.ImplicitJobs; 13 module dwtx.core.internal.jobs.ImplicitJobs;
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 import dwtx.dwtxhelper.Collection; 19 import dwtx.dwtxhelper.Collection;
20 20
21 import dwtx.core.internal.runtime.RuntimeLog; 21 import dwtx.core.internal.runtime.RuntimeLog;
63 * @see IJobManager#beginRule 63 * @see IJobManager#beginRule
64 */ 64 */
65 void begin(ISchedulingRule rule, IProgressMonitor monitor, bool suspend) { 65 void begin(ISchedulingRule rule, IProgressMonitor monitor, bool suspend) {
66 if (JobManager.DEBUG_BEGIN_END) 66 if (JobManager.DEBUG_BEGIN_END)
67 JobManager.debug_(Format("Begin rule: {}", rule)); //$NON-NLS-1$ 67 JobManager.debug_(Format("Begin rule: {}", rule)); //$NON-NLS-1$
68 final Thread getThis = Thread.getThis(); 68 final JThread getThis = JThread.currentThread();
69 ThreadJob threadJob; 69 ThreadJob threadJob;
70 synchronized (this) { 70 synchronized (this) {
71 threadJob = cast(ThreadJob) threadJobs.get(getThis); 71 threadJob = cast(ThreadJob) threadJobs.get(getThis);
72 if (threadJob !is null) { 72 if (threadJob !is null) {
73 //nested rule, just push on stack and return 73 //nested rule, just push on stack and return
96 threadJob.push(rule); 96 threadJob.push(rule);
97 //join the thread job outside sync block 97 //join the thread job outside sync block
98 if (threadJob.acquireRule) { 98 if (threadJob.acquireRule) {
99 //no need to re-acquire any locks because the thread did not wait to get this lock 99 //no need to re-acquire any locks because the thread did not wait to get this lock
100 if (manager.runNow_package(threadJob)) 100 if (manager.runNow_package(threadJob))
101 manager.getLockManager().addLockThread(Thread.getThis(), rule); 101 manager.getLockManager().addLockThread(JThread.currentThread(), rule);
102 else 102 else
103 threadJob = threadJob.joinRun(monitor); 103 threadJob = threadJob.joinRun(monitor);
104 } 104 }
105 } finally { 105 } finally {
106 //remember this thread job - only do this 106 //remember this thread job - only do this
122 * @see IJobManager#endRule 122 * @see IJobManager#endRule
123 */ 123 */
124 synchronized void end(ISchedulingRule rule, bool resume) { 124 synchronized void end(ISchedulingRule rule, bool resume) {
125 if (JobManager.DEBUG_BEGIN_END) 125 if (JobManager.DEBUG_BEGIN_END)
126 JobManager.debug_(Format("End rule: {}", rule)); //$NON-NLS-1$ 126 JobManager.debug_(Format("End rule: {}", rule)); //$NON-NLS-1$
127 ThreadJob threadJob = cast(ThreadJob) threadJobs.get(Thread.getThis()); 127 ThreadJob threadJob = cast(ThreadJob) threadJobs.get(JThread.currentThread());
128 if (threadJob is null) 128 if (threadJob is null)
129 Assert.isLegal(rule is null, Format("endRule without matching beginRule: {}", rule)); //$NON-NLS-1$ 129 Assert.isLegal(rule is null, Format("endRule without matching beginRule: {}", rule)); //$NON-NLS-1$
130 else if (threadJob.pop(rule)) { 130 else if (threadJob.pop(rule)) {
131 endThreadJob(threadJob, resume); 131 endThreadJob(threadJob, resume);
132 } 132 }
136 * Called when a worker thread has finished running a job. At this 136 * Called when a worker thread has finished running a job. At this
137 * point, the worker thread must not own any scheduling rules 137 * point, the worker thread must not own any scheduling rules
138 * @param lastJob The last job to run in this thread 138 * @param lastJob The last job to run in this thread
139 */ 139 */
140 void endJob(InternalJob lastJob) { 140 void endJob(InternalJob lastJob) {
141 final Thread getThis = Thread.getThis(); 141 final JThread getThis = JThread.currentThread();
142 IStatus error; 142 IStatus error;
143 synchronized (this) { 143 synchronized (this) {
144 ThreadJob threadJob = cast(ThreadJob) threadJobs.get(getThis); 144 ThreadJob threadJob = cast(ThreadJob) threadJobs.get(getThis);
145 if (threadJob is null) { 145 if (threadJob is null) {
146 if (lastJob.getRule() !is null) 146 if (lastJob.getRule() !is null)
159 Stderr.formatln("{}", error.getMessage()); 159 Stderr.formatln("{}", error.getMessage());
160 } 160 }
161 } 161 }
162 162
163 private void endThreadJob(ThreadJob threadJob, bool resume) { 163 private void endThreadJob(ThreadJob threadJob, bool resume) {
164 Thread getThis = Thread.getThis(); 164 JThread getThis = JThread.currentThread();
165 //clean up when last rule scope exits 165 //clean up when last rule scope exits
166 threadJobs.remove(getThis); 166 threadJobs.remove(getThis);
167 ISchedulingRule rule = threadJob.getRule(); 167 ISchedulingRule rule = threadJob.getRule();
168 if (resume && rule !is null) 168 if (resume && rule !is null)
169 suspendedRules.remove(cast(Object)rule); 169 suspendedRules.remove(cast(Object)rule);
249 } 249 }
250 250
251 /** 251 /**
252 * Implements IJobManager#transferRule(ISchedulingRule, Thread) 252 * Implements IJobManager#transferRule(ISchedulingRule, Thread)
253 */ 253 */
254 synchronized void transfer(ISchedulingRule rule, Thread destinationThread) { 254 synchronized void transfer(ISchedulingRule rule, JThread destinationThread) {
255 //nothing to do for null 255 //nothing to do for null
256 if (rule is null) 256 if (rule is null)
257 return; 257 return;
258 Thread getThis = Thread.getThis(); 258 JThread getThis = JThread.currentThread();
259 //nothing to do if transferring to the same thread 259 //nothing to do if transferring to the same thread
260 if (getThis is destinationThread) 260 if (getThis is destinationThread)
261 return; 261 return;
262 //ensure destination thread doesn't already have a rule 262 //ensure destination thread doesn't already have a rule
263 ThreadJob job = cast(ThreadJob) threadJobs.get(destinationThread); 263 ThreadJob job = cast(ThreadJob) threadJobs.get(destinationThread);