comparison org.eclipse.core.jobs/src/org/eclipse/core/internal/jobs/Worker.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 6f068362a363
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2003, 2006 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.internal.jobs.Worker;
14
15 import java.lang.JThread;
16 import tango.text.convert.Format;
17 import java.lang.all;
18 import java.util.Set;
19
20 import org.eclipse.core.internal.runtime.RuntimeLog;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.OperationCanceledException;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.core.runtime.jobs.Job;
25 import org.osgi.util.NLS;
26
27 import org.eclipse.core.internal.jobs.InternalJob;
28 import org.eclipse.core.internal.jobs.WorkerPool;
29 import org.eclipse.core.internal.jobs.JobMessages;
30 import org.eclipse.core.internal.jobs.JobManager;
31
32 /**
33 * A worker thread processes jobs supplied to it by the worker pool. When
34 * the worker pool gives it a null job, the worker dies.
35 */
36 public class Worker : JThread {
37 //worker number used for debugging purposes only
38 private static int nextWorkerNumber = 0;
39 private /+volatile+/ InternalJob currentJob_;
40 private final WorkerPool pool;
41
42 public this(WorkerPool pool) {
43 super();
44 this.setName( Format("Worker-{}", nextWorkerNumber++)); //$NON-NLS-1$
45 this.pool = pool;
46 //set the context loader to avoid leaking the current context loader
47 //for the thread that spawns this worker (bug 98376)
48 // SWT
49 // setContextClassLoader(pool.defaultContextLoader);
50 }
51
52 /**
53 * Returns the currently running job, or null if none.
54 */
55 public Job currentJob() {
56 return cast(Job) currentJob_;
57 }
58
59 private IStatus handleException(InternalJob job, Exception t) {
60 String message = NLS.bind(JobMessages.jobs_internalError, job.getName_package());
61 return new Status(IStatus.ERROR, JobManager.PI_JOBS, JobManager.PLUGIN_ERROR, message, t);
62 }
63
64 public override void run() {
65 this.setPriority(NORM_PRIORITY);
66 try {
67 while ((currentJob_ = pool.startJob_package(this)) !is null) {
68 currentJob_.setThread_package(this);
69 IStatus result = Status.OK_STATUS;
70 try {
71 result = currentJob_.run_package(currentJob_.getProgressMonitor());
72 } catch (OperationCanceledException e) {
73 result = Status.CANCEL_STATUS;
74 } catch (Exception e) {
75 result = handleException(currentJob_, e);
76 // } catch (ThreadDeath e) {
77 // //must not consume thread death
78 // result = handleException(currentJob_, e);
79 // throw e;
80 // } catch (Error e) {
81 // result = handleException(currentJob_, e);
82 } finally {
83 //clear interrupted state for this thread
84 JThread.interrupted();
85
86 //result must not be null
87 if (result is null)
88 result = handleException(currentJob_, new NullPointerException());
89 pool.endJob_package(currentJob_, result);
90 if ((result.getSeverity() & (IStatus.ERROR | IStatus.WARNING)) !is 0)
91 RuntimeLog.log(result);
92 currentJob_ = null;
93 }
94 }
95 } catch (Exception t) {
96 ExceptionPrintStackTrace(t);
97 } finally {
98 currentJob_ = null;
99 pool.endWorker_package(this);
100 }
101 }
102 }