Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b2a6109a8e47612c7db1919a99b25c5b7031a6ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*******************************************************************************
 * Copyright (c) 2003, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corp. - Rational Software - initial implementation
 *******************************************************************************/
/*
 * Created on May 30, 2003
 */
package org.eclipse.cdt.internal.core.search.processing;

import java.util.HashSet;

import org.eclipse.cdt.core.ICLogConstants;
import org.eclipse.cdt.internal.core.Util;
import org.eclipse.cdt.internal.core.search.indexing.IndexRequest;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
 
public abstract class JobManager implements Runnable {

	/* queue of jobs to execute */
	protected IJob[] awaitingJobs = new IJob[10];
	protected int jobStart = 0;
	protected int jobEnd = -1;
	protected boolean executing = false;

	/* background processing */
	protected Thread thread;

	/* flag indicating whether job execution is enabled or not */
	public static final int ENABLED = 1;
	public static final int DISABLED = 0;
	public static final int WAITING = 2;
	private int enabled = ENABLED;

	public static boolean VERBOSE = false;
	/* flag indicating that the activation has completed */
	public boolean activated = false;
	
	private int awaitingClients = 0;
	
	protected HashSet jobSet;
	
	protected IndexingJob indexJob = null;
	
	static private final IStatus OK_STATUS = new Status( IStatus.OK, "org.eclipse.cdt.core", IStatus.OK, "", null );  //$NON-NLS-1$//$NON-NLS-2$
	static private final IStatus ERROR_STATUS = new Status( IStatus.ERROR, "org.eclipse.cdt.core", IStatus.ERROR, "", null );  //$NON-NLS-1$//$NON-NLS-2$
	
	public static void verbose(String log) {
		System.out.println("(" + Thread.currentThread() + ") " + log); //$NON-NLS-1$//$NON-NLS-2$
	}

	public IProgressMonitor getIndexJobProgressGroup(){
		if( indexJob == null )
			return null;
		
		return indexJob.getProgressGroup();
	}
	
	/**
	 * Invoked exactly once, in background, before starting processing any job
	 */
	public void activateProcessing() {
		this.activated = true;
	}
	/**
	 * Answer the amount of awaiting jobs.
	 */
	public synchronized int awaitingJobsCount() {

		// pretend busy in case concurrent job attempts performing before activated
		if (!activated)
			return 1;

		return jobEnd - jobStart + 1;

	}
	/**
	 * Answers the first job in the queue, or null if there is no job available
	 * Until the job has completed, the job manager will keep answering the same job.
	 */
	public synchronized IJob currentJob() {

		if ( enabled != ENABLED )
			return null;

		if (jobStart <= jobEnd) {
			return awaitingJobs[jobStart];
		}
		return null;
	}
	
	public synchronized void disable() {
		enabled = DISABLED;
		if (VERBOSE)
			JobManager.verbose("DISABLING background indexing"); //$NON-NLS-1$
	}
	/**
	 * Remove the index from cache for a given project.
	 * Passing null as a job family discards them all.
	 */
	public void discardJobs(String jobFamily) {

		if (VERBOSE)
			JobManager.verbose("DISCARD   background job family - " + jobFamily); //$NON-NLS-1$

		int oldEnabledState = 0;
		try {
			IJob currentJob;
			// cancel current job if it belongs to the given family
			synchronized(this){
				currentJob = this.currentJob();
				oldEnabledState = enabledState();
				disable();
			}
			if (currentJob != null 
					&& (jobFamily == null || currentJob.belongsTo(jobFamily))) {
	
				currentJob.cancel();
			
				// wait until current active job has finished
				while (thread != null && executing){
					try {
						if (VERBOSE)
							JobManager.verbose("-> waiting end of current background job - " + currentJob); //$NON-NLS-1$ //$NON-NLS-2$
						Thread.sleep(50);
					} catch(InterruptedException e){
					}
				}
			}
	
			// flush and compact awaiting jobs
			int loc = -1;
			synchronized(this) {
				for (int i = jobStart; i <= jobEnd; i++) {
					currentJob = awaitingJobs[i];
					awaitingJobs[i] = null;
					if (!(jobFamily == null
						|| currentJob.belongsTo(jobFamily))) { // copy down, compacting
						awaitingJobs[++loc] = currentJob;
					} else {
						if (VERBOSE)
							JobManager.verbose("-> discarding background job  - " + currentJob); //$NON-NLS-1$
						currentJob.cancel();
						if( indexJob != null ){
							if( indexJob.tickDown( null ) <= 0 ){
								indexJob.done( OK_STATUS );
								indexJob = null;
							}
						}
					}
				}
				jobStart = 0;
				jobEnd = loc;
			}
		} finally {
			if ( oldEnabledState == ENABLED )
				enable();
			else if( oldEnabledState == WAITING )
				pause();
		}
		if (VERBOSE)
			JobManager.verbose("DISCARD   DONE with background job family - " + jobFamily); //$NON-NLS-1$
	}
	
	public synchronized void enable() {
		if( enabled == WAITING ){
			//stop waiting, restore the indexing Job for progress
			indexJob = new IndexingJob( thread, this );
			indexJob.setTicks(awaitingJobsCount());
		}
		enabled = ENABLED;
		if (VERBOSE)
			JobManager.verbose("ENABLING  background indexing"); //$NON-NLS-1$
	}
	
	public synchronized int enabledState() {
		return enabled;
	}
	
	public synchronized void pause(){
		enabled = WAITING;
		if( VERBOSE )
			JobManager.verbose("WAITING  pausing background indexing"); //$NON-NLS-1$
	}
	/**
	 * Advance to the next available job, once the current one has been completed.
	 * Note: clients awaiting until the job count is zero are still waiting at this point.
	 */
	protected synchronized void moveToNextJob() {

		//if (!enabled) return;

		if (jobStart <= jobEnd) {
			awaitingJobs[jobStart++] = null;
			if (jobStart > jobEnd) {
				jobStart = 0;
				jobEnd = -1;
			}
		}
		if( indexJob != null ){
			String progressString = null;
			IJob job = currentJob();
			if( job instanceof IndexRequest ){
				progressString = " ("; //$NON-NLS-1$
				progressString += job.toString();
				progressString += ")"; //$NON-NLS-1$
			}
			if( indexJob.tickDown( progressString ) <= 0 ){
				indexJob.done( OK_STATUS );
				indexJob = null;
			}
		}
	}
	/**
	 * When idle, give chance to do something
	 */
	protected void notifyIdle(long idlingTime) {
	}
	/**
	 * This API is allowing to run one job in concurrence with background processing.
	 * Indeed since other jobs are performed in background, resource sharing might be 
	 * an issue.Therefore, this functionality allows a given job to be run without
	 * colliding with background ones.
	 * Note: multiple thread might attempt to perform concurrent jobs at the same time,
	 *            and should synchronize (it is deliberately left to clients to decide whether
	 *            concurrent jobs might interfere or not. In general, multiple read jobs are ok).
	 *
	 * Waiting policy can be:
	 * 		IJobConstants.ForceImmediateSearch
	 * 		IJobConstants.CancelIfNotReadyToSearch
	 * 		IJobConstants.WaitUntilReadyToSearch
	 *
	 */
	public boolean performConcurrentJob(
		IJob searchJob,
		int waitingPolicy,
		IProgressMonitor progress, 
		IJob jobToIgnore) {
			
		if (VERBOSE)
			JobManager.verbose("STARTING  concurrent job - " + searchJob); //$NON-NLS-1$
		if (!searchJob.isReadyToRun()) {
			if (VERBOSE)
				JobManager.verbose("ABORTED   concurrent job - " + searchJob); //$NON-NLS-1$
			return IJob.FAILED;
		}

		int concurrentJobWork = 100;
		if (progress != null)
			progress.beginTask("", concurrentJobWork); //$NON-NLS-1$
		boolean status = IJob.FAILED;
		if (awaitingJobsCount() > 0) {
			if( enabledState() == WAITING ){
				//the indexer is paused, resume now that we have been asked for something
				enable();
			}
			boolean attemptPolicy = true;
			policy: while( attemptPolicy ){
				attemptPolicy = false;
				switch (waitingPolicy) {
	
					case IJob.ForceImmediate :
						if (VERBOSE)
							JobManager.verbose("-> NOT READY - forcing immediate - " + searchJob);//$NON-NLS-1$
						boolean wasEnabled = ( enabledState() == ENABLED );
						try {
							if( wasEnabled )
								disable(); // pause indexing
							status = searchJob.execute(progress == null ? null : new SubProgressMonitor(progress, concurrentJobWork));
						} finally {
							if(wasEnabled)
								enable();
						}
						if (VERBOSE)
							JobManager.verbose("FINISHED  concurrent job - " + searchJob); //$NON-NLS-1$
						return status;
						
					case IJob.CancelIfNotReady :
						if (VERBOSE)
							JobManager.verbose("-> NOT READY - cancelling - " + searchJob); //$NON-NLS-1$
						if (progress != null) progress.setCanceled(true);
						if (VERBOSE)
							JobManager.verbose("CANCELED concurrent job - " + searchJob); //$NON-NLS-1$
						throw new OperationCanceledException();
	
					case IJob.WaitUntilReady :
						int awaitingWork;
						IJob previousJob = null;
						IJob currentJob;
						IProgressMonitor subProgress = null;
						int totalWork = this.awaitingJobsCount();
						if (progress != null && totalWork > 0) {
							subProgress = new SubProgressMonitor(progress, concurrentJobWork / 2);
							subProgress.beginTask("", totalWork); //$NON-NLS-1$
							concurrentJobWork = concurrentJobWork / 2;
						}
						int originalPriority = this.thread.getPriority();
						try {
							synchronized(this) {
								
								// use local variable to avoid potential NPE (see Bug 20435 NPE when searching java method)
								Thread t = this.thread;
								if (t != null) {
									t.setPriority(Thread.currentThread().getPriority());
								}
								this.awaitingClients++;
							}
							while (((awaitingWork = awaitingJobsCount()) > 0)
								    && (!jobShouldBeIgnored(jobToIgnore))) {
								if (subProgress != null && subProgress.isCanceled())
									throw new OperationCanceledException();
								currentJob = currentJob();
								// currentJob can be null when jobs have been added to the queue but job manager is not enabled
								if (currentJob != null && currentJob != previousJob) {
									if (VERBOSE)
										JobManager.verbose("-> NOT READY - waiting until ready - " + searchJob);//$NON-NLS-1$
									if (subProgress != null) {
										subProgress.subTask(
											Util.bind("manager.filesToIndex", Integer.toString(awaitingWork))); //$NON-NLS-1$
										subProgress.worked(1);
									}
									previousJob = currentJob;
								}
								
								if( enabledState() == WAITING ){
									//user canceled the index we are waiting on, force immediate
									waitingPolicy = IJob.ForceImmediate;
									attemptPolicy = true;
									continue policy;
								}
								try {
									Thread.sleep(50);
								} catch (InterruptedException e) {
								}
							}
						} finally {
							synchronized(this) {
								this.awaitingClients--;
								
								// use local variable to avoid potential NPE (see Bug 20435 NPE when searching java method)
								Thread t = this.thread;
								if (t != null) {
									t.setPriority(originalPriority);
								}
							}
							if (subProgress != null) {
								subProgress.done();
							}
						}
				}//switch
			} //while
		} // if
		status = searchJob.execute(progress == null ? null : new SubProgressMonitor(progress, concurrentJobWork));
		if (progress != null) {
			progress.done();
		}
		if (VERBOSE)
			JobManager.verbose("FINISHED  concurrent job - " + searchJob); //$NON-NLS-1$
		return status;
	}
	
