Coroutines/MYCoroutine.h
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
//  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@0
    18
    struct Coro *_coro;
jens@0
    19
    NSString *_name;
jens@0
    20
}
jens@0
    21
jens@0
    22
/** The "main" coroutine: the one corresponding to the execution context before the
jens@0
    23
    first coroutine is started. */
jens@0
    24
+ (MYCoroutine*) mainCoroutine;
jens@0
    25
jens@0
    26
/** The currently active coroutine. */
jens@0
    27
+ (MYCoroutine*) currentCoroutine;
jens@0
    28
jens@0
    29
/** Creates a new coroutine and starts it, performing the given invocation on it.
jens@0
    30
    That method must not return! */
jens@0
    31
+ (MYCoroutine*) startWithInvocation: (NSInvocation*)invocation;
jens@0
    32
jens@0
    33
/** Creates but does not start a coroutine. */
jens@0
    34
- (id) init;
jens@0
    35
jens@0
    36
/** Starts a new coroutine, and performs the given invocation on it.
jens@0
    37
    The current coroutine will block (i.e. this call won't return)
jens@0
    38
    until some other coroutine tells it to resume. */
jens@0
    39
- (void) startWithInvocation: (NSInvocation*)invocation;
jens@0
    40
jens@0
    41
/** Starts a new coroutine, invoking its -main method as its body.
jens@0
    42
    Since the default implementation of -main is empty, this only makes sense to call
jens@0
    43
    on an instance of a subclass of MYCoroutine that's overridden -main.
jens@0
    44
    The current coroutine will block (i.e. this call won't return)
jens@0
    45
    until some other coroutine tells it to resume. */
jens@0
    46
- (void) start;
jens@0
    47
jens@0
    48
/** The "main" method that will be called if the coroutine is started with -start.
jens@0
    49
    The default implementation is empty, so a subclass using -start must override this. */
jens@0
    50
- (void) main;
jens@0
    51
jens@0
    52
/** Returns control to an already-started (but blocked) 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@0
    58
jens@0
    59
/** The coroutine's name. You can put anything you want here, as a debugging aid. */
jens@0
    60
@property (copy) NSString* name;
jens@0
    61
jens@0
    62
/** Returns YES if this is the currently executing coroutine. */
jens@0
    63
@property (readonly) BOOL isCurrent;
jens@0
    64
jens@0
    65
/** The stack size of the coroutine. You can only change this before calling -start! */
jens@0
    66
@property size_t stackSize;
jens@0
    67
jens@0
    68
/** The number of bytes of stack space left on this coroutine. */
jens@0
    69
@property (readonly) size_t bytesLeftOnStack;
jens@0
    70
jens@0
    71
/** Returns YES if this coroutine is almost out of stack space (less than 8k left) */
jens@0
    72
@property (readonly) BOOL stackSpaceAlmostGone;
jens@0
    73
jens@0
    74
@end