/** * Check that the addition of a waiting thread did not produce deadlock. * If deadlock is detected return true, else return false. */ private boolean checkWaitCycles(int[] waitingThreads, int lockIndex) { /** * find the lock that this thread is waiting for * recursively check if this is a cycle (i.e. a thread waiting on itself) */ for (int i = 0; i < graph.length; i++) { if (graph[i][lockIndex] > NO_STATE) { if (waitingThreads[i] > NO_STATE) { return true; } //keep track that we already visited this thread waitingThreads[i]++; for (int j = 0; j < graph[i].length; j++) { if (graph[i][j] == WAITING_FOR_LOCK) { if (checkWaitCycles(waitingThreads, j)) return true; } } //this thread is not involved in a cycle yet, so remove the visited flag waitingThreads[i]--; } } return false; }
/** * Check that the addition of a waiting thread did not produce deadlock. * If deadlock is detected return true, else return false. */ private boolean checkWaitCycles(int[] waitingThreads, int lockIndex) { /** * find the lock that this thread is waiting for * recursively check if this is a cycle (i.e. a thread waiting on itself) */ for (int i = 0; i < graph.length; i++) { if (graph[i][lockIndex] > NO_STATE) { if (waitingThreads[i] > NO_STATE) { return true; } //keep track that we already visited this thread waitingThreads[i]++; for (int j = 0; j < graph[i].length; j++) { if (graph[i][j] == WAITING_FOR_LOCK) { if (checkWaitCycles(waitingThreads, j)) return true; } } //this thread is not involved in a cycle yet, so remove the visited flag waitingThreads[i]--; } } return false; }
/** * Check that the addition of a waiting thread did not produce deadlock. * If deadlock is detected return true, else return false. */ private boolean checkWaitCycles(int[] waitingThreads, int lockIndex) { /** * find the lock that this thread is waiting for * recursively check if this is a cycle (i.e. a thread waiting on itself) */ for (int i = 0; i < graph.length; i++) { if (graph[i][lockIndex] > NO_STATE) { if (waitingThreads[i] > NO_STATE) { return true; } //keep track that we already visited this thread waitingThreads[i]++; for (int j = 0; j < graph[i].length; j++) { if (graph[i][j] == WAITING_FOR_LOCK) { if (checkWaitCycles(waitingThreads, j)) return true; } } //this thread is not involved in a cycle yet, so remove the visited flag waitingThreads[i]--; } } return false; }
/** * Check that the addition of a waiting thread did not produce deadlock. * If deadlock is detected return true, else return false. */ private boolean checkWaitCycles(int[] waitingThreads, int lockIndex) { /** * find the lock that this thread is waiting for * recursively check if this is a cycle (i.e. a thread waiting on itself) */ for (int i = 0; i < graph.length; i++) { if (graph[i][lockIndex] > NO_STATE) { if (waitingThreads[i] > NO_STATE) { return true; } //keep track that we already visited this thread waitingThreads[i]++; for (int j = 0; j < graph[i].length; j++) { if (graph[i][j] == WAITING_FOR_LOCK) { if (checkWaitCycles(waitingThreads, j)) return true; } } //this thread is not involved in a cycle yet, so remove the visited flag waitingThreads[i]--; } } return false; }
/** * The given thread could not get the given lock and is waiting for it. * Update the graph. */ Deadlock lockWaitStart(Thread client, ISchedulingRule lock) { setToWait(client, lock, false); int lockIndex = indexOf(lock, false); int[] temp = new int[lockThreads.size()]; //check if the addition of the waiting thread caused deadlock if (!checkWaitCycles(temp, lockIndex)) return null; //there is a deadlock in the graph Thread[] threads = getThreadsInDeadlock(client); //find a thread whose locks can be suspended to resolve the deadlock Thread candidate = resolutionCandidate(threads); ISchedulingRule[] locksToSuspend = realLocksForThread(candidate); Deadlock deadlock = new Deadlock(threads, locksToSuspend, candidate); reportDeadlock(deadlock); if (JobManager.DEBUG_DEADLOCK) throw new IllegalStateException("Deadlock detected. Caused by thread " + client.getName() + '.'); //$NON-NLS-1$ // Update the graph to indicate that the locks will now be suspended. // To indicate that the lock will be suspended, we set the thread to wait for the lock. // When the lock is forced to be released, the entry will be cleared. for (int i = 0; i < locksToSuspend.length; i++) setToWait(deadlock.getCandidate(), locksToSuspend[i], true); return deadlock; }
/** * The given thread could not get the given lock and is waiting for it. * Update the graph. */ Deadlock lockWaitStart(Thread client, ISchedulingRule lock) { setToWait(client, lock, false); int lockIndex = indexOf(lock, false); int[] temp = new int[lockThreads.size()]; //check if the addition of the waiting thread caused deadlock if (!checkWaitCycles(temp, lockIndex)) return null; //there is a deadlock in the graph Thread[] threads = getThreadsInDeadlock(client); Thread candidate = resolutionCandidate(threads); ISchedulingRule[] locksToSuspend = realLocksForThread(candidate); Deadlock deadlock = new Deadlock(threads, locksToSuspend, candidate); //find a thread whose locks can be suspended to resolve the deadlock if (JobManager.DEBUG_LOCKS) reportDeadlock(deadlock); if (JobManager.DEBUG_DEADLOCK) throw new IllegalStateException("Deadlock detected. Caused by thread " + client.getName() + '.'); //$NON-NLS-1$ // Update the graph to indicate that the locks will now be suspended. // To indicate that the lock will be suspended, we set the thread to wait for the lock. // When the lock is forced to be released, the entry will be cleared. for (int i = 0; i < locksToSuspend.length; i++) setToWait(deadlock.getCandidate(), locksToSuspend[i], true); return deadlock; }
/** * The given thread could not get the given lock and is waiting for it. * Update the graph. */ Deadlock lockWaitStart(Thread client, ISchedulingRule lock) { setToWait(client, lock, false); int lockIndex = indexOf(lock, false); int[] temp = new int[lockThreads.size()]; //check if the addition of the waiting thread caused deadlock if (!checkWaitCycles(temp, lockIndex)) return null; //there is a deadlock in the graph Thread[] threads = getThreadsInDeadlock(client); //find a thread whose locks can be suspended to resolve the deadlock Thread candidate = resolutionCandidate(threads); ISchedulingRule[] locksToSuspend = realLocksForThread(candidate); Deadlock deadlock = new Deadlock(threads, locksToSuspend, candidate); reportDeadlock(deadlock); if (JobManager.DEBUG_DEADLOCK) throw new IllegalStateException("Deadlock detected. Caused by thread " + client.getName() + '.'); //$NON-NLS-1$ // Update the graph to indicate that the locks will now be suspended. // To indicate that the lock will be suspended, we set the thread to wait for the lock. // When the lock is forced to be released, the entry will be cleared. for (int i = 0; i < locksToSuspend.length; i++) setToWait(deadlock.getCandidate(), locksToSuspend[i], true); return deadlock; }
/** * The given thread could not get the given lock and is waiting for it. * Update the graph. */ Deadlock lockWaitStart(Thread client, ISchedulingRule lock) { setToWait(client, lock, false); int lockIndex = indexOf(lock, false); int[] temp = new int[lockThreads.size()]; //check if the addition of the waiting thread caused deadlock if (!checkWaitCycles(temp, lockIndex)) return null; //there is a deadlock in the graph Thread[] threads = getThreadsInDeadlock(client); //find a thread whose locks can be suspended to resolve the deadlock Thread candidate = resolutionCandidate(threads); ISchedulingRule[] locksToSuspend = realLocksForThread(candidate); Deadlock deadlock = new Deadlock(threads, locksToSuspend, candidate); reportDeadlock(deadlock); if (JobManager.DEBUG_DEADLOCK) throw new IllegalStateException("Deadlock detected. Caused by thread " + client.getName() + '.'); //$NON-NLS-1$ // Update the graph to indicate that the locks will now be suspended. // To indicate that the lock will be suspended, we set the thread to wait for the lock. // When the lock is forced to be released, the entry will be cleared. for (int i = 0; i < locksToSuspend.length; i++) setToWait(deadlock.getCandidate(), locksToSuspend[i], true); return deadlock; }