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 0 /* 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 @implementation MYCoroutine
27 static MYCoroutine *sMain, *sCurrent;
32 if( self == [MYCoroutine class] ) {
33 sMain = [[self alloc] init];
34 Coro_initializeMainCoro(sMain->_coro);
46 LogTo(Coroutine,@"INIT %@ : _coro=%p",self,_coro);
54 LogTo(Coroutine,@"DEALLOC %@",self);
60 - (NSString*) description
63 return [NSString stringWithFormat: @"%@[%@]", [self class],_name];
65 return [NSString stringWithFormat: @"%@[%p]", [self class],self];
69 @synthesize name=_name;
72 + (MYCoroutine*) mainCoroutine {return sMain;}
73 + (MYCoroutine*) currentCoroutine {return sCurrent;}
75 - (BOOL) isCurrent {return self==sCurrent;}
77 - (const void*) stack {return Coro_stack(_coro);}
78 - (size_t) stackSize {return Coro_stackSize(_coro);}
79 - (void) setStackSize: (size_t)size {Coro_setStackSize_(_coro,size);}
81 - (size_t) bytesLeftOnStack {return Coro_bytesLeftOnStack(_coro);}
82 - (BOOL) stackSpaceAlmostGone {return Coro_stackSpaceAlmostGone(_coro);}
85 static void startWithInvocation( void *context )
87 [(NSInvocation*)context invoke];
90 static void startWithMain( void *context )
92 [(MYCoroutine*)context main];
95 - (void) startWithInvocation: (NSInvocation*)invocation
97 LogTo(Coroutine,@"Starting %@ (currently in %@)",self,sCurrent);
98 MYCoroutine *current = sCurrent;
102 Coro_startCoro_(current->_coro, _coro, invocation, &startWithInvocation);
104 Coro_startCoro_(current->_coro, _coro, self, &startWithMain);
107 LogTo(Coroutine,@"...resumed %@ after starting %@",sCurrent,self);
112 [self startWithInvocation: nil];
116 + (MYCoroutine*) startWithInvocation: (NSInvocation*)invocation
118 MYCoroutine *cr = [[self alloc] init];
119 [cr startWithInvocation: invocation];
126 LogTo(Coroutine,@"Resuming %@ (currently in %@)",self,sCurrent);
127 MYCoroutine *current = sCurrent;
129 Coro_switchTo_(current->_coro,_coro);
131 LogTo(Coroutine,@"...resumed %@",sCurrent);
137 // subclasses should override this.
147 (This is a BSD License)
149 Copyright (c) 2008 Jens Alfke
152 Redistribution and use in source and binary forms, with or without modification, are
153 permitted provided that the following conditions are met:
155 • Redistributions of source code must retain the above copyright notice, this list of
156 conditions and the following disclaimer.
157 • Redistributions in binary form must reproduce the above copyright notice, this list
158 of conditions and the following disclaimer in the documentation and/or other materials
159 provided with the distribution.
160 • Neither the name of the author nor the names of other contributors may be used to
161 endorse or promote products derived from this software without specific prior written
164 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
165 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
166 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
167 THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
168 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
169 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
170 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
171 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
172 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.