1 11 package org.eclipse.core.internal.jobs; 12 13 import java.util.Map ; 14 import org.eclipse.core.runtime.*; 15 import org.eclipse.core.runtime.jobs.*; 16 17 21 public abstract class InternalJob extends PlatformObject implements Comparable { 22 27 static final int ABOUT_TO_RUN = 0x10; 28 29 34 static final int ABOUT_TO_SCHEDULE = 0x20; 35 39 static final int BLOCKED = 0x08; 40 41 private static final int M_STATE = 0xFF; 43 private static final int M_SYSTEM = 0x0100; 44 private static final int M_USER = 0x0200; 45 46 49 private static final int M_ABOUT_TO_RUN_CANCELED = 0x0400; 50 51 protected static final JobManager manager = JobManager.getInstance(); 52 private static int nextJobNumber = 0; 53 54 58 static final long T_INFINITE = Long.MAX_VALUE; 59 62 static final long T_NONE = -1; 63 64 private volatile int flags = Job.NONE; 65 private final int jobNumber = nextJobNumber++; 66 private ListenerList listeners = null; 67 private IProgressMonitor monitor; 68 private String name; 69 72 private InternalJob next; 73 76 private InternalJob previous; 77 private int priority = Job.LONG; 78 82 private ObjectMap properties; 83 private IStatus result; 84 private ISchedulingRule schedulingRule; 85 91 private long startTime; 92 95 private volatile Thread thread = null; 96 97 protected InternalJob(String name) { 98 Assert.isNotNull(name); 99 this.name = name; 100 } 101 102 105 protected void addJobChangeListener(IJobChangeListener listener) { 106 if (listeners == null) 107 listeners = new ListenerList(ListenerList.IDENTITY); 108 listeners.add(listener); 109 } 110 111 114 final void addLast(InternalJob entry) { 115 InternalJob last = this; 116 while (last.previous != null) 118 last = last.previous; 119 last.previous = entry; 121 entry.next = last; 122 entry.previous = null; 123 } 124 125 128 protected boolean belongsTo(Object family) { 129 return false; 130 } 131 132 135 protected boolean cancel() { 136 return manager.cancel(this); 137 } 138 139 142 protected void canceling() { 143 } 145 146 149 public final int compareTo(Object otherJob) { 150 return ((InternalJob) otherJob).startTime >= startTime ? 1 : -1; 151 } 152 153 156 protected void done(IStatus endResult) { 157 manager.endJob(this, endResult, true); 158 } 159 160 164 final ListenerList getListeners() { 165 return listeners; 166 } 167 168 171 protected String getName() { 172 return name; 173 } 174 175 178 protected int getPriority() { 179 return priority; 180 } 181 182 185 final IProgressMonitor getProgressMonitor() { 186 return monitor; 187 } 188 189 192 protected Object getProperty(QualifiedName key) { 193 Map temp = properties; 195 if (temp == null) 196 return null; 197 return temp.get(key); 198 } 199 200 203 protected IStatus getResult() { 204 return result; 205 } 206 207 210 protected ISchedulingRule getRule() { 211 return schedulingRule; 212 } 213 214 219 final long getStartTime() { 220 return startTime; 221 } 222 223 226 protected int getState() { 227 int state = flags & M_STATE; 228 switch (state) { 229 case BLOCKED : 231 return Job.WAITING; 232 case ABOUT_TO_RUN : 233 return Job.RUNNING; 234 case ABOUT_TO_SCHEDULE : 235 return Job.WAITING; 236 default : 237 return state; 238 } 239 } 240 241 244 protected Thread getThread() { 245 return thread; 246 } 247 248 251 final int internalGetState() { 252 return flags & M_STATE; 253 } 254 255 258 final void internalSetPriority(int newPriority) { 259 this.priority = newPriority; 260 } 261 262 265 final void internalSetRule(ISchedulingRule rule) { 266 this.schedulingRule = rule; 267 } 268 269 272 final void internalSetState(int i) { 273 flags = (flags & ~M_STATE) | i; 274 } 275 276 279 final boolean isAboutToRunCanceled(){ 280 return (flags & M_ABOUT_TO_RUN_CANCELED) != 0; 281 } 282 283 286 protected boolean isBlocking() { 287 return manager.isBlocking(this); 288 } 289 290 293 final boolean isConflicting(InternalJob otherJob) { 294 ISchedulingRule otherRule = otherJob.getRule(); 295 if (schedulingRule == null || otherRule == null) 296 return false; 297 if (schedulingRule.getClass() == MultiRule.class) 299 return schedulingRule.isConflicting(otherRule); 300 return otherRule.isConflicting(schedulingRule); 301 } 302 303 306 protected boolean isSystem() { 307 return (flags & M_SYSTEM) != 0; 308 } 309 310 313 protected boolean isUser() { 314 return (flags & M_USER) != 0; 315 } 316 317 320 protected void join() throws InterruptedException { 321 manager.join(this); 322 } 323 324 327 final InternalJob next() { 328 return next; 329 } 330 331 334 final InternalJob previous() { 335 return previous; 336 } 337 338 341 final InternalJob remove() { 342 if (next != null) 343 next.setPrevious(previous); 344 if (previous != null) 345 previous.setNext(next); 346 next = previous = null; 347 return this; 348 } 349 350 353 protected void removeJobChangeListener(IJobChangeListener listener) { 354 if (listeners != null) 355 listeners.remove(listener); 356 } 357 358 361 protected abstract IStatus run(IProgressMonitor progressMonitor); 362 363 366 protected void schedule(long delay) { 367 if (shouldSchedule()) 368 manager.schedule(this, delay, false); 369 } 370 371 374 final void setAboutToRunCanceled(boolean value) { 375 flags = value ? flags | M_ABOUT_TO_RUN_CANCELED : flags & ~M_ABOUT_TO_RUN_CANCELED; 376 377 } 378 379 382 protected void setName(String name) { 383 Assert.isNotNull(name); 384 this.name = name; 385 } 386 387 391 final void setNext(InternalJob entry) { 392 this.next = entry; 393 } 394 395 399 final void setPrevious(InternalJob entry) { 400 this.previous = entry; 401 } 402 403 406 protected void setPriority(int newPriority) { 407 switch (newPriority) { 408 case Job.INTERACTIVE : 409 case Job.SHORT : 410 case Job.LONG : 411 case Job.BUILD : 412 case Job.DECORATE : 413 manager.setPriority(this, newPriority); 414 break; 415 default : 416 throw new IllegalArgumentException (String.valueOf(newPriority)); 417 } 418 } 419 420 423 protected void setProgressGroup(IProgressMonitor group, int ticks) { 424 Assert.isNotNull(group); 425 IProgressMonitor pm = manager.createMonitor(this, group, ticks); 426 if (pm != null) 427 setProgressMonitor(pm); 428 } 429 430 435 final void setProgressMonitor(IProgressMonitor monitor) { 436 this.monitor = monitor; 437 } 438 439 442 protected void setProperty(QualifiedName key, Object value) { 443 if (value == null) { 445 if (properties == null) 446 return; 447 ObjectMap temp = (ObjectMap) properties.clone(); 448 temp.remove(key); 449 if (temp.isEmpty()) 450 properties = null; 451 else 452 properties = temp; 453 } else { 454 ObjectMap temp = properties; 455 if (temp == null) 456 temp = new ObjectMap(5); 457 else 458 temp = (ObjectMap) properties.clone(); 459 temp.put(key, value); 460 properties = temp; 461 } 462 } 463 464 468 final void setResult(IStatus result) { 469 this.result = result; 470 } 471 472 475 protected void setRule(ISchedulingRule rule) { 476 manager.setRule(this, rule); 477 } 478 479 484 final void setStartTime(long time) { 485 startTime = time; 486 } 487 488 491 protected void setSystem(boolean value) { 492 if (getState() != Job.NONE) 493 throw new IllegalStateException (); 494 flags = value ? flags | M_SYSTEM : flags & ~M_SYSTEM; 495 } 496 497 500 protected void setThread(Thread thread) { 501 this.thread = thread; 502 } 503 504 507 protected void setUser(boolean value) { 508 if (getState() != Job.NONE) 509 throw new IllegalStateException (); 510 flags = value ? flags | M_USER : flags & ~M_USER; 511 } 512 513 516 protected boolean shouldSchedule() { 517 return true; 518 } 519 520 523 protected boolean sleep() { 524 return manager.sleep(this); 525 } 526 527 531 public String toString() { 532 return getName() + "(" + jobNumber + ")"; } 534 535 538 protected void wakeUp(long delay) { 539 manager.wakeUp(this, delay); 540 } 541 } 542 | Popular Tags |