coroutine.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Licensed under LGPLv2.1+ - see LICENSE file for details */
  2. #ifndef CCAN_COROUTINE_H
  3. #define CCAN_COROUTINE_H
  4. /*#define CCAN_COROUTINE_DEBUG 1*/
  5. #include "config.h"
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include <assert.h>
  10. #include <ccan/compiler/compiler.h>
  11. #include <ccan/typesafe_cb/typesafe_cb.h>
  12. /**
  13. * struct coroutine_stack
  14. *
  15. * Describes a stack suitable for executing a coroutine. This
  16. * structure is always contained within the stack it describes.
  17. */
  18. struct coroutine_stack {
  19. uint64_t magic;
  20. size_t size;
  21. int valgrind_id;
  22. };
  23. /**
  24. * struct coroutine_state
  25. *
  26. * Describes the state of an in-progress coroutine.
  27. */
  28. struct coroutine_state;
  29. /*
  30. * Stack management
  31. */
  32. /**
  33. * COROUTINE_STK_OVERHEAD - internal stack overhead
  34. *
  35. * Number of bytes of a stack which coroutine needs for its own
  36. * tracking information.
  37. */
  38. #define COROUTINE_STK_OVERHEAD sizeof(struct coroutine_stack)
  39. /**
  40. * COROUTINE_MIN_STKSZ - Minimum coroutine stack size
  41. *
  42. * Contains the minimum size for a coroutine stack (not including
  43. * overhead). On systems with MINSTKSZ, guaranteed to be at least as
  44. * large as MINSTKSZ.
  45. */
  46. #define COROUTINE_MIN_STKSZ 2048
  47. /**
  48. * COROUTINE_STACK_MAGIC_BUF - Magic number for coroutine stacks in a user
  49. * supplied buffer
  50. */
  51. #define COROUTINE_STACK_MAGIC_BUF 0xc040c040574cb00f
  52. /**
  53. * COROUTINE_STACK_MAGIC_ALLOC - Magic number for coroutine stacks
  54. * allocated by this module
  55. */
  56. #define COROUTINE_STACK_MAGIC_ALLOC 0xc040c040574ca110
  57. /**
  58. * coroutine_stack_init - Prepare a coroutine stack in an existing buffer
  59. * @buf: buffer to use for the coroutine stack
  60. * @bufsize: size of @buf
  61. * @metasize: size of metadata to add to the stack (not including
  62. * coroutine internal overhead)
  63. *
  64. * Prepares @buf for use as a coroutine stack, returning a
  65. * coroutine_stack *, allocated from within the buffer. Returns NULL
  66. * on failure.
  67. *
  68. * This will fail if the bufsize < (COROUTINE_MIN_STKSZ +
  69. * COROUTINE_STK_OVERHEAD + metasize).
  70. */
  71. struct coroutine_stack *coroutine_stack_init(void *buf, size_t bufsize,
  72. size_t metasize);
  73. /**
  74. * coroutine_stack_alloc - Allocate a coroutine stack
  75. * @totalsize: total size to allocate
  76. * @metasize: size of metadata to add to the stack (not including
  77. * coroutine internal overhead)
  78. *
  79. * Allocates a coroutine stack of size @totalsize, including both
  80. * internal overhead (COROUTINE_STK_OVERHEAD) and metadata of size
  81. * @metasize. Where available this will also create a guard page, so
  82. * that overruning the stack will result in an immediate crash, rather
  83. * than data corruption.
  84. *
  85. * This will fail if the totalsize < (COROUTINE_MIN_STKSZ +
  86. * COROUTINE_STK_OVERHEAD + metasize).
  87. */
  88. struct coroutine_stack *coroutine_stack_alloc(size_t bufsize, size_t metasize);
  89. /**
  90. * coroutine_stack_release - Stop using a coroutine stack
  91. * @stack: coroutine stack to release
  92. * @metasize: size of metadata
  93. *
  94. * This releases @stack, making it no longer suitable for use as a
  95. * coroutine stack. @metasize must be equal to the metasize passed to
  96. * coroutine_stack_init.
  97. */
  98. void coroutine_stack_release(struct coroutine_stack *stack, size_t metasize);
  99. /**
  100. * coroutine_stack_check - Validate and return a coroutine stack
  101. * @stack: stack to check
  102. * @abortstr: the location to print on aborting, or NULL.
  103. *
  104. * Debugging check if @stack doesn't appear to be a valid coroutine
  105. * stack, and @abortstr is non-NULL it will be printed and the
  106. * function will abort.
  107. *
  108. * Returns @stack if it appears valid, NULL if not (it can never
  109. * return NULL if @abortstr is set).
  110. */
  111. struct coroutine_stack *coroutine_stack_check(struct coroutine_stack *stack,
  112. const char *abortstr);
  113. /**
  114. * coroutine_stack_to_metadata - Returns pointer to user's metadata
  115. * allocated within the stack
  116. * @stack: coroutine stack
  117. * @metasize: size of metadata
  118. *
  119. * Returns a pointer to the metadata area within @stack. This is of
  120. * size given at initialization time, and won't be overwritten by
  121. * coroutines executing on the stack. It's up to the caller what to
  122. * put in here. @metasize must be equal to the value passed to
  123. * coroutine_stack_init().
  124. */
  125. static inline void *coroutine_stack_to_metadata(struct coroutine_stack *stack,
  126. size_t metasize)
  127. {
  128. #if HAVE_STACK_GROWS_UPWARDS
  129. return (char *)stack - metasize;
  130. #else
  131. return (char *)stack + COROUTINE_STK_OVERHEAD;
  132. #endif
  133. }
  134. /**
  135. * coroutine_stack_from_metadata - Returns pointer to coroutine stack
  136. * pointer given pointer to user metadata
  137. * @metadat: user metadata within a stack
  138. * @metasize: size of metadata
  139. *
  140. * Returns a pointer to the coroutine_stack handle within a stack.
  141. * The argument must be a pointer returned by
  142. * coroutine_stack_to_metadata() at an earlier time. @metasize must be
  143. * equal to the value passed to coroutine_stack_init().
  144. */
  145. static inline struct coroutine_stack *
  146. coroutine_stack_from_metadata(void *metadata, size_t metasize)
  147. {
  148. #if HAVE_STACK_GROWS_UPWARDS
  149. return (struct coroutine_stack *)((char *)metadata + metasize);
  150. #else
  151. return (struct coroutine_stack *)((char *)metadata
  152. - COROUTINE_STK_OVERHEAD);
  153. #endif
  154. }
  155. /**
  156. * coroutine_stack_size - Return size of a coroutine stack
  157. * @stack: coroutine stack
  158. *
  159. * Returns the size of the coroutine stack @stack. This does not
  160. * include the overhead of struct coroutine_stack or metdata.
  161. */
  162. size_t coroutine_stack_size(const struct coroutine_stack *stack);
  163. /*
  164. * Coroutine switching
  165. */
  166. #if HAVE_UCONTEXT
  167. #include <ucontext.h>
  168. #define COROUTINE_AVAILABLE 1
  169. #else
  170. #define COROUTINE_AVAILABLE 0
  171. #endif
  172. struct coroutine_state {
  173. #if HAVE_UCONTEXT
  174. ucontext_t uc;
  175. #endif /* HAVE_UCONTEXT */
  176. };
  177. #if COROUTINE_AVAILABLE
  178. /**
  179. * coroutine_init - Prepare a coroutine for execution
  180. * @cs: coroutine_state structure to initialize
  181. * @fn: function to start executing in the coroutine
  182. * @arg: argument for @fn
  183. * @stack: stack to use for the coroutine
  184. *
  185. * Prepares @cs as a new coroutine which will execute starting with
  186. * function @fn, using stack @stack.
  187. */
  188. void coroutine_init_(struct coroutine_state *cs,
  189. void (*fn)(void *), void *arg,
  190. struct coroutine_stack *stack);
  191. #define coroutine_init(cs, fn, arg, stack) \
  192. coroutine_init_((cs), \
  193. typesafe_cb(void, void *, (fn), (arg)), \
  194. (arg), (stack))
  195. /**
  196. * coroutine_jump - Irreversibly switch to executing a coroutine
  197. * @to: coroutine to switch to
  198. *
  199. * Immediately jump to executing coroutine @to (at whatever point in
  200. * execution it was up to). Never returns.
  201. */
  202. void NORETURN coroutine_jump(const struct coroutine_state *to);
  203. /**
  204. * coroutine_switch - Switch coroutines
  205. * @from: coroutine in which to store current execution state
  206. * @to: coroutine to switch to
  207. *
  208. * Stop executing the current routine, saving its state in @from, and
  209. * switch to executing the coroutine @to. Returns only when something
  210. * switches or jumps back to @from.
  211. */
  212. void coroutine_switch(struct coroutine_state *from,
  213. const struct coroutine_state *to);
  214. #else
  215. static inline void coroutine_init(struct coroutine_state *cs,
  216. void (*fn)(void *), void *arg,
  217. struct coroutine_stack *stack)
  218. {
  219. assert(0);
  220. }
  221. static inline void NORETURN coroutine_jump(const struct coroutine_state *to)
  222. {
  223. assert(0);
  224. }
  225. static inline void coroutine_switch(struct coroutine_state *from,
  226. const struct coroutine_state *to)
  227. {
  228. assert(0);
  229. }
  230. #endif /* !COROUTINE_AVAILABLE */
  231. #endif /* CCAN_COROUTINE_H */