122
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2003, 2007 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM - Initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.core.internal.jobs.InternalJob;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16 import dwtx.dwtxhelper.Collection;
|
|
17
|
|
18 import dwtx.core.runtime.Assert;
|
|
19 import dwtx.core.runtime.IProgressMonitor;
|
|
20 import dwtx.core.runtime.IStatus;
|
|
21 import dwtx.core.runtime.ListenerList;
|
|
22 import dwtx.core.runtime.PlatformObject;
|
|
23 import dwtx.core.runtime.QualifiedName;
|
|
24 import dwtx.core.runtime.jobs.IJobChangeListener;
|
|
25 import dwtx.core.runtime.jobs.ISchedulingRule;
|
|
26 import dwtx.core.runtime.jobs.Job;
|
|
27 import dwtx.core.runtime.jobs.MultiRule;
|
|
28
|
|
29 import dwtx.core.internal.jobs.JobManager;
|
|
30 import dwtx.core.internal.jobs.ObjectMap;
|
|
31
|
|
32 import tango.core.Thread;
|
|
33 import tango.text.convert.Format;
|
|
34
|
|
35 /**
|
|
36 * Internal implementation class for jobs. Clients must not implement this class
|
|
37 * directly. All jobs must be subclasses of the API <code>dwtx.core.runtime.jobs.Job</code> class.
|
|
38 */
|
|
39 public abstract class InternalJob : PlatformObject, Comparable {
|
|
40 /**
|
|
41 * Job state code (value 16) indicating that a job has been removed from
|
|
42 * the wait queue and is about to start running. From an API point of view,
|
|
43 * this is the same as RUNNING.
|
|
44 */
|
|
45 static const int ABOUT_TO_RUN = 0x10;
|
|
46
|
|
47 /**
|
|
48 * Job state code (value 32) indicating that a job has passed scheduling
|
|
49 * precondition checks and is about to be added to the wait queue. From an API point of view,
|
|
50 * this is the same as WAITING.
|
|
51 */
|
|
52 static const int ABOUT_TO_SCHEDULE = 0x20;
|
|
53 /**
|
|
54 * Job state code (value 8) indicating that a job is blocked by another currently
|
|
55 * running job. From an API point of view, this is the same as WAITING.
|
|
56 */
|
|
57 static const int BLOCKED = 0x08;
|
|
58
|
|
59 //flag mask bits
|
|
60 private static const int M_STATE = 0xFF;
|
|
61 private static const int M_SYSTEM = 0x0100;
|
|
62 private static const int M_USER = 0x0200;
|
|
63
|
|
64 /*
|
|
65 * flag on a job indicating that it was about to run, but has been canceled
|
|
66 */
|
|
67 private static const int M_ABOUT_TO_RUN_CANCELED = 0x0400;
|
|
68
|
|
69 private static JobManager manager_;
|
|
70 protected static JobManager manager(){
|
|
71 if( manager_ is null ){
|
|
72 synchronized( InternalJob.classinfo ){
|
|
73 if( manager_ is null ){
|
|
74 manager_ = JobManager.getInstance();
|
|
75 }
|
|
76 }
|
|
77 }
|
|
78 return manager_;
|
|
79 }
|
|
80 private static int nextJobNumber = 0;
|
|
81
|
|
82 /**
|
|
83 * Start time constant indicating a job should be started at
|
|
84 * a time in the infinite future, causing it to sleep forever.
|
|
85 */
|
|
86 static const long T_INFINITE = Long.MAX_VALUE;
|
|
87 /**
|
|
88 * Start time constant indicating that the job has no start time.
|
|
89 */
|
|
90 static const long T_NONE = -1;
|
|
91
|
|
92 private /+volatile+/ int flags = Job.NONE;
|
|
93 private const int jobNumber;
|
|
94 private ListenerList listeners = null;
|
|
95 private IProgressMonitor monitor;
|
|
96 private String name;
|
|
97 /**
|
|
98 * The job ahead of me in a queue or list.
|
|
99 */
|
|
100 private InternalJob next_;
|
|
101 /**
|
|
102 * The job behind me in a queue or list.
|
|
103 */
|
|
104 private InternalJob previous_;
|
|
105 private int priority = Job.LONG;
|
|
106 /**
|
|
107 * Arbitrary properties (key,value) pairs, attached
|
|
108 * to a job instance by a third party.
|
|
109 */
|
|
110 private ObjectMap properties;
|
|
111 private IStatus result;
|
|
112 private ISchedulingRule schedulingRule;
|
|
113 /**
|
|
114 * If the job is waiting, this represents the time the job should start by.
|
|
115 * If this job is sleeping, this represents the time the job should wake up.
|
|
116 * If this job is running, this represents the delay automatic rescheduling,
|
|
117 * or -1 if the job should not be rescheduled.
|
|
118 */
|
|
119 private long startTime;
|
|
120
|
|
121 /**
|
|
122 * Stamp added when a job is added to the wait queue. Used to ensure
|
|
123 * jobs in the wait queue maintain their insertion order even if they are
|
|
124 * removed from the wait queue temporarily while blocked
|
|
125 */
|
|
126 private long waitQueueStamp = T_NONE;
|
|
127
|
|
128 /*
|
|
129 * The thread that is currently running this job
|
|
130 */
|
|
131 private /+volatile+/ Thread thread = null;
|
|
132
|
|
133 protected this(String name) {
|
|
134 Assert.isNotNull(name);
|
|
135 jobNumber = nextJobNumber++;
|
|
136 this.name = name;
|
|
137 }
|
|
138
|
|
139 /* (non-Javadoc)
|
|
140 * @see Job#addJobListener(IJobChangeListener)
|
|
141 */
|
|
142 protected void addJobChangeListener(IJobChangeListener listener) {
|
|
143 if (listeners is null)
|
|
144 listeners = new ListenerList(ListenerList.IDENTITY);
|
|
145 listeners.add(cast(Object)listener);
|
|
146 }
|
|
147 package void addJobChangeListener_package(IJobChangeListener listener) {
|
|
148 addJobChangeListener(listener);
|
|
149 }
|
|
150
|
|
151 /**
|
|
152 * Adds an entry at the end of the list of which this item is the head.
|
|
153 */
|
|
154 final void addLast(InternalJob entry) {
|
|
155 InternalJob last = this;
|
|
156 //find the end of the queue
|
|
157 while (last.previous_ !is null)
|
|
158 last = last.previous_;
|
|
159 //add the new entry to the end of the queue
|
|
160 last.previous_ = entry;
|
|
161 entry.next_ = last;
|
|
162 entry.previous_ = null;
|
|
163 }
|
|
164
|
|
165 /* (non-Javadoc)
|
|
166 * @see Job#belongsTo(Object)
|
|
167 */
|
|
168 protected bool belongsTo(Object family) {
|
|
169 return false;
|
|
170 }
|
|
171 package bool belongsTo_package(Object family) {
|
|
172 return belongsTo(family);
|
|
173 }
|
|
174
|
|
175 /* (non-Javadoc)
|
|
176 * @see Job#cancel()
|
|
177 */
|
|
178 protected package bool cancel() {
|
|
179 return manager.cancel_package(this);
|
|
180 }
|
|
181
|
|
182 /* (non-Javadoc)
|
|
183 * @see Job#canceling()
|
|
184 */
|
|
185 protected package void canceling() {
|
|
186 //default implementation does nothing
|
|
187 }
|
|
188
|
|
189 /* (on-Javadoc)
|
|
190 * @see java.lang.Comparable#compareTo(java.lang.Object)
|
|
191 */
|
|
192 public final int compareTo(Object otherJob) {
|
|
193 return (cast(InternalJob) otherJob).startTime >= startTime ? 1 : -1;
|
|
194 }
|
|
195
|
|
196 /* (non-Javadoc)
|
|
197 * @see Job#done(IStatus)
|
|
198 */
|
|
199 protected void done(IStatus endResult) {
|
|
200 manager.endJob_package(this, endResult, true);
|
|
201 }
|
|
202
|
|
203 /**
|
|
204 * Returns the job listeners that are only listening to this job. Returns
|
|
205 * <code>null</code> if this job has no listeners.
|
|
206 */
|
|
207 final ListenerList getListeners() {
|
|
208 return listeners;
|
|
209 }
|
|
210
|
|
211 /* (non-Javadoc)
|
|
212 * @see Job#getName()
|
|
213 */
|
|
214 protected String getName() {
|
|
215 return name;
|
|
216 }
|
|
217 package String getName_package() {
|
|
218 return name;
|
|
219 }
|
|
220
|
|
221 /* (non-Javadoc)
|
|
222 * @see Job#getPriority()
|
|
223 */
|
|
224 protected package int getPriority() {
|
|
225 return priority;
|
|
226 }
|
|
227
|
|
228 /**
|
|
229 * Returns the job's progress monitor, or null if it is not running.
|
|
230 */
|
|
231 final IProgressMonitor getProgressMonitor() {
|
|
232 return monitor;
|
|
233 }
|
|
234
|
|
235 /* (non-Javadoc)
|
|
236 * @see Job#getProperty
|
|
237 */
|
|
238 protected Object getProperty(QualifiedName key) {
|
|
239 // thread safety: (Concurrency001 - copy on write)
|
|
240 Map temp = properties;
|
|
241 if (temp is null)
|
|
242 return null;
|
|
243 return temp.get(key);
|
|
244 }
|
|
245
|
|
246 /* (non-Javadoc)
|
|
247 * @see Job#getResult
|
|
248 */
|
|
249 protected IStatus getResult() {
|
|
250 return result;
|
|
251 }
|
|
252
|
|
253 /* (non-Javadoc)
|
|
254 * @see Job#getRule
|
|
255 */
|
|
256 protected package ISchedulingRule getRule() {
|
|
257 return schedulingRule;
|
|
258 }
|
|
259 package ISchedulingRule getRule_package() {
|
|
260 return getRule();
|
|
261 }
|
|
262
|
|
263 /**
|
|
264 * Returns the time that this job should be started, awakened, or
|
|
265 * rescheduled, depending on the current state.
|
|
266 * @return time in milliseconds
|
|
267 */
|
|
268 final long getStartTime() {
|
|
269 return startTime;
|
|
270 }
|
|
271
|
|
272 /* (non-Javadoc)
|
|
273 * @see Job#getState()
|
|
274 */
|
|
275 protected int getState() {
|
|
276 int state = flags & M_STATE;
|
|
277 switch (state) {
|
|
278 //blocked state is equivalent to waiting state for clients
|
|
279 case BLOCKED :
|
|
280 return Job.WAITING;
|
|
281 case ABOUT_TO_RUN :
|
|
282 return Job.RUNNING;
|
|
283 case ABOUT_TO_SCHEDULE :
|
|
284 return Job.WAITING;
|
|
285 default :
|
|
286 return state;
|
|
287 }
|
|
288 }
|
|
289 package int getState_package() {
|
|
290 return getState();
|
|
291 }
|
|
292
|
|
293 /* (non-javadoc)
|
|
294 * @see Job.getThread
|
|
295 */
|
|
296 protected Thread getThread() {
|
|
297 return thread;
|
|
298 }
|
|
299 package Thread getThread_package() {
|
|
300 return getThread();
|
|
301 }
|
|
302
|
|
303 /**
|
|
304 * Returns the raw job state, including internal states no exposed as API.
|
|
305 */
|
|
306 final int internalGetState() {
|
|
307 return flags & M_STATE;
|
|
308 }
|
|
309
|
|
310 /**
|
|
311 * Must be called from JobManager#setPriority
|
|
312 */
|
|
313 final void internalSetPriority(int newPriority) {
|
|
314 this.priority = newPriority;
|
|
315 }
|
|
316
|
|
317 /**
|
|
318 * Must be called from JobManager#setRule
|
|
319 */
|
|
320 final void internalSetRule(ISchedulingRule rule) {
|
|
321 this.schedulingRule = rule;
|
|
322 }
|
|
323
|
|
324 /**
|
|
325 * Must be called from JobManager#changeState
|
|
326 */
|
|
327 final void internalSetState(int i) {
|
|
328 flags = (flags & ~M_STATE) | i;
|
|
329 }
|
|
330
|
|
331 /**
|
|
332 * Returns whether this job was canceled when it was about to run
|
|
333 */
|
|
334 final bool isAboutToRunCanceled() {
|
|
335 return (flags & M_ABOUT_TO_RUN_CANCELED) !is 0;
|
|
336 }
|
|
337
|
|
338 /* (non-Javadoc)
|
|
339 * @see Job#isBlocking()
|
|
340 */
|
|
341 protected bool isBlocking() {
|
|
342 return manager.isBlocking_package(this);
|
|
343 }
|
|
344
|
|
345 /**
|
|
346 * Returns true if this job conflicts with the given job, and false otherwise.
|
|
347 */
|
|
348 final bool isConflicting(InternalJob otherJob) {
|
|
349 ISchedulingRule otherRule = otherJob.getRule();
|
|
350 if (schedulingRule is null || otherRule is null)
|
|
351 return false;
|
|
352 //if one of the rules is a compound rule, it must be asked the question.
|
|
353 if (schedulingRule.classinfo is MultiRule.classinfo)
|
|
354 return schedulingRule.isConflicting(otherRule);
|
|
355 return otherRule.isConflicting(schedulingRule);
|
|
356 }
|
|
357
|
|
358 /* (non-javadoc)
|
|
359 * @see Job.isSystem()
|
|
360 */
|
|
361 protected bool isSystem() {
|
|
362 return (flags & M_SYSTEM) !is 0;
|
|
363 }
|
|
364 package bool isSystem_package() {
|
|
365 return isSystem();
|
|
366 }
|
|
367
|
|
368 /* (non-javadoc)
|
|
369 * @see Job.isUser()
|
|
370 */
|
|
371 protected bool isUser() {
|
|
372 return (flags & M_USER) !is 0;
|
|
373 }
|
|
374
|
|
375 /* (non-Javadoc)
|
|
376 * @see Job#join()
|
|
377 */
|
|
378 protected void join() {
|
|
379 manager.join_package(this);
|
|
380 }
|
|
381
|
|
382 /**
|
|
383 * Returns the next_ entry (ahead of this one) in the list, or null if there is no next_ entry
|
|
384 */
|
|
385 final InternalJob next() {
|
|
386 return next_;
|
|
387 }
|
|
388
|
|
389 /**
|
|
390 * Returns the previous_ entry (behind this one) in the list, or null if there is no previous_ entry
|
|
391 */
|
|
392 final InternalJob previous() {
|
|
393 return previous_;
|
|
394 }
|
|
395
|
|
396 /**
|
|
397 * Removes this entry from any list it belongs to. Returns the receiver.
|
|
398 */
|
|
399 final InternalJob remove() {
|
|
400 if (next_ !is null)
|
|
401 next_.setPrevious(previous_);
|
|
402 if (previous_ !is null)
|
|
403 previous_.setNext(next_);
|
|
404 next_ = previous_ = null;
|
|
405 return this;
|
|
406 }
|
|
407
|
|
408 /* (non-Javadoc)
|
|
409 * @see Job#removeJobListener(IJobChangeListener)
|
|
410 */
|
|
411 protected void removeJobChangeListener(IJobChangeListener listener) {
|
|
412 if (listeners !is null)
|
|
413 listeners.remove(cast(Object)listener);
|
|
414 }
|
|
415 package void removeJobChangeListener_package(IJobChangeListener listener) {
|
|
416 removeJobChangeListener(listener);
|
|
417 }
|
|
418
|
|
419 /* (non-Javadoc)
|
|
420 * @see Job#run(IProgressMonitor)
|
|
421 */
|
|
422 protected abstract IStatus run(IProgressMonitor progressMonitor);
|
|
423 package IStatus run_package(IProgressMonitor progressMonitor){
|
|
424 return run(progressMonitor);
|
|
425 }
|
|
426
|
|
427 /* (non-Javadoc)
|
|
428 * @see Job#schedule(long)
|
|
429 */
|
|
430 protected void schedule(long delay) {
|
|
431 if (shouldSchedule())
|
|
432 manager.schedule_package(this, delay, false);
|
|
433 }
|
|
434 package void schedule_package(long delay) {
|
|
435 schedule(delay);
|
|
436 }
|
|
437
|
|
438 /**
|
|
439 * Sets whether this job was canceled when it was about to run
|
|
440 */
|
|
441 final void setAboutToRunCanceled(bool value) {
|
|
442 flags = value ? flags | M_ABOUT_TO_RUN_CANCELED : flags & ~M_ABOUT_TO_RUN_CANCELED;
|
|
443
|
|
444 }
|
|
445
|
|
446 /* (non-Javadoc)
|
|
447 * @see Job#setName(String)
|
|
448 */
|
|
449 protected void setName(String name) {
|
|
450 Assert.isNotNull(name);
|
|
451 this.name = name;
|
|
452 }
|
|
453
|
|
454 /**
|
|
455 * Sets the next_ entry in this linked list of jobs.
|
|
456 * @param entry
|
|
457 */
|
|
458 final void setNext(InternalJob entry) {
|
|
459 this.next_ = entry;
|
|
460 }
|
|
461
|
|
462 /**
|
|
463 * Sets the previous_ entry in this linked list of jobs.
|
|
464 * @param entry
|
|
465 */
|
|
466 final void setPrevious(InternalJob entry) {
|
|
467 this.previous_ = entry;
|
|
468 }
|
|
469
|
|
470 /* (non-Javadoc)
|
|
471 * @see Job#setPriority(int)
|
|
472 */
|
|
473 protected void setPriority(int newPriority) {
|
|
474 switch (newPriority) {
|
|
475 case Job.INTERACTIVE :
|
|
476 case Job.SHORT :
|
|
477 case Job.LONG :
|
|
478 case Job.BUILD :
|
|
479 case Job.DECORATE :
|
|
480 manager.setPriority_package(this, newPriority);
|
|
481 break;
|
|
482 default :
|
|
483 throw new IllegalArgumentException(Integer.toString(newPriority));
|
|
484 }
|
|
485 }
|
|
486
|
|
487 /* (non-Javadoc)
|
|
488 * @see Job#setProgressGroup(IProgressMonitor, int)
|
|
489 */
|
|
490 protected void setProgressGroup(IProgressMonitor group, int ticks) {
|
|
491 Assert.isNotNull(cast(Object)group);
|
|
492 IProgressMonitor pm = manager.createMonitor_package(this, group, ticks);
|
|
493 if (pm !is null)
|
|
494 setProgressMonitor(pm);
|
|
495 }
|
|
496
|
|
497 /**
|
|
498 * Sets the progress monitor to use for the next_ execution of this job,
|
|
499 * or for clearing the monitor when a job completes.
|
|
500 * @param monitor a progress monitor
|
|
501 */
|
|
502 final void setProgressMonitor(IProgressMonitor monitor) {
|
|
503 this.monitor = monitor;
|
|
504 }
|
|
505
|
|
506 /* (non-Javadoc)
|
|
507 * @see Job#setProperty(QualifiedName,Object)
|
|
508 */
|
|
509 protected void setProperty(QualifiedName key, Object value) {
|
|
510 // thread safety: (Concurrency001 - copy on write)
|
|
511 if (value is null) {
|
|
512 if (properties is null)
|
|
513 return;
|
|
514 ObjectMap temp = cast(ObjectMap) properties.clone();
|
|
515 temp.remove(key);
|
|
516 if (temp.isEmpty())
|
|
517 properties = null;
|
|
518 else
|
|
519 properties = temp;
|
|
520 } else {
|
|
521 ObjectMap temp = properties;
|
|
522 if (temp is null)
|
|
523 temp = new ObjectMap(5);
|
|
524 else
|
|
525 temp = cast(ObjectMap) properties.clone();
|
|
526 temp.put(key, value);
|
|
527 properties = temp;
|
|
528 }
|
|
529 }
|
|
530
|
|
531 /**
|
|
532 * Sets or clears the result of an execution of this job.
|
|
533 * @param result a result status, or <code>null</code>
|
|
534 */
|
|
535 final void setResult(IStatus result) {
|
|
536 this.result = result;
|
|
537 }
|
|
538
|
|
539 /* (non-Javadoc)
|
|
540 * @see Job#setRule(ISchedulingRule)
|
|
541 */
|
|
542 protected void setRule(ISchedulingRule rule) {
|
|
543 manager.setRule(this, rule);
|
|
544 }
|
|
545
|
|
546 /**
|
|
547 * Sets a time to start, wake up, or schedule this job,
|
|
548 * depending on the current state
|
|
549 * @param time a time in milliseconds
|
|
550 */
|
|
551 final void setStartTime(long time) {
|
|
552 startTime = time;
|
|
553 }
|
|
554
|
|
555 /* (non-javadoc)
|
|
556 * @see Job.setSystem
|
|
557 */
|
|
558 protected void setSystem(bool value) {
|
|
559 if (getState() !is Job.NONE)
|
|
560 throw new IllegalStateException();
|
|
561 flags = value ? flags | M_SYSTEM : flags & ~M_SYSTEM;
|
|
562 }
|
|
563
|
|
564 /* (non-javadoc)
|
|
565 * @see Job.setThread
|
|
566 */
|
|
567 protected void setThread(Thread thread) {
|
|
568 this.thread = thread;
|
|
569 }
|
|
570 package void setThread_package(Thread thread) {
|
|
571 setThread(thread);
|
|
572 }
|
|
573
|
|
574 /* (non-javadoc)
|
|
575 * @see Job.setUser
|
|
576 */
|
|
577 protected void setUser(bool value) {
|
|
578 if (getState() !is Job.NONE)
|
|
579 throw new IllegalStateException();
|
|
580 flags = value ? flags | M_USER : flags & ~M_USER;
|
|
581 }
|
|
582
|
|
583 /* (Non-javadoc)
|
|
584 * @see Job#shouldSchedule
|
|
585 */
|
|
586 protected bool shouldSchedule() {
|
|
587 return true;
|
|
588 }
|
|
589 package bool shouldSchedule_package() {
|
|
590 return shouldSchedule();
|
|
591 }
|
|
592
|
|
593 /* (non-Javadoc)
|
|
594 * @see Job#sleep()
|
|
595 */
|
|
596 protected bool sleep() {
|
|
597 return manager.sleep_package(this);
|
|
598 }
|
|
599
|
|
600 /* (non-Javadoc)
|
|
601 * Prints a string-based representation of this job instance.
|
|
602 * For debugging purposes only.
|
|
603 */
|
|
604 public String toString() {
|
|
605 return Format( "{}({})", getName(), jobNumber ); //$NON-NLS-1$//$NON-NLS-2$
|
|
606 }
|
|
607
|
|
608 /* (non-Javadoc)
|
|
609 * @see Job#wakeUp(long)
|
|
610 */
|
|
611 protected void wakeUp(long delay) {
|
|
612 manager.wakeUp_package(this, delay);
|
|
613 }
|
|
614
|
|
615 /**
|
|
616 * @param waitQueueStamp The waitQueueStamp to set.
|
|
617 */
|
|
618 void setWaitQueueStamp(long waitQueueStamp) {
|
|
619 this.waitQueueStamp = waitQueueStamp;
|
|
620 }
|
|
621
|
|
622 /**
|
|
623 * @return Returns the waitQueueStamp.
|
|
624 */
|
|
625 long getWaitQueueStamp() {
|
|
626 return waitQueueStamp;
|
|
627 }
|
|
628 }
|