Minor fixes/tweaks

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@28605 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2009-09-03 09:45:23 +00:00
parent e833003ca5
commit 80afb705cf
4 changed files with 184 additions and 158 deletions

View file

@ -1,3 +1,9 @@
2009-09-03 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSLock.m: Fix a few variable declarations to work with older
compilers. Fix indentation/whitespace for coding standards consistency.
* Source/NSURLProtocol.m: Fix attempt to use deallocated stream.
2009-09-01 David Chisnall <csdavec@swan.ac.uk>
* Source/NSLock.m
@ -9,16 +15,17 @@
* 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.
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:
including Windows. If you are building GNUstep on Windows,
please install the pthreads-win32 package, available from:
http://sourceware.org/pthreads-win32/

View file

@ -31,13 +31,13 @@
#ifndef __NSLock_h_GNUSTEP_BASE_INCLUDE
#define __NSLock_h_GNUSTEP_BASE_INCLUDE
#import <GNUstepBase/GSVersionMacros.h>
#import <GNUstepBase/GSVersionMacros.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSObject.h>
#include <pthread.h>
#if defined(__cplusplus)
#if defined(__cplusplus)
extern "C" {
#endif
@ -114,9 +114,9 @@ extern "C" {
@interface NSCondition : NSObject <NSLocking>
{
@private
pthread_cond_t _condition;
pthread_mutex_t _mutex;
NSString *_name;
pthread_cond_t _condition;
pthread_mutex_t _mutex;
NSString *_name;
}
/**
* Blocks atomically unlocks the receiver. This method should only be called
@ -124,28 +124,28 @@ extern "C" {
* is sent either a -signal or -broadcast message from another thread. At this
* point, the calling thread will reacquire the lock.
*/
- (void)wait;
- (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;
- (BOOL) waitUntilDate: (NSDate*)limit;
/**
* Wakes a single thread that is waiting on this condition.
*/
- (void)signal;
- (void) signal;
/**
* Wakes all threads that are waiting on this condition.
*/
- (void)broadcast;
- (void) broadcast;
/**
* Sets the name used for debugging messages.
*/
- (void)setName:(NSString*)newName;
- (void) setName: (NSString*)newName;
/**
* Returns the name used for debugging messages.
*/
- (NSString*)name;
- (NSString*) name;
@end
/**
@ -372,9 +372,9 @@ extern "C" {
+ (id) newLockAt: (id *)location;
@end
#endif /* GS_API_NONE */
#endif /* GS_API_NONE */
#if defined(__cplusplus)
#if defined(__cplusplus)
}
#endif

View file

@ -42,74 +42,74 @@
* code.
*/
#define NSLOCKING_METHODS \
- (void)lock\
- (void) lock\
{\
int err = pthread_mutex_lock(&_mutex);\
if (EINVAL == err)\
{\
[NSException raise: NSLockException\
format: @"failed to lock mutex"];\
}\
if (EDEADLK == err)\
{\
_NSLockError(self, _cmd);\
}\
int err = pthread_mutex_lock(&_mutex);\
if (EINVAL == err)\
{\
[NSException raise: NSLockException\
format: @"failed to lock mutex"];\
}\
if (EDEADLK == err)\
{\
_NSLockError(self, _cmd);\
}\
}\
- (void)unlock\
- (void) unlock\
{\
if (0 != pthread_mutex_unlock(&_mutex))\
{\
[NSException raise: NSLockException\
format: @"failed to unlock mutex"];\
}\
if (0 != pthread_mutex_unlock(&_mutex))\
{\
[NSException raise: NSLockException\
format: @"failed to unlock mutex"];\
}\
}\
- (NSString*) description\
{\
if (_name == nil)\
{\
return [super description];\
}\
return [NSString stringWithFormat: @"%@ '%@'",\
[super description], _name];\
if (_name == nil)\
{\
return [super description];\
}\
return [NSString stringWithFormat: @"%@ '%@'",\
[super description], _name];\
}\
- (BOOL) tryLock\
{\
int err = pthread_mutex_trylock(&_mutex);\
if (EDEADLK == err)\
{\
_NSLockError(self, _cmd);\
return YES;\
}\
return (0 == err);\
int err = pthread_mutex_trylock(&_mutex);\
if (EDEADLK == err)\
{\
_NSLockError(self, _cmd);\
return YES;\
}\
return (0 == err);\
}\
- (BOOL) lockBeforeDate: (NSDate*)limit\
{\
do\
do\
{\
int err = pthread_mutex_trylock(&_mutex);\
if (EDEADLK == err)\
{\
int err = pthread_mutex_trylock(&_mutex);\
if (EDEADLK == err)\
{\
_NSLockError(self, _cmd);\
return YES;\
}\
if (0 == err)\
{\
return YES;\
}\
sched_yield();\
} while([limit timeIntervalSinceNow] < 0);\
return NO;\
_NSLockError(self, _cmd);\
return YES;\
}\
if (0 == err)\
{\
return YES;\
}\
sched_yield();\
} while([limit timeIntervalSinceNow] < 0);\
return NO;\
}\
NAME_METHODS
#define NAME_METHODS \
- (void)setName:(NSString*)newName\
- (void) setName: (NSString*)newName\
{\
ASSIGNCOPY(_name, newName);\
ASSIGNCOPY(_name, newName);\
}\
- (NSString*)name\
- (NSString*) name\
{\
return _name;\
return _name;\
}
/**
@ -120,9 +120,9 @@ NAME_METHODS
*/
void _NSLockError(id obj, SEL _cmd)
{
NSLog(@"*** -[%@ %@]: deadlock (%@)", [obj class],
NSStringFromSelector(_cmd), obj);
NSLog(@"*** Break on _NSLockError() to debug.");
NSLog(@"*** -[%@ %@]: deadlock (%@)", [obj class],
NSStringFromSelector(_cmd), obj);
NSLog(@"*** Break on _NSLockError() to debug.");
}
/**
@ -132,20 +132,23 @@ void _NSLockError(id obj, SEL _cmd)
#define INIT_LOCK_WITH_TYPE(lock_type) \
- (id) init\
{\
if (nil == (self = [super init])) { return nil; }\
pthread_mutexattr_t attr;\
pthread_mutexattr_init(&attr);\
pthread_mutexattr_settype(&attr, lock_type);\
if (0 != pthread_mutex_init(&_mutex, &attr))\
{\
[self release];\
return nil;\
}\
return self;\
pthread_mutexattr_t attr;\
if (nil == (self = [super init]))\
{\
return nil;\
}\
pthread_mutexattr_init(&attr);\
pthread_mutexattr_settype(&attr, lock_type);\
if (0 != pthread_mutex_init(&_mutex, &attr))\
{\
[self release];\
return nil;\
}\
return self;\
}\
- (void) finalize\
{\
pthread_mutex_destroy(&_mutex);\
pthread_mutex_destroy(&_mutex);\
}\
- (void) dealloc\
{\
@ -174,58 +177,70 @@ NSLOCKING_METHODS
@implementation NSCondition
- (id)init
{
if (nil == (self = [super init])) { return nil; }
if (0 != pthread_cond_init(&_condition, NULL))
{
[self release];
return nil;
}
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
if (0 != pthread_mutex_init(&_mutex, &attr))
{
pthread_cond_destroy(&_condition);
[self release];
return nil;
}
return self;
pthread_mutexattr_t attr;
if (nil == (self = [super init]))
{
return nil;
}
if (0 != pthread_cond_init(&_condition, NULL))
{
[self release];
return nil;
}
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
if (0 != pthread_mutex_init(&_mutex, &attr))
{
pthread_cond_destroy(&_condition);
[self release];
return nil;
}
return self;
}
- (void)finalize
- (void) finalize
{
pthread_cond_destroy(&_condition);
pthread_mutex_destroy(&_mutex);
pthread_cond_destroy(&_condition);
pthread_mutex_destroy(&_mutex);
}
- (void)dealloc
- (void) dealloc
{
[self finalize];
[_name release];
[super dealloc];
[self finalize];
[_name release];
[super dealloc];
}
- (void)wait
- (void) wait
{
pthread_cond_wait(&_condition, &_mutex);
pthread_cond_wait(&_condition, &_mutex);
}
- (BOOL)waitUntilDate: (NSDate*)limit
- (BOOL) waitUntilDate: (NSDate*)limit
{
NSTimeInterval t = [limit timeIntervalSinceReferenceDate];
double secs, subsecs;
struct timespec timeout;
// Split the float into seconds and fractions of a second
subsecs = modf(t, &secs);
timeout.tv_sec = secs;
// Convert fractions of a second to nanoseconds
timeout.tv_nsec = subsecs * 1e9;
return (0 == pthread_cond_timedwait(&_condition, &_mutex, &timeout));
NSTimeInterval t = [limit timeIntervalSinceReferenceDate];
double secs, subsecs;
struct timespec timeout;
// Split the float into seconds and fractions of a second
subsecs = modf(t, &secs);
timeout.tv_sec = secs;
// Convert fractions of a second to nanoseconds
timeout.tv_nsec = subsecs * 1e9;
return (0 == pthread_cond_timedwait(&_condition, &_mutex, &timeout));
}
- (void)signal
- (void) signal
{
pthread_cond_signal(&_condition);
pthread_cond_signal(&_condition);
}
- (void)broadcast
- (void) broadcast
{
pthread_cond_broadcast(&_condition);
pthread_cond_broadcast(&_condition);
}
NSLOCKING_METHODS
@end
@ -237,14 +252,17 @@ NSLOCKING_METHODS
- (id) initWithCondition: (NSInteger)value
{
if (nil == (self = [super init])) { return nil; }
if (nil == (_condition = [NSCondition new]))
{
[self release];
return nil;
}
_condition_value = value;
return self;
if (nil == (self = [super init]))
{
return nil;
}
if (nil == (_condition = [NSCondition new]))
{
[self release];
return nil;
}
_condition_value = value;
return self;
}
- (void) dealloc
@ -261,63 +279,63 @@ NSLOCKING_METHODS
- (void) lockWhenCondition: (NSInteger)value
{
[_condition lock];
while (value != _condition_value)
{
[_condition wait];
}
[_condition lock];
while (value != _condition_value)
{
[_condition wait];
}
}
- (void) unlockWithCondition: (NSInteger)value
{
_condition_value = value;
[_condition broadcast];
[_condition unlock];
_condition_value = value;
[_condition broadcast];
[_condition unlock];
}
- (BOOL) tryLockWhenCondition: (NSInteger)value
{
return [self lockWhenCondition: value
beforeDate: [NSDate date]];
return [self lockWhenCondition: value
beforeDate: [NSDate date]];
}
- (BOOL) lockBeforeDate: (NSDate*)limit
{
return [_condition lockBeforeDate: limit];
return [_condition lockBeforeDate: limit];
}
- (BOOL) lockWhenCondition: (NSInteger)condition_to_meet
beforeDate: (NSDate*)limitDate
{
[_condition lock];
if (condition_to_meet == _condition_value)
{
return YES;
}
if ([_condition waitUntilDate: limitDate]
&&
(condition_to_meet == _condition_value))
{
return YES;
}
return NO;
[_condition lock];
if (condition_to_meet == _condition_value)
{
return YES;
}
if ([_condition waitUntilDate: limitDate]
&& (condition_to_meet == _condition_value))
{
return YES;
}
return NO;
}
// NSLocking methods. These aren't instantiated with the macro as they are
// delegated to the NSCondition.
- (void) lock
{
[_condition lock];
[_condition lock];
}
- (void) unlock
{
[_condition unlock];
[_condition unlock];
}
- (BOOL) tryLock
{
return [_condition tryLock];
return [_condition tryLock];
}
NAME_METHODS
@end

View file

@ -1463,10 +1463,11 @@ static NSURLProtocol *placeholder = nil;
}
if (event == NSStreamEventErrorOccurred)
{
NSLog(@"An error %@ occurred on stream %@ of %@",
[stream streamError], stream, self);
NSError *error = [[[stream streamError] retain] autorelease];
NSLog(@"An error %@ occurred on stream %@ of %@", error, stream, self);
[self stopLoading];
[this->client URLProtocol: self didFailWithError: [stream streamError]];
[this->client URLProtocol: self didFailWithError: error];
}
else
{