synchronized int endWrite() { if (activeWriter_ != Thread.currentThread()) { throw new IllegalMonitorStateException(); } --writeHolds_; if (writeHolds_ > 0) // still being held return NONE; else { activeWriter_ = null; if (waitingReaders_ > 0 && allowReader()) return READER; else if (waitingWriters_ > 0) return WRITER; else return NONE; } }
InterruptedException ie = null; synchronized (this) { if (!lock.sync.startReadFromNewReader()) { for (; ; ) { try { ReadLock.this.wait(); if (lock.sync.startReadFromWaitingReader()) return; lock.sync.cancelledWaitingReader(); ie = ex; break;
InterruptedException ie = null; synchronized (this) { if (!lock.sync.startWriteFromNewWriter()) { for (; ; ) { try { WriteLock.this.wait(); if (lock.sync.startWriteFromWaitingWriter()) return; lock.sync.cancelledWaitingWriter(); WriteLock.this.notify(); ie = ex;
InterruptedException ie = null; synchronized (this) { if (!lock.sync.startWriteFromNewWriter()) { for (; ; ) { try { WriteLock.this.wait(); if (lock.sync.startWriteFromWaitingWriter()) return; lock.sync.cancelledWaitingWriter(); WriteLock.this.notify(); ie = ex;
InterruptedException ie = null; synchronized (this) { if (!lock.sync.startReadFromNewReader()) { for (; ; ) { try { ReadLock.this.wait(); if (lock.sync.startReadFromWaitingReader()) return; lock.sync.cancelledWaitingReader(); ie = ex; break;
/** * Attempts to release this lock. * * <p>If the current thread is the holder of this lock then * the hold count is decremented. If the hold count is now * zero then the lock is released. If the current thread is * not the holder of this lock then {@link * IllegalMonitorStateException} is thrown. * * @throws IllegalMonitorStateException if the current thread does not * hold this lock. */ public void unlock() { switch (lock.sync.endWrite()) { case Sync.NONE: return; case Sync.READER: lock.readerLock_.signalWaiters(); return; case Sync.WRITER: lock.writerLock_.signalWaiters(); return; } }
/** * Attempts to release this lock. * * <p>If the current thread is the holder of this lock then * the hold count is decremented. If the hold count is now * zero then the lock is released. If the current thread is * not the holder of this lock then {@link * IllegalMonitorStateException} is thrown. * * @throws IllegalMonitorStateException if the current thread does not * hold this lock. */ public void unlock() { switch (lock.sync.endWrite()) { case Sync.NONE: return; case Sync.READER: lock.readerLock_.signalWaiters(); return; case Sync.WRITER: lock.writerLock_.signalWaiters(); return; } }
synchronized boolean startRead() { Thread t = Thread.currentThread(); Integer c = (Integer) readers_.get(t); if (c != null) { // already held -- just increment hold count readers_.put(t, new Integer(c.intValue() + 1)); ++activeReaders_; return true; } else if (allowReader()) { readers_.put(t, IONE); ++activeReaders_; return true; } else return false; }
/** * Returns the thread that currently owns the write lock, or * {@code null} if not owned. When this method is called by a * thread that is not the owner, the return value reflects a * best-effort approximation of current lock status. For example, * the owner may be momentarily {@code null} even if there are * threads trying to acquire the lock but have not yet done so. * This method is designed to facilitate construction of * subclasses that provide more extensive lock monitoring * facilities. * * @return the owner, or {@code null} if not owned */ protected Thread getOwner() { return sync.getOwner(); }
/** * Returns the thread that currently owns the write lock, or * {@code null} if not owned. When this method is called by a * thread that is not the owner, the return value reflects a * best-effort approximation of current lock status. For example, * the owner may be momentarily {@code null} even if there are * threads trying to acquire the lock but have not yet done so. * This method is designed to facilitate construction of * subclasses that provide more extensive lock monitoring * facilities. * * @return the owner, or {@code null} if not owned */ protected Thread getOwner() { return sync.getOwner(); }
synchronized boolean startRead() { Thread t = Thread.currentThread(); Integer c = (Integer) readers_.get(t); if (c != null) { // already held -- just increment hold count readers_.put(t, new Integer(c.intValue() + 1)); ++activeReaders_; return true; } else if (allowReader()) { readers_.put(t, IONE); ++activeReaders_; return true; } else return false; }
synchronized int endWrite() { if (activeWriter_ != Thread.currentThread()) { throw new IllegalMonitorStateException(); } --writeHolds_; if (writeHolds_ > 0) // still being held return NONE; else { activeWriter_ = null; if (waitingReaders_ > 0 && allowReader()) return READER; else if (waitingWriters_ > 0) return WRITER; else return NONE; } }
/** * Returns an estimate of the number of threads waiting to acquire * either the read or write lock. The value is only an estimate * because the number of threads may change dynamically while this * method traverses internal data structures. This method is * designed for use in monitoring of the system state, not for * synchronization control. * * @return the estimated number of threads waiting for this lock */ public final int getQueueLength() { return sync.getQueueLength(); }
/** * Attempts to release this lock. * * <p> If the number of readers is now zero then the lock * is made available for write lock attempts. */ public void unlock() { switch (lock.sync.endRead()) { case Sync.NONE: return; case Sync.READER: lock.readerLock_.signalWaiters(); return; case Sync.WRITER: lock.writerLock_.signalWaiters(); return; } }
/** * Attempts to release this lock. * * <p> If the number of readers is now zero then the lock * is made available for write lock attempts. */ public void unlock() { switch (lock.sync.endRead()) { case Sync.NONE: return; case Sync.READER: lock.readerLock_.signalWaiters(); return; case Sync.WRITER: lock.writerLock_.signalWaiters(); return; } }
/** * Queries the number of read locks held for this lock. This * method is designed for use in monitoring system state, not for * synchronization control. * @return the number of read locks held. */ public int getReadLockCount() { return sync.getReadLockCount(); }
/** * Queries the number of read locks held for this lock. This * method is designed for use in monitoring system state, not for * synchronization control. * @return the number of read locks held. */ public int getReadLockCount() { return sync.getReadLockCount(); }
/** * Queries the number of reentrant write holds on this lock by the * current thread. A writer thread has a hold on a lock for * each lock action that is not matched by an unlock action. * * @return the number of holds on the write lock by the current thread, * or zero if the write lock is not held by the current thread */ public int getWriteHoldCount() { return sync.getWriteHoldCount(); }
/** * Queries the number of reentrant write holds on this lock by the * current thread. A writer thread has a hold on a lock for * each lock action that is not matched by an unlock action. * * @return the number of holds on the write lock by the current thread, * or zero if the write lock is not held by the current thread */ public int getWriteHoldCount() { return sync.getWriteHoldCount(); }
/** * Returns an estimate of the number of threads waiting to acquire * either the read or write lock. The value is only an estimate * because the number of threads may change dynamically while this * method traverses internal data structures. This method is * designed for use in monitoring of the system state, not for * synchronization control. * * @return the estimated number of threads waiting for this lock */ public final int getQueueLength() { return sync.getQueueLength(); }