Index: src/OFApplication.m ================================================================== --- src/OFApplication.m +++ src/OFApplication.m @@ -293,10 +293,12 @@ #undef REGISTER_SIGNAL } - (void)run { + [OFThread _createMainThread]; + [delegate applicationDidFinishLaunching]; for (;;) [OFThread sleepForTimeInterval: 86400]; } Index: src/OFThread.h ================================================================== --- src/OFThread.h +++ src/OFThread.h @@ -154,14 +154,21 @@ + (id)objectForTLSKey: (OFTLSKey*)key; /** * \brief Returns the current thread. * - * \return The current thread or nil if we are in the main thread + * \return The current thread */ + (OFThread*)currentThread; +/** + * \brief Returns the main thread. + * + * \return The main thread + */ ++ (OFThread*)mainThread; + /** * \brief Suspends execution of the current thread for the specified time * interval. * * \param seconds The number of seconds to sleep @@ -190,10 +197,12 @@ * \brief Terminates the current thread, letting it return the specified object. * * \param object The object which the terminated thread will return */ + (void)terminateWithObject: (id)object; + ++ (void)_createMainThread; /** * \brief Initializes an already allocated thread with the specified object. * * \param object An object which is passed for use in the main method or nil Index: src/OFThread.m ================================================================== --- src/OFThread.m +++ src/OFThread.m @@ -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];