coop-threads.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001 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., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 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. /* A counter of the current number of threads */
  45. size_t scm_thread_count = 0;
  46. /* This is included rather than compiled separately in order
  47. to simplify the configuration mechanism. */
  48. #include "libguile/coop.c"
  49. /* A count-down counter used to determine when to switch
  50. contexts */
  51. size_t scm_switch_counter = SCM_THREAD_SWITCH_COUNT;
  52. coop_m scm_critical_section_mutex;
  53. void
  54. scm_threads_init (SCM_STACKITEM *i)
  55. {
  56. coop_init();
  57. scm_thread_count = 1;
  58. #ifndef GUILE_PTHREAD_COMPAT
  59. coop_global_main.sto = i;
  60. #endif
  61. coop_global_main.base = i;
  62. coop_global_curr = &coop_global_main;
  63. coop_all_qput (&coop_global_allq, coop_global_curr);
  64. coop_mutex_init (&scm_critical_section_mutex);
  65. coop_global_main.data = 0; /* Initialized in init.c */
  66. }
  67. void
  68. scm_threads_mark_stacks (void)
  69. {
  70. coop_t *thread;
  71. for (thread = coop_global_allq.t.all_next;
  72. thread != NULL; thread = thread->all_next)
  73. {
  74. if (thread == coop_global_curr)
  75. {
  76. /* Active thread */
  77. /* stack_len is long rather than sizet in order to guarantee
  78. that &stack_len is long aligned */
  79. #ifdef STACK_GROWS_UP
  80. long stack_len = ((SCM_STACKITEM *) (&thread) -
  81. (SCM_STACKITEM *) thread->base);
  82. /* Protect from the C stack. This must be the first marking
  83. * done because it provides information about what objects
  84. * are "in-use" by the C code. "in-use" objects are those
  85. * for which the information about length and base address must
  86. * remain usable. This requirement is stricter than a liveness
  87. * requirement -- in particular, it constrains the implementation
  88. * of scm_resizuve.
  89. */
  90. SCM_FLUSH_REGISTER_WINDOWS;
  91. /* This assumes that all registers are saved into the jmp_buf */
  92. setjmp (scm_save_regs_gc_mark);
  93. scm_mark_locations ((SCM_STACKITEM *) scm_save_regs_gc_mark,
  94. ((size_t) sizeof scm_save_regs_gc_mark
  95. / sizeof (SCM_STACKITEM)));
  96. scm_mark_locations (((size_t) thread->base,
  97. (sizet) stack_len));
  98. #else
  99. long stack_len = ((SCM_STACKITEM *) thread->base -
  100. (SCM_STACKITEM *) (&thread));
  101. /* Protect from the C stack. This must be the first marking
  102. * done because it provides information about what objects
  103. * are "in-use" by the C code. "in-use" objects are those
  104. * for which the information about length and base address must
  105. * remain usable. This requirement is stricter than a liveness
  106. * requirement -- in particular, it constrains the implementation
  107. * of scm_resizuve.
  108. */
  109. SCM_FLUSH_REGISTER_WINDOWS;
  110. /* This assumes that all registers are saved into the jmp_buf */
  111. setjmp (scm_save_regs_gc_mark);
  112. scm_mark_locations ((SCM_STACKITEM *) scm_save_regs_gc_mark,
  113. ((size_t) sizeof scm_save_regs_gc_mark
  114. / sizeof (SCM_STACKITEM)));
  115. scm_mark_locations ((SCM_STACKITEM *) &thread,
  116. stack_len);
  117. #endif
  118. }
  119. else
  120. {
  121. /* Suspended thread */
  122. #ifdef STACK_GROWS_UP
  123. long stack_len = ((SCM_STACKITEM *) (thread->sp) -
  124. (SCM_STACKITEM *) thread->base);
  125. scm_mark_locations ((size_t)thread->base,
  126. (sizet) stack_len);
  127. #else
  128. long stack_len = ((SCM_STACKITEM *) thread->base -
  129. (SCM_STACKITEM *) (thread->sp));
  130. /* Registers are already on the stack. No need to mark. */
  131. scm_mark_locations ((SCM_STACKITEM *) (size_t)thread->sp,
  132. stack_len);
  133. #endif
  134. }
  135. /* Mark this thread's root */
  136. scm_gc_mark (((scm_root_state *) thread->data) -> handle);
  137. }
  138. }
  139. /* NOTE: There are TWO mechanisms for starting a thread: The first one
  140. is used when spawning a thread from Scheme, while the second one is
  141. used from C.
  142. It might be argued that the first should be implemented in terms of
  143. the second. The reason it isn't is that that would require an
  144. extra unnecessary malloc (the thread_args structure). By providing
  145. one pair of extra functions (c_launch_thread, scm_spawn_thread) the
  146. Scheme threads are started more efficiently. */
  147. /* This is the first thread spawning mechanism: threads from Scheme */
  148. typedef struct scheme_launch_data {
  149. SCM rootcont;
  150. SCM body;
  151. SCM handler;
  152. } scheme_launch_data;
  153. static SCM
  154. scheme_body_bootstrip (scheme_launch_data* data)
  155. {
  156. /* First save the new root continuation */
  157. data->rootcont = scm_root->rootcont;
  158. return scm_call_0 (data->body);
  159. }
  160. static SCM
  161. scheme_handler_bootstrip (scheme_launch_data* data, SCM tag, SCM throw_args)
  162. {
  163. scm_root->rootcont = data->rootcont;
  164. return scm_apply_1 (data->handler, tag, throw_args);
  165. }
  166. static void
  167. scheme_launch_thread (void *p)
  168. {
  169. /* The thread object will be GC protected by being a member of the
  170. list given as argument to launch_thread. It will be marked
  171. during the conservative sweep of the stack. */
  172. register SCM argl = (SCM) p;
  173. SCM thread = SCM_CAR (argl);
  174. scheme_launch_data data;
  175. data.rootcont = SCM_BOOL_F;
  176. data.body = SCM_CADR (argl);
  177. data.handler = SCM_CADDR (argl);
  178. scm_internal_cwdr ((scm_t_catch_body) scheme_body_bootstrip,
  179. &data,
  180. (scm_t_catch_handler) scheme_handler_bootstrip,
  181. &data,
  182. (SCM_STACKITEM *) &thread);
  183. SCM_SET_CELL_WORD_1 (thread, 0);
  184. scm_thread_count--;
  185. SCM_DEFER_INTS;
  186. }
  187. SCM
  188. scm_call_with_new_thread (SCM argl)
  189. #define FUNC_NAME s_call_with_new_thread
  190. {
  191. SCM thread;
  192. /* Check arguments. */
  193. {
  194. register SCM args = argl;
  195. SCM thunk, handler;
  196. if (!SCM_CONSP (args))
  197. SCM_WRONG_NUM_ARGS ();
  198. thunk = SCM_CAR (args);
  199. SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (thunk)),
  200. thunk,
  201. SCM_ARG1,
  202. s_call_with_new_thread);
  203. args = SCM_CDR (args);
  204. if (!SCM_CONSP (args))
  205. SCM_WRONG_NUM_ARGS ();
  206. handler = SCM_CAR (args);
  207. SCM_ASSERT (SCM_NFALSEP (scm_procedure_p (handler)),
  208. handler,
  209. SCM_ARG2,
  210. s_call_with_new_thread);
  211. if (!SCM_NULLP (SCM_CDR (args)))
  212. SCM_WRONG_NUM_ARGS ();
  213. }
  214. /* Make new thread. */
  215. {
  216. coop_t *t;
  217. SCM root, old_winds;
  218. /* Unwind wind chain. */
  219. old_winds = scm_dynwinds;
  220. scm_dowinds (SCM_EOL, scm_ilength (scm_root->dynwinds));
  221. /* Allocate thread locals. */
  222. root = scm_make_root (scm_root->handle);
  223. /* Make thread. */
  224. SCM_NEWCELL (thread);
  225. SCM_DEFER_INTS;
  226. SCM_SETCAR (thread, scm_tc16_thread);
  227. argl = scm_cons (thread, argl);
  228. /* Note that we couldn't pass a pointer to argl as data since the
  229. argl variable may not exist in memory when the thread starts. */
  230. t = coop_create (scheme_launch_thread, (void *) argl);
  231. t->data = SCM_ROOT_STATE (root);
  232. SCM_SET_CELL_WORD_1 (thread, (scm_t_bits) t);
  233. scm_thread_count++;
  234. /* Note that the following statement also could cause coop_yield.*/
  235. SCM_ALLOW_INTS;
  236. /* We're now ready for the thread to begin. */
  237. coop_yield();
  238. /* Return to old dynamic context. */
  239. scm_dowinds (old_winds, - scm_ilength (old_winds));
  240. }
  241. return thread;
  242. }
  243. #undef FUNC_NAME
  244. /* This is the second thread spawning mechanism: threads from C */
  245. typedef struct c_launch_data {
  246. union {
  247. SCM thread;
  248. SCM rootcont;
  249. } u;
  250. scm_t_catch_body body;
  251. void *body_data;
  252. scm_t_catch_handler handler;
  253. void *handler_data;
  254. } c_launch_data;
  255. static SCM
  256. c_body_bootstrip (c_launch_data* data)
  257. {
  258. /* First save the new root continuation */
  259. data->u.rootcont = scm_root->rootcont;
  260. return (data->body) (data->body_data);
  261. }
  262. static SCM
  263. c_handler_bootstrip (c_launch_data* data, SCM tag, SCM throw_args)
  264. {
  265. scm_root->rootcont = data->u.rootcont;
  266. return (data->handler) (data->handler_data, tag, throw_args);
  267. }
  268. static void
  269. c_launch_thread (void *p)
  270. {
  271. register c_launch_data *data = (c_launch_data *) p;
  272. /* The thread object will be GC protected by being on this stack */
  273. SCM thread = data->u.thread;
  274. /* We must use the address of `thread', otherwise the compiler will
  275. optimize it away. This is OK since the longest SCM_STACKITEM
  276. also is a long. */
  277. scm_internal_cwdr ((scm_t_catch_body) c_body_bootstrip,
  278. data,
  279. (scm_t_catch_handler) c_handler_bootstrip,
  280. data,
  281. (SCM_STACKITEM *) &thread);
  282. scm_thread_count--;
  283. scm_must_free ((char *) data);
  284. }
  285. SCM
  286. scm_spawn_thread (scm_t_catch_body body, void *body_data,
  287. scm_t_catch_handler handler, void *handler_data)
  288. {
  289. SCM thread;
  290. coop_t *t;
  291. SCM root, old_winds;
  292. c_launch_data *data = (c_launch_data *) scm_must_malloc (sizeof (*data),
  293. "scm_spawn_thread");
  294. /* Unwind wind chain. */
  295. old_winds = scm_dynwinds;
  296. scm_dowinds (SCM_EOL, scm_ilength (scm_root->dynwinds));
  297. /* Allocate thread locals. */
  298. root = scm_make_root (scm_root->handle);
  299. /* Make thread. */
  300. SCM_NEWCELL (thread);
  301. SCM_DEFER_INTS;
  302. SCM_SETCAR (thread, scm_tc16_thread);
  303. data->u.thread = thread;
  304. data->body = body;
  305. data->body_data = body_data;
  306. data->handler = handler;
  307. data->handler_data = handler_data;
  308. t = coop_create (c_launch_thread, (void *) data);
  309. t->data = SCM_ROOT_STATE (root);
  310. SCM_SET_CELL_WORD_1 (thread, (scm_t_bits) t);
  311. scm_thread_count++;
  312. /* Note that the following statement also could cause coop_yield.*/
  313. SCM_ALLOW_INTS;
  314. /* We're now ready for the thread to begin. */
  315. coop_yield();
  316. /* Return to old dynamic context. */
  317. scm_dowinds (old_winds, - scm_ilength (old_winds));
  318. return thread;
  319. }
  320. SCM
  321. scm_join_thread (SCM thread)
  322. #define FUNC_NAME s_join_thread
  323. {
  324. coop_t *thread_data;
  325. SCM_VALIDATE_THREAD (1, thread);
  326. /* Dirk:FIXME:: SCM_THREAD_DATA is a handle for a thread. It may be that a
  327. * certain thread implementation uses a value of 0 as a valid thread handle.
  328. * With the following code, this thread would always be considered finished.
  329. */
  330. /* Dirk:FIXME:: With preemptive threading, a thread may finish immediately
  331. * after SCM_THREAD_DATA is read. Thus, it must be guaranteed that the
  332. * handle remains valid until the thread-object is garbage collected, or
  333. * a mutex has to be used for reading and modifying SCM_THREAD_DATA.
  334. */
  335. thread_data = SCM_THREAD_DATA (thread);
  336. if (thread_data)
  337. /* The thread is still alive */
  338. coop_join (thread_data);
  339. return SCM_BOOL_T;
  340. }
  341. #undef FUNC_NAME
  342. SCM
  343. scm_yield (void)
  344. {
  345. /* Yield early */
  346. scm_switch_counter = SCM_THREAD_SWITCH_COUNT;
  347. coop_yield();
  348. return SCM_BOOL_T;
  349. }
  350. SCM
  351. scm_single_thread_p (void)
  352. {
  353. return (coop_global_runq.tail == &coop_global_runq.t
  354. ? SCM_BOOL_T
  355. : SCM_BOOL_F);
  356. }
  357. SCM
  358. scm_make_mutex (void)
  359. {
  360. SCM m;
  361. coop_m *data = (coop_m *) scm_must_malloc (sizeof (coop_m), "mutex");
  362. SCM_NEWSMOB (m, scm_tc16_mutex, (scm_t_bits) data);
  363. coop_mutex_init (data);
  364. return m;
  365. }
  366. SCM
  367. scm_lock_mutex (SCM m)
  368. {
  369. SCM_ASSERT (SCM_MUTEXP (m), m, SCM_ARG1, s_lock_mutex);
  370. coop_mutex_lock (SCM_MUTEX_DATA (m));
  371. return SCM_BOOL_T;
  372. }
  373. SCM
  374. scm_unlock_mutex (SCM m)
  375. {
  376. SCM_ASSERT (SCM_MUTEXP (m), m, SCM_ARG1, s_unlock_mutex);
  377. coop_mutex_unlock(SCM_MUTEX_DATA (m));
  378. /* Yield early */
  379. scm_switch_counter = SCM_THREAD_SWITCH_COUNT;
  380. coop_yield();
  381. return SCM_BOOL_T;
  382. }
  383. SCM
  384. scm_make_condition_variable (void)
  385. {
  386. SCM c;
  387. coop_c *data = (coop_c *) scm_must_malloc (sizeof (coop_c), "condvar");
  388. SCM_NEWSMOB (c, scm_tc16_condvar, (scm_t_bits) data);
  389. coop_condition_variable_init (SCM_CONDVAR_DATA (c));
  390. return c;
  391. }
  392. SCM
  393. scm_wait_condition_variable (SCM c, SCM m)
  394. {
  395. SCM_ASSERT (SCM_CONDVARP (c),
  396. c,
  397. SCM_ARG1,
  398. s_wait_condition_variable);
  399. SCM_ASSERT (SCM_MUTEXP (m),
  400. m,
  401. SCM_ARG2,
  402. s_wait_condition_variable);
  403. coop_condition_variable_wait_mutex (SCM_CONDVAR_DATA (c),
  404. SCM_MUTEX_DATA (m));
  405. return SCM_BOOL_T;
  406. }
  407. SCM
  408. scm_signal_condition_variable (SCM c)
  409. {
  410. SCM_ASSERT (SCM_CONDVARP (c),
  411. c,
  412. SCM_ARG1,
  413. s_signal_condition_variable);
  414. coop_condition_variable_signal (SCM_CONDVAR_DATA (c));
  415. return SCM_BOOL_T;
  416. }
  417. /*
  418. Local Variables:
  419. c-file-style: "gnu"
  420. End:
  421. */