From d2fc504e832d16795544372615af08ad15527dbc Mon Sep 17 00:00:00 2001 From: Ofri Wolfus Date: Wed, 8 Aug 2007 12:21:05 +0300 Subject: [PATCH] Set the current coroutine to nil when none is active and prevent coroutine creating with nil message or receiver. --- Source/DPCoroutine.m | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Source/DPCoroutine.m b/Source/DPCoroutine.m index 10c0b1f..238f574 100644 --- a/Source/DPCoroutine.m +++ b/Source/DPCoroutine.m @@ -7,6 +7,7 @@ // #import "DPCoroutine.h" +#include @interface DPCoroutineStack (private) @@ -20,6 +21,8 @@ @interface DPCoroutine (private) ++ (void)setCurrentCoroutine:(id)coro; + - (void)setMain; - (BOOL)isMain; @@ -44,6 +47,13 @@ return [[[NSThread currentThread] threadDictionary] objectForKey:DPCURRENT_COROUTINE_KEY]; } ++ (void)setCurrentCoroutine:(id)coro { + if (coro) + [[[NSThread currentThread] threadDictionary] setObject:coro forKey:DPCURRENT_COROUTINE_KEY]; + else + [[[NSThread currentThread] threadDictionary] removeObjectForKey:DPCURRENT_COROUTINE_KEY]; +} + - (id)init { if ((self = [super init])) { _refCount = 1; @@ -56,6 +66,12 @@ } - (id)initWithMessage:(DPMessage *)msg target:(id)t { + // Can't initialize without a message or a targe + if (!msg || !t) { + [self release]; + return nil; + } + if ((self = [self init])) { message = [msg copy]; target = [t retain]; @@ -251,6 +267,10 @@ static void __attribute__((constructor)) _DPSetupMainCoroStack(void) { shouldContinue = [coro isActive]; } + // There's no active coroutine + [DPCoroutine setCurrentCoroutine:nil]; + + // Clean up shouldContinue ? [stack enqueu:coro] : [unusedCoroutines enqueu:coro]; [coro release]; [self release]; -- 2.11.4.GIT