Rewrote CoroX. Simplified APIs. Improved stack size checks.
5 // Created by Jens Alfke on 4/29/08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "MYCoroutine.h"
13 @interface CoroTest1 : MYCoroutine
20 @interface CoroTest2 : CoroTest1
24 CoroTest1 *firstCoro, *secondCoro;
27 @implementation CoroTest2
33 NSLog(@"secondTask created with value %d", self.value);
37 NSLog(@"secondTask: %d %d", self.bytesLeftOnStack, num++);
45 @implementation CoroTest1
49 - (void) regress: (int)depth
51 char useUpSpace[1024];
53 NSLog(@"infinite regress: depth=%i, stack space=%d", depth,self.bytesLeftOnStack);
54 if( [[MYCoroutine currentCoroutine] stackSpaceAlmostGone] )
55 NSLog(@"infinite regress: bailing out!");
57 [self regress: depth+1];
64 NSLog(@"firstTask created with value %d", self.value);
65 secondCoro = [[CoroTest2 alloc] init];
66 secondCoro.name = @"second";
71 NSLog(@"firstTask: %d %d", self.bytesLeftOnStack, num++);
77 NSLog(@"*** TESTING STACK LIMITS ***");
80 [[MYCoroutine mainCoroutine] resume];
85 NSLog(@"*** TESTING COROUTINES ***");
86 firstCoro = [[CoroTest1 alloc] init];
87 firstCoro.name = @"first";
91 NSLog(@"Returned from coroutines; exiting");
101 @interface Generator : MYCoroutine
103 - (id) initWithCount: (int)count;
106 @implementation Generator
108 - (id) initWithCount: (int)count
120 for( int i=1; i<=_count; i++ )
121 [MYCoroutine yieldToCaller: [NSNumber numberWithInt: i]];
126 NSLog(@"*** TESTING GENERATOR ***");
127 Generator *g = [[Generator alloc] initWithCount: 10];
129 while( nil != (value = [g call]) )
130 NSLog(@"Generator yielded %@",value);
131 NSLog(@"Generator returned nil");
140 NSAutoreleasePool *pool = [NSAutoreleasePool new];
141 NSLog(@"Starting test...");
143 [MYCoroutine setDefaultStackSize: kCoroX_minStackSize];