Up

# NSLock class reference

### Authors

David Chisnall (`csdavec@swan.ac.uk`)

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

### Contents -

1. Software documentation for the NSCondition class
2. Software documentation for the NSConditionLock class
3. Software documentation for the NSLock class
4. Software documentation for the NSRecursiveLock class
5. Software documentation for the NSObject(GSTraceLocks) informal protocol
6. Software documentation for the NSLocking protocol

# Software documentation for the NSCondition class

## NSCondition : NSObject

> Declared in:
>   Foundation/NSLock.h

> Conforms to:
>   NSLocking

Availability: OpenStep
NSCondition provides an interface to POSIX condition variables.

Instance Variables
Method summary

- -broadcast
- -name
- -setName:
- -signal
- -wait
- -waitUntilDate:

### broadcast

- (void) broadcast;
Availability: OpenStep
Wakes all threads that are waiting on this condition.

### name

- (NSString*) name;
Availability: OpenStep
Returns the name used for debugging messages.

### setName:

- (void) setName: (NSString*)newName;
Availability: OpenStep
Sets the name used for debugging messages.

### signal

- (void) signal;
Availability: OpenStep
Wakes wany one of the threads that are waiting on this condition.

### wait

- (void) wait;
Availability: OpenStep
Blocks and atomically unlocks the receiver. This method should only be called when the receiver is locked. The caller will then block until the receiver is sent either a -signal or -broadcast message from another thread. At which point, the calling thread will reacquire the lock.

### waitUntilDate:

- (BOOL) waitUntilDate: (NSDate*)limit;
Availability: OpenStep
Blocks the calling thread and acquires the lock, in the same way as -wait . Returns `YES` if the condition is signaled, or `NO` if the timeout is reached.

## Instance Variables for NSCondition Class

### _condition

@protected gs_cond_public_t _condition;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _mutex

@protected gs_mutex_public_t _mutex;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _name

@protected NSString* _name;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

# 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.

Instance Variables
Method summary

- -condition
- -initWithCondition:
- -isLockedByCurrentThread
- -lock
- -lockBeforeDate:
- -lockWhenCondition:
- -lockWhenCondition:beforeDate:
- -name
- -setName:
- -tryLock
- -tryLockWhenCondition:
- -unlock
- -unlockWithCondition:

### condition

- (NSInteger) condition;
Availability: OpenStep
Return the current condition of the lock.

### initWithCondition:

- (id) initWithCondition: (NSInteger)value;
Availability: OpenStep
Initialize lock with given condition.

### isLockedByCurrentThread

- (BOOL) isLockedByCurrentThread;
Availability: OpenStep
Report whether this lock is held by the current thread.
Raises an exception if this is not supported by the system lock mechanism.

### 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: (NSInteger)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: (NSInteger)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: (NSInteger)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: (NSInteger)value;
Availability: OpenStep
Relinquish the lock, setting internal condition to value.

## Instance Variables for NSConditionLock Class

### _condition

@protected NSCondition* _condition;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _condition_value

@protected int _condition_value;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _name

@protected NSString* _name;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

# 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.

Instance Variables
Method summary

- -isLockedByCurrentThread
- -lock
- -lockBeforeDate:
- -name
- -setName:
- -tryLock
- -unlock

### isLockedByCurrentThread

- (BOOL) isLockedByCurrentThread;
Availability: OpenStep
Report whether this lock is held by the current thread.
Raises an exception if this is not supported by the system lock mechanism.

### 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.

### 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.

### unlock

- (void) unlock;
Availability: OpenStep
Relinquish lock.

## Instance Variables for NSLock Class

### _mutex

@protected gs_mutex_public_t _mutex;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _name

@protected NSString* _name;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

# 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.

Instance Variables
Method summary

- -isLockedByCurrentThread
- -lock
- -lockBeforeDate:
- -name
- -setName:
- -tryLock
- -unlock

### isLockedByCurrentThread

- (BOOL) isLockedByCurrentThread;
Availability: OpenStep
Report whether this lock is held by the current thread.
Raises an exception if this is not supported by the system lock mechanism.

### 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.

### 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.

### unlock

- (void) unlock;
Availability: OpenStep
Relinquish lock.

## Instance Variables for NSRecursiveLock Class

### _mutex

@protected gs_mutex_public_t _mutex;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _name

@protected NSString* _name;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

# Software documentation for the NSObject(GSTraceLocks) informal protocol

## NSObject(GSTraceLocks)

> Declared in:
>   Foundation/NSLock.h

Availability: OpenStep
Controls tracing of locks for deadlocking.
Method summary

- +shouldCreateTraceableLocks:
- +tracedCondition
- +tracedConditionLockWithCondition:
- +tracedLock
- +tracedRecursiveLock

### shouldCreateTraceableLocks:

+ (BOOL) shouldCreateTraceableLocks: (BOOL)shouldTrace;
Availability: OpenStep
Sets whether newly created lock objects (NSCondition, NSConditionLock, NSLock, NSRecursiveLock but NOT NSDistributedLock) should be created so that their use by threads is traced and deadlocks can be detected.
Returns the old value of the setting.

### tracedCondition

+ (NSCondition*) tracedCondition;
Availability: OpenStep
Creates and returns a single autoreleased traced condition.

### tracedConditionLockWithCondition:

+ (NSConditionLock*) tracedConditionLockWithCondition: (NSInteger)value;
Availability: OpenStep
Creates and returns a single autoreleased traced condition lock.

### tracedLock

+ (NSLock*) tracedLock;
Availability: OpenStep
Creates and returns a single autoreleased traced lock.

### tracedRecursiveLock

+ (NSRecursiveLock*) tracedRecursiveLock;
Availability: OpenStep
Creates and returns a single autoreleased traced recursive lock.

# Software documentation for the NSLocking protocol

## NSLocking

> Declared in:
>   Foundation/NSLock.h

Availability: OpenStep
Protocol defining lock and unlock operations.
Method summary

- -lock
- -unlock

### lock

- (void) lock;
Availability: OpenStep
Block until acquiring lock.

### unlock

- (void) unlock;
Availability: OpenStep
Relinquish lock.

Up