async.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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/eval.h"
  24. #include "libguile/throw.h"
  25. #include "libguile/root.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. * There are two kinds of asyncs: system asyncs and user asyncs. The
  39. * two kinds have some concepts in commen but work slightly
  40. * differently and are not interchangeable.
  41. *
  42. * System asyncs are used to run arbitrary code at the next safe point
  43. * in a specified thread. You can use them to trigger execution of
  44. * Scheme code from signal handlers or to interrupt a thread, for
  45. * example.
  46. *
  47. * Each thread has a list of 'activated asyncs', which is a normal
  48. * Scheme list of procedures with zero arguments. When a thread
  49. * executes a SCM_ASYNC_TICK statement (which is included in
  50. * SCM_TICK), it will call all procedures on this list.
  51. *
  52. * Also, a thread will wake up when a procedure is added to its list
  53. * of active asyncs and call them. After that, it will go to sleep
  54. * again. (Not implemented yet.)
  55. *
  56. *
  57. * User asyncs are a little data structure that consists of a
  58. * procedure of zero arguments and a mark. There are functions for
  59. * setting the mark of a user async and for calling all procedures of
  60. * marked asyncs in a given list. Nothing you couldn't quickly
  61. * implement yourself.
  62. */
  63. /* User asyncs. */
  64. static scm_t_bits tc16_async;
  65. /* cmm: this has SCM_ prefix because SCM_MAKE_VALIDATE expects it.
  66. this is ugly. */
  67. #define SCM_ASYNCP(X) SCM_TYP16_PREDICATE (tc16_async, X)
  68. #define VALIDATE_ASYNC(pos, a) SCM_MAKE_VALIDATE_MSG(pos, a, ASYNCP, "user async")
  69. #define ASYNC_GOT_IT(X) (SCM_SMOB_FLAGS (X))
  70. #define SET_ASYNC_GOT_IT(X, V) (SCM_SET_SMOB_FLAGS ((X), ((V))))
  71. #define ASYNC_THUNK(X) SCM_SMOB_OBJECT_1 (X)
  72. SCM_DEFINE (scm_async, "async", 1, 0, 0,
  73. (SCM thunk),
  74. "Create a new async for the procedure @var{thunk}.")
  75. #define FUNC_NAME s_scm_async
  76. {
  77. SCM_RETURN_NEWSMOB (tc16_async, SCM_UNPACK (thunk));
  78. }
  79. #undef FUNC_NAME
  80. SCM_DEFINE (scm_async_mark, "async-mark", 1, 0, 0,
  81. (SCM a),
  82. "Mark the async @var{a} for future execution.")
  83. #define FUNC_NAME s_scm_async_mark
  84. {
  85. VALIDATE_ASYNC (1, a);
  86. SET_ASYNC_GOT_IT (a, 1);
  87. return SCM_UNSPECIFIED;
  88. }
  89. #undef FUNC_NAME
  90. SCM_DEFINE (scm_run_asyncs, "run-asyncs", 1, 0, 0,
  91. (SCM list_of_a),
  92. "Execute all thunks from the asyncs of the list @var{list_of_a}.")
  93. #define FUNC_NAME s_scm_run_asyncs
  94. {
  95. while (! SCM_NULL_OR_NIL_P (list_of_a))
  96. {
  97. SCM a;
  98. SCM_VALIDATE_CONS (1, list_of_a);
  99. a = SCM_CAR (list_of_a);
  100. VALIDATE_ASYNC (SCM_ARG1, a);
  101. if (ASYNC_GOT_IT (a))
  102. {
  103. SET_ASYNC_GOT_IT (a, 0);
  104. scm_call_0 (ASYNC_THUNK (a));
  105. }
  106. list_of_a = SCM_CDR (list_of_a);
  107. }
  108. return SCM_BOOL_T;
  109. }
  110. #undef FUNC_NAME
  111. static scm_i_pthread_mutex_t async_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  112. /* System asyncs. */
  113. void
  114. scm_async_tick (void)
  115. {
  116. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  117. SCM asyncs;
  118. /* Reset pending_asyncs even when asyncs are blocked and not really
  119. executed since this will avoid future futile calls to this
  120. function. When asyncs are unblocked again, this function is
  121. invoked even when pending_asyncs is zero.
  122. */
  123. scm_i_scm_pthread_mutex_lock (&async_mutex);
  124. t->pending_asyncs = 0;
  125. if (t->block_asyncs == 0)
  126. {
  127. asyncs = t->active_asyncs;
  128. t->active_asyncs = SCM_EOL;
  129. }
  130. else
  131. asyncs = SCM_EOL;
  132. scm_i_pthread_mutex_unlock (&async_mutex);
  133. while (scm_is_pair (asyncs))
  134. {
  135. SCM next = SCM_CDR (asyncs);
  136. SCM_SETCDR (asyncs, SCM_BOOL_F);
  137. scm_call_0 (SCM_CAR (asyncs));
  138. asyncs = next;
  139. }
  140. }
  141. void
  142. scm_i_queue_async_cell (SCM c, scm_i_thread *t)
  143. {
  144. SCM sleep_object;
  145. scm_i_pthread_mutex_t *sleep_mutex;
  146. int sleep_fd;
  147. SCM p;
  148. scm_i_scm_pthread_mutex_lock (&async_mutex);
  149. p = t->active_asyncs;
  150. SCM_SETCDR (c, SCM_EOL);
  151. if (!scm_is_pair (p))
  152. t->active_asyncs = c;
  153. else
  154. {
  155. SCM pp;
  156. while (scm_is_pair (pp = SCM_CDR (p)))
  157. {
  158. if (scm_is_eq (SCM_CAR (p), SCM_CAR (c)))
  159. {
  160. scm_i_pthread_mutex_unlock (&async_mutex);
  161. return;
  162. }
  163. p = pp;
  164. }
  165. SCM_SETCDR (p, c);
  166. }
  167. t->pending_asyncs = 1;
  168. sleep_object = t->sleep_object;
  169. sleep_mutex = t->sleep_mutex;
  170. sleep_fd = t->sleep_fd;
  171. scm_i_pthread_mutex_unlock (&async_mutex);
  172. if (sleep_mutex)
  173. {
  174. /* By now, the thread T might be out of its sleep already, or
  175. might even be in the next, unrelated sleep. Interrupting it
  176. anyway does no harm, however.
  177. The important thing to prevent here is to signal sleep_cond
  178. before T waits on it. This can not happen since T has
  179. sleep_mutex locked while setting t->sleep_mutex and will only
  180. unlock it again while waiting on sleep_cond.
  181. */
  182. scm_i_scm_pthread_mutex_lock (sleep_mutex);
  183. scm_i_pthread_cond_signal (&t->sleep_cond);
  184. scm_i_pthread_mutex_unlock (sleep_mutex);
  185. }
  186. if (sleep_fd >= 0)
  187. {
  188. char dummy = 0;
  189. /* Likewise, T might already been done with sleeping here, but
  190. interrupting it once too often does no harm. T might also
  191. not yet have started sleeping, but this is no problem either
  192. since the data written to a pipe will not be lost, unlike a
  193. condition variable signal. */
  194. full_write (sleep_fd, &dummy, 1);
  195. }
  196. /* This is needed to protect sleep_mutex.
  197. */
  198. scm_remember_upto_here_1 (sleep_object);
  199. }
  200. int
  201. scm_i_setup_sleep (scm_i_thread *t,
  202. SCM sleep_object, scm_i_pthread_mutex_t *sleep_mutex,
  203. int sleep_fd)
  204. {
  205. int pending;
  206. scm_i_scm_pthread_mutex_lock (&async_mutex);
  207. pending = t->pending_asyncs;
  208. if (!pending)
  209. {
  210. t->sleep_object = sleep_object;
  211. t->sleep_mutex = sleep_mutex;
  212. t->sleep_fd = sleep_fd;
  213. }
  214. scm_i_pthread_mutex_unlock (&async_mutex);
  215. return pending;
  216. }
  217. void
  218. scm_i_reset_sleep (scm_i_thread *t)
  219. {
  220. scm_i_scm_pthread_mutex_lock (&async_mutex);
  221. t->sleep_object = SCM_BOOL_F;
  222. t->sleep_mutex = NULL;
  223. t->sleep_fd = -1;
  224. scm_i_pthread_mutex_unlock (&async_mutex);
  225. }
  226. SCM_DEFINE (scm_system_async_mark_for_thread, "system-async-mark", 1, 1, 0,
  227. (SCM proc, SCM thread),
  228. "Mark @var{proc} (a procedure with zero arguments) for future execution\n"
  229. "in @var{thread}. If @var{proc} has already been marked for\n"
  230. "@var{thread} but has not been executed yet, this call has no effect.\n"
  231. "If @var{thread} is omitted, the thread that called\n"
  232. "@code{system-async-mark} is used.\n\n"
  233. "This procedure is not safe to be called from C signal handlers. Use\n"
  234. "@code{scm_sigaction} or @code{scm_sigaction_for_thread} to install\n"
  235. "signal handlers.")
  236. #define FUNC_NAME s_scm_system_async_mark_for_thread
  237. {
  238. /* The current thread might not have a handle yet. This can happen
  239. when the GC runs immediately before allocating the handle. At
  240. the end of that GC, a system async might be marked. Thus, we can
  241. not use scm_current_thread here.
  242. */
  243. scm_i_thread *t;
  244. if (SCM_UNBNDP (thread))
  245. t = SCM_I_CURRENT_THREAD;
  246. else
  247. {
  248. SCM_VALIDATE_THREAD (2, thread);
  249. if (scm_c_thread_exited_p (thread))
  250. SCM_MISC_ERROR ("thread has already exited", SCM_EOL);
  251. t = SCM_I_THREAD_DATA (thread);
  252. }
  253. scm_i_queue_async_cell (scm_cons (proc, SCM_BOOL_F), t);
  254. return SCM_UNSPECIFIED;
  255. }
  256. #undef FUNC_NAME
  257. SCM
  258. scm_system_async_mark (SCM proc)
  259. #define FUNC_NAME s_scm_system_async_mark_for_thread
  260. {
  261. return scm_system_async_mark_for_thread (proc, SCM_UNDEFINED);
  262. }
  263. #undef FUNC_NAME
  264. SCM_DEFINE (scm_noop, "noop", 0, 0, 1,
  265. (SCM args),
  266. "Do nothing. When called without arguments, return @code{#f},\n"
  267. "otherwise return the first argument.")
  268. #define FUNC_NAME s_scm_noop
  269. {
  270. SCM_VALIDATE_REST_ARGUMENT (args);
  271. return (SCM_NULL_OR_NIL_P (args) ? SCM_BOOL_F : SCM_CAR (args));
  272. }
  273. #undef FUNC_NAME
  274. static void
  275. increase_block (void *data)
  276. {
  277. scm_i_thread *t = data;
  278. t->block_asyncs++;
  279. }
  280. static void
  281. decrease_block (void *data)
  282. {
  283. scm_i_thread *t = data;
  284. if (--t->block_asyncs == 0)
  285. scm_async_tick ();
  286. }
  287. void
  288. scm_dynwind_block_asyncs (void)
  289. {
  290. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  291. scm_dynwind_rewind_handler (increase_block, t, SCM_F_WIND_EXPLICITLY);
  292. scm_dynwind_unwind_handler (decrease_block, t, SCM_F_WIND_EXPLICITLY);
  293. }
  294. void
  295. scm_dynwind_unblock_asyncs (void)
  296. {
  297. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  298. if (t->block_asyncs == 0)
  299. scm_misc_error ("scm_with_unblocked_asyncs",
  300. "asyncs already unblocked", SCM_EOL);
  301. scm_dynwind_rewind_handler (decrease_block, t, SCM_F_WIND_EXPLICITLY);
  302. scm_dynwind_unwind_handler (increase_block, t, SCM_F_WIND_EXPLICITLY);
  303. }
  304. SCM_DEFINE (scm_call_with_blocked_asyncs, "call-with-blocked-asyncs", 1, 0, 0,
  305. (SCM proc),
  306. "Call @var{proc} with no arguments and block the execution\n"
  307. "of system asyncs by one level for the current thread while\n"
  308. "it is running. Return the value returned by @var{proc}.\n")
  309. #define FUNC_NAME s_scm_call_with_blocked_asyncs
  310. {
  311. SCM ans;
  312. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  313. scm_dynwind_block_asyncs ();
  314. ans = scm_call_0 (proc);
  315. scm_dynwind_end ();
  316. return ans;
  317. }
  318. #undef FUNC_NAME
  319. void *
  320. scm_c_call_with_blocked_asyncs (void *(*proc) (void *data), void *data)
  321. {
  322. void* ans;
  323. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  324. scm_dynwind_block_asyncs ();
  325. ans = proc (data);
  326. scm_dynwind_end ();
  327. return ans;
  328. }
  329. SCM_DEFINE (scm_call_with_unblocked_asyncs, "call-with-unblocked-asyncs", 1, 0, 0,
  330. (SCM proc),
  331. "Call @var{proc} with no arguments and unblock the execution\n"
  332. "of system asyncs by one level for the current thread while\n"
  333. "it is running. Return the value returned by @var{proc}.\n")
  334. #define FUNC_NAME s_scm_call_with_unblocked_asyncs
  335. {
  336. SCM ans;
  337. if (SCM_I_CURRENT_THREAD->block_asyncs == 0)
  338. SCM_MISC_ERROR ("asyncs already unblocked", SCM_EOL);
  339. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  340. scm_dynwind_unblock_asyncs ();
  341. ans = scm_call_0 (proc);
  342. scm_dynwind_end ();
  343. return ans;
  344. }
  345. #undef FUNC_NAME
  346. void *
  347. scm_c_call_with_unblocked_asyncs (void *(*proc) (void *data), void *data)
  348. {
  349. void* ans;
  350. if (SCM_I_CURRENT_THREAD->block_asyncs == 0)
  351. scm_misc_error ("scm_c_call_with_unblocked_asyncs",
  352. "asyncs already unblocked", SCM_EOL);
  353. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  354. scm_dynwind_unblock_asyncs ();
  355. ans = proc (data);
  356. scm_dynwind_end ();
  357. return ans;
  358. }
  359. /* These are function variants of the same-named macros (uppercase) for use
  360. outside of libguile. This is so that `SCM_I_CURRENT_THREAD', which may
  361. reside in TLS, is not accessed from outside of libguile. It thus allows
  362. libguile to be built with the "local-dynamic" TLS model. */
  363. void
  364. scm_critical_section_start (void)
  365. {
  366. SCM_CRITICAL_SECTION_START;
  367. }
  368. void
  369. scm_critical_section_end (void)
  370. {
  371. SCM_CRITICAL_SECTION_END;
  372. }
  373. void
  374. scm_init_async ()
  375. {
  376. tc16_async = scm_make_smob_type ("async", 0);
  377. #include "libguile/async.x"
  378. }
  379. /*
  380. Local Variables:
  381. c-file-style: "gnu"
  382. End:
  383. */