comparison org.eclipse.core.jobs/src/org/eclipse/core/runtime/jobs/Job.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children 52184e4b815c
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.core.runtime.jobs.Job;
14
15 import java.lang.JThread;
16 import java.lang.all;
17 import java.util.Set;
18
19 import org.eclipse.core.internal.jobs.InternalJob;
20 import org.eclipse.core.internal.jobs.JobManager;
21 import org.eclipse.core.runtime.IAdaptable;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.QualifiedName;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.core.runtime.jobs.IJobManager;
27 import org.eclipse.core.runtime.jobs.IJobChangeListener;
28 import org.eclipse.core.runtime.jobs.ISchedulingRule;
29
30 /**
31 * Jobs are units of runnable work that can be scheduled to be run with the job
32 * manager. Once a job has completed, it can be scheduled to run again (jobs are
33 * reusable).
34 * <p>
35 * Jobs have a state that indicates what they are currently doing. When constructed,
36 * jobs start with a state value of <code>NONE</code>. When a job is scheduled
37 * to be run, it moves into the <code>WAITING</code> state. When a job starts
38 * running, it moves into the <code>RUNNING</code> state. When execution finishes
39 * (either normally or through cancelation), the state changes back to
40 * <code>NONE</code>.
41 * </p><p>
42 * A job can also be in the <code>SLEEPING</code> state. This happens if a user
43 * calls Job.sleep() on a waiting job, or if a job is scheduled to run after a specified
44 * delay. Only jobs in the <code>WAITING</code> state can be put to sleep.
45 * Sleeping jobs can be woken at any time using Job.wakeUp(), which will put the
46 * job back into the <code>WAITING</code> state.
47 * </p><p>
48 * Jobs can be assigned a priority that is used as a hint about how the job should
49 * be scheduled. There is no guarantee that jobs of one priority will be run before
50 * all jobs of lower priority. The javadoc for the various priority constants provide
51 * more detail about what each priority means. By default, jobs start in the
52 * <code>LONG</code> priority class.
53 *
54 * @see IJobManager
55 * @since 3.0
56 */
57 public abstract class Job : InternalJob, IAdaptable {
58 // SWT from IAdaptable
59 public Object getAdapter(ClassInfo adapter){
60 return super.getAdapter(adapter);
61 }
62
63 /**
64 * Job status return value that is used to indicate asynchronous job completion.
65 * @see Job#run(IProgressMonitor)
66 * @see Job#done(IStatus)
67 */
68 private static IStatus ASYNC_FINISH_;
69 public static IStatus ASYNC_FINISH(){
70 if( ASYNC_FINISH_ is null ){
71 synchronized( Job.classinfo ) {
72 if( ASYNC_FINISH_ is null ){
73 ASYNC_FINISH_ = new Status(IStatus.OK, JobManager.PI_JOBS, 1, "", null);//$NON-NLS-1$
74 }
75 }
76 }
77 return ASYNC_FINISH_;
78 }
79
80 /* Job priorities */
81 /**
82 * Job priority constant (value 10) for interactive jobs.
83 * Interactive jobs generally have priority over all other jobs.
84 * Interactive jobs should be either fast running or very low on CPU
85 * usage to avoid blocking other interactive jobs from running.
86 *
87 * @see #getPriority()
88 * @see #setPriority(int)
89 * @see #run(IProgressMonitor)
90 */
91 public static const int INTERACTIVE = 10;
92 /**
93 * Job priority constant (value 20) for short background jobs.
94 * Short background jobs are jobs that typically complete within a second,
95 * but may take longer in some cases. Short jobs are given priority
96 * over all other jobs except interactive jobs.
97 *
98 * @see #getPriority()
99 * @see #setPriority(int)
100 * @see #run(IProgressMonitor)
101 */
102 public static const int SHORT = 20;
103 /**
104 * Job priority constant (value 30) for long-running background jobs.
105 *
106 * @see #getPriority()
107 * @see #setPriority(int)
108 * @see #run(IProgressMonitor)
109 */
110 public static const int LONG = 30;
111 /**
112 * Job priority constant (value 40) for build jobs. Build jobs are
113 * generally run after all other background jobs complete.
114 *
115 * @see #getPriority()
116 * @see #setPriority(int)
117 * @see #run(IProgressMonitor)
118 */
119 public static const int BUILD = 40;
120
121 /**
122 * Job priority constant (value 50) for decoration jobs.
123 * Decoration jobs have lowest priority. Decoration jobs generally
124 * compute extra information that the user may be interested in seeing
125 * but is generally not waiting for.
126 *
127 * @see #getPriority()
128 * @see #setPriority(int)
129 * @see #run(IProgressMonitor)
130 */
131 public static const int DECORATE = 50;
132 /**
133 * Job state code (value 0) indicating that a job is not
134 * currently sleeping, waiting, or running (i.e., the job manager doesn't know
135 * anything about the job).
136 *
137 * @see #getState()
138 */
139 public static const int NONE = 0;
140 /**
141 * Job state code (value 1) indicating that a job is sleeping.
142 *
143 * @see #run(IProgressMonitor)
144 * @see #getState()
145 */
146 public static const int SLEEPING = 0x01;
147 /**
148 * Job state code (value 2) indicating that a job is waiting to be run.
149 *
150 * @see #getState()
151 */
152 public static const int WAITING = 0x02;
153 /**
154 * Job state code (value 4) indicating that a job is currently running
155 *
156 * @see #getState()
157 */
158 public static const int RUNNING = 0x04;
159
160 /**
161 * Returns the job manager.
162 *
163 * @return the job manager
164 * @since org.eclipse.core.jobs 3.2
165 */
166 public static final IJobManager getJobManager() {
167 return manager;
168 }
169
170 /**
171 * Creates a new job with the specified name. The job name is a human-readable
172 * value that is displayed to users. The name does not need to be unique, but it
173 * must not be <code>null</code>.
174 *
175 * @param name the name of the job.
176 */
177 public this(String name) {
178 super(name);
179 }
180
181 /**
182 * Registers a job listener with this job
183 * Has no effect if an identical listener is already registered.
184 *
185 * @param listener the listener to be added.
186 */
187 public final void addJobChangeListener(IJobChangeListener listener) {
188 super.addJobChangeListener(listener);
189 }
190
191 /**
192 * Returns whether this job belongs to the given family. Job families are
193 * represented as objects that are not interpreted or specified in any way
194 * by the job manager. Thus, a job can choose to belong to any number of
195 * families.
196 * <p>
197 * Clients may override this method. This default implementation always returns
198 * <code>false</code>. Overriding implementations must return <code>false</code>
199 * for families they do not recognize.
200 * </p>
201 *
202 * @param family the job family identifier
203 * @return <code>true</code> if this job belongs to the given family, and
204 * <code>false</code> otherwise.
205 */
206 public bool belongsTo(Object family) {
207 return false;
208 }
209
210 /**
211 * Stops the job. If the job is currently waiting,
212 * it will be removed from the queue. If the job is sleeping,
213 * it will be discarded without having a chance to resume and its sleeping state
214 * will be cleared. If the job is currently executing, it will be asked to
215 * stop but there is no guarantee that it will do so.
216 *
217 * @return <code>false</code> if the job is currently running (and thus may not
218 * respond to cancelation), and <code>true</code> in all other cases.
219 */
220 public final bool cancel() {
221 return super.cancel();
222 }
223
224 /**
225 * A hook method indicating that this job is running and {@link #cancel()}
226 * is being called for the first time.
227 * <p>
228 * Subclasses may override this method to perform additional work when
229 * a cancelation request is made. This default implementation does nothing.
230 * @since 3.3
231 */
232 protected void canceling() {
233 //default implementation does nothing
234 }
235
236 /**
237 * Jobs that complete their execution asynchronously must indicate when they
238 * are finished by calling this method. This method must not be called by
239 * a job that has not indicated that it is executing asynchronously.
240 * <p>
241 * This method must not be called from within the scope of a job's <code>run</code>
242 * method. Jobs should normally indicate completion by returning an appropriate
243 * status from the <code>run</code> method. Jobs that return a status of
244 * <code>ASYNC_FINISH</code> from their run method must later call
245 * <code>done</code> to indicate completion.
246 *
247 * @param result a status object indicating the result of the job's execution.
248 * @see #ASYNC_FINISH
249 * @see #run(IProgressMonitor)
250 */
251 public final void done(IStatus result) {
252 super.done(result);
253 }
254
255 /**
256 * Returns the human readable name of this job. The name is never
257 * <code>null</code>.
258 *
259 * @return the name of this job
260 */
261 public final String getName() {
262 return super.getName();
263 }
264
265 /**
266 * Returns the priority of this job. The priority is used as a hint when the job
267 * is scheduled to be run.
268 *
269 * @return the priority of the job. One of INTERACTIVE, SHORT, LONG, BUILD,
270 * or DECORATE.
271 */
272 public final int getPriority() {
273 return super.getPriority();
274 }
275
276 /**
277 * Returns the value of the property of this job identified by the given key,
278 * or <code>null</code> if this job has no such property.
279 *
280 * @param key the name of the property
281 * @return the value of the property,
282 * or <code>null</code> if this job has no such property
283 * @see #setProperty(QualifiedName, Object)
284 */
285 public final Object getProperty(QualifiedName key) {
286 return super.getProperty(key);
287 }
288
289 /**
290 * Returns the result of this job's last run.
291 *
292 * @return the result of this job's last run, or <code>null</code> if this
293 * job has never finished running.
294 */
295 public final IStatus getResult() {
296 return super.getResult();
297 }
298
299 /**
300 * Returns the scheduling rule for this job. Returns <code>null</code> if this job has no
301 * scheduling rule.
302 *
303 * @return the scheduling rule for this job, or <code>null</code>.
304 * @see ISchedulingRule
305 * @see #setRule(ISchedulingRule)
306 */
307 public final ISchedulingRule getRule() {
308 return super.getRule();
309 }
310
311 /**
312 * Returns the state of the job. Result will be one of:
313 * <ul>
314 * <li><code>Job.RUNNING</code> - if the job is currently running.</li>
315 * <li><code>Job.WAITING</code> - if the job is waiting to be run.</li>
316 * <li><code>Job.SLEEPING</code> - if the job is sleeping.</li>
317 * <li><code>Job.NONE</code> - in all other cases.</li>
318 * </ul>
319 * <p>
320 * Note that job state is inherently volatile, and in most cases clients
321 * cannot rely on the result of this method being valid by the time the
322 * result is obtained. For example, if <tt>getState</tt> returns
323 * <tt>RUNNING</tt>, the job may have actually completed by the
324 * time the <tt>getState</tt> method returns. All clients can infer from
325 * invoking this method is that the job was recently in the returned state.
326 *
327 * @return the job state
328 */
329 public final int getState() {
330 return super.getState();
331 }
332
333 /**
334 * Returns the thread that this job is currently running in.
335 *
336 * @return the thread this job is running in, or <code>null</code>
337 * if this job is not running or the thread is unknown.
338 */
339 public final JThread getThread() {
340 return super.getThread();
341 }
342
343 /**
344 * Returns whether this job is blocking a higher priority non-system job from
345 * starting due to a conflicting scheduling rule. Returns <code>false</code>
346 * if this job is not running, or is not blocking a higher priority non-system job.
347 *
348 * @return <code>true</code> if this job is blocking a higher priority non-system
349 * job, and <code>false</code> otherwise.
350 * @see #getRule()
351 * @see #isSystem()
352 */
353 public final bool isBlocking() {
354 return super.isBlocking();
355 }
356
357 /**
358 * Returns whether this job is a system job. System jobs are typically not
359 * revealed to users in any UI presentation of jobs. Other than their UI presentation,
360 * system jobs act exactly like other jobs. If this value is not explicitly set, jobs
361 * are treated as non-system jobs. The default value is <code>false</code>.
362 *
363 * @return <code>true</code> if this job is a system job, and
364 * <code>false</code> otherwise.
365 * @see #setSystem(bool)
366 */
367 public final bool isSystem() {
368 return super.isSystem();
369 }
370
371 /**
372 * Returns whether this job has been directly initiated by a UI end user.
373 * These jobs may be presented differently in the UI. The default value
374 * is <code>false</code>.
375 *
376 * @return <code>true</code> if this job is a user-initiated job, and
377 * <code>false</code> otherwise.
378 * @see #setUser(bool)
379 */
380 public final bool isUser() {
381 return super.isUser();
382 }
383
384 /**
385 * Waits until this job is finished. This method will block the calling thread until the
386 * job has finished executing, or until this thread has been interrupted. If the job
387 * has not been scheduled, this method returns immediately. A job must not
388 * be joined from within the scope of its run method.
389 * <p>
390 * If this method is called on a job that reschedules itself from within the
391 * <tt>run</tt> method, the join will return at the end of the first execution.
392 * In other words, join will return the first time this job exits the
393 * {@link #RUNNING} state, or as soon as this job enters the {@link #NONE} state.
394 * </p>
395 * <p>
396 * If this method is called while the job manager is suspended, this job
397 * will only be joined if it is already running; if this job is waiting or sleeping,
398 * this method returns immediately.
399 * </p>
400 * <p>
401 * Note that there is a deadlock risk when using join. If the calling thread owns
402 * a lock or object monitor that the joined thread is waiting for, deadlock
403 * will occur.
404 * </p>
405 *
406 * @exception InterruptedException if this thread is interrupted while waiting
407 * @see ILock
408 * @see IJobManager#suspend()
409 */
410 public final void join() {
411 super.join();
412 }
413
414 /**
415 * Removes a job listener from this job.
416 * Has no effect if an identical listener is not already registered.
417 *
418 * @param listener the listener to be removed
419 */
420 public final void removeJobChangeListener(IJobChangeListener listener) {
421 super.removeJobChangeListener(listener);
422 }
423
424 /**
425 * Executes this job. Returns the result of the execution.
426 * <p>
427 * The provided monitor can be used to report progress and respond to
428 * cancellation. If the progress monitor has been canceled, the job
429 * should finish its execution at the earliest convenience and return a result
430 * status of severity {@link IStatus#CANCEL}. The singleton
431 * cancel status {@link Status#CANCEL_STATUS} can be used for
432 * this purpose. The monitor is only valid for the duration of the invocation
433 * of this method.
434 * <p>
435 * This method must not be called directly by clients. Clients should call
436 * <code>schedule</code>, which will in turn cause this method to be called.
437 * <p>
438 * Jobs can optionally finish their execution asynchronously (in another thread) by
439 * returning a result status of {@link #ASYNC_FINISH}. Jobs that finish
440 * asynchronously <b>must</b> specify the execution thread by calling
441 * <code>setThread</code>, and must indicate when they are finished by calling
442 * the method <code>done</code>.
443 *
444 * @param monitor the monitor to be used for reporting progress and
445 * responding to cancelation. The monitor is never <code>null</code>
446 * @return resulting status of the run. The result must not be <code>null</code>
447 * @see #ASYNC_FINISH
448 * @see #done(IStatus)
449 */
450 protected abstract IStatus run(IProgressMonitor monitor);
451
452 /**
453 * Schedules this job to be run. The job is added to a queue of waiting
454 * jobs, and will be run when it arrives at the beginning of the queue.
455 * <p>
456 * This is a convenience method, fully equivalent to
457 * <code>schedule(0L)</code>.
458 * </p>
459 * @see #schedule(long)
460 */
461 public final void schedule() {
462 super.schedule(0L);
463 }
464
465 /**
466 * Schedules this job to be run after a specified delay. The job is put in the
467 * {@link #SLEEPING} state until the specified delay has elapsed, after which
468 * the job is added to a queue of {@link #WAITING} jobs. Once the job arrives
469 * at the beginning of the queue, it will be run at the first available opportunity.
470 * </p><p>
471 * Jobs of equal priority and <code>delay</code> with conflicting scheduling
472 * rules are guaranteed to run in the order they are scheduled. No guarantees
473 * are made about the relative execution order of jobs with unrelated or
474 * <code>null</code> scheduling rules, or different priorities.
475 * <p>
476 * If this job is currently running, it will be rescheduled with the specified
477 * delay as soon as it finishes. If this method is called multiple times
478 * while the job is running, the job will still only be rescheduled once,
479 * with the most recent delay value that was provided.
480 * </p><p>
481 * Scheduling a job that is waiting or sleeping has no effect.
482 * </p>
483 *
484 * @param delay a time delay in milliseconds before the job should run
485 * @see ISchedulingRule
486 */
487 public final void schedule(long delay) {
488 super.schedule(delay);
489 }
490
491 /**
492 * Changes the name of this job. If the job is currently running, waiting,
493 * or sleeping, the new job name may not take effect until the next time the
494 * job is scheduled.
495 * <p>
496 * The job name is a human-readable value that is displayed to users. The name
497 * does not need to be unique, but it must not be <code>null</code>.
498 *
499 * @param name the name of the job.
500 */
501 public final void setName(String name) {
502 super.setName(name);
503 }
504
505 /**
506 * Sets the priority of the job. This will not affect the execution of
507 * a running job, but it will affect how the job is scheduled while
508 * it is waiting to be run.
509 *
510 * @param priority the new job priority. One of
511 * INTERACTIVE, SHORT, LONG, BUILD, or DECORATE.
512 */
513 public final void setPriority(int priority) {
514 super.setPriority(priority);
515 }
516
517 /**
518 * Associates this job with a progress group. Progress feedback
519 * on this job's next execution will be displayed together with other
520 * jobs in that group. The provided monitor must be a monitor
521 * created by the method <tt>IJobManager.createProgressGroup</tt>
522 * and must have at least <code>ticks</code> units of available work.
523 * <p>
524 * The progress group must be set before the job is scheduled.
525 * The group will be used only for a single invocation of the job's
526 * <tt>run</tt> method, after which any association of this job to the
527 * group will be lost.
528 *
529 * @see IJobManager#createProgressGroup()
530 * @param group The progress group to use for this job
531 * @param ticks the number of work ticks allocated from the
532 * parent monitor, or {@link IProgressMonitor#UNKNOWN}
533 */
534 public final void setProgressGroup(IProgressMonitor group, int ticks) {
535 super.setProgressGroup(group, ticks);
536 }
537
538 /**
539 * Sets the value of the property of this job identified
540 * by the given key. If the supplied value is <code>null</code>,
541 * the property is removed from this resource.
542 * <p>
543 * Properties are intended to be used as a caching mechanism
544 * by ISV plug-ins. They allow key-object associations to be stored with
545 * a job instance. These key-value associations are maintained in
546 * memory (at all times), and the information is never discarded automatically.
547 * </p><p>
548 * The qualifier part of the property name must be the unique identifier
549 * of the declaring plug-in (e.g. <code>"com.example.plugin"</code>).
550 * </p>
551 *
552 * @param key the qualified name of the property
553 * @param value the value of the property,
554 * or <code>null</code> if the property is to be removed
555 * @see #getProperty(QualifiedName)
556 */
557 public void setProperty(QualifiedName key, Object value) {
558 super.setProperty(key, value);
559 }
560
561 /**
562 * Sets the scheduling rule to be used when scheduling this job. This method
563 * must be called before the job is scheduled.
564 *
565 * @param rule the new scheduling rule, or <code>null</code> if the job
566 * should have no scheduling rule
567 * @see #getRule()
568 */
569 public final void setRule(ISchedulingRule rule) {
570 super.setRule(rule);
571 }
572
573 /**
574 * Sets whether or not this job is a system job. System jobs are typically not
575 * revealed to users in any UI presentation of jobs. Other than their UI presentation,
576 * system jobs act exactly like other jobs. If this value is not explicitly set, jobs
577 * are treated as non-system jobs. This method must be called before the job
578 * is scheduled.
579 *
580 * @param value <code>true</code> if this job should be a system job, and
581 * <code>false</code> otherwise.
582 * @see #isSystem()
583 */
584 public final void setSystem(bool value) {
585 super.setSystem(value);
586 }
587
588 /**
589 * Sets whether or not this job has been directly initiated by a UI end user.
590 * These jobs may be presented differently in the UI. This method must be
591 * called before the job is scheduled.
592 *
593 * @param value <code>true</code> if this job is a user-initiated job, and
594 * <code>false</code> otherwise.
595 * @see #isUser()
596 */
597 public final void setUser(bool value) {
598 super.setUser(value);
599 }
600
601 /**
602 * Sets the thread that this job is currently running in, or <code>null</code>
603 * if this job is not running or the thread is unknown.
604 * <p>
605 * Jobs that use the {@link #ASYNC_FINISH} return code should tell
606 * the job what thread it is running in. This is used to prevent deadlocks.
607 *
608 * @param thread the thread that this job is running in.
609 *
610 * @see #ASYNC_FINISH
611 * @see #run(IProgressMonitor)
612 */
613 public final void setThread(JThread thread) {
614 super.setThread(thread);
615 }
616
617 /**
618 * Returns whether this job should be run.
619 * If <code>false</code> is returned, this job will be discarded by the job manager
620 * without running.
621 * <p>
622 * This method is called immediately prior to calling the job's
623 * run method, so it can be used for last minute pre-condition checking before
624 * a job is run. This method must not attempt to schedule or change the
625 * state of any other job.
626 * </p><p>
627 * Clients may override this method. This default implementation always returns
628 * <code>true</code>.
629 * </p>
630 *
631 * @return <code>true</code> if this job should be run
632 * and <code>false</code> otherwise
633 */
634 public bool shouldRun() {
635 return true;
636 }
637
638 /**
639 * Returns whether this job should be scheduled.
640 * If <code>false</code> is returned, this job will be discarded by the job manager
641 * without being added to the queue.
642 * <p>
643 * This method is called immediately prior to adding the job to the waiting job
644 * queue.,so it can be used for last minute pre-condition checking before
645 * a job is scheduled.
646 * </p><p>
647 * Clients may override this method. This default implementation always returns
648 * <code>true</code>.
649 * </p>
650 *
651 * @return <code>true</code> if the job manager should schedule this job
652 * and <code>false</code> otherwise
653 */
654 public bool shouldSchedule() {
655 return true;
656 }
657
658 /**
659 * Requests that this job be suspended. If the job is currently waiting to be run, it
660 * will be removed from the queue move into the {@link #SLEEPING} state.
661 * The job will remain asleep until either resumed or canceled. If this job is not
662 * currently waiting to be run, this method has no effect.
663 * <p>
664 * Sleeping jobs can be resumed using <code>wakeUp</code>.
665 *
666 * @return <code>false</code> if the job is currently running (and thus cannot
667 * be put to sleep), and <code>true</code> in all other cases
668 * @see #wakeUp()
669 */
670 public final bool sleep() {
671 return super.sleep();
672 }
673
674 /**
675 * Puts this job immediately into the {@link #WAITING} state so that it is
676 * eligible for immediate execution. If this job is not currently sleeping,
677 * the request is ignored.
678 * <p>
679 * This is a convenience method, fully equivalent to
680 * <code>wakeUp(0L)</code>.
681 * </p>
682 * @see #sleep()
683 */
684 public final void wakeUp() {
685 super.wakeUp(0L);
686 }
687
688 /**
689 * Puts this job back into the {@link #WAITING} state after
690 * the specified delay. This is equivalent to canceling the sleeping job and
691 * rescheduling with the given delay. If this job is not currently sleeping,
692 * the request is ignored.
693 *
694 * @param delay the number of milliseconds to delay
695 * @see #sleep()
696 */
697 public final void wakeUp(long delay) {
698 super.wakeUp(delay);
699 }
700 }