PATH |
NSMultiReaderLock
- Inherits from:
- Object
- Implements:
- NSLocking
- Package:
- com.webobjects.foundation
Class Description
The NSMultiReaderLock class provides reader and writer locks. The locks are recursive; a single thread can request a lock many times, but a lock is actually taken only on the first request. Likewise, when a thread indicates it's finished with a lock, it takes an equal number of unlock... invocations to return the lock.
There's no limit on the number of reader locks that a process can take. However, there can only be one writer lock at a time, and a writer lock is not issued until all reader locks are returned. Reader locks aren't issued to new threads when there is a thread waiting for a writer lock, but threads that already have a reader lock can increment their lock count.
NSMultiReaderLock correctly handles promotion of a reader lock to a writer lock, and the extension of a reader lock to the current writer. This prevents a thread from deadlocking on itself when requesting a combination of lock types.
NSMultiReaderLocks are slightly more time-expensive than NSRecursiveLocks because the recursion count has to be stored per-thread, causing each request for a reader lock to incur at least one hash lookup. Writer locks are even more expensive because NSMultiReaderLock must poll the hashtable until all reader locks have been returned before the writer lock can be taken.
Method Types
- Constructors
- NSMultiReaderLock
- Managing reader locks
- lockForReading
- retrieveReaderLocks
- suspendReaderLocks
- tryLockForReading
- unlockForReading
- Managing writer locks
- lock
- lockForWriting
- tryLockForWriting
- unlock
- unlockForWriting
- Methods inherited from Object
- toString
Constructors
NSMultiReaderLock
public NSMultiReaderLock()
Instance Methods
lock
public void lock()
lockForReading
public void lockForReading()
lockForWriting
public void lockForWriting()
retrieveReaderLocks
public void retrieveReaderLocks()
suspendReaderLocks
public void suspendReaderLocks()
toString
public String toString()
tryLockForReading
public boolean tryLockForReading()
true
if the current thread is able to immediately obtain a reader lock. There are three ways this can happen:
- There are no outstanding writer locks.
- The writer lock is held by the current thread.
- The current thread already has a reader lock.
This method implicitly calls lockForReading, so you must call unlockForReading if tryLockForReading returns true
.
tryLockForWriting
public boolean tryLockForWriting()
true
if the current thread is able to immediately obtain a writer lock. Returns false
if another thread already has the lock or is queued to receive it. This method implicitly calls lockForWriting, so you must call unlockForWriting if tryLockForWriting returns true
.
unlock
public void unlock()
unlockForReading
public void unlockForReading()
unlockForWriting
public void unlockForWriting()
© 2001 Apple Computer, Inc. (Last Published April 17, 2001)