author | Jens Alfke <jens@mooseyard.com> |
Wed Apr 30 14:18:49 2008 -0700 (2008-04-30) | |
changeset 1 | 2475f871c218 |
parent 0 | deb0ee0c5b21 |
permissions | -rw-r--r-- |
jens@0 | 1 |
// |
jens@0 | 2 |
// MYCoroutine.h |
jens@0 | 3 |
// Coroutines for Mac OS X |
jens@0 | 4 |
// |
jens@0 | 5 |
// Created by Jens Alfke on 4/29/08. |
jens@0 | 6 |
// Copyright 2008 Jens Alfke. All rights reserved. |
jens@0 | 7 |
// |
jens@0 | 8 |
|
jens@0 | 9 |
#import <Foundation/Foundation.h> |
jens@0 | 10 |
|
jens@0 | 11 |
|
jens@0 | 12 |
/** Objective-C coroutine class. |
jens@0 | 13 |
See: http://en.wikipedia.org/wiki/Coroutine |
jens@0 | 14 |
*/ |
jens@0 | 15 |
@interface MYCoroutine : NSObject |
jens@0 | 16 |
{ |
jens@0 | 17 |
@private |
jens@1 | 18 |
struct CoroX *_coro; |
jens@1 | 19 |
NSInvocation *_invocation; |
jens@0 | 20 |
NSString *_name; |
jens@0 | 21 |
} |
jens@0 | 22 |
|
jens@0 | 23 |
/** The "main" coroutine: the one corresponding to the execution context before the |
jens@0 | 24 |
first coroutine is started. */ |
jens@0 | 25 |
+ (MYCoroutine*) mainCoroutine; |
jens@0 | 26 |
|
jens@0 | 27 |
/** The currently active coroutine. */ |
jens@0 | 28 |
+ (MYCoroutine*) currentCoroutine; |
jens@0 | 29 |
|
jens@0 | 30 |
/** Creates a new coroutine and starts it, performing the given invocation on it. |
jens@0 | 31 |
That method must not return! */ |
jens@0 | 32 |
+ (MYCoroutine*) startWithInvocation: (NSInvocation*)invocation; |
jens@0 | 33 |
|
jens@0 | 34 |
/** Creates but does not start a coroutine. */ |
jens@0 | 35 |
- (id) init; |
jens@0 | 36 |
|
jens@0 | 37 |
|
jens@1 | 38 |
@property (retain) NSInvocation *invocation; |
jens@1 | 39 |
|
jens@1 | 40 |
/** The stack size of the coroutine. You can only change this before calling -start! */ |
jens@1 | 41 |
@property size_t stackSize; |
jens@1 | 42 |
|
jens@1 | 43 |
/** The coroutine's name. You can put anything you want here, as a debugging aid. */ |
jens@1 | 44 |
@property (copy) NSString* name; |
jens@1 | 45 |
|
jens@0 | 46 |
|
jens@0 | 47 |
/** The "main" method that will be called if the coroutine is started with -start. |
jens@0 | 48 |
The default implementation is empty, so a subclass using -start must override this. */ |
jens@0 | 49 |
- (void) main; |
jens@0 | 50 |
|
jens@1 | 51 |
|
jens@1 | 52 |
/** Returns control to a coroutine. |
jens@0 | 53 |
The most recent -resume call made from within that coroutine will return. |
jens@0 | 54 |
The current coroutine will block (i.e. this call won't return) |
jens@0 | 55 |
until some other coroutine tells it to resume. */ |
jens@0 | 56 |
- (void) resume; |
jens@0 | 57 |
|
jens@1 | 58 |
- (id) call; |
jens@0 | 59 |
|
jens@1 | 60 |
+ (void) yieldToCaller: (id)value; |
jens@1 | 61 |
|
jens@0 | 62 |
|
jens@0 | 63 |
/** Returns YES if this is the currently executing coroutine. */ |
jens@0 | 64 |
@property (readonly) BOOL isCurrent; |
jens@0 | 65 |
|
jens@0 | 66 |
/** The number of bytes of stack space left on this coroutine. */ |
jens@0 | 67 |
@property (readonly) size_t bytesLeftOnStack; |
jens@0 | 68 |
|
jens@0 | 69 |
/** Returns YES if this coroutine is almost out of stack space (less than 8k left) */ |
jens@0 | 70 |
@property (readonly) BOOL stackSpaceAlmostGone; |
jens@0 | 71 |
|
jens@1 | 72 |
|
jens@1 | 73 |
+ (void) setDefaultStackSize: (size_t)stackSize; |
jens@1 | 74 |
|
jens@0 | 75 |
@end |