coop-threads.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include "libguile/validate.h"
  42. #include "libguile/coop-threads.h"
  43. #include "libguile/root.h"
  44. #include "libguile/strings.h"
  45. /* A counter of the current number of threads */
  46. size_t scm_thread_count = 0;
  47. /* This is included rather than compiled separately in order
  48. to simplify the configuration mechanism. */
  49. #include "libguile/coop.c"
  50. /* A count-down counter used to determine when to switch
  51. contexts */
  52. size_t scm_switch_counter = SCM_THREAD_SWITCH_COUNT;
  53. coop_m scm_critical_section_mutex;
  54. void
  55. scm_threads_init (SCM_STACKITEM *i)
  56. {
  57. coop_init();
  58. scm_thread_count = 1;
  59. #ifndef GUILE_PTHREAD_COMPAT
  60. coop_global_main.sto = i;
  61. #endif
  62. coop_global_main.base = i;
  63. coop_global_curr = &coop_global_main;
  64. coop_all_qput (&coop_global_allq, coop_global_curr);
  65. coop_mutex_init (&scm_critical_section_mutex);
  66. coop_global_main.data = 0; /* Initialized in init.c */
  67. }
  68. void
  69. scm_threads_mark_stacks (void)
  70. {
  71. coop_t *thread;
  72. for (thread = coop_global_allq.t.all_next;
  73. thread != NULL; thread = thread->all_next)
  74. {
  75. if (thread == coop_global_curr)
  76. {
  77. /* Active thread */
  78. /* stack_len is long rather than sizet in order to guarantee
  79. that &stack_len is long aligned */
  80. #ifdef STACK_GROWS_UP
  81. long stack_len = ((SCM_STACKITEM *) (&thread) -
  82. (SCM_STACKITEM *) thread->base);
  83. /* Protect from the C stack. This must be the first marking
  84. * done because it provides information about what objects
  85. * are "in-use" by the C code. "in-use" objects are those
  86. * for which the values from SCM_LENGTH and SCM_CHARS must remain
  87. * usable. This requirement is stricter than a liveness
  88. * requirement -- in particular, it constrains the implementation
  89. * of scm_resizuve.
  90. */
  91. SCM_FLUSH_REGISTER_WINDOWS;
  92. /* This assumes that all registers are saved into the jmp_buf */
  93. setjmp (scm_save_regs_gc_mark);
  94. scm_mark_locations ((SCM_STACKITEM *) scm_save_regs_gc_mark,
  95. ((scm_sizet) sizeof scm_save_regs_gc_mark
  96. / sizeof (SCM_STACKITEM)));
  97. scm_mark_locations (((size_t) thread->base,
  98. (sizet) stack_len));
  99. #else
  100. long stack_len = ((SCM_STACKITEM *) thread->base -
  101. (SCM_STACKITEM *) (&thread));
  102. /* Protect from the C stack. This must be the first marking
  103. * done because it provides information about what objects
  104. * are "in-use" by the C code. "in-use" objects are those
  105. * for which the values from SCM_LENGTH and SCM_CHARS must remain
  106. * usable. This requirement is stricter than a liveness
  107. * requirement -- in particular, it constrains the implementation
  108. * of scm_resizuve.
  109. */
  110. SCM_FLUSH_REGISTER_WINDOWS;
  111. /* This assumes that all registers are saved into the jmp_buf */
  112. setjmp (scm_save_regs_gc_mark);
  113. scm_mark_locations ((SCM_STACKITEM *) scm_save_regs_gc_mark,
  114. ((scm_sizet) sizeof scm_save_regs_gc_mark
  115. / sizeof (SCM_STACKITEM)));
  116. scm_mark_locations ((SCM_STACKITEM *) &thread,
  117. stack_len);
  118. #endif
  119. }
  120. else
  121. {
  122. /* Suspended thread */
  123. #ifdef STACK_GROWS_UP
  124. long stack_len = ((SCM_STACKITEM *) (thread->sp) -
  125. (SCM_STACKITEM *) thread->base);
  126. scm_mark_locations ((size_t)thread->base,
  127. (sizet) stack_len);
  128. #else
  129. long stack_len = ((SCM_STACKITEM *) thread->base -
  130. (SCM_STACKITEM *) (thread->sp));
  131. /* Registers are already on the stack. No need to mark. */
  132. scm_mark_locations ((SCM_STACKITEM *) (size_t)thread->sp,
  133. stack_len);
  134. #endif
  135. }
  136. /* Mark this thread's root */
  137. scm_gc_mark (((scm_root_state *) thread->data) -> handle);
  138. }
  139. }
  140. /* NOTE: There are TWO mechanisms for starting a thread: The first one
  141. is used when spawning a thread from Scheme, while the second one is
  142. used from C.
  143. It might be argued that the first should be implemented in terms of
  144. the second. The reason it isn't is that that would require an
  145. extra unnecessary malloc (the thread_args structure). By providing
  146. one pair of extra functions (c_launch_thread, scm_spawn_thread) the
  147. Scheme threads are started more efficiently. */
  148. /* This is the first thread spawning mechanism: threads from Scheme */
  149. typedef struct scheme_launch_data {
  150. SCM rootcont;
  151. SCM body;
  152. SCM handler;
  153. } scheme_launch_data;
  154. extern SCM scm_apply (SCM, SCM, SCM);
  155. static SCM
  156. scheme_body_bootstrip (scheme_launch_data* data)
  157. {
  158. /* First save the new root continuation */
  159. data->rootcont = scm_root->rootcont;
  160. return scm_apply (data->body, SCM_EOL, SCM_EOL);
  161. }
  162. static SCM
  163. scheme_handler_bootstrip (scheme_launch_data* data, SCM tag, SCM throw_args)
  164. {
  165. scm_root->rootcont = data->rootcont;
  166. return scm_apply (data->handler, scm_cons (tag, throw_args), SCM_EOL);
  167. }
  168. static void
  169. scheme_launch_thread (void *p)
  170. {
  171. /* The thread object will be GC protected by being a member of the
  172. list given as argument to launch_thread. It will be marked
  173. during the conservative sweep of the stack. */
  174. register SCM argl = (SCM) p;
  175. SCM thread = SCM_CAR (argl);
  176. scheme_launch_data data;
  177. data.rootcont = SCM_BOOL_F;
  178. data.body = SCM_CADR (argl);
  179. data.handler = SCM_CADDR (argl);
  180. scm_internal_cwdr ((scm_catch_body_t) scheme_body_bootstrip,
  181. &data,
  182. (scm_catch_handler_t) scheme_handler_bootstrip,
  183. &data,
  184. (SCM_STACKITEM *) &thread);
  185. SCM_SET_CELL_WORD_1 (thread, 0);
  186. scm_thread_count--;
  187. SCM_DEFER_INTS;
  188. }
  189. SCM
  190. scm_call_with_new_thread (SCM argl)
  191. {
  192. SCM thread;
  193. /* Check arguments. */
  194. {
  195. register SCM args = argl;
  196. SCM thunk, handler;
  197. SCM_ASSERT (SCM_NIMP (args),
  198. scm_makfrom0str (s_call_with_new_thread),
  199. SCM_WNA, NULL);
  200. thunk = SCM_CAR (args);
  201. SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (thunk)),
  202. thunk,
  203. SCM_ARG1,
  204. s_call_with_new_thread);
  205. args = SCM_CDR (args);
  206. SCM_ASSERT (SCM_NIMP (args),
  207. scm_makfrom0str (s_call_with_new_thread),
  208. SCM_WNA, NULL);
  209. handler = SCM_CAR (args);
  210. SCM_ASSERT (SCM_NFALSEP (scm_procedure_p (handler)),
  211. handler,
  212. SCM_ARG2,
  213. s_call_with_new_thread);
  214. SCM_ASSERT (SCM_NULLP (SCM_CDR (args)),
  215. scm_makfrom0str (s_call_with_new_thread),
  216. SCM_WNA, NULL);
  217. }
  218. /* Make new thread. */
  219. {
  220. coop_t *t;
  221. SCM root, old_winds;
  222. /* Unwind wind chain. */
  223. old_winds = scm_dynwinds;
  224. scm_dowinds (SCM_EOL, scm_ilength (scm_root->dynwinds));
  225. /* Allocate thread locals. */
  226. root = scm_make_root (scm_root->handle);
  227. /* Make thread. */
  228. SCM_NEWCELL (thread);
  229. SCM_DEFER_INTS;
  230. SCM_SETCAR (thread, scm_tc16_thread);
  231. argl = scm_cons (thread, argl);
  232. /* Note that we couldn't pass a pointer to argl as data since the
  233. argl variable may not exist in memory when the thread starts. */
  234. t = coop_create (scheme_launch_thread, (void *) argl);
  235. t->data = SCM_ROOT_STATE (root);
  236. SCM_SET_CELL_WORD_1 (thread, (scm_bits_t) t);
  237. scm_thread_count++;
  238. /* Note that the following statement also could cause coop_yield.*/
  239. SCM_ALLOW_INTS;
  240. /* We're now ready for the thread to begin. */
  241. coop_yield();
  242. /* Return to old dynamic context. */
  243. scm_dowinds (old_winds, - scm_ilength (old_winds));
  244. }
  245. return thread;
  246. }
  247. /* This is the second thread spawning mechanism: threads from C */
  248. typedef struct c_launch_data {
  249. union {
  250. SCM thread;
  251. SCM rootcont;
  252. } u;
  253. scm_catch_body_t body;
  254. void *body_data;
  255. scm_catch_handler_t handler;
  256. void *handler_data;
  257. } c_launch_data;
  258. static SCM
  259. c_body_bootstrip (c_launch_data* data)
  260. {
  261. /* First save the new root continuation */
  262. data->u.rootcont = scm_root->rootcont;
  263. return (data->body) (data->body_data);
  264. }
  265. static SCM
  266. c_handler_bootstrip (c_launch_data* data, SCM tag, SCM throw_args)
  267. {
  268. scm_root->rootcont = data->u.rootcont;
  269. return (data->handler) (data->handler_data, tag, throw_args);
  270. }
  271. static void
  272. c_launch_thread (void *p)
  273. {
  274. register c_launch_data *data = (c_launch_data *) p;
  275. /* The thread object will be GC protected by being on this stack */
  276. SCM thread = data->u.thread;
  277. /* We must use the address of `thread', otherwise the compiler will
  278. optimize it away. This is OK since the longest SCM_STACKITEM
  279. also is a long. */
  280. scm_internal_cwdr ((scm_catch_body_t) c_body_bootstrip,
  281. data,
  282. (scm_catch_handler_t) c_handler_bootstrip,
  283. data,
  284. (SCM_STACKITEM *) &thread);
  285. scm_thread_count--;
  286. scm_must_free ((char *) data);
  287. }
  288. SCM
  289. scm_spawn_thread (scm_catch_body_t body, void *body_data,
  290. scm_catch_handler_t handler, void *handler_data)
  291. {
  292. SCM thread;
  293. coop_t *t;
  294. SCM root, old_winds;
  295. c_launch_data *data = (c_launch_data *) scm_must_malloc (sizeof (*data),
  296. "scm_spawn_thread");
  297. /* Unwind wind chain. */
  298. old_winds = scm_dynwinds;
  299. scm_dowinds (SCM_EOL, scm_ilength (scm_root->dynwinds));
  300. /* Allocate thread locals. */
  301. root = scm_make_root (scm_root->handle);
  302. /* Make thread. */
  303. SCM_NEWCELL (thread);
  304. SCM_DEFER_INTS;
  305. SCM_SETCAR (thread, scm_tc16_thread);
  306. data->u.thread = thread;
  307. data->body = body;
  308. data->body_data = body_data;
  309. data->handler = handler;
  310. data->handler_data = handler_data;
  311. t = coop_create (c_launch_thread, (void *) data);
  312. t->data = SCM_ROOT_STATE (root);
  313. SCM_SET_CELL_WORD_1 (thread, (scm_bits_t) t);
  314. scm_thread_count++;
  315. /* Note that the following statement also could cause coop_yield.*/
  316. SCM_ALLOW_INTS;
  317. /* We're now ready for the thread to begin. */
  318. coop_yield();
  319. /* Return to old dynamic context. */
  320. scm_dowinds (old_winds, - scm_ilength (old_winds));
  321. return thread;
  322. }
  323. SCM
  324. scm_join_thread (SCM t)
  325. #define FUNC_NAME s_join_thread
  326. {
  327. SCM_VALIDATE_THREAD (1,t);
  328. coop_join (SCM_THREAD_DATA (t));
  329. return SCM_BOOL_T;
  330. }
  331. #undef FUNC_NAME
  332. SCM
  333. scm_yield (void)
  334. {
  335. /* Yield early */
  336. scm_switch_counter = SCM_THREAD_SWITCH_COUNT;
  337. coop_yield();
  338. return SCM_BOOL_T;
  339. }
  340. SCM
  341. scm_single_thread_p (void)
  342. {
  343. return (coop_global_runq.tail == &coop_global_runq.t
  344. ? SCM_BOOL_T
  345. : SCM_BOOL_F);
  346. }
  347. SCM
  348. scm_make_mutex (void)
  349. {
  350. SCM m;
  351. coop_m *data = (coop_m *) scm_must_malloc (sizeof (coop_m), "mutex");
  352. SCM_NEWSMOB (m, scm_tc16_mutex, (scm_bits_t) data);
  353. coop_mutex_init (data);
  354. return m;
  355. }
  356. SCM
  357. scm_lock_mutex (SCM m)
  358. {
  359. SCM_ASSERT (SCM_MUTEXP (m), m, SCM_ARG1, s_lock_mutex);
  360. coop_mutex_lock (SCM_MUTEX_DATA (m));
  361. return SCM_BOOL_T;
  362. }
  363. SCM
  364. scm_unlock_mutex (SCM m)
  365. {
  366. SCM_ASSERT (SCM_MUTEXP (m), m, SCM_ARG1, s_unlock_mutex);
  367. coop_mutex_unlock(SCM_MUTEX_DATA (m));
  368. /* Yield early */
  369. scm_switch_counter = SCM_THREAD_SWITCH_COUNT;
  370. coop_yield();
  371. return SCM_BOOL_T;
  372. }
  373. SCM
  374. scm_make_condition_variable (void)
  375. {
  376. SCM c;
  377. coop_c *data = (coop_c *) scm_must_malloc (sizeof (coop_c), "condvar");
  378. SCM_NEWSMOB (c, scm_tc16_condvar, (scm_bits_t) data);
  379. coop_condition_variable_init (SCM_CONDVAR_DATA (c));
  380. return c;
  381. }
  382. SCM
  383. scm_wait_condition_variable (SCM c, SCM m)
  384. {
  385. SCM_ASSERT (SCM_CONDVARP (c),
  386. c,
  387. SCM_ARG1,
  388. s_wait_condition_variable);
  389. SCM_ASSERT (SCM_MUTEXP (m),
  390. m,
  391. SCM_ARG2,
  392. s_wait_condition_variable);
  393. coop_condition_variable_wait_mutex (SCM_CONDVAR_DATA (c),
  394. SCM_MUTEX_DATA (m));
  395. return SCM_BOOL_T;
  396. }
  397. SCM
  398. scm_signal_condition_variable (SCM c)
  399. {
  400. SCM_ASSERT (SCM_CONDVARP (c),
  401. c,
  402. SCM_ARG1,
  403. s_signal_condition_variable);
  404. coop_condition_variable_signal (SCM_CONDVAR_DATA (c));
  405. return SCM_BOOL_T;
  406. }
  407. /*
  408. Local Variables:
  409. c-file-style: "gnu"
  410. End:
  411. */