initialise before use

This commit is contained in:
Richard Frith-Macdonald 2018-04-16 13:18:36 +01:00
parent c2fb3a0954
commit 635b71e442
2 changed files with 27 additions and 7 deletions

View file

@ -1,3 +1,8 @@
2018-04-16 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSThread.m: ensure that the thread specific memory key is
initialised before use.
2018-04-15 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSException.m: Improve -symbold method on platforms without

View file

@ -412,7 +412,8 @@ static BOOL entered_multi_threaded_state = NO;
static NSThread *defaultThread;
static pthread_key_t thread_object_key;
static BOOL keyInitialized = NO;
static pthread_key_t thread_object_key;
static NSHashTable *_activeBlocked = nil;
@ -651,8 +652,18 @@ static void exitedThread(void *thread)
inline NSThread*
GSCurrentThread(void)
{
NSThread *thr = pthread_getspecific(thread_object_key);
NSThread *thr;
if (NO == keyInitialized)
{
if (pthread_key_create(&thread_object_key, exitedThread))
{
[NSException raise: NSInternalInconsistencyException
format: @"Unable to create thread key!"];
}
keyInitialized = YES;
}
thr = pthread_getspecific(thread_object_key);
if (nil == thr)
{
NSValue *selfThread = NSValueCreateFromPthread(pthread_self());
@ -909,11 +920,15 @@ unregisterActiveThread(NSThread *thread)
{
if (self == [NSThread class])
{
if (pthread_key_create(&thread_object_key, exitedThread))
{
[NSException raise: NSInternalInconsistencyException
format: @"Unable to create thread key!"];
}
if (NO == keyInitialized)
{
if (pthread_key_create(&thread_object_key, exitedThread))
{
[NSException raise: NSInternalInconsistencyException
format: @"Unable to create thread key!"];
}
keyInitialized = YES;
}
/* Ensure that the default thread exists.
* It's safe to create a lock here (since [NSObject+initialize]
* creates locks, and locks don't depend on any other class),