@@ -50,18 +50,19 @@ #import "OFThreadStillRunningException.h" #import "threading.h" static OFList *TLSKeys; -static of_tlskey_t threadSelf; +static of_tlskey_t threadSelfKey; +static OFThread *mainThread; static id call_main(id object) { OFThread *thread = (OFThread*)object; - if (!of_tlskey_set(threadSelf, thread)) + if (!of_tlskey_set(threadSelfKey, thread)) @throw [OFInitializationFailedException exceptionWithClass: [thread class]]; /* * Nasty workaround for thread implementations which can't return a @@ -96,11 +97,11 @@ + (void)initialize { if (self != [OFThread class]) return; - if (!of_tlskey_new(&threadSelf)) + if (!of_tlskey_new(&threadSelfKey)) @throw [OFInitializationFailedException exceptionWithClass: self]; } + thread @@ -144,11 +145,16 @@ return [[(id)of_tlskey_get(key->key) retain] autorelease]; } + (OFThread*)currentThread { - return [[(id)of_tlskey_get(threadSelf) retain] autorelease]; + return [[(id)of_tlskey_get(threadSelfKey) retain] autorelease]; +} + ++ (OFThread*)mainThread +{ + return mainThread; } + (void)sleepForTimeInterval: (double)seconds { if (seconds < 0) @@ -200,11 +206,11 @@ [self terminateWithObject: nil]; } + (void)terminateWithObject: (id)object { - OFThread *thread = of_tlskey_get(threadSelf); + OFThread *thread = of_tlskey_get(threadSelfKey); if (thread != nil) { thread->returnValue = [object retain]; [thread handleTermination]; @@ -217,10 +223,20 @@ [thread release]; of_thread_exit(); } + ++ (void)_createMainThread +{ + mainThread = [[OFThread alloc] init]; + mainThread->thread = of_thread_current(); + + if (!of_tlskey_set(threadSelfKey, mainThread)) + @throw [OFInitializationFailedException + exceptionWithClass: self]; +} - initWithObject: (id)object_ { self = [super init];