Up

NSLock class reference

Authors

Scott Christley (scottc@net-community.com)
Richard Frith-Macdonald (rfm@gnu.org)

Version: 27618

Date: 2009-01-17 08:30:11 +0000 (Sat, 17 Jan 2009)

Copyright: (C) 1996,2003 Free Software Foundation, Inc.


Contents -

  1. Software documentation for the NSConditionLock class
  2. Software documentation for the NSLock class
  3. Software documentation for the NSRecursiveLock class
  4. Software documentation for the NSLock(GSCategories) category
  5. Software documentation for the NSRecursiveLock(GSCategories) category
  6. Software documentation for the NSLocking protocol

Software documentation for the NSConditionLock class

NSConditionLock : NSObject

Declared in:
Foundation/NSLock.h
Conforms to:
NSLocking
Availability: OpenStep

Lock that allows user to request it only when an internal integer condition is equal to a particular value. The condition is set on initialization and whenever the lock is relinquished.
Method summary

condition 

- (int) condition;
Availability: OpenStep

Return the current condition of the lock.

initWithCondition: 

- (id) initWithCondition: (int)value;
Availability: OpenStep

Initialize lock with given condition.

lock 

- (void) lock;
Availability: OpenStep

Block until acquiring lock.

lockBeforeDate: 

- (BOOL) lockBeforeDate: (NSDate*)limit;
Availability: OpenStep

Try to acquire lock and return before limit, YES if succeeded, NO if not.

lockWhenCondition: 

- (void) lockWhenCondition: (int)value;
Availability: OpenStep

Acquire lock when it is available and the internal condition is equal to value. Blocks until this occurs.

lockWhenCondition: beforeDate: 

- (BOOL) lockWhenCondition: (int)condition_to_meet beforeDate: (NSDate*)limitDate;
Availability: OpenStep

Try to acquire lock, when internal condition is equal to condition_to_meet, and return before limit, YES if succeeded, NO if not.

name 

- (NSString*) name;
Availability: MacOS-X 10.5.0

Return the name of the receiver or nil of none has been set.

setName: 

- (void) setName: (NSString*)name;
Availability: MacOS-X 10.5.0

Sets the name of the receiver (for use in debugging).

tryLock 

- (BOOL) tryLock;
Availability: OpenStep

Try to acquire lock regardless of condition and return immediately, YES if succeeded, NO if not.

tryLockWhenCondition: 

- (BOOL) tryLockWhenCondition: (int)value;
Availability: OpenStep

Try to acquire lock if condition is equal to value and return immediately in any case, YES if succeeded, NO if not.

unlock 

- (void) unlock;
Availability: OpenStep

Relinquish lock.

unlockWithCondition: 

- (void) unlockWithCondition: (int)value;
Availability: OpenStep

Relinquish the lock, setting internal condition to value.

Software documentation for the NSLock class

NSLock : NSObject

Declared in:
Foundation/NSLock.h
Conforms to:
NSLocking
Availability: OpenStep

Simplest lock for protecting critical sections of code.
An NSLock is used in multi-threaded applications to protect critical pieces of code. While one thread holds a lock within a piece of code, another thread cannot execute that code until the first thread has given up its hold on the lock. The limitation of NSLock is that you can only lock an NSLock once and it must be unlocked before it can be acquired again.
Other lock classes, notably NSRecursiveLock , have different restrictions.
Method summary

lock 

- (void) lock;
Availability: OpenStep

Block until acquiring lock.
Attempts to acquire a lock, and waits until it can do so.

lockBeforeDate: 

- (BOOL) lockBeforeDate: (NSDate*)limit;
Availability: OpenStep

Try to acquire lock and return before limit, YES if succeeded, NO if not.
Attempts to acquire a lock before the date limit passes. It returns YES if it can. It returns NO if it cannot, or if the current thread already has the lock (but it waits until the time limit is up before returning NO).

name 

- (NSString*) name;
Availability: MacOS-X 10.5.0

Return the name of the receiver or nil of none has been set.

setName: 

- (void) setName: (NSString*)name;
Availability: MacOS-X 10.5.0

Sets the name of the receiver (for use in debugging).

tryLock 

- (BOOL) tryLock;
Availability: OpenStep

Try to acquire lock and return immediately, YES if succeeded, NO if not.
Attempts to acquire a lock, but returns immediately if the lock cannot be acquired. It returns YES if the lock is acquired. It returns NO if the lock cannot be acquired or if the current thread already has the lock.

unlock 

- (void) unlock;
Availability: OpenStep

Relinquish lock.

Software documentation for the NSRecursiveLock class

NSRecursiveLock : NSObject

Declared in:
Foundation/NSLock.h
Conforms to:
NSLocking
Availability: OpenStep

Allows the lock to be recursively acquired by the same thread. If the same thread locks the mutex (n) times then that same thread must also unlock it (n) times before another thread can acquire the lock.
See NSLock for more information about what a lock is. A recursive lock extends NSLock in that you can lock a recursive lock multiple times. Each lock must be balanced by a corresponding unlock, and the lock is not released for another thread to acquire until the last unlock call is made (corresponding to the first lock message).
Method summary

lock 

- (void) lock;
Availability: OpenStep

Block until acquiring lock.

lockBeforeDate: 

- (BOOL) lockBeforeDate: (NSDate*)limit;
Availability: OpenStep

Try to acquire lock and return before limit, YES if succeeded, NO if not.
Attempts to acquire a lock before the date limit passes. It returns YES if it can. It returns NO if it cannot (but it waits until the time limit is up before returning NO).

name 

- (NSString*) name;
Availability: MacOS-X 10.5.0

Return the name of the receiver or nil of none has been set.

setName: 

- (void) setName: (NSString*)name;
Availability: MacOS-X 10.5.0

Sets the name of the receiver (for use in debugging).

tryLock 

- (BOOL) tryLock;
Availability: OpenStep

Try to acquire lock regardless of condition and return immediately, YES if succeeded, NO if not.
Attempts to acquire a lock, but returns NO immediately if the lock cannot be acquired. It returns YES if the lock is acquired. Can be called multiple times to make nested locks.

unlock 

- (void) unlock;
Availability: OpenStep

Relinquish lock.

Software documentation for the NSLock(GSCategories) category

NSLock(GSCategories)

Declared in:
Foundation/NSLock.h
Availability: Not in OpenStep/MacOS-X

Defines the newLockAt: method.
Method summary

newLockAt: 

+ (id) newLockAt: (id*)location;
Availability: Not in OpenStep/MacOS-X

Initializes the id pointed to by location with a new instance of the receiver's class in a thread safe manner, unless it has been previously initialized. Returns the contents pointed to by location. The location is considered unintialized if it contains nil.
This method is used in the GS_INITIALIZED_LOCK macro to initialize lock variables when it cannot be insured that they can be initialized in a thread safe environment.
 NSLock *my_lock = nil;

 void function (void)
 {
   [GS_INITIALIZED_LOCK(my_lock, NSLock) lock];
   do_work ();
   [my_lock unlock];
 }
 
 

Software documentation for the NSRecursiveLock(GSCategories) category

NSRecursiveLock(GSCategories)

Declared in:
Foundation/NSLock.h
Availability: Not in OpenStep/MacOS-X

Defines the newLockAt: method.
Method summary

newLockAt: 

+ (id) newLockAt: (id*)location;
Availability: Not in OpenStep/MacOS-X

Initializes the id pointed to by location with a new instance of the receiver's class in a thread safe manner, unless it has been previously initialized. Returns the contents pointed to by location. The location is considered unintialized if it contains nil.
This method is used in the GS_INITIALIZED_LOCK macro to initialize lock variables when it cannot be insured that they can be initialized in a thread safe environment.
 NSLock *my_lock = nil;

 void function (void)
 {
   [GS_INITIALIZED_LOCK(my_lock, NSRecursiveLock) lock];
   do_work ();
   [my_lock unlock];
 }
 
 

Software documentation for the NSLocking protocol

NSLocking

Declared in:
Foundation/NSLock.h
Availability: OpenStep

Protocol defining lock and unlock operations.
Method summary

lock 

- (void) lock;
Availability: OpenStep

Block until acquiring lock.

unlock 

- (void) unlock;
Availability: OpenStep

Relinquish lock.


Up