mirror of
https://github.com/jrcutler/threadless.io.git
synced 2024-07-07 10:35:49 +00:00
71 lines
2.3 KiB
C
71 lines
2.3 KiB
C
#ifndef THREADLESS_COROUTINE_H
|
|
#define THREADLESS_COROUTINE_H
|
|
|
|
/* bool, true, false */
|
|
#include <stdbool.h>
|
|
/* NULL, size_t */
|
|
#include <stddef.h>
|
|
|
|
/** Opaque coroutine type */
|
|
typedef struct coroutine coroutine;
|
|
|
|
/** Coroutine function type
|
|
* @param[in,out] coro coroutine
|
|
* @param[in,out] data data passed via first call to coroutine_resume()
|
|
* @returns user-defined pointer (which should correspond to value(s) passed to
|
|
* coroutine_yield())
|
|
*/
|
|
typedef void *(coroutine_function)(coroutine *coro, void *data);
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif /* __cplusplus */
|
|
|
|
/** Create a coroutine
|
|
* @param function function to run in coroutine
|
|
* @param stack_pages number of pages to map for coroutine stack
|
|
* @retval non-NULL new coroutine
|
|
* @retval NULL error (check @c errno for reason)
|
|
* @post upon success, return value may be passed to coroutine_resume()
|
|
* @post upon success, return value must be passed to coroutine_destroy()
|
|
*/
|
|
coroutine *coroutine_create(coroutine_function *function, size_t stack_pages);
|
|
|
|
/** Destroy a coroutine
|
|
* @param[in,out] coro coroutine to destroy
|
|
* @pre @p coro must have been returned by coroutine_create()
|
|
* @post @p coro may no longer be used
|
|
*/
|
|
void coroutine_destroy(coroutine *coro);
|
|
|
|
/** Test if a coroutine has ended
|
|
* @param[in] coro coroutine to test
|
|
* @retval true coroutine has ended (or is @c NULL)
|
|
* @retval false coroutine has not ended
|
|
* @pre @p coro must have been returned by coroutine_create()
|
|
*/
|
|
bool coroutine_ended(const coroutine *coro);
|
|
|
|
/** Resume a coroutine, passing a value to coroutine_yield()
|
|
* @param[in,out] coro coroutine to resume
|
|
* @param[in,out] value value to pass (to initial call or as return from
|
|
* coroutine_yield())
|
|
* @returns value passed by @p coro to coroutine_yield()
|
|
* @pre @p coro must have been returned by coroutine_create()
|
|
*/
|
|
void *coroutine_resume(coroutine *coro, void *value);
|
|
|
|
/** Yield from a coroutine, passing a value to coroutine_resume()
|
|
* @param[in,out] coro coroutine to yield from
|
|
* @param[in,out] value value to return from coroutine_resume()
|
|
* @returns value passed by @p coro to coroutine_resume()
|
|
* @pre @p coro must have been returned by coroutine_create()
|
|
*/
|
|
void *coroutine_yield(coroutine *coro, void *value);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif /* __cplusplus */
|
|
|
|
#endif /* THREADLESS_COROUTINE_H */
|