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