Rewrote CoroX. Simplified APIs. Improved stack size checks.
3 // Coroutines for Mac OS X
5 // Created by Jens Alfke on 4/29/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
9 #import <Foundation/Foundation.h>
12 /** Objective-C coroutine class.
13 See: http://en.wikipedia.org/wiki/Coroutine
15 @interface MYCoroutine : NSObject
19 NSInvocation *_invocation;
23 /** The "main" coroutine: the one corresponding to the execution context before the
24 first coroutine is started. */
25 + (MYCoroutine*) mainCoroutine;
27 /** The currently active coroutine. */
28 + (MYCoroutine*) currentCoroutine;
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;
34 /** Creates but does not start a coroutine. */
38 @property (retain) NSInvocation *invocation;
40 /** The stack size of the coroutine. You can only change this before calling -start! */
41 @property size_t stackSize;
43 /** The coroutine's name. You can put anything you want here, as a debugging aid. */
44 @property (copy) NSString* name;
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. */
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. */
60 + (void) yieldToCaller: (id)value;
63 /** Returns YES if this is the currently executing coroutine. */
64 @property (readonly) BOOL isCurrent;
66 /** The number of bytes of stack space left on this coroutine. */
67 @property (readonly) size_t bytesLeftOnStack;
69 /** Returns YES if this coroutine is almost out of stack space (less than 8k left) */
70 @property (readonly) BOOL stackSpaceAlmostGone;
73 + (void) setDefaultStackSize: (size_t)stackSize;