/** * If there is another semaphore with the same runnable in the * queue, the other is returned and the new one is not added. */ private synchronized Semaphore enqueue(Semaphore newSemaphore) { Semaphore semaphore = (Semaphore) operations.get(newSemaphore); if (semaphore == null) { operations.enqueue(newSemaphore); return newSemaphore; } return semaphore; }
public Iterator elements() { /**/ if (isEmpty()) return new ArrayList(0).iterator(); /* if head < tail we can use the same array */ if (head <= tail) return Arrays.asList(elements).iterator(); /* otherwise we need to create a new array */ Object[] newElements = new Object[size()]; int end = (elements.length - head); System.arraycopy(elements, head, newElements, 0, end); System.arraycopy(elements, 0, newElements, end, tail); return Arrays.asList(newElements).iterator(); }
@Override public String toString() { StringBuffer sb = new StringBuffer(); sb.append("["); //$NON-NLS-1$ if (!isEmpty()) { Iterator it = elements(); while (true) { sb.append(it.next()); if (it.hasNext()) sb.append(", "); //$NON-NLS-1$ else break; } } sb.append("]"); //$NON-NLS-1$ return sb.toString(); } }
/** * Removes the given object from the queue. Shifts the underlying array. */ public boolean remove(Object o) { int index = head; //find the object to remove while (index != tail) { if (elements[index].equals(o)) break; index = increment(index); } //if element wasn't found, return if (index == tail) return false; //store a reference to it (needed for reuse of objects) Object toRemove = elements[index]; int nextIndex = -1; while (index != tail) { nextIndex = increment(index); if (nextIndex != tail) elements[index] = elements[nextIndex]; index = nextIndex; } //decrement tail tail = decrement(tail); //if objects are reused, transfer the reference that is removed to the end of the queue //otherwise set the element after the last one to null (to avoid duplicate references) elements[tail] = reuse ? toRemove : null; return true; }
public Object get(Object o) { int index = head; while (index != tail) { if (elements[index].equals(o)) return elements[index]; index = increment(index); } return null; }
/** * This lock has just been granted to a new thread (the thread waited for it). * Remove the request from the queue and update both the graph and the lock. */ private synchronized void updateCurrentOperation() { operations.dequeue(); setCurrentOperationThread(Thread.currentThread()); } }
/** * Removes a semaphore from the queue of waiting operations. * * @param semaphore The semaphore to remove */ private synchronized void removeFromQueue(Semaphore semaphore) { operations.remove(semaphore); }
/** * Releases this lock from the thread that used to own it. * Grants this lock to the next thread in the queue. */ private synchronized void doRelease() { //notify hook manager.aboutToRelease(); depth = 0; Semaphore next = (Semaphore) operations.peek(); setCurrentOperationThread(null); if (next != null) next.release(); }
/** * Attempts to acquire the lock. Returns false if the lock is not available and * true if the lock has been successfully acquired. */ private synchronized boolean attempt() { //return true if we already own the lock //also, if nobody is waiting, grant the lock immediately if ((currentOperationThread == Thread.currentThread()) || (currentOperationThread == null && operations.isEmpty())) { depth++; setCurrentOperationThread(Thread.currentThread()); return true; } return false; }
public Iterator elements() { /**/ if (isEmpty()) return new ArrayList(0).iterator(); /* if head < tail we can use the same array */ if (head <= tail) return Arrays.asList(elements).iterator(); /* otherwise we need to create a new array */ Object[] newElements = new Object[size()]; int end = (elements.length - head); System.arraycopy(elements, head, newElements, 0, end); System.arraycopy(elements, 0, newElements, end, tail); return Arrays.asList(newElements).iterator(); }
/** * Removes the given object from the queue. Shifts the underlying array. */ public boolean remove(Object o) { int index = head; //find the object to remove while (index != tail) { if (elements[index].equals(o)) break; index = increment(index); } //if element wasn't found, return if (index == tail) return false; //store a reference to it (needed for reuse of objects) Object toRemove = elements[index]; int nextIndex = -1; while (index != tail) { nextIndex = increment(index); if (nextIndex != tail) elements[index] = elements[nextIndex]; index = nextIndex; } //decrement tail tail = decrement(tail); //if objects are reused, transfer the reference that is removed to the end of the queue //otherwise set the element after the last one to null (to avoid duplicate references) elements[tail] = reuse ? toRemove : null; return true; }
public Object get(Object o) { int index = head; while (index != tail) { if (elements[index].equals(o)) return elements[index]; index = increment(index); } return null; }
/** * This lock has just been granted to a new thread (the thread waited for it). * Remove the request from the queue and update both the graph and the lock. */ private synchronized void updateCurrentOperation() { operations.dequeue(); setCurrentOperationThread(Thread.currentThread()); }
/** * Removes a semaphore from the queue of waiting operations. * * @param semaphore The semaphore to remove */ private synchronized void removeFromQueue(Semaphore semaphore) { operations.remove(semaphore); }
/** * Releases this lock from the thread that used to own it. * Grants this lock to the next thread in the queue. */ private synchronized void doRelease() { //notify hook manager.aboutToRelease(); depth = 0; Semaphore next = (Semaphore) operations.peek(); setCurrentOperationThread(null); if (next != null) next.release(); }
/** * Attempts to acquire the lock. Returns false if the lock is not available and * true if the lock has been successfully acquired. */ private synchronized boolean attempt() { //return true if we already own the lock //also, if nobody is waiting, grant the lock immediately if ((currentOperationThread == Thread.currentThread()) || (currentOperationThread == null && operations.isEmpty())) { depth++; setCurrentOperationThread(Thread.currentThread()); return true; } return false; }