// // MYCoroutine.m // Coroutines // // Created by Jens Alfke on 4/29/08. // Copyright 2008 Jens Alfke. All rights reserved. // License is at the bottom of this file. // #import "MYCoroutine.h" #import "CoroX.h" #ifndef LogTo #define kEnableLog 1 /* 1 enables logging, 0 disables it */ #define LogTo(DOMAIN,MSG,...) do{ if(kEnableLog) NSLog(@""#DOMAIN ": " MSG,__VA_ARGS__); }while(0) #endif #ifndef Warn #define Warn(MSG,...) NSLog(@"WARNING: " #MSG,__VA_ARGS__) #endif static void MYCoroutineStart( CoroX *coro ); @implementation MYCoroutine static MYCoroutine *sMain, *sCurrent; static NSMutableArray *sCallers; static id sYieldResult; - (id) _initMain { self = [super init]; if (self != nil) { NSAssert(!sMain,@"Already created a main coroutine"); _coro = CoroX_new(NULL,NULL); self.name = @"MAIN"; sMain = self; LogTo(Coroutine,@"INIT %@ : _coro=%p",self,_coro); } return self; } - (id) init { self = [super init]; if (self != nil) { _coro = CoroX_new(&MYCoroutineStart,self); LogTo(Coroutine,@"INIT %@ : _coro=%p",self,_coro); } return self; } - (void) dealloc { LogTo(Coroutine,@"DEALLOC %@",self); CoroX_free(_coro); [super dealloc]; } + (void) initialize { if( self == [MYCoroutine class] ) { sCurrent = [[self alloc] _initMain]; sCallers = [[NSMutableArray alloc] init]; } } - (NSString*) description { if( _name ) return [NSString stringWithFormat: @"%@[%@]", [self class],_name]; else return [NSString stringWithFormat: @"%@[%p]", [self class],self]; } @synthesize name=_name, invocation=_invocation; + (MYCoroutine*) mainCoroutine {return sMain;} + (MYCoroutine*) currentCoroutine {return sCurrent;} - (BOOL) isCurrent {return self==sCurrent;} - (const void*) stack {return CoroX_stack(_coro);} - (size_t) stackSize {return CoroX_stackSize(_coro);} - (void) setStackSize: (size_t)size {CoroX_setStackSize_(_coro,size);} + (void) setDefaultStackSize: (size_t)s {CoroX_setDefaultStackSize(s);} - (size_t) bytesLeftOnStack {return CoroX_bytesLeftOnStack(_coro);} - (BOOL) stackSpaceAlmostGone {return CoroX_stackSpaceAlmostGone(_coro);} - (BOOL) hasStarted {return CoroX_stack(_coro)!=NULL || self==sMain;} - (void) _start { if( _invocation ) [_invocation invoke]; else [self main]; // If body returns, yield control to caller: LogTo(Coroutine,@"%@ finished",self); [MYCoroutine yieldToCaller: nil]; } static void MYCoroutineStart( CoroX *coro ) { MYCoroutine *self = CoroX_userData(coro); [self _start]; } + (MYCoroutine*) startWithInvocation: (NSInvocation*)invocation { MYCoroutine *cr = [[self alloc] init]; cr.invocation = invocation; [cr resume]; return cr; } - (void) main { // subclasses should override this if they don't use an invocation. } #pragma mark - #pragma mark CALLING: - (void) resume { LogTo(Coroutine,@"Resuming %@ (currently in %@)",self,sCurrent); MYCoroutine *current = sCurrent; sCurrent = self; CoroX_switchTo_(current->_coro,_coro); sCurrent = current; LogTo(Coroutine,@"...resumed %@",sCurrent); } - (id) call { NSAssert(sCurrent!=self,@"Cannot call the current coroutine"); [sCallers addObject: sCurrent]; [self resume]; id result = sYieldResult; sYieldResult = nil; return result; } + (void) yieldToCaller: (id)value { MYCoroutine *caller = sCallers.lastObject; NSAssert(caller, @"No caller to yield to"); sYieldResult = value; [sCallers removeLastObject]; [caller resume]; } @end /* (This is a BSD License) Copyright (c) 2008 Jens Alfke All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. • Neither the name of the author nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */