Coroutines/MYCoroutineTest.m
changeset 0 deb0ee0c5b21
child 1 2475f871c218
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Coroutines/MYCoroutineTest.m	Tue Apr 29 17:05:32 2008 -0700
     1.3 @@ -0,0 +1,88 @@
     1.4 +//
     1.5 +//  MYCoroutineTest.m
     1.6 +//  Coroutines
     1.7 +//
     1.8 +//  Created by Jens Alfke on 4/29/08.
     1.9 +//  Copyright 2008 __MyCompanyName__. All rights reserved.
    1.10 +//
    1.11 +
    1.12 +#import "MYCoroutine.h"
    1.13 +
    1.14 +
    1.15 +@interface CoroTest1 : MYCoroutine
    1.16 +{
    1.17 +    int value;
    1.18 +}
    1.19 +@property int value;
    1.20 +@end
    1.21 +
    1.22 +@interface CoroTest2 : CoroTest1
    1.23 +@end
    1.24 +
    1.25 +
    1.26 +CoroTest1 *firstCoro, *secondCoro;
    1.27 +
    1.28 +
    1.29 +@implementation CoroTest2
    1.30 +
    1.31 +- (void) main
    1.32 +{
    1.33 +    int num = 0;
    1.34 +    
    1.35 +	NSLog(@"secondTask created with value %d", self.value);
    1.36 +	
    1.37 +	while (1) 
    1.38 +	{
    1.39 +		NSLog(@"secondTask: %d %d", self.bytesLeftOnStack, num++);
    1.40 +		[firstCoro resume];
    1.41 +	}
    1.42 +}
    1.43 +
    1.44 +@end
    1.45 +
    1.46 +
    1.47 +@implementation CoroTest1
    1.48 +
    1.49 +@synthesize value;
    1.50 +
    1.51 +- (void) main
    1.52 +{
    1.53 +	int num = 0;
    1.54 +	
    1.55 +	NSLog(@"firstTask created with value %d", self.value);
    1.56 +	secondCoro = [[CoroTest2 alloc] init];
    1.57 +    secondCoro.name = @"second";
    1.58 +    secondCoro.value = 2;
    1.59 +	[secondCoro start];
    1.60 +	
    1.61 +	while ( num < 100 ) 
    1.62 +	{
    1.63 +		NSLog(@"firstTask:  %d %d", self.bytesLeftOnStack, num++);
    1.64 +        [secondCoro resume];
    1.65 +	}
    1.66 +    
    1.67 +    [secondCoro release];
    1.68 +    
    1.69 +    [[MYCoroutine mainCoroutine] resume];
    1.70 +}
    1.71 +
    1.72 +@end
    1.73 +
    1.74 +
    1.75 +int main()
    1.76 +{
    1.77 +    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    1.78 +    
    1.79 +    NSLog(@"Starting test...");
    1.80 +    //[[[NSThread alloc] init] start];
    1.81 +	firstCoro = [[CoroTest1 alloc] init];
    1.82 +    firstCoro.name = @"first";
    1.83 +    firstCoro.value = 1;
    1.84 +    [firstCoro start];
    1.85 +    
    1.86 +    NSLog(@"Returned from coroutines; exiting");
    1.87 +    
    1.88 +    [firstCoro release];
    1.89 +    
    1.90 +    [pool drain];
    1.91 +}