From 2d97ccb6b46b4bbdc4a8a68612fc0ddbcce100ca Mon Sep 17 00:00:00 2001 From: nico Date: Mon, 19 Mar 2001 12:20:21 +0000 Subject: [PATCH] Methods to register/unregister an alien thread turned into functions for maximum thread safety git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@9449 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 11 +++++++++ Headers/gnustep/base/NSThread.h | 22 ++++++++--------- Source/NSThread.m | 42 +++++++++++++++++++++++---------- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index 633cf56f6..af8855183 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2001-03-19 Nicola Pero + + * Source/NSThread.m: registerCurrentThread and + unregisterCurrentThread turned into functions - called + GSRegisterCurrentThread and GSUnregisterCurrentThread. + * Headers/gnustep/base/NSThread.h: Updated for change. + + * Source/NSThread.m (GSRegisterCurrentThread): Call + objc_thread_add before creating the NSThread object using a method + call. + 2001-03-19 Jonathan Gapen * Source/NSString.m: In ([-rangeOfComposedCharacterSequenceAtIndex:]), diff --git a/Headers/gnustep/base/NSThread.h b/Headers/gnustep/base/NSThread.h index 0e31eac72..c07e9de3b 100644 --- a/Headers/gnustep/base/NSThread.h +++ b/Headers/gnustep/base/NSThread.h @@ -66,9 +66,9 @@ typedef enum #ifndef NO_GNUSTEP /* - * Don't use the following methods unless you really know what you are + * Don't use the following functions unless you really know what you are * doing ! - * The following methods are low-levelish and special. + * The following functions are low-levelish and special. * They are meant to make it possible to run GNUstep code in threads * created in completely different environment, eg inside a JVM. * @@ -77,26 +77,24 @@ typedef enum * other thread. To initialize NSThread, simply call GSCurrentThread * (). The main thread will not need to be registered. */ -@interface NSThread (GNUstepRegister) + /* * Register an external thread (created using your OS thread interface * directly) to GNUstep. This means that it creates a NSThread object * corresponding to the current thread, and sets things up so that you * can run GNUstep code inside the thread. If the thread was not - * known to GNUstep, this methods registers it, and returns YES. If - * the thread was already known to GNUstep, this method does nothing + * known to GNUstep, this function registers it, and returns YES. If + * the thread was already known to GNUstep, this function does nothing * and returns NO. */ -+ (BOOL) registerCurrentThread; +GS_EXPORT BOOL GSRegisterCurrentThread (void); /* * Unregister the current thread from GNUstep. You must only - * unregister threads which have been register using + (BOOL) - * registerCurrentThread. This method is basically the same as + * unregister threads which have been register using + * registerCurrentThread (). This method is basically the same as * `+exit', but does not exit the thread - just destroys all objects * associated with the thread. Warning: using any GNUstep code after - * this method call is not safe. - */ -+ (void) unregisterCurrentThread; -@end + * this method call is not safe. */ +GS_EXPORT void GSUnregisterCurrentThread (void); #endif /* diff --git a/Source/NSThread.m b/Source/NSThread.m index f22d489c9..8435bb091 100644 --- a/Source/NSThread.m +++ b/Source/NSThread.m @@ -35,6 +35,8 @@ #include #include +static Class threadClass = Nil; + #ifndef NO_GNUSTEP #ifndef HAVE_OBJC_THREAD_ADD /* We need to access these private vars in the objc runtime - because @@ -265,6 +267,7 @@ gnustep_base_thread_callback() withObject: nil]; defaultThread->_active = YES; objc_thread_set_data(defaultThread); + threadClass = self; } } @@ -419,7 +422,22 @@ gnustep_base_thread_callback() @end @implementation NSThread (GNUstepRegister) +#warning "NSThread +registerCurrentThread will be removed in 1.0.0" +/* Deprecated - will be removed in 1.0.0 */ + (BOOL) registerCurrentThread +{ + return GSRegisterCurrentThread (); +} + +/* Deprecated - will be removed in 1.0.0 */ ++ (void) unregisterCurrentThread +{ + GSUnregisterCurrentThread (); +} +@end + +BOOL +GSRegisterCurrentThread (void) { NSThread *thread; @@ -431,19 +449,20 @@ gnustep_base_thread_callback() return NO; } - /* - * Create the new thread object. - */ - thread = (NSThread*)NSAllocateObject (self, 0, NSDefaultMallocZone ()); - thread = [thread _initWithSelector: NULL toTarget: nil withObject: nil]; - objc_thread_set_data (thread); - ((NSThread *)thread)->_active = YES; - /* * Make sure the Objective-C runtime knows there is an additional thread. */ objc_thread_add (); + /* + * Create the new thread object. + */ + thread = (NSThread*)NSAllocateObject (threadClass, 0, + NSDefaultMallocZone ()); + thread = [thread _initWithSelector: NULL toTarget: nil withObject: nil]; + objc_thread_set_data (thread); + ((NSThread *)thread)->_active = YES; + /* * We post the notification after we register the thread. */ @@ -452,12 +471,13 @@ gnustep_base_thread_callback() return YES; } -+ (void) unregisterCurrentThread +void +GSUnregisterCurrentThread (void) { NSThread *thread; thread = GSCurrentThread(); - + if (thread->_active == YES) { /* @@ -478,5 +498,3 @@ gnustep_base_thread_callback() objc_thread_remove (); } } -@end -