9
3

my_context.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. Copyright 2011 Kristian Nielsen and Monty Program Ab
  3. This file is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. Simple API for spawning a co-routine, to be used for async libmysqlclient.
  16. Idea is that by implementing this interface using whatever facilities are
  17. available for given platform, we can use the same code for the generic
  18. libmysqlclient-async code.
  19. (This particular implementation uses Posix ucontext swapcontext().)
  20. */
  21. #ifdef __WIN__
  22. #define MY_CONTEXT_USE_WIN32_FIBERS 1
  23. #elif defined(__GNUC__) && __GNUC__ >= 3 && defined(__x86_64__) && !defined(__ILP32__)
  24. #define MY_CONTEXT_USE_X86_64_GCC_ASM
  25. #elif defined(__GNUC__) && __GNUC__ >= 3 && defined(__i386__)
  26. #define MY_CONTEXT_USE_I386_GCC_ASM
  27. #elif defined(HAVE_UCONTEXT_H)
  28. #define MY_CONTEXT_USE_UCONTEXT
  29. #else
  30. #define MY_CONTEXT_DISABLE
  31. #endif
  32. #ifdef MY_CONTEXT_USE_WIN32_FIBERS
  33. struct my_context {
  34. void (*user_func)(void *);
  35. void *user_arg;
  36. void *app_fiber;
  37. void *lib_fiber;
  38. int return_value;
  39. #ifndef DBUG_OFF
  40. void *dbug_state;
  41. #endif
  42. };
  43. #endif
  44. #ifdef MY_CONTEXT_USE_UCONTEXT
  45. #include <ucontext.h>
  46. struct my_context {
  47. void (*user_func)(void *);
  48. void *user_data;
  49. void *stack;
  50. size_t stack_size;
  51. ucontext_t base_context;
  52. ucontext_t spawned_context;
  53. int active;
  54. #ifdef HAVE_VALGRIND
  55. unsigned int valgrind_stack_id;
  56. #endif
  57. #ifndef DBUG_OFF
  58. void *dbug_state;
  59. #endif
  60. };
  61. #endif
  62. #ifdef MY_CONTEXT_USE_X86_64_GCC_ASM
  63. #include <stdint.h>
  64. struct my_context {
  65. uint64_t save[9];
  66. void *stack_top;
  67. void *stack_bot;
  68. #ifdef HAVE_VALGRIND
  69. unsigned int valgrind_stack_id;
  70. #endif
  71. #ifndef DBUG_OFF
  72. void *dbug_state;
  73. #endif
  74. };
  75. #endif
  76. #ifdef MY_CONTEXT_USE_I386_GCC_ASM
  77. #include <stdint.h>
  78. struct my_context {
  79. uint64_t save[7];
  80. void *stack_top;
  81. void *stack_bot;
  82. #ifdef HAVE_VALGRIND
  83. unsigned int valgrind_stack_id;
  84. #endif
  85. #ifndef DBUG_OFF
  86. void *dbug_state;
  87. #endif
  88. };
  89. #endif
  90. #ifdef MY_CONTEXT_DISABLE
  91. struct my_context {
  92. int dummy;
  93. };
  94. #endif
  95. /*
  96. Initialize an asynchroneous context object.
  97. Returns 0 on success, non-zero on failure.
  98. */
  99. extern int my_context_init(struct my_context *c, size_t stack_size);
  100. /* Free an asynchroneous context object, deallocating any resources used. */
  101. extern void my_context_destroy(struct my_context *c);
  102. /*
  103. Spawn an asynchroneous context. The context will run the supplied user
  104. function, passing the supplied user data pointer.
  105. The context must have been initialised with my_context_init() prior to
  106. this call.
  107. The user function may call my_context_yield(), which will cause this
  108. function to return 1. Then later my_context_continue() may be called, which
  109. will resume the asynchroneous context by returning from the previous
  110. my_context_yield() call.
  111. When the user function returns, this function returns 0.
  112. In case of error, -1 is returned.
  113. */
  114. extern int my_context_spawn(struct my_context *c, void (*f)(void *), void *d);
  115. /*
  116. Suspend an asynchroneous context started with my_context_spawn.
  117. When my_context_yield() is called, execution immediately returns from the
  118. last my_context_spawn() or my_context_continue() call. Then when later
  119. my_context_continue() is called, execution resumes by returning from this
  120. my_context_yield() call.
  121. Returns 0 if ok, -1 in case of error.
  122. */
  123. extern int my_context_yield(struct my_context *c);
  124. /*
  125. Resume an asynchroneous context. The context was spawned by
  126. my_context_spawn(), and later suspended inside my_context_yield().
  127. The asynchroneous context may be repeatedly suspended with
  128. my_context_yield() and resumed with my_context_continue().
  129. Each time it is suspended, this function returns 1. When the originally
  130. spawned user function returns, this function returns 0.
  131. In case of error, -1 is returned.
  132. */
  133. extern int my_context_continue(struct my_context *c);
  134. struct mysql_async_context {
  135. /*
  136. This is set to the value that should be returned from foo_start() or
  137. foo_cont() when a call is suspended.
  138. */
  139. unsigned int events_to_wait_for;
  140. /*
  141. It is also set to the event(s) that triggered when a suspended call is
  142. resumed, eg. whether we woke up due to connection completed or timeout
  143. in mysql_real_connect_cont().
  144. */
  145. unsigned int events_occurred;
  146. /*
  147. This is set to the result of the whole asynchronous operation when it
  148. completes. It uses a union, as different calls have different return
  149. types.
  150. */
  151. union {
  152. void *r_ptr;
  153. const void *r_const_ptr;
  154. int r_int;
  155. my_bool r_my_bool;
  156. } ret_result;
  157. /*
  158. The timeout value (in millisecods), for suspended calls that need to wake
  159. up on a timeout (eg. mysql_real_connect_start().
  160. */
  161. unsigned int timeout_value;
  162. /*
  163. This flag is set when we are executing inside some asynchronous call
  164. foo_start() or foo_cont(). It is used to decide whether to use the
  165. synchronous or asynchronous version of calls that may block such as
  166. recv().
  167. Note that this flag is not set when a call is suspended, eg. after
  168. returning from foo_start() and before re-entering foo_cont().
  169. */
  170. my_bool active;
  171. /*
  172. This flag is set when an asynchronous operation is in progress, but
  173. suspended. Ie. it is set when foo_start() or foo_cont() returns because
  174. the operation needs to block, suspending the operation.
  175. It is used to give an error (rather than crash) if the application
  176. attempts to call some foo_cont() method when no suspended operation foo is
  177. in progress.
  178. */
  179. my_bool suspended;
  180. /*
  181. If non-NULL, this is a pointer to a callback hook that will be invoked with
  182. the user data argument just before the context is suspended, and just after
  183. it is resumed.
  184. */
  185. void (*suspend_resume_hook)(my_bool suspend, void *user_data);
  186. void *suspend_resume_hook_user_data;
  187. /*
  188. This is used to save the execution contexts so that we can suspend an
  189. operation and switch back to the application context, to resume the
  190. suspended context later when the application re-invokes us with
  191. foo_cont().
  192. */
  193. struct my_context async_context;
  194. };