coop-pthreads.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002, 2006 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
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library 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 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 02110-1301 USA
  16. */
  17. #include "libguile/_scm.h" /* config.h, _scm.h, __scm.h should be first */
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include <assert.h>
  21. #include <sys/time.h>
  22. #include "libguile/validate.h"
  23. #include "libguile/coop-pthreads.h"
  24. #include "libguile/root.h"
  25. #include "libguile/eval.h"
  26. #include "libguile/async.h"
  27. #include "libguile/ports.h"
  28. #include "libguile/gc.h"
  29. #undef DEBUG
  30. /*** Queues */
  31. static SCM
  32. make_queue ()
  33. {
  34. return scm_cons (SCM_EOL, SCM_EOL);
  35. }
  36. static void
  37. enqueue (SCM q, SCM t)
  38. {
  39. SCM c = scm_cons (t, SCM_EOL);
  40. if (scm_is_null (SCM_CAR (q)))
  41. SCM_SETCAR (q, c);
  42. else
  43. SCM_SETCDR (SCM_CDR (q), c);
  44. SCM_SETCDR (q, c);
  45. }
  46. static SCM
  47. dequeue (SCM q)
  48. {
  49. SCM c = SCM_CAR (q);
  50. if (scm_is_null (c))
  51. return SCM_BOOL_F;
  52. else
  53. {
  54. SCM_SETCAR (q, SCM_CDR (c));
  55. if (scm_is_null (SCM_CAR (q)))
  56. SCM_SETCDR (q, SCM_EOL);
  57. return SCM_CAR (c);
  58. }
  59. }
  60. /*** Threads */
  61. typedef struct scm_copt_thread {
  62. /* A condition variable for sleeping on.
  63. */
  64. pthread_cond_t sleep_cond;
  65. /* A link for waiting queues.
  66. */
  67. struct scm_copt_thread *next_waiting;
  68. scm_root_state *root;
  69. SCM handle;
  70. pthread_t pthread;
  71. SCM result;
  72. SCM joining_threads;
  73. /* For keeping track of the stack and registers. */
  74. SCM_STACKITEM *base;
  75. SCM_STACKITEM *top;
  76. jmp_buf regs;
  77. } scm_copt_thread;
  78. static SCM
  79. make_thread (SCM creation_protects)
  80. {
  81. SCM z;
  82. scm_copt_thread *t = scm_gc_malloc (sizeof(*t), "thread");
  83. z = scm_cell (scm_tc16_thread, (scm_t_bits)t);
  84. t->handle = z;
  85. t->result = creation_protects;
  86. t->base = NULL;
  87. t->joining_threads = make_queue ();
  88. pthread_cond_init (&t->sleep_cond, NULL);
  89. return z;
  90. }
  91. static void
  92. init_thread_creator (SCM thread, pthread_t th, scm_root_state *r)
  93. {
  94. scm_copt_thread *t = SCM_THREAD_DATA(thread);
  95. t->root = r;
  96. t->pthread = th;
  97. #ifdef DEBUG
  98. // fprintf (stderr, "%ld created %ld\n", pthread_self (), th);
  99. #endif
  100. }
  101. static void
  102. init_thread_creatant (SCM thread, SCM_STACKITEM *base)
  103. {
  104. scm_copt_thread *t = SCM_THREAD_DATA(thread);
  105. t->base = base;
  106. t->top = NULL;
  107. }
  108. static SCM
  109. thread_mark (SCM obj)
  110. {
  111. scm_copt_thread *t = SCM_THREAD_DATA (obj);
  112. scm_gc_mark (t->result);
  113. scm_gc_mark (t->joining_threads);
  114. return t->root->handle;
  115. }
  116. static int
  117. thread_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  118. {
  119. scm_copt_thread *t = SCM_THREAD_DATA (exp);
  120. scm_puts ("#<thread ", port);
  121. scm_uintprint ((scm_t_bits)t, 16, port);
  122. if (t->pthread != -1)
  123. {
  124. scm_putc (' ', port);
  125. scm_intprint (t->pthread, 10, port);
  126. }
  127. else
  128. scm_puts (" (exited)", port);
  129. scm_putc ('>', port);
  130. return 1;
  131. }
  132. static size_t
  133. thread_free (SCM obj)
  134. {
  135. scm_copt_thread *t = SCM_THREAD_DATA (obj);
  136. if (t->pthread != -1)
  137. abort ();
  138. scm_gc_free (t, sizeof (*t), "thread");
  139. return 0;
  140. }
  141. /*** Fair mutexes */
  142. /* POSIX mutexes are not necessarily fair but since we'd like to use a
  143. mutex for scheduling, we build a fair one on top of POSIX.
  144. */
  145. typedef struct fair_mutex {
  146. pthread_mutex_t lock;
  147. scm_copt_thread *owner;
  148. scm_copt_thread *next_waiting, *last_waiting;
  149. } fair_mutex;
  150. static void
  151. fair_mutex_init (fair_mutex *m)
  152. {
  153. pthread_mutex_init (&m->lock, NULL);
  154. m->owner = NULL;
  155. m->next_waiting = NULL;
  156. m->last_waiting = NULL;
  157. }
  158. static void
  159. fair_mutex_lock_1 (fair_mutex *m, scm_copt_thread *t)
  160. {
  161. if (m->owner == NULL)
  162. m->owner = t;
  163. else
  164. {
  165. t->next_waiting = NULL;
  166. if (m->last_waiting)
  167. m->last_waiting->next_waiting = t;
  168. else
  169. m->next_waiting = t;
  170. m->last_waiting = t;
  171. do
  172. {
  173. pthread_cond_wait (&t->sleep_cond, &m->lock);
  174. }
  175. while (m->owner != t);
  176. assert (m->next_waiting == t);
  177. m->next_waiting = t->next_waiting;
  178. if (m->next_waiting == NULL)
  179. m->last_waiting = NULL;
  180. }
  181. pthread_mutex_unlock (&m->lock);
  182. }
  183. static void
  184. fair_mutex_lock (fair_mutex *m, scm_copt_thread *t)
  185. {
  186. pthread_mutex_lock (&m->lock);
  187. fair_mutex_lock_1 (m, t);
  188. }
  189. static void
  190. fair_mutex_unlock_1 (fair_mutex *m)
  191. {
  192. scm_copt_thread *t;
  193. pthread_mutex_lock (&m->lock);
  194. // fprintf (stderr, "%ld unlocking\n", m->owner->pthread);
  195. if ((t = m->next_waiting) != NULL)
  196. {
  197. m->owner = t;
  198. pthread_cond_signal (&t->sleep_cond);
  199. }
  200. else
  201. m->owner = NULL;
  202. // fprintf (stderr, "%ld unlocked\n", pthread_self ());
  203. }
  204. static void
  205. fair_mutex_unlock (fair_mutex *m)
  206. {
  207. fair_mutex_unlock_1 (m);
  208. pthread_mutex_unlock (&m->lock);
  209. }
  210. /* Temporarily give up the mutex. This function makes sure that we
  211. are on the wait queue before starting the next thread. Otherwise
  212. the next thread might preempt us and we will have a hard time
  213. getting on the wait queue.
  214. */
  215. #if 0
  216. static void
  217. fair_mutex_yield (fair_mutex *m)
  218. {
  219. scm_copt_thread *self, *next;
  220. pthread_mutex_lock (&m->lock);
  221. /* get next thread
  222. */
  223. if ((next = m->next_waiting) == NULL)
  224. {
  225. /* No use giving it up. */
  226. pthread_mutex_unlock (&m->lock);
  227. return;
  228. }
  229. /* put us on queue
  230. */
  231. self = m->owner;
  232. self->next_waiting = NULL;
  233. if (m->last_waiting)
  234. m->last_waiting->next_waiting = self;
  235. else
  236. m->next_waiting = self;
  237. m->last_waiting = self;
  238. /* wake up next thread
  239. */
  240. m->owner = next;
  241. pthread_cond_signal (&next->sleep_cond);
  242. /* wait for mutex
  243. */
  244. do
  245. {
  246. pthread_cond_wait (&self->sleep_cond, &m->lock);
  247. }
  248. while (m->owner != self);
  249. assert (m->next_waiting == self);
  250. m->next_waiting = self->next_waiting;
  251. if (m->next_waiting == NULL)
  252. m->last_waiting = NULL;
  253. pthread_mutex_unlock (&m->lock);
  254. }
  255. #else
  256. static void
  257. fair_mutex_yield (fair_mutex *m)
  258. {
  259. scm_copt_thread *self = m->owner;
  260. fair_mutex_unlock_1 (m);
  261. fair_mutex_lock_1 (m, self);
  262. }
  263. #endif
  264. static void
  265. fair_cond_wait (pthread_cond_t *c, fair_mutex *m)
  266. {
  267. scm_copt_thread *t = m->owner;
  268. fair_mutex_unlock_1 (m);
  269. pthread_cond_wait (c, &m->lock);
  270. fair_mutex_lock_1 (m, t);
  271. }
  272. /* Return 1 when the mutex was signalled and 0 when not. */
  273. static int
  274. fair_cond_timedwait (pthread_cond_t *c, fair_mutex *m, scm_t_timespec *at)
  275. {
  276. int res;
  277. scm_copt_thread *t = m->owner;
  278. fair_mutex_unlock_1 (m);
  279. res = pthread_cond_timedwait (c, &m->lock, at); /* XXX - signals? */
  280. fair_mutex_lock_1 (m, t);
  281. return res == 0;
  282. }
  283. /*** Scheduling */
  284. /* When a thread wants to execute Guile functions, it locks the
  285. guile_mutex.
  286. */
  287. static fair_mutex guile_mutex;
  288. static SCM cur_thread;
  289. void *scm_i_copt_thread_data;
  290. void
  291. scm_i_copt_set_thread_data (void *data)
  292. {
  293. scm_copt_thread *t = SCM_THREAD_DATA (cur_thread);
  294. scm_i_copt_thread_data = data;
  295. t->root = (scm_root_state *)data;
  296. }
  297. static void
  298. resume (scm_copt_thread *t)
  299. {
  300. cur_thread = t->handle;
  301. scm_i_copt_thread_data = t->root;
  302. t->top = NULL;
  303. }
  304. static void
  305. enter_guile (scm_copt_thread *t)
  306. {
  307. fair_mutex_lock (&guile_mutex, t);
  308. resume (t);
  309. }
  310. static scm_copt_thread *
  311. suspend ()
  312. {
  313. SCM cur = cur_thread;
  314. scm_copt_thread *c = SCM_THREAD_DATA (cur);
  315. /* record top of stack for the GC */
  316. c->top = (SCM_STACKITEM *)&c;
  317. /* save registers. */
  318. SCM_FLUSH_REGISTER_WINDOWS;
  319. setjmp (c->regs);
  320. return c;
  321. }
  322. static scm_copt_thread *
  323. leave_guile ()
  324. {
  325. scm_copt_thread *c = suspend ();
  326. fair_mutex_unlock (&guile_mutex);
  327. return c;
  328. }
  329. int scm_i_switch_counter;
  330. SCM
  331. scm_yield ()
  332. {
  333. /* Testing guile_mutex.next_waiting without locking guile_mutex.lock
  334. is OK since the outcome is not critical. Even when it changes
  335. after the test, we do the right thing.
  336. */
  337. if (guile_mutex.next_waiting)
  338. {
  339. scm_copt_thread *t = suspend ();
  340. fair_mutex_yield (&guile_mutex);
  341. resume (t);
  342. }
  343. return SCM_BOOL_T;
  344. }
  345. /* Put the current thread to sleep until it is explicitely unblocked.
  346. */
  347. static void
  348. block ()
  349. {
  350. scm_copt_thread *t = suspend ();
  351. fair_cond_wait (&t->sleep_cond, &guile_mutex);
  352. resume (t);
  353. }
  354. /* Put the current thread to sleep until it is explicitely unblocked
  355. or until a signal arrives or until time AT (absolute time) is
  356. reached. Return 1 when it has been unblocked; 0 otherwise.
  357. */
  358. static int
  359. timed_block (scm_t_timespec *at)
  360. {
  361. int res;
  362. scm_copt_thread *t = suspend ();
  363. res = fair_cond_timedwait (&t->sleep_cond, &guile_mutex, at);
  364. resume (t);
  365. return res;
  366. }
  367. /* Unblock a sleeping thread.
  368. */
  369. static void
  370. unblock (scm_copt_thread *t)
  371. {
  372. pthread_cond_signal (&t->sleep_cond);
  373. }
  374. /*** Thread creation */
  375. static SCM all_threads;
  376. static int thread_count;
  377. typedef struct launch_data {
  378. SCM thread;
  379. SCM rootcont;
  380. scm_t_catch_body body;
  381. void *body_data;
  382. scm_t_catch_handler handler;
  383. void *handler_data;
  384. } launch_data;
  385. static SCM
  386. body_bootstrip (launch_data* data)
  387. {
  388. /* First save the new root continuation */
  389. data->rootcont = scm_root->rootcont;
  390. return (data->body) (data->body_data);
  391. // return scm_call_0 (data->body);
  392. }
  393. static SCM
  394. handler_bootstrip (launch_data* data, SCM tag, SCM throw_args)
  395. {
  396. scm_root->rootcont = data->rootcont;
  397. return (data->handler) (data->handler_data, tag, throw_args);
  398. // return scm_apply_1 (data->handler, tag, throw_args);
  399. }
  400. static void
  401. really_launch (SCM_STACKITEM *base, launch_data *data)
  402. {
  403. SCM thread = data->thread;
  404. scm_copt_thread *t = SCM_THREAD_DATA (thread);
  405. init_thread_creatant (thread, base);
  406. enter_guile (t);
  407. data->rootcont = SCM_BOOL_F;
  408. t->result =
  409. scm_internal_cwdr ((scm_t_catch_body) body_bootstrip,
  410. data,
  411. (scm_t_catch_handler) handler_bootstrip,
  412. data, base);
  413. free (data);
  414. pthread_detach (t->pthread);
  415. all_threads = scm_delq (thread, all_threads);
  416. t->pthread = -1;
  417. thread_count--;
  418. leave_guile ();
  419. }
  420. static void *
  421. launch_thread (void *p)
  422. {
  423. really_launch ((SCM_STACKITEM *)&p, (launch_data *)p);
  424. return NULL;
  425. }
  426. static SCM
  427. create_thread (scm_t_catch_body body, void *body_data,
  428. scm_t_catch_handler handler, void *handler_data,
  429. SCM protects)
  430. {
  431. SCM thread;
  432. /* Make new thread. The first thing the new thread will do is to
  433. lock guile_mutex. Thus, we can safely complete its
  434. initialization after creating it. While the new thread starts,
  435. all its data is protected via all_threads.
  436. */
  437. {
  438. pthread_t th;
  439. SCM root, old_winds;
  440. launch_data *data;
  441. /* Unwind wind chain. */
  442. old_winds = scm_dynwinds;
  443. scm_dowinds (SCM_EOL, scm_ilength (scm_root->dynwinds));
  444. /* Allocate thread locals. */
  445. root = scm_make_root (scm_root->handle);
  446. data = scm_malloc (sizeof (launch_data));
  447. /* Make thread. */
  448. thread = make_thread (protects);
  449. data->thread = thread;
  450. data->body = body;
  451. data->body_data = body_data;
  452. data->handler = handler;
  453. data->handler_data = handler_data;
  454. pthread_create (&th, NULL, launch_thread, (void *) data);
  455. init_thread_creator (thread, th, SCM_ROOT_STATE (root));
  456. all_threads = scm_cons (thread, all_threads);
  457. thread_count++;
  458. /* Return to old dynamic context. */
  459. scm_dowinds (old_winds, - scm_ilength (old_winds));
  460. }
  461. return thread;
  462. }
  463. SCM
  464. scm_call_with_new_thread (SCM argl)
  465. #define FUNC_NAME s_call_with_new_thread
  466. {
  467. SCM thunk, handler;
  468. /* Check arguments. */
  469. {
  470. register SCM args = argl;
  471. if (!scm_is_pair (args))
  472. SCM_WRONG_NUM_ARGS ();
  473. thunk = SCM_CAR (args);
  474. SCM_ASSERT (scm_is_true (scm_thunk_p (thunk)),
  475. thunk,
  476. SCM_ARG1,
  477. s_call_with_new_thread);
  478. args = SCM_CDR (args);
  479. if (!scm_is_pair (args))
  480. SCM_WRONG_NUM_ARGS ();
  481. handler = SCM_CAR (args);
  482. SCM_ASSERT (scm_is_true (scm_procedure_p (handler)),
  483. handler,
  484. SCM_ARG2,
  485. s_call_with_new_thread);
  486. if (!scm_is_null (SCM_CDR (args)))
  487. SCM_WRONG_NUM_ARGS ();
  488. }
  489. return create_thread ((scm_t_catch_body) scm_call_0, thunk,
  490. (scm_t_catch_handler) scm_apply_1, handler,
  491. argl);
  492. }
  493. #undef FUNC_NAME
  494. SCM
  495. scm_spawn_thread (scm_t_catch_body body, void *body_data,
  496. scm_t_catch_handler handler, void *handler_data)
  497. {
  498. return create_thread (body, body_data, handler, handler_data, SCM_BOOL_F);
  499. }
  500. /*** Mutexes */
  501. /* We implement our own mutex type since we want them to be 'fair', we
  502. want to do fancy things while waiting for them (like running
  503. asyncs) and we want to support waiting on many things at once.
  504. Also, we might add things that are nice for debugging.
  505. */
  506. typedef struct scm_copt_mutex {
  507. /* the thread currently owning the mutex, or SCM_BOOL_F. */
  508. SCM owner;
  509. /* how much the owner owns us. */
  510. int level;
  511. /* the threads waiting for this mutex. */
  512. SCM waiting;
  513. } scm_copt_mutex;
  514. static SCM
  515. mutex_mark (SCM mx)
  516. {
  517. scm_copt_mutex *m = SCM_MUTEX_DATA (mx);
  518. scm_gc_mark (m->owner);
  519. return m->waiting;
  520. }
  521. SCM
  522. scm_make_mutex ()
  523. {
  524. SCM mx = scm_make_smob (scm_tc16_mutex);
  525. scm_copt_mutex *m = SCM_MUTEX_DATA (mx);
  526. m->owner = SCM_BOOL_F;
  527. m->level = 0;
  528. m->waiting = make_queue ();
  529. return mx;
  530. }
  531. SCM
  532. scm_lock_mutex (SCM mx)
  533. #define FUNC_NAME s_lock_mutex
  534. {
  535. scm_copt_mutex *m;
  536. SCM_ASSERT (SCM_MUTEXP (mx), mx, SCM_ARG1, FUNC_NAME);
  537. m = SCM_MUTEX_DATA (mx);
  538. if (m->owner == SCM_BOOL_F)
  539. m->owner = cur_thread;
  540. else if (m->owner == cur_thread)
  541. m->level++;
  542. else
  543. {
  544. while (m->owner != cur_thread)
  545. {
  546. enqueue (m->waiting, cur_thread);
  547. block ();
  548. SCM_ASYNC_TICK;
  549. }
  550. }
  551. return SCM_BOOL_T;
  552. }
  553. #undef FUNC_NAME
  554. SCM
  555. scm_try_mutex (SCM mx)
  556. #define FUNC_NAME s_try_mutex
  557. {
  558. scm_copt_mutex *m;
  559. SCM_ASSERT (SCM_MUTEXP (mx), mx, SCM_ARG1, FUNC_NAME);
  560. m = SCM_MUTEX_DATA (mx);
  561. if (m->owner == SCM_BOOL_F)
  562. m->owner = cur_thread;
  563. else if (m->owner == cur_thread)
  564. m->level++;
  565. else
  566. return SCM_BOOL_F;
  567. return SCM_BOOL_T;
  568. }
  569. #undef FUNC_NAME
  570. SCM
  571. scm_unlock_mutex (SCM mx)
  572. #define FUNC_NAME s_unlock_mutex
  573. {
  574. scm_copt_mutex *m;
  575. SCM_ASSERT (SCM_MUTEXP (mx), mx, SCM_ARG1, FUNC_NAME);
  576. m = SCM_MUTEX_DATA (mx);
  577. if (m->owner != cur_thread)
  578. {
  579. if (m->owner == SCM_BOOL_F)
  580. SCM_MISC_ERROR ("mutex not locked", SCM_EOL);
  581. else
  582. SCM_MISC_ERROR ("mutex not locked by this thread", SCM_EOL);
  583. }
  584. else if (m->level > 0)
  585. m->level--;
  586. else
  587. {
  588. SCM next = dequeue (m->waiting);
  589. if (scm_is_true (next))
  590. {
  591. m->owner = next;
  592. unblock (SCM_THREAD_DATA (next));
  593. scm_yield ();
  594. }
  595. else
  596. m->owner = SCM_BOOL_F;
  597. }
  598. return SCM_BOOL_T;
  599. }
  600. #undef FUNC_NAME
  601. /*** Condition variables */
  602. /* Like mutexes, we implement our own condition variables using the
  603. primitives above.
  604. */
  605. /* yeah, we don't need a structure for this, but more things (like a
  606. name) will likely follow... */
  607. typedef struct scm_copt_cond {
  608. /* the threads waiting for this condition. */
  609. SCM waiting;
  610. } scm_copt_cond;
  611. static SCM
  612. cond_mark (SCM cv)
  613. {
  614. scm_copt_cond *c = SCM_CONDVAR_DATA (cv);
  615. return c->waiting;
  616. }
  617. SCM
  618. scm_make_condition_variable (void)
  619. {
  620. SCM cv = scm_make_smob (scm_tc16_condvar);
  621. scm_copt_cond *c = SCM_CONDVAR_DATA (cv);
  622. c->waiting = make_queue ();
  623. return cv;
  624. }
  625. SCM
  626. scm_timed_wait_condition_variable (SCM cv, SCM mx, SCM t)
  627. #define FUNC_NAME s_wait_condition_variable
  628. {
  629. scm_copt_cond *c;
  630. scm_t_timespec waittime;
  631. int res;
  632. SCM_ASSERT (SCM_CONDVARP (cv),
  633. cv,
  634. SCM_ARG1,
  635. s_wait_condition_variable);
  636. SCM_ASSERT (SCM_MUTEXP (mx),
  637. mx,
  638. SCM_ARG2,
  639. s_wait_condition_variable);
  640. if (!SCM_UNBNDP (t))
  641. {
  642. if (scm_is_pair (t))
  643. {
  644. SCM_VALIDATE_UINT_COPY (3, SCM_CAR(t), waittime.tv_sec);
  645. SCM_VALIDATE_UINT_COPY (3, SCM_CDR(t), waittime.tv_nsec);
  646. waittime.tv_nsec *= 1000;
  647. }
  648. else
  649. {
  650. SCM_VALIDATE_UINT_COPY (3, t, waittime.tv_sec);
  651. waittime.tv_nsec = 0;
  652. }
  653. }
  654. c = SCM_CONDVAR_DATA (cv);
  655. enqueue (c->waiting, cur_thread);
  656. scm_unlock_mutex (mx);
  657. if (SCM_UNBNDP (t))
  658. {
  659. block ();
  660. res = 1;
  661. }
  662. else
  663. res = timed_block (&waittime);
  664. scm_lock_mutex (mx);
  665. return scm_from_bool (res);
  666. }
  667. #undef FUNC_NAME
  668. SCM
  669. scm_signal_condition_variable (SCM cv)
  670. #define FUNC_NAME s_signal_condition_variable
  671. {
  672. SCM th;
  673. scm_copt_cond *c;
  674. SCM_ASSERT (SCM_CONDVARP (cv),
  675. cv,
  676. SCM_ARG1,
  677. s_signal_condition_variable);
  678. c = SCM_CONDVAR_DATA (cv);
  679. if (scm_is_true (th = dequeue (c->waiting)))
  680. unblock (SCM_THREAD_DATA (th));
  681. return SCM_BOOL_T;
  682. }
  683. #undef FUNC_NAME
  684. SCM
  685. scm_broadcast_condition_variable (SCM cv)
  686. #define FUNC_NAME s_broadcast_condition_variable
  687. {
  688. SCM th;
  689. scm_copt_cond *c;
  690. SCM_ASSERT (SCM_CONDVARP (cv),
  691. cv,
  692. SCM_ARG1,
  693. s_signal_condition_variable);
  694. c = SCM_CONDVAR_DATA (cv);
  695. while (scm_is_true (th = dequeue (c->waiting)))
  696. unblock (SCM_THREAD_DATA (th));
  697. return SCM_BOOL_T;
  698. }
  699. #undef FUNC_NAME
  700. /*** Initialization */
  701. void
  702. scm_threads_init (SCM_STACKITEM *base)
  703. {
  704. scm_tc16_thread = scm_make_smob_type ("thread", 0);
  705. scm_tc16_mutex = scm_make_smob_type ("mutex", sizeof (scm_copt_mutex));
  706. scm_tc16_condvar = scm_make_smob_type ("condition-variable",
  707. sizeof (scm_copt_cond));
  708. scm_i_switch_counter = SCM_I_THREAD_SWITCH_COUNT;
  709. fair_mutex_init (&guile_mutex);
  710. cur_thread = make_thread (SCM_BOOL_F);
  711. enter_guile (SCM_THREAD_DATA (cur_thread));
  712. /* root is set later from init.c */
  713. init_thread_creator (cur_thread, pthread_self(), NULL);
  714. init_thread_creatant (cur_thread, base);
  715. thread_count = 1;
  716. scm_gc_register_root (&all_threads);
  717. all_threads = scm_cons (cur_thread, SCM_EOL);
  718. scm_set_smob_mark (scm_tc16_thread, thread_mark);
  719. scm_set_smob_print (scm_tc16_thread, thread_print);
  720. scm_set_smob_free (scm_tc16_thread, thread_free);
  721. scm_set_smob_mark (scm_tc16_mutex, mutex_mark);
  722. scm_set_smob_mark (scm_tc16_condvar, cond_mark);
  723. }
  724. /*** Marking stacks */
  725. /* XXX - what to do with this? Do we need to handle this for blocked
  726. threads as well?
  727. */
  728. #ifdef __ia64__
  729. # define SCM_MARK_BACKING_STORE() do { \
  730. ucontext_t ctx; \
  731. SCM_STACKITEM * top, * bot; \
  732. getcontext (&ctx); \
  733. scm_mark_locations ((SCM_STACKITEM *) &ctx.uc_mcontext, \
  734. ((size_t) (sizeof (SCM_STACKITEM) - 1 + sizeof ctx.uc_mcontext) \
  735. / sizeof (SCM_STACKITEM))); \
  736. bot = (SCM_STACKITEM *) scm_ia64_register_backing_store_base (); \
  737. top = (SCM_STACKITEM *) scm_ia64_ar_bsp (&ctx); \
  738. scm_mark_locations (bot, top - bot); } while (0)
  739. #else
  740. # define SCM_MARK_BACKING_STORE()
  741. #endif
  742. void
  743. scm_threads_mark_stacks (void)
  744. {
  745. volatile SCM c;
  746. for (c = all_threads; !scm_is_null (c); c = SCM_CDR (c))
  747. {
  748. scm_copt_thread *t = SCM_THREAD_DATA (SCM_CAR (c));
  749. if (t->base == NULL)
  750. {
  751. /* Not fully initialized yet. */
  752. continue;
  753. }
  754. if (t->top == NULL)
  755. {
  756. /* Active thread */
  757. /* stack_len is long rather than sizet in order to guarantee
  758. that &stack_len is long aligned */
  759. #if SCM_STACK_GROWS_UP
  760. long stack_len = ((SCM_STACKITEM *) (&t) -
  761. (SCM_STACKITEM *) thread->base);
  762. /* Protect from the C stack. This must be the first marking
  763. * done because it provides information about what objects
  764. * are "in-use" by the C code. "in-use" objects are those
  765. * for which the information about length and base address must
  766. * remain usable. This requirement is stricter than a liveness
  767. * requirement -- in particular, it constrains the implementation
  768. * of scm_resizuve.
  769. */
  770. SCM_FLUSH_REGISTER_WINDOWS;
  771. /* This assumes that all registers are saved into the jmp_buf */
  772. setjmp (scm_save_regs_gc_mark);
  773. scm_mark_locations ((SCM_STACKITEM *) scm_save_regs_gc_mark,
  774. ((size_t) sizeof scm_save_regs_gc_mark
  775. / sizeof (SCM_STACKITEM)));
  776. scm_mark_locations (((size_t) t->base,
  777. (sizet) stack_len));
  778. #else
  779. long stack_len = ((SCM_STACKITEM *) t->base -
  780. (SCM_STACKITEM *) (&t));
  781. /* Protect from the C stack. This must be the first marking
  782. * done because it provides information about what objects
  783. * are "in-use" by the C code. "in-use" objects are those
  784. * for which the information about length and base address must
  785. * remain usable. This requirement is stricter than a liveness
  786. * requirement -- in particular, it constrains the implementation
  787. * of scm_resizuve.
  788. */
  789. SCM_FLUSH_REGISTER_WINDOWS;
  790. /* This assumes that all registers are saved into the jmp_buf */
  791. setjmp (scm_save_regs_gc_mark);
  792. scm_mark_locations ((SCM_STACKITEM *) scm_save_regs_gc_mark,
  793. ((size_t) sizeof scm_save_regs_gc_mark
  794. / sizeof (SCM_STACKITEM)));
  795. scm_mark_locations ((SCM_STACKITEM *) &t,
  796. stack_len);
  797. #endif
  798. }
  799. else
  800. {
  801. /* Suspended thread */
  802. #if SCM_STACK_GROWS_UP
  803. long stack_len = t->top - t->base;
  804. scm_mark_locations (t->base, stack_len);
  805. #else
  806. long stack_len = t->base - t->top;
  807. scm_mark_locations (t->top, stack_len);
  808. #endif
  809. scm_mark_locations ((SCM_STACKITEM *) t->regs,
  810. ((size_t) sizeof(t->regs)
  811. / sizeof (SCM_STACKITEM)));
  812. }
  813. }
  814. }
  815. /*** Select */
  816. int
  817. scm_internal_select (int nfds,
  818. SELECT_TYPE *readfds,
  819. SELECT_TYPE *writefds,
  820. SELECT_TYPE *exceptfds,
  821. struct timeval *timeout)
  822. {
  823. int res, eno;
  824. scm_copt_thread *c = leave_guile ();
  825. res = select (nfds, readfds, writefds, exceptfds, timeout);
  826. eno = errno;
  827. enter_guile (c);
  828. SCM_ASYNC_TICK;
  829. errno = eno;
  830. return res;
  831. }
  832. void
  833. scm_init_iselect ()
  834. {
  835. }
  836. unsigned long
  837. scm_thread_usleep (unsigned long usec)
  838. {
  839. scm_copt_thread *c = leave_guile ();
  840. usleep (usec);
  841. enter_guile (c);
  842. return 0;
  843. }
  844. unsigned long
  845. scm_thread_sleep (unsigned long sec)
  846. {
  847. unsigned long res;
  848. scm_copt_thread *c = leave_guile ();
  849. res = sleep (sec);
  850. enter_guile (c);
  851. return res;
  852. }
  853. /*** Misc */
  854. SCM
  855. scm_current_thread (void)
  856. {
  857. return cur_thread;
  858. }
  859. SCM
  860. scm_all_threads (void)
  861. {
  862. return all_threads;
  863. }
  864. scm_root_state *
  865. scm_i_thread_root (SCM thread)
  866. {
  867. if (thread == cur_thread)
  868. return scm_i_copt_thread_data;
  869. else
  870. return ((scm_copt_thread *)SCM_THREAD_DATA (thread))->root;
  871. }
  872. SCM
  873. scm_join_thread (SCM thread)
  874. #define FUNC_NAME s_join_thread
  875. {
  876. scm_copt_thread *t;
  877. SCM res;
  878. SCM_VALIDATE_THREAD (1, thread);
  879. t = SCM_THREAD_DATA (thread);
  880. if (t->pthread != -1)
  881. {
  882. scm_copt_thread *c = leave_guile ();
  883. pthread_join (t->pthread, NULL);
  884. enter_guile (c);
  885. }
  886. res = t->result;
  887. t->result = SCM_BOOL_F;
  888. return res;
  889. }
  890. #undef FUNC_NAME
  891. int
  892. scm_c_thread_exited_p (SCM thread)
  893. #define FUNC_NAME s_scm_thread_exited_p
  894. {
  895. scm_copt_thread *t;
  896. SCM_VALIDATE_THREAD (1, thread);
  897. t = SCM_THREAD_DATA (thread);
  898. return t->pthread == -1;
  899. }
  900. #undef FUNC_NAME
  901. /*
  902. Local Variables:
  903. c-file-style: "gnu"
  904. End:
  905. */