Coroutines/MYCoroutineTest.m
author Jens Alfke <jens@mooseyard.com>
Tue Apr 29 17:05:32 2008 -0700 (2008-04-29)
changeset 0 deb0ee0c5b21
child 1 2475f871c218
permissions -rw-r--r--
First checkin
     1 //
     2 //  MYCoroutineTest.m
     3 //  Coroutines
     4 //
     5 //  Created by Jens Alfke on 4/29/08.
     6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
     7 //
     8 
     9 #import "MYCoroutine.h"
    10 
    11 
    12 @interface CoroTest1 : MYCoroutine
    13 {
    14     int value;
    15 }
    16 @property int value;
    17 @end
    18 
    19 @interface CoroTest2 : CoroTest1
    20 @end
    21 
    22 
    23 CoroTest1 *firstCoro, *secondCoro;
    24 
    25 
    26 @implementation CoroTest2
    27 
    28 - (void) main
    29 {
    30     int num = 0;
    31     
    32 	NSLog(@"secondTask created with value %d", self.value);
    33 	
    34 	while (1) 
    35 	{
    36 		NSLog(@"secondTask: %d %d", self.bytesLeftOnStack, num++);
    37 		[firstCoro resume];
    38 	}
    39 }
    40 
    41 @end
    42 
    43 
    44 @implementation CoroTest1
    45 
    46 @synthesize value;
    47 
    48 - (void) main
    49 {
    50 	int num = 0;
    51 	
    52 	NSLog(@"firstTask created with value %d", self.value);
    53 	secondCoro = [[CoroTest2 alloc] init];
    54     secondCoro.name = @"second";
    55     secondCoro.value = 2;
    56 	[secondCoro start];
    57 	
    58 	while ( num < 100 ) 
    59 	{
    60 		NSLog(@"firstTask:  %d %d", self.bytesLeftOnStack, num++);
    61         [secondCoro resume];
    62 	}
    63     
    64     [secondCoro release];
    65     
    66     [[MYCoroutine mainCoroutine] resume];
    67 }
    68 
    69 @end
    70 
    71 
    72 int main()
    73 {
    74     NSAutoreleasePool *pool = [NSAutoreleasePool new];
    75     
    76     NSLog(@"Starting test...");
    77     //[[[NSThread alloc] init] start];
    78 	firstCoro = [[CoroTest1 alloc] init];
    79     firstCoro.name = @"first";
    80     firstCoro.value = 1;
    81     [firstCoro start];
    82     
    83     NSLog(@"Returned from coroutines; exiting");
    84     
    85     [firstCoro release];
    86     
    87     [pool drain];
    88 }