Rewrote CoroX. Simplified APIs. Improved stack size checks.
5 // Created by Jens Alfke on 4/29/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
7 // License is at the bottom of this file.
10 #import "MYCoroutine.h"
15 #define kEnableLog 1 /* 1 enables logging, 0 disables it */
16 #define LogTo(DOMAIN,MSG,...) do{ if(kEnableLog) NSLog(@""#DOMAIN ": " MSG,__VA_ARGS__); }while(0)
20 #define Warn(MSG,...) NSLog(@"WARNING: " #MSG,__VA_ARGS__)
24 static void MYCoroutineStart( CoroX *coro );
27 @implementation MYCoroutine
30 static MYCoroutine *sMain, *sCurrent;
32 static NSMutableArray *sCallers;
33 static id sYieldResult;
40 NSAssert(!sMain,@"Already created a main coroutine");
41 _coro = CoroX_new(NULL,NULL);
44 LogTo(Coroutine,@"INIT %@ : _coro=%p",self,_coro);
54 _coro = CoroX_new(&MYCoroutineStart,self);
55 LogTo(Coroutine,@"INIT %@ : _coro=%p",self,_coro);
63 LogTo(Coroutine,@"DEALLOC %@",self);
71 if( self == [MYCoroutine class] ) {
72 sCurrent = [[self alloc] _initMain];
73 sCallers = [[NSMutableArray alloc] init];
78 - (NSString*) description
81 return [NSString stringWithFormat: @"%@[%@]", [self class],_name];
83 return [NSString stringWithFormat: @"%@[%p]", [self class],self];
87 @synthesize name=_name, invocation=_invocation;
90 + (MYCoroutine*) mainCoroutine {return sMain;}
91 + (MYCoroutine*) currentCoroutine {return sCurrent;}
93 - (BOOL) isCurrent {return self==sCurrent;}
95 - (const void*) stack {return CoroX_stack(_coro);}
96 - (size_t) stackSize {return CoroX_stackSize(_coro);}
97 - (void) setStackSize: (size_t)size {CoroX_setStackSize_(_coro,size);}
99 + (void) setDefaultStackSize: (size_t)s {CoroX_setDefaultStackSize(s);}
101 - (size_t) bytesLeftOnStack {return CoroX_bytesLeftOnStack(_coro);}
102 - (BOOL) stackSpaceAlmostGone {return CoroX_stackSpaceAlmostGone(_coro);}
104 - (BOOL) hasStarted {return CoroX_stack(_coro)!=NULL || self==sMain;}
110 [_invocation invoke];
113 // If body returns, yield control to caller:
114 LogTo(Coroutine,@"%@ finished",self);
115 [MYCoroutine yieldToCaller: nil];
118 static void MYCoroutineStart( CoroX *coro )
120 MYCoroutine *self = CoroX_userData(coro);
125 + (MYCoroutine*) startWithInvocation: (NSInvocation*)invocation
127 MYCoroutine *cr = [[self alloc] init];
128 cr.invocation = invocation;
136 // subclasses should override this if they don't use an invocation.
141 #pragma mark CALLING:
146 LogTo(Coroutine,@"Resuming %@ (currently in %@)",self,sCurrent);
147 MYCoroutine *current = sCurrent;
149 CoroX_switchTo_(current->_coro,_coro);
151 LogTo(Coroutine,@"...resumed %@",sCurrent);
157 NSAssert(sCurrent!=self,@"Cannot call the current coroutine");
158 [sCallers addObject: sCurrent];
160 id result = sYieldResult;
166 + (void) yieldToCaller: (id)value
168 MYCoroutine *caller = sCallers.lastObject;
169 NSAssert(caller, @"No caller to yield to");
170 sYieldResult = value;
171 [sCallers removeLastObject];
183 (This is a BSD License)
185 Copyright (c) 2008 Jens Alfke
188 Redistribution and use in source and binary forms, with or without modification, are
189 permitted provided that the following conditions are met:
191 • Redistributions of source code must retain the above copyright notice, this list of
192 conditions and the following disclaimer.
193 • Redistributions in binary form must reproduce the above copyright notice, this list
194 of conditions and the following disclaimer in the documentation and/or other materials
195 provided with the distribution.
196 • Neither the name of the author nor the names of other contributors may be used to
197 endorse or promote products derived from this software without specific prior written
200 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
201 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
202 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
203 THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
204 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
205 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
206 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
207 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
208 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.