Skip to content

Commit 6d19032

Browse files
eclipse-platform-botakurtakov
authored andcommitted
Perform clean code of runtime/bundles/org.eclipse.core.jobs
1 parent 7eeaf35 commit 6d19032

File tree

15 files changed

+496
-253
lines changed

15 files changed

+496
-253
lines changed

runtime/bundles/org.eclipse.core.jobs/src/org/eclipse/core/internal/jobs/DeadlockDetector.java

Lines changed: 85 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ private boolean addCycleThreads(ArrayList<Thread> deadlockedThreads, Thread next
9999
//get the thread that block the given thread from running
100100
Thread[] blocking = blockingThreads(next);
101101
//if the thread is not blocked by other threads, then it is not part of a deadlock
102-
if (blocking.length == 0)
102+
if (blocking.length == 0) {
103103
return false;
104+
}
104105
boolean inCycle = false;
105106
for (Thread element : blocking) {
106107
//if we have already visited the given thread, then we found a cycle
@@ -110,10 +111,11 @@ private boolean addCycleThreads(ArrayList<Thread> deadlockedThreads, Thread next
110111
//otherwise, add the thread to our list and recurse deeper
111112
deadlockedThreads.add(element);
112113
//if the thread is not part of a cycle, remove it from the list
113-
if (addCycleThreads(deadlockedThreads, element))
114+
if (addCycleThreads(deadlockedThreads, element)) {
114115
inCycle = true;
115-
else
116+
} else {
116117
deadlockedThreads.remove(element);
118+
}
117119
}
118120
}
119121
return inCycle;
@@ -146,8 +148,9 @@ private boolean checkWaitCycles(int[] waitingThreads, int lockIndex) {
146148
waitingThreads[i]++;
147149
for (int j = 0; j < graph[i].length; j++) {
148150
if (graph[i][j] == WAITING_FOR_LOCK) {
149-
if (checkWaitCycles(waitingThreads, j))
151+
if (checkWaitCycles(waitingThreads, j)) {
150152
return true;
153+
}
151154
}
152155
}
153156
//this thread is not involved in a cycle yet, so remove the visited flag
@@ -201,11 +204,13 @@ private Object[] getOwnedLocks(Thread current) {
201204
int index = indexOf(current, false);
202205

203206
for (int j = 0; j < graph[index].length; j++) {
204-
if (graph[index][j] > NO_STATE)
207+
if (graph[index][j] > NO_STATE) {
205208
ownedLocks.add(locks.get(j));
209+
}
206210
}
207-
if (ownedLocks.isEmpty())
211+
if (ownedLocks.isEmpty()) {
208212
Assert.isLegal(false, "A thread with no locks is part of a deadlock."); //$NON-NLS-1$
213+
}
209214
return ownedLocks.toArray();
210215
}
211216

@@ -218,8 +223,9 @@ private Thread[] getThreadsInDeadlock(Thread cause) {
218223
* if the thread that caused deadlock doesn't own any locks, then it is not part
219224
* of the deadlock (it just caused it because of a rule it tried to acquire)
220225
*/
221-
if (ownsLocks(cause))
226+
if (ownsLocks(cause)) {
222227
deadlockedThreads.add(cause);
228+
}
223229
addCycleThreads(deadlockedThreads, cause);
224230
return deadlockedThreads.toArray(new Thread[deadlockedThreads.size()]);
225231
}
@@ -228,18 +234,22 @@ private Thread[] getThreadsInDeadlock(Thread cause) {
228234
* Returns the thread(s) that own the given lock.
229235
*/
230236
private Thread[] getThreadsOwningLock(ISchedulingRule rule) {
231-
if (rule == null)
237+
if (rule == null) {
232238
return new Thread[0];
239+
}
233240
int lockIndex = indexOf(rule, false);
234241
ArrayList<Thread> blocking = new ArrayList<>(1);
235242
for (int i = 0; i < graph.length; i++) {
236-
if (graph[i][lockIndex] > NO_STATE)
243+
if (graph[i][lockIndex] > NO_STATE) {
237244
blocking.add(lockThreads.get(i));
245+
}
238246
}
239-
if ((blocking.isEmpty()) && (JobManager.DEBUG_LOCKS))
247+
if ((blocking.isEmpty()) && (JobManager.DEBUG_LOCKS)) {
240248
System.out.println("Lock " + rule + " is involved in deadlock but is not owned by any thread."); //$NON-NLS-1$ //$NON-NLS-2$
241-
if ((blocking.size() > 1) && (rule instanceof ILock) && (JobManager.DEBUG_LOCKS))
249+
}
250+
if ((blocking.size() > 1) && (rule instanceof ILock) && (JobManager.DEBUG_LOCKS)) {
242251
System.out.println("Lock " + rule + " is owned by more than 1 thread, but it is not a rule."); //$NON-NLS-1$ //$NON-NLS-2$
252+
}
243253
return blocking.toArray(new Thread[blocking.size()]);
244254
}
245255

@@ -250,8 +260,9 @@ private Object getWaitingLock(Thread current) {
250260
int index = indexOf(current, false);
251261
//find the lock that this thread is waiting for
252262
for (int j = 0; j < graph[index].length; j++) {
253-
if (graph[index][j] == WAITING_FOR_LOCK)
263+
if (graph[index][j] == WAITING_FOR_LOCK) {
254264
return locks.get(j);
265+
}
255266
}
256267
//it can happen that a thread is not waiting for any lock (it is not really part of the deadlock)
257268
return null;
@@ -298,10 +309,12 @@ boolean isEmpty() {
298309
void lockAcquired(Thread owner, ISchedulingRule lock) {
299310
int lockIndex = indexOf(lock, true);
300311
int threadIndex = indexOf(owner, true);
301-
if (resize)
312+
if (resize) {
302313
resizeGraph();
303-
if (graph[threadIndex][lockIndex] == WAITING_FOR_LOCK)
314+
}
315+
if (graph[threadIndex][lockIndex] == WAITING_FOR_LOCK) {
304316
graph[threadIndex][lockIndex] = NO_STATE;
317+
}
305318
/**
306319
* acquire all locks that conflict with the given lock
307320
* or conflict with a lock the given lock will acquire implicitly
@@ -340,13 +353,15 @@ void lockReleased(Thread owner, ISchedulingRule lock) {
340353
int threadIndex = indexOf(owner, false);
341354
//make sure the lock and thread exist in the graph
342355
if (threadIndex < 0) {
343-
if (JobManager.DEBUG_LOCKS)
356+
if (JobManager.DEBUG_LOCKS) {
344357
System.out.println("[lockReleased] Lock " + lock + " was already released by thread " + owner.getName()); //$NON-NLS-1$ //$NON-NLS-2$
358+
}
345359
return;
346360
}
347361
if (lockIndex < 0) {
348-
if (JobManager.DEBUG_LOCKS)
362+
if (JobManager.DEBUG_LOCKS) {
349363
System.out.println("[lockReleased] Thread " + owner.getName() + " already released lock " + lock); //$NON-NLS-1$ //$NON-NLS-2$
364+
}
350365
return;
351366
}
352367
//if this lock was suspended, set it to NO_STATE
@@ -360,16 +375,18 @@ void lockReleased(Thread owner, ISchedulingRule lock) {
360375
if (((!(lock instanceof ILock) && !(locks.get(j) instanceof ILock) && (graph[threadIndex][j] > NO_STATE))
361376
|| lock.isConflicting(locks.get(j)))) {
362377
if (graph[threadIndex][j] == NO_STATE) {
363-
if (JobManager.DEBUG_LOCKS)
378+
if (JobManager.DEBUG_LOCKS) {
364379
System.out.println("[lockReleased] More releases than acquires for thread " + owner.getName() + " and lock " + lock); //$NON-NLS-1$ //$NON-NLS-2$
380+
}
365381
} else {
366382
graph[threadIndex][j]--;
367383
}
368384
}
369385
}
370386
//if this thread just released the given lock, try to simplify the graph
371-
if (graph[threadIndex][lockIndex] == NO_STATE)
387+
if (graph[threadIndex][lockIndex] == NO_STATE) {
372388
reduceGraph(threadIndex, lock);
389+
}
373390
}
374391

375392
/**
@@ -381,13 +398,15 @@ void lockReleasedCompletely(Thread owner, ISchedulingRule rule) {
381398
int threadIndex = indexOf(owner, false);
382399
//need to make sure that the given thread and rule were not already removed from the graph
383400
if (threadIndex < 0) {
384-
if (JobManager.DEBUG_LOCKS)
401+
if (JobManager.DEBUG_LOCKS) {
385402
System.out.println("[lockReleasedCompletely] Lock " + rule + " was already released by thread " + owner.getName()); //$NON-NLS-1$ //$NON-NLS-2$
403+
}
386404
return;
387405
}
388406
if (ruleIndex < 0) {
389-
if (JobManager.DEBUG_LOCKS)
407+
if (JobManager.DEBUG_LOCKS) {
390408
System.out.println("[lockReleasedCompletely] Thread " + owner.getName() + " already released lock " + rule); //$NON-NLS-1$ //$NON-NLS-2$
409+
}
391410
return;
392411
}
393412
/**
@@ -396,8 +415,9 @@ void lockReleasedCompletely(Thread owner, ISchedulingRule rule) {
396415
* if we are releasing a lock, then only update the one entry for the lock
397416
*/
398417
for (int j = 0; j < graph[threadIndex].length; j++) {
399-
if (!(locks.get(j) instanceof ILock) && (graph[threadIndex][j] > NO_STATE))
418+
if (!(locks.get(j) instanceof ILock) && (graph[threadIndex][j] > NO_STATE)) {
400419
graph[threadIndex][j] = NO_STATE;
420+
}
401421
}
402422
reduceGraph(threadIndex, rule);
403423
}
@@ -411,22 +431,25 @@ Deadlock lockWaitStart(Thread client, ISchedulingRule lock) {
411431
int lockIndex = indexOf(lock, false);
412432
int[] temp = new int[lockThreads.size()];
413433
//check if the addition of the waiting thread caused deadlock
414-
if (!checkWaitCycles(temp, lockIndex))
434+
if (!checkWaitCycles(temp, lockIndex)) {
415435
return null;
436+
}
416437
//there is a deadlock in the graph
417438
Thread[] threads = getThreadsInDeadlock(client);
418439
//find a thread whose locks can be suspended to resolve the deadlock
419440
Thread candidate = resolutionCandidate(threads);
420441
ISchedulingRule[] locksToSuspend = realLocksForThread(candidate);
421442
Deadlock deadlock = new Deadlock(threads, locksToSuspend, candidate);
422443
reportDeadlock(deadlock);
423-
if (JobManager.DEBUG_DEADLOCK)
444+
if (JobManager.DEBUG_DEADLOCK) {
424445
throw new IllegalStateException("Deadlock detected. Caused by thread " + client.getName() + '.'); //$NON-NLS-1$
446+
}
425447
// Update the graph to indicate that the locks will now be suspended.
426448
// To indicate that the lock will be suspended, we set the thread to wait for the lock.
427449
// When the lock is forced to be released, the entry will be cleared.
428-
for (ISchedulingRule element : locksToSuspend)
450+
for (ISchedulingRule element : locksToSuspend) {
429451
setToWait(deadlock.getCandidate(), element, true);
452+
}
430453
return deadlock;
431454
}
432455

@@ -440,19 +463,22 @@ void lockWaitStop(Thread owner, ISchedulingRule lock) {
440463
int threadIndex = indexOf(owner, false);
441464
//make sure the thread and lock exist in the graph
442465
if (threadIndex < 0) {
443-
if (JobManager.DEBUG_LOCKS)
466+
if (JobManager.DEBUG_LOCKS) {
444467
System.out.println("Thread " + owner.getName() + " was already removed."); //$NON-NLS-1$ //$NON-NLS-2$
468+
}
445469
return;
446470
}
447471
if (lockIndex < 0) {
448-
if (JobManager.DEBUG_LOCKS)
472+
if (JobManager.DEBUG_LOCKS) {
449473
System.out.println("Lock " + lock + " was already removed."); //$NON-NLS-1$ //$NON-NLS-2$
474+
}
450475
return;
451476
}
452477
if (graph[threadIndex][lockIndex] != WAITING_FOR_LOCK) {
453478
// Lock has already been granted, nothing to do...
454-
if (JobManager.DEBUG_LOCKS)
479+
if (JobManager.DEBUG_LOCKS) {
455480
System.out.println("Lock " + lock + " already granted to depth: " + graph[threadIndex][lockIndex]); //$NON-NLS-1$ //$NON-NLS-2$
481+
}
456482
return;
457483
}
458484
graph[threadIndex][lockIndex] = NO_STATE;
@@ -465,8 +491,9 @@ void lockWaitStop(Thread owner, ISchedulingRule lock) {
465491
private boolean ownsLocks(Thread cause) {
466492
int threadIndex = indexOf(cause, false);
467493
for (int j = 0; j < graph[threadIndex].length; j++) {
468-
if (graph[threadIndex][j] > NO_STATE)
494+
if (graph[threadIndex][j] > NO_STATE) {
469495
return true;
496+
}
470497
}
471498
return false;
472499
}
@@ -480,8 +507,9 @@ private boolean ownsRealLocks(Thread owner) {
480507
for (int j = 0; j < graph[threadIndex].length; j++) {
481508
if (graph[threadIndex][j] > NO_STATE) {
482509
Object lock = locks.get(j);
483-
if (lock instanceof ILock)
510+
if (lock instanceof ILock) {
484511
return true;
512+
}
485513
}
486514
}
487515
return false;
@@ -496,8 +524,9 @@ private boolean ownsRuleLocks(Thread owner) {
496524
for (int j = 0; j < graph[threadIndex].length; j++) {
497525
if (graph[threadIndex][j] > NO_STATE) {
498526
Object lock = locks.get(j);
499-
if (!(lock instanceof ILock))
527+
if (!(lock instanceof ILock)) {
500528
return true;
529+
}
501530
}
502531
}
503532
return false;
@@ -511,11 +540,13 @@ private ISchedulingRule[] realLocksForThread(Thread owner) {
511540
int threadIndex = indexOf(owner, false);
512541
ArrayList<ISchedulingRule> ownedLocks = new ArrayList<>(1);
513542
for (int j = 0; j < graph[threadIndex].length; j++) {
514-
if ((graph[threadIndex][j] > NO_STATE) && (locks.get(j) instanceof ILock))
543+
if ((graph[threadIndex][j] > NO_STATE) && (locks.get(j) instanceof ILock)) {
515544
ownedLocks.add(locks.get(j));
545+
}
516546
}
517-
if (ownedLocks.isEmpty())
547+
if (ownedLocks.isEmpty()) {
518548
Assert.isLegal(false, "A thread with no real locks was chosen to resolve deadlock."); //$NON-NLS-1$
549+
}
519550
return ownedLocks.toArray(new ISchedulingRule[ownedLocks.size()]);
520551
}
521552

@@ -532,8 +563,9 @@ private void reduceGraph(int row, ISchedulingRule lock) {
532563
* (consist of locks which conflict with the given lock, or of locks which are rules)
533564
*/
534565
for (int j = 0; j < numLocks; j++) {
535-
if (!(locks.get(j) instanceof ILock) || (lock.isConflicting(locks.get(j))))
566+
if (!(locks.get(j) instanceof ILock) || (lock.isConflicting(locks.get(j)))) {
536567
emptyColumns[j] = true;
568+
}
537569
}
538570

539571
boolean rowEmpty = true;
@@ -564,11 +596,13 @@ private void reduceGraph(int row, ISchedulingRule lock) {
564596
}
565597
}
566598
//if no columns or rows are empty, return right away
567-
if ((numEmpty == 0) && (!rowEmpty))
599+
if ((numEmpty == 0) && (!rowEmpty)) {
568600
return;
601+
}
569602

570-
if (rowEmpty)
603+
if (rowEmpty) {
571604
lockThreads.remove(row);
605+
}
572606

573607
//new graph (the list of locks and the list of threads are already updated)
574608
final int numThreads = lockThreads.size();
@@ -586,8 +620,9 @@ private void reduceGraph(int row, ISchedulingRule lock) {
586620
if ((i == row) && rowEmpty) {
587621
numRowsSkipped++;
588622
//check if we need to skip the last row
589-
if (i >= graph.length - numRowsSkipped)
623+
if (i >= graph.length - numRowsSkipped) {
590624
break;
625+
}
591626
}
592627
//the number of columns we need to skip to get the correct entry from the old graph
593628
//needs to be reset for every new row
@@ -596,12 +631,14 @@ private void reduceGraph(int row, ISchedulingRule lock) {
596631
while (emptyColumns[j + numColsSkipped]) {
597632
numColsSkipped++;
598633
//check if we need to skip the last column
599-
if (j >= graph[i].length - numColsSkipped)
634+
if (j >= graph[i].length - numColsSkipped) {
600635
break;
636+
}
601637
}
602638
//need to break out of the outer loop
603-
if (j >= graph[i].length - numColsSkipped)
639+
if (j >= graph[i].length - numColsSkipped) {
604640
break;
641+
}
605642
tempGraph[i][j] = graph[i + numRowsSkipped][j + numColsSkipped];
606643
}
607644
}
@@ -676,8 +713,9 @@ private void resizeGraph() {
676713
return;
677714
}
678715
int[][] tempGraph = new int[newRows][newCols];
679-
for (int i = 0; i < graph.length; i++)
716+
for (int i = 0; i < graph.length; i++) {
680717
System.arraycopy(graph[i], 0, tempGraph[i], 0, graph[i].length);
718+
}
681719
graph = tempGraph;
682720
resize = false;
683721
}
@@ -695,8 +733,9 @@ private Thread resolutionCandidate(Thread[] candidates) {
695733
}
696734
//next look for any candidate with a real lock (a lock that can be suspended)
697735
for (Thread candidate : candidates) {
698-
if (ownsRealLocks(candidate))
736+
if (ownsRealLocks(candidate)) {
699737
return candidate;
738+
}
700739
}
701740
//unnecessary, return the first entry in the array by default
702741
return candidates[0];
@@ -712,16 +751,19 @@ private void setToWait(Thread owner, ISchedulingRule lock, boolean suspend) {
712751
* then we need to transfer all positive entries for a conflicting rule to the
713752
* newly added rule in order to synchronize the graph.
714753
*/
715-
if (!suspend && !(lock instanceof ILock))
754+
if (!suspend && !(lock instanceof ILock)) {
716755
needTransfer = true;
756+
}
717757
int lockIndex = indexOf(lock, !suspend);
718758
int threadIndex = indexOf(owner, !suspend);
719-
if (resize)
759+
if (resize) {
720760
resizeGraph();
761+
}
721762

722763
graph[threadIndex][lockIndex] = WAITING_FOR_LOCK;
723-
if (needTransfer)
764+
if (needTransfer) {
724765
fillPresentEntries(lock, lockIndex);
766+
}
725767
}
726768

727769
/**

0 commit comments

Comments
 (0)