	/**
	 * @param jobToIgnore
	 * @return
	 */
	private boolean jobShouldBeIgnored(IJob jobToIgnore) {
		if (jobToIgnore == null)
			return false;
		
		if (currentJob() == jobToIgnore)
			return true;
		
		return false;
	}

	public abstract String processName();
	
	public synchronized void request(IJob job) {
		if (!job.isReadyToRun()) {
			if (VERBOSE)
				JobManager.verbose("ABORTED request of background job - " + job); //$NON-NLS-1$
			return;
		}

		// append the job to the list of ones to process later on
		int size = awaitingJobs.length;
		if (++jobEnd == size) { // when growing, relocate jobs starting at position 0
			jobEnd -= jobStart;
			System.arraycopy(
				awaitingJobs,
				jobStart,
				(awaitingJobs = new IJob[size * 2]),
				0,
				jobEnd);
			jobStart = 0;
		}
		awaitingJobs[jobEnd] = job;
		
		if (enabledState() ==WAITING){
			//Put back into enabled state
			enable();
		}
		else if( enabledState() == ENABLED ){
			if( indexJob == null ){
				indexJob = new IndexingJob( thread, this );
			} else {
				indexJob.tickUp();
			}
		}
		
		if (VERBOSE)
			JobManager.verbose("REQUEST   background job - " + job); //$NON-NLS-1$

	}
	/**
	 * Flush current state
	 */
	public void reset() {
		if (VERBOSE)
			JobManager.verbose("Reset"); //$NON-NLS-1$

		if (thread != null) {
			discardJobs(null); // discard all jobs
		} else {
			/* initiate background processing */
			thread = new Thread(this, this.processName());
			thread.setDaemon(true);
			// less prioritary by default, priority is raised if clients are actively waiting on it
			thread.setPriority(Thread.NORM_PRIORITY-1); 
			thread.start();
		}
		
		jobSet = new HashSet();
	}

	/**
	 * Infinite loop performing resource indexing
	 */
	public void run() {

		long idlingStart = -1;
		activateProcessing();
		try {
			while (this.thread != null) {
				try {
					IJob job;
					if ((job = currentJob()) == null) {
						if (idlingStart < 0)
							idlingStart = System.currentTimeMillis();
						notifyIdle(System.currentTimeMillis() - idlingStart);
						Thread.sleep(500);
						continue;
					} 
					
					idlingStart = -1;
					if (VERBOSE) {
						JobManager.verbose(awaitingJobsCount() + " awaiting jobs"); //$NON-NLS-1$
						JobManager.verbose("STARTING background job - " + job); //$NON-NLS-1$
					}
					try {
						executing = true;
						/*boolean status = */job.execute(null);
						//if (status == FAILED) request(job);
					} finally {
						executing = false;
						
						//Answer the job directly from the array; using currentJob()
						//results in no notification if indexing is disabled (Bug 78678)
						jobFinishedNotification(awaitingJobs[jobStart]);
						
						if (VERBOSE) {
							JobManager.verbose("FINISHED background job - " + job); //$NON-NLS-1$
						}
						moveToNextJob();
						if (this.awaitingClients == 0) {
							Thread.sleep(50);
						}
					}
				} catch (InterruptedException e) { // background indexing was interrupted
				}
			}
		} catch (RuntimeException e) {
			if( indexJob != null ){
				indexJob.done( ERROR_STATUS );
				indexJob = null;
			}
			if (this.thread != null) { // if not shutting down
				// log exception
				org.eclipse.cdt.internal.core.model.Util.log(e, "Background Indexer Crash Recovery", ICLogConstants.PDE); //$NON-NLS-1$
				
				// keep job manager alive
				this.discardJobs(null);
				this.thread = null;
				this.reset(); // this will fork a new thread with no waiting jobs, some indexes will be inconsistent
			}
			throw e;
		} catch (Error e) {
			if( indexJob != null ){
				indexJob.done( ERROR_STATUS );
				indexJob = null;
			}
			if (this.thread != null && !(e instanceof ThreadDeath)) {
				// log exception
				org.eclipse.cdt.internal.core.model.Util.log(e, "Background Indexer Crash Recovery", ICLogConstants.PDE); //$NON-NLS-1$
				
				// keep job manager alive
				this.discardJobs(null);
				this.thread = null;
				this.reset(); // this will fork a new thread with no waiting jobs, some indexes will be inconsistent
			}
			throw e;
		}
	}
	/**
	 * Stop background processing, and wait until the current job is completed before returning
	 */
	public void shutdown() {

		disable();
		discardJobs(null); // will wait until current executing job has completed
		Thread thread = this.thread;
		this.thread = null; // mark the job manager as shutting down so that the thread will stop by itself
		try {
			if (thread != null) { // see http://bugs.eclipse.org/bugs/show_bug.cgi?id=31858
				thread.join();
			}
		} catch (InterruptedException e) {
		}
	}
	
	public String toString() {
		StringBuffer buffer = new StringBuffer(10);
		buffer.append("Enabled:").append(this.enabled).append('\n'); //$NON-NLS-1$
		int numJobs = jobEnd - jobStart + 1;
		buffer.append("Jobs in queue:").append(numJobs).append('\n'); //$NON-NLS-1$
		for (int i = 0; i < numJobs && i < 15; i++) {
			buffer.append(i).append(" - job["+i+"]: ").append(awaitingJobs[jobStart+i]).append('\n'); //$NON-NLS-1$ //$NON-NLS-2$
		}
		return buffer.toString();
	}	
	
	protected abstract void jobFinishedNotification(IJob job);

}

Back to the top