jens@0: // jens@0: // MYCoroutine.h jens@0: // Coroutines for Mac OS X jens@0: // jens@0: // Created by Jens Alfke on 4/29/08. jens@0: // Copyright 2008 Jens Alfke. All rights reserved. jens@0: // jens@0: jens@0: #import jens@0: jens@0: jens@0: /** Objective-C coroutine class. jens@0: See: http://en.wikipedia.org/wiki/Coroutine jens@0: */ jens@0: @interface MYCoroutine : NSObject jens@0: { jens@0: @private jens@1: struct CoroX *_coro; jens@1: NSInvocation *_invocation; jens@0: NSString *_name; jens@0: } jens@0: jens@0: /** The "main" coroutine: the one corresponding to the execution context before the jens@0: first coroutine is started. */ jens@0: + (MYCoroutine*) mainCoroutine; jens@0: jens@0: /** The currently active coroutine. */ jens@0: + (MYCoroutine*) currentCoroutine; jens@0: jens@0: /** Creates a new coroutine and starts it, performing the given invocation on it. jens@0: That method must not return! */ jens@0: + (MYCoroutine*) startWithInvocation: (NSInvocation*)invocation; jens@0: jens@0: /** Creates but does not start a coroutine. */ jens@0: - (id) init; jens@0: jens@0: jens@1: @property (retain) NSInvocation *invocation; jens@1: jens@1: /** The stack size of the coroutine. You can only change this before calling -start! */ jens@1: @property size_t stackSize; jens@1: jens@1: /** The coroutine's name. You can put anything you want here, as a debugging aid. */ jens@1: @property (copy) NSString* name; jens@1: jens@0: jens@0: /** The "main" method that will be called if the coroutine is started with -start. jens@0: The default implementation is empty, so a subclass using -start must override this. */ jens@0: - (void) main; jens@0: jens@1: jens@1: /** Returns control to a coroutine. jens@0: The most recent -resume call made from within that coroutine will return. jens@0: The current coroutine will block (i.e. this call won't return) jens@0: until some other coroutine tells it to resume. */ jens@0: - (void) resume; jens@0: jens@1: - (id) call; jens@0: jens@1: + (void) yieldToCaller: (id)value; jens@1: jens@0: jens@0: /** Returns YES if this is the currently executing coroutine. */ jens@0: @property (readonly) BOOL isCurrent; jens@0: jens@0: /** The number of bytes of stack space left on this coroutine. */ jens@0: @property (readonly) size_t bytesLeftOnStack; jens@0: jens@0: /** Returns YES if this coroutine is almost out of stack space (less than 8k left) */ jens@0: @property (readonly) BOOL stackSpaceAlmostGone; jens@0: jens@1: jens@1: + (void) setDefaultStackSize: (size_t)stackSize; jens@1: jens@0: @end