async.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002, 2004, 2006, 2008,
  2. * 2009, 2010, 2011, 2014 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include "libguile/_scm.h"
  23. #include "libguile/atomics-internal.h"
  24. #include "libguile/eval.h"
  25. #include "libguile/throw.h"
  26. #include "libguile/smob.h"
  27. #include "libguile/dynwind.h"
  28. #include "libguile/deprecation.h"
  29. #include "libguile/validate.h"
  30. #include "libguile/async.h"
  31. #ifdef HAVE_STRING_H
  32. #include <string.h>
  33. #endif
  34. #include <unistd.h>
  35. #include <full-write.h>
  36. /* {Asynchronous Events}
  37. *
  38. * Asyncs are used to run arbitrary code at the next safe point in a
  39. * specified thread. You can use them to trigger execution of Scheme
  40. * code from signal handlers or to interrupt a thread, for example.
  41. *
  42. * Each thread has a list of 'activated asyncs', which is a normal
  43. * Scheme list of procedures with zero arguments. When a thread
  44. * executes an scm_async_tick (), it will call all procedures on this
  45. * list in the order they were added to the list.
  46. */
  47. void
  48. scm_i_async_push (scm_i_thread *t, SCM proc)
  49. {
  50. SCM asyncs;
  51. /* The usual algorithm you'd use for atomics with GC would be
  52. something like:
  53. repeat
  54. l = get(asyncs);
  55. until swap(l, cons(proc, l))
  56. But this is a LIFO list of asyncs, and that's not so great. To
  57. make it FIFO, you'd do:
  58. repeat
  59. l = get(asyncs);
  60. until swap(l, append(l, list(proc)))
  61. However, some parts of Guile need to add entries to the async list
  62. from a context in which allocation is unsafe, for example right
  63. before GC or from a signal handler. They do that by pre-allocating
  64. a pair, then when the interrupt fires the code does a setcdr of
  65. that pair to the t->pending_asyncs and atomically updates
  66. t->pending_asyncs. So the append strategy doesn't work.
  67. Instead to preserve the FIFO behavior we atomically cut off the
  68. tail of the asyncs every time we want to run an interrupt, then
  69. disable that newly-severed tail by setting its cdr to #f. Not so
  70. nice, but oh well. */
  71. asyncs = scm_atomic_ref_scm (&t->pending_asyncs);
  72. do
  73. {
  74. /* Traverse the asyncs list atomically. */
  75. SCM walk;
  76. for (walk = asyncs;
  77. scm_is_pair (walk);
  78. walk = scm_atomic_ref_scm (SCM_CDRLOC (walk)))
  79. if (scm_is_eq (SCM_CAR (walk), proc))
  80. return;
  81. }
  82. while (!scm_atomic_compare_and_swap_scm (&t->pending_asyncs, &asyncs,
  83. scm_cons (proc, asyncs)));
  84. }
  85. /* Precondition: there are pending asyncs. */
  86. SCM
  87. scm_i_async_pop (scm_i_thread *t)
  88. {
  89. while (1)
  90. {
  91. SCM asyncs, last_pair, penultimate_pair;
  92. last_pair = asyncs = scm_atomic_ref_scm (&t->pending_asyncs);
  93. penultimate_pair = SCM_BOOL_F;
  94. /* Since we are the only writer to cdrs of pairs in ASYNCS, and these
  95. pairs were given to us after an atomic update to t->pending_asyncs,
  96. no need to use atomic ops to traverse the list. */
  97. while (scm_is_pair (SCM_CDR (last_pair)))
  98. {
  99. penultimate_pair = last_pair;
  100. last_pair = SCM_CDR (last_pair);
  101. }
  102. /* Sever the tail. */
  103. if (scm_is_false (penultimate_pair))
  104. {
  105. if (!scm_atomic_compare_and_swap_scm (&t->pending_asyncs, &asyncs,
  106. SCM_EOL))
  107. continue;
  108. }
  109. else
  110. scm_atomic_set_scm (SCM_CDRLOC (penultimate_pair), SCM_EOL);
  111. /* Disable it. */
  112. scm_atomic_set_scm (SCM_CDRLOC (last_pair), SCM_BOOL_F);
  113. return SCM_CAR (last_pair);
  114. }
  115. }
  116. void
  117. scm_async_tick (void)
  118. {
  119. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  120. if (t->block_asyncs)
  121. return;
  122. while (!scm_is_null (scm_atomic_ref_scm (&t->pending_asyncs)))
  123. scm_call_0 (scm_i_async_pop (t));
  124. }
  125. struct scm_thread_wake_data {
  126. enum { WAIT_FD, WAIT_COND } kind;
  127. union {
  128. struct {
  129. int fd;
  130. } wait_fd;
  131. struct {
  132. scm_i_pthread_mutex_t *mutex;
  133. scm_i_pthread_cond_t *cond;
  134. } wait_cond;
  135. } data;
  136. };
  137. int
  138. scm_i_prepare_to_wait (scm_i_thread *t,
  139. struct scm_thread_wake_data *wake)
  140. {
  141. if (t->block_asyncs)
  142. return 0;
  143. scm_atomic_set_pointer ((void **)&t->wake, wake);
  144. /* If no interrupt was registered in the meantime, then any future
  145. wakeup will signal the FD or cond var. */
  146. if (scm_is_null (scm_atomic_ref_scm (&t->pending_asyncs)))
  147. return 0;
  148. /* Otherwise clear the wake pointer and indicate that the caller
  149. should handle interrupts directly. */
  150. scm_i_wait_finished (t);
  151. return 1;
  152. }
  153. void
  154. scm_i_wait_finished (scm_i_thread *t)
  155. {
  156. scm_atomic_set_pointer ((void **)&t->wake, NULL);
  157. }
  158. int
  159. scm_i_prepare_to_wait_on_fd (scm_i_thread *t, int fd)
  160. {
  161. struct scm_thread_wake_data *wake;
  162. wake = scm_gc_typed_calloc (struct scm_thread_wake_data);
  163. wake->kind = WAIT_FD;
  164. wake->data.wait_fd.fd = fd;
  165. return scm_i_prepare_to_wait (t, wake);
  166. }
  167. int
  168. scm_c_prepare_to_wait_on_fd (int fd)
  169. {
  170. return scm_i_prepare_to_wait_on_fd (SCM_I_CURRENT_THREAD, fd);
  171. }
  172. int
  173. scm_i_prepare_to_wait_on_cond (scm_i_thread *t,
  174. scm_i_pthread_mutex_t *m,
  175. scm_i_pthread_cond_t *c)
  176. {
  177. struct scm_thread_wake_data *wake;
  178. wake = scm_gc_typed_calloc (struct scm_thread_wake_data);
  179. wake->kind = WAIT_COND;
  180. wake->data.wait_cond.mutex = m;
  181. wake->data.wait_cond.cond = c;
  182. return scm_i_prepare_to_wait (t, wake);
  183. }
  184. int
  185. scm_c_prepare_to_wait_on_cond (scm_i_pthread_mutex_t *m,
  186. scm_i_pthread_cond_t *c)
  187. {
  188. return scm_i_prepare_to_wait_on_cond (SCM_I_CURRENT_THREAD, m, c);
  189. }
  190. void
  191. scm_c_wait_finished (void)
  192. {
  193. scm_i_wait_finished (SCM_I_CURRENT_THREAD);
  194. }
  195. SCM_DEFINE (scm_system_async_mark_for_thread, "system-async-mark", 1, 1, 0,
  196. (SCM proc, SCM thread),
  197. "Mark @var{proc} (a procedure with zero arguments) for future execution\n"
  198. "in @var{thread}. If @var{proc} has already been marked for\n"
  199. "@var{thread} but has not been executed yet, this call has no effect.\n"
  200. "If @var{thread} is omitted, the thread that called\n"
  201. "@code{system-async-mark} is used.\n\n"
  202. "This procedure is not safe to be called from C signal handlers. Use\n"
  203. "@code{scm_sigaction} or @code{scm_sigaction_for_thread} to install\n"
  204. "signal handlers.")
  205. #define FUNC_NAME s_scm_system_async_mark_for_thread
  206. {
  207. scm_i_thread *t;
  208. struct scm_thread_wake_data *wake;
  209. if (SCM_UNBNDP (thread))
  210. t = SCM_I_CURRENT_THREAD;
  211. else
  212. {
  213. SCM_VALIDATE_THREAD (2, thread);
  214. t = SCM_I_THREAD_DATA (thread);
  215. }
  216. scm_i_async_push (t, proc);
  217. /* At this point the async is enqueued. However if the thread is
  218. sleeping, we have to wake it up. */
  219. if ((wake = scm_atomic_ref_pointer ((void **) &t->wake)))
  220. {
  221. /* By now, the thread T might be out of its sleep already, or
  222. might even be in the next, unrelated sleep. Interrupting it
  223. anyway does no harm, however.
  224. The important thing to prevent here is to signal the cond
  225. before T waits on it. This can not happen since T has its
  226. mutex locked while preparing the wait and will only unlock it
  227. again while waiting on the cond.
  228. */
  229. if (wake->kind == WAIT_COND)
  230. {
  231. scm_i_scm_pthread_mutex_lock (wake->data.wait_cond.mutex);
  232. scm_i_pthread_cond_signal (wake->data.wait_cond.cond);
  233. scm_i_pthread_mutex_unlock (wake->data.wait_cond.mutex);
  234. }
  235. else if (wake->kind == WAIT_FD)
  236. {
  237. char dummy = 0;
  238. /* Likewise, T might already been done with sleeping here, but
  239. interrupting it once too often does no harm. T might also
  240. not yet have started sleeping, but this is no problem
  241. either since the data written to a pipe will not be lost,
  242. unlike a condition variable signal. */
  243. full_write (wake->data.wait_fd.fd, &dummy, 1);
  244. }
  245. else
  246. abort ();
  247. }
  248. return SCM_UNSPECIFIED;
  249. }
  250. #undef FUNC_NAME
  251. SCM
  252. scm_system_async_mark (SCM proc)
  253. #define FUNC_NAME s_scm_system_async_mark_for_thread
  254. {
  255. return scm_system_async_mark_for_thread (proc, SCM_UNDEFINED);
  256. }
  257. #undef FUNC_NAME
  258. SCM_DEFINE (scm_noop, "noop", 0, 0, 1,
  259. (SCM args),
  260. "Do nothing. When called without arguments, return @code{#f},\n"
  261. "otherwise return the first argument.")
  262. #define FUNC_NAME s_scm_noop
  263. {
  264. SCM_VALIDATE_REST_ARGUMENT (args);
  265. return (SCM_NULL_OR_NIL_P (args) ? SCM_BOOL_F : SCM_CAR (args));
  266. }
  267. #undef FUNC_NAME
  268. static void
  269. increase_block (void *data)
  270. {
  271. scm_i_thread *t = data;
  272. t->block_asyncs++;
  273. }
  274. static void
  275. decrease_block (void *data)
  276. {
  277. scm_i_thread *t = data;
  278. if (--t->block_asyncs == 0)
  279. scm_async_tick ();
  280. }
  281. void
  282. scm_dynwind_block_asyncs (void)
  283. {
  284. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  285. scm_dynwind_rewind_handler (increase_block, t, SCM_F_WIND_EXPLICITLY);
  286. scm_dynwind_unwind_handler (decrease_block, t, SCM_F_WIND_EXPLICITLY);
  287. }
  288. void
  289. scm_dynwind_unblock_asyncs (void)
  290. {
  291. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  292. if (t->block_asyncs == 0)
  293. scm_misc_error ("scm_with_unblocked_asyncs",
  294. "asyncs already unblocked", SCM_EOL);
  295. scm_dynwind_rewind_handler (decrease_block, t, SCM_F_WIND_EXPLICITLY);
  296. scm_dynwind_unwind_handler (increase_block, t, SCM_F_WIND_EXPLICITLY);
  297. }
  298. SCM_DEFINE (scm_call_with_blocked_asyncs, "call-with-blocked-asyncs", 1, 0, 0,
  299. (SCM proc),
  300. "Call @var{proc} with no arguments and block the execution\n"
  301. "of system asyncs by one level for the current thread while\n"
  302. "it is running. Return the value returned by @var{proc}.\n")
  303. #define FUNC_NAME s_scm_call_with_blocked_asyncs
  304. {
  305. SCM ans;
  306. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  307. scm_dynwind_block_asyncs ();
  308. ans = scm_call_0 (proc);
  309. scm_dynwind_end ();
  310. return ans;
  311. }
  312. #undef FUNC_NAME
  313. void *
  314. scm_c_call_with_blocked_asyncs (void *(*proc) (void *data), void *data)
  315. {
  316. void* ans;
  317. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  318. scm_dynwind_block_asyncs ();
  319. ans = proc (data);
  320. scm_dynwind_end ();
  321. return ans;
  322. }
  323. SCM_DEFINE (scm_call_with_unblocked_asyncs, "call-with-unblocked-asyncs", 1, 0, 0,
  324. (SCM proc),
  325. "Call @var{proc} with no arguments and unblock the execution\n"
  326. "of system asyncs by one level for the current thread while\n"
  327. "it is running. Return the value returned by @var{proc}.\n")
  328. #define FUNC_NAME s_scm_call_with_unblocked_asyncs
  329. {
  330. SCM ans;
  331. if (SCM_I_CURRENT_THREAD->block_asyncs == 0)
  332. SCM_MISC_ERROR ("asyncs already unblocked", SCM_EOL);
  333. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  334. scm_dynwind_unblock_asyncs ();
  335. ans = scm_call_0 (proc);
  336. scm_dynwind_end ();
  337. return ans;
  338. }
  339. #undef FUNC_NAME
  340. void *
  341. scm_c_call_with_unblocked_asyncs (void *(*proc) (void *data), void *data)
  342. {
  343. void* ans;
  344. if (SCM_I_CURRENT_THREAD->block_asyncs == 0)
  345. scm_misc_error ("scm_c_call_with_unblocked_asyncs",
  346. "asyncs already unblocked", SCM_EOL);
  347. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  348. scm_dynwind_unblock_asyncs ();
  349. ans = proc (data);
  350. scm_dynwind_end ();
  351. return ans;
  352. }
  353. void
  354. scm_init_async ()
  355. {
  356. #include "libguile/async.x"
  357. }
  358. /*
  359. Local Variables:
  360. c-file-style: "gnu"
  361. End:
  362. */