* Source/NSLock.m

* Headers/Foundation/NSLock.h
	Completely rewritten implementations of NSLock.h classes.  These are now
	faster, more complete, OS X-compatible, and most importantly actually
	work.  The old ones, for example, called functions that were not
	implemented on Windows.  
	* Source/NSThread.m
	Call pthread functions directly in NSThread instead of via the libobjc
	abstraction layer.  Also fixed a few issues, such as GC not being
	initialized properly for NSThread subclasses that override -main (Javaism
	supported by OS X) and tidies up the code in several places, removing
	premature optimizations, especially those that introduce a test for an
	unlikely case at the start of a method and thus slow everything down.

	As a result of this change, GNUstep now depends on an implementation of
	POSIX threads.  This is included as standard on all modern UNIX systems,
	and as an option on less-modern UNIX systems and non-UNIX systems,
	including Windows.  If you are building GNUstep on Windows, please install
	the pthreads-win32 package, available from:

	http://sourceware.org/pthreads-win32/

	PLEASE TEST THIS!  There may be some code that depended on the old
	behaviour.  I have been running the new NSLock implementation on FreeBSD
	for a few weeks without issue; please report to me any problems that you
	have on your platform.



git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@28598 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
David Chisnall 2009-09-02 13:03:13 +00:00
parent 9f5f6bc382
commit d7a877b871
4 changed files with 458 additions and 920 deletions

View file

@ -35,6 +35,8 @@
#import <Foundation/NSObject.h>
#include <pthread.h>
#if defined(__cplusplus)
extern "C" {
#endif
@ -58,12 +60,20 @@ extern "C" {
/**
* Simplest lock for protecting critical sections of code.
*
* An <code>NSLock</code> 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 <code>NSLock</code> is
* that you can only lock an <code>NSLock</code> once and it must be unlocked
* before it can be acquired again.<br /> Other lock classes, notably
* [NSRecursiveLock], have different restrictions.
*/
@interface NSLock : NSObject <NSLocking>
{
@private
void *_mutex;
NSString *_name;
pthread_mutex_t _mutex;
NSString *_name;
}
/**
@ -98,6 +108,46 @@ extern "C" {
@end
/**
* NSCondition provides an interface to POSIX condition variables.
*/
@interface NSCondition : NSObject <NSLocking>
{
@private
pthread_cond_t _condition;
pthread_mutex_t _mutex;
NSString *_name;
}
/**
* Blocks 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 this
* point, the calling thread will reacquire the lock.
*/
- (void)wait;
/**
* 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.
*/
- (BOOL)waitUntilDate: (NSDate*)limit;
/**
* Wakes a single thread that is waiting on this condition.
*/
- (void)signal;
/**
* Wakes all threads that are waiting on this condition.
*/
- (void)broadcast;
/**
* Sets the name used for debugging messages.
*/
- (void)setName:(NSString*)newName;
/**
* Returns the name used for debugging messages.
*/
- (NSString*)name;
@end
/**
* Lock that allows user to request it only when an internal integer
* condition is equal to a particular value. The condition is set on
@ -106,8 +156,7 @@ extern "C" {
@interface NSConditionLock : NSObject <NSLocking>
{
@private
void *_condition;
void *_mutex;
NSCondition *_condition;
int _condition_value;
NSString *_name;
}
@ -198,7 +247,7 @@ extern "C" {
@interface NSRecursiveLock : NSObject <NSLocking>
{
@private
void *_mutex;
pthread_mutex_t _mutex;
NSString *_name;
}