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