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