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
22 /** The "main" coroutine: the one corresponding to the execution context before the
23 first coroutine is started. */
24 + (MYCoroutine*) mainCoroutine;
26 /** The currently active coroutine. */
27 + (MYCoroutine*) currentCoroutine;
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;
33 /** Creates but does not start a coroutine. */
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;
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. */
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. */
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. */
59 /** The coroutine's name. You can put anything you want here, as a debugging aid. */
60 @property (copy) NSString* name;
62 /** Returns YES if this is the currently executing coroutine. */
63 @property (readonly) BOOL isCurrent;
65 /** The stack size of the coroutine. You can only change this before calling -start! */
66 @property size_t stackSize;
68 /** The number of bytes of stack space left on this coroutine. */
69 @property (readonly) size_t bytesLeftOnStack;
71 /** Returns YES if this coroutine is almost out of stack space (less than 8k left) */
72 @property (readonly) BOOL stackSpaceAlmostGone;