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