async.c 12 KB

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