*** empty log message ***
[bpt/guile.git] / libguile / coop-threads.c
CommitLineData
d90ca38d 1/* Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
7bfd3b9e
JB
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
7bfd3b9e
JB
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
7bfd3b9e
JB
41\f
42
43#include "coop-threads.h"
44
45/* A counter of the current number of threads */
46size_t scm_thread_count = 0;
47
48/* This is included rather than compiled separately in order
49 to simplify the configuration mechanism. */
50#include "coop.c"
51
52/* A count-down counter used to determine when to switch
53 contexts */
54size_t scm_switch_counter = SCM_THREAD_SWITCH_COUNT;
55
56coop_m scm_critical_section_mutex;
57
7bfd3b9e
JB
58#ifdef __STDC__
59void
60scm_threads_init (SCM_STACKITEM *i)
61#else
62void
63scm_threads_init (i)
64 SCM_STACKITEM *i;
65#endif
66{
67 coop_init();
68
69 scm_thread_count = 1;
70
71 coop_global_main.sto = i;
72 coop_global_main.base = i;
73 coop_global_curr = &coop_global_main;
74 coop_all_qput (&coop_global_allq, coop_global_curr);
75
76 coop_mutex_init (&scm_critical_section_mutex);
77
78 coop_global_main.data = 0; /* Initialized in init.c */
79}
80
81#ifdef __STDC__
82void
83scm_threads_mark_stacks ()
84#else
85void
86scm_threads_mark_stacks ()
87#endif
88{
89 coop_t *thread;
90
91 for (thread = coop_global_allq.t.all_next;
92 thread != NULL; thread = thread->all_next)
93 {
94 if (thread == coop_global_curr)
95 {
96 /* Active thread */
97 /* stack_len is long rather than sizet in order to guarantee
98 that &stack_len is long aligned */
99#ifdef STACK_GROWS_UP
100 long stack_len = ((SCM_STACKITEM *) (&thread) -
101 (SCM_STACKITEM *) thread->base);
102
103 /* Protect from the C stack. This must be the first marking
104 * done because it provides information about what objects
105 * are "in-use" by the C code. "in-use" objects are those
106 * for which the values from SCM_LENGTH and SCM_CHARS must remain
107 * usable. This requirement is stricter than a liveness
108 * requirement -- in particular, it constrains the implementation
109 * of scm_resizuve.
110 */
111 SCM_FLUSH_REGISTER_WINDOWS;
112 /* This assumes that all registers are saved into the jmp_buf */
113 setjmp (scm_save_regs_gc_mark);
114 scm_mark_locations ((SCM_STACKITEM *) scm_save_regs_gc_mark,
115 ((scm_sizet) sizeof scm_save_regs_gc_mark
116 / sizeof (SCM_STACKITEM)));
117
118 scm_mark_locations (((size_t) thread->base,
119 (sizet) stack_len));
120#else
121 long stack_len = ((SCM_STACKITEM *) thread->base -
122 (SCM_STACKITEM *) (&thread));
123
124 /* Protect from the C stack. This must be the first marking
125 * done because it provides information about what objects
126 * are "in-use" by the C code. "in-use" objects are those
127 * for which the values from SCM_LENGTH and SCM_CHARS must remain
128 * usable. This requirement is stricter than a liveness
129 * requirement -- in particular, it constrains the implementation
130 * of scm_resizuve.
131 */
132 SCM_FLUSH_REGISTER_WINDOWS;
133 /* This assumes that all registers are saved into the jmp_buf */
134 setjmp (scm_save_regs_gc_mark);
135 scm_mark_locations ((SCM_STACKITEM *) scm_save_regs_gc_mark,
136 ((scm_sizet) sizeof scm_save_regs_gc_mark
137 / sizeof (SCM_STACKITEM)));
138
139 scm_mark_locations ((SCM_STACKITEM *) &thread,
140 stack_len);
141#endif
142 }
143 else
144 {
145 /* Suspended thread */
146#ifdef STACK_GROWS_UP
147 long stack_len = ((SCM_STACKITEM *) (thread->sp) -
148 (SCM_STACKITEM *) thread->base);
149
150 scm_mark_locations ((size_t)thread->base,
151 (sizet) stack_len);
152#else
153 long stack_len = ((SCM_STACKITEM *) thread->base -
154 (SCM_STACKITEM *) (thread->sp));
155
156 /* Registers are already on the stack. No need to mark. */
157
158 scm_mark_locations ((SCM_STACKITEM *) (size_t)thread->sp,
159 stack_len);
160#endif
161 }
162
163 /* Mark this thread's root */
164 scm_gc_mark (((scm_root_state *) thread->data) -> handle);
165 }
166}
167
df366c26
MD
168/* NOTE: There are TWO mechanisms for starting a thread: The first one
169 is used when spawning a thread from Scheme, while the second one is
170 used from C.
171
172 It might be argued that the first should be implemented in terms of
173 the second. The reason it isn't is that that would require an
174 extra unnecessary malloc (the thread_args structure). By providing
175 one pair of extra functions (c_launch_thread, scm_spawn_thread) the
176 Scheme threads are started more efficiently. */
177
178/* This is the first thread spawning mechanism: threads from Scheme */
179
0a1a92ab
MD
180typedef struct scheme_launch_data {
181 SCM rootcont;
182 SCM body;
183 SCM handler;
184} scheme_launch_data;
185
186extern SCM scm_apply (SCM, SCM, SCM);
187
188static SCM
39752bec 189scheme_body_bootstrip (scheme_launch_data* data)
0a1a92ab
MD
190{
191 /* First save the new root continuation */
192 data->rootcont = scm_root->rootcont;
193 return scm_apply (data->body, SCM_EOL, SCM_EOL);
194}
195
196static SCM
197scheme_handler_bootstrip (scheme_launch_data* data, SCM tag, SCM throw_args)
198{
199 scm_root->rootcont = data->rootcont;
200 return scm_apply (data->handler, scm_cons (tag, throw_args), SCM_EOL);
201}
202
df366c26
MD
203static void
204scheme_launch_thread (void *p)
7bfd3b9e
JB
205{
206 /* The thread object will be GC protected by being a member of the
207 list given as argument to launch_thread. It will be marked
208 during the conservative sweep of the stack. */
0a1a92ab
MD
209 register SCM argl = (SCM) p;
210 SCM thread = SCM_CAR (argl);
211 scheme_launch_data data;
212 data.rootcont = SCM_BOOL_F;
213 data.body = SCM_CADR (argl);
214 data.handler = SCM_CADDR (argl);
215 scm_internal_cwdr ((scm_catch_body_t) scheme_body_bootstrip,
216 &data,
217 (scm_catch_handler_t) scheme_handler_bootstrip,
218 &data,
219 &thread);
7bfd3b9e 220 scm_thread_count--;
0a1a92ab 221 SCM_DEFER_INTS;
7bfd3b9e
JB
222}
223
224#ifdef __STDC__
225SCM
226scm_call_with_new_thread (SCM argl)
227#else
228SCM
229scm_call_with_new_thread (argl)
230 SCM argl;
231#endif
232{
233 SCM thread;
234
235 /* Check arguments. */
236 {
237 register SCM args = argl;
238 SCM thunk, handler;
0824b524
MD
239 SCM_ASSERT (SCM_NIMP (args),
240 scm_makfrom0str (s_call_with_new_thread),
241 SCM_WNA, NULL);
7bfd3b9e
JB
242 thunk = SCM_CAR (args);
243 SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (thunk)),
244 thunk,
245 SCM_ARG1,
246 s_call_with_new_thread);
247 args = SCM_CDR (args);
0824b524
MD
248 SCM_ASSERT (SCM_NIMP (args),
249 scm_makfrom0str (s_call_with_new_thread),
250 SCM_WNA, NULL);
7bfd3b9e
JB
251 handler = SCM_CAR (args);
252 SCM_ASSERT (SCM_NFALSEP (scm_procedure_p (handler)),
253 handler,
254 SCM_ARG2,
255 s_call_with_new_thread);
0824b524
MD
256 SCM_ASSERT (SCM_NULLP (SCM_CDR (args)),
257 scm_makfrom0str (s_call_with_new_thread),
258 SCM_WNA, NULL);
7bfd3b9e
JB
259 }
260
261 /* Make new thread. */
262 {
263 coop_t *t;
264 SCM root, old_winds;
265
266 /* Unwind wind chain. */
267 old_winds = scm_dynwinds;
268 scm_dowinds (SCM_EOL, scm_ilength (scm_root->dynwinds));
269
270 /* Allocate thread locals. */
271 root = scm_make_root (scm_root->handle);
272 /* Make thread. */
273 SCM_NEWCELL (thread);
274 SCM_DEFER_INTS;
275 SCM_SETCAR (thread, scm_tc16_thread);
276 argl = scm_cons (thread, argl);
0a1a92ab
MD
277 /* Note that we couldn't pass a pointer to argl as data since the
278 argl variable may not exist in memory when the thread starts. */
df366c26 279 t = coop_create (scheme_launch_thread, (void *) argl);
7bfd3b9e
JB
280 t->data = SCM_ROOT_STATE (root);
281 SCM_SETCDR (thread, t);
282 scm_thread_count++;
283 /* Note that the following statement also could cause coop_yield.*/
284 SCM_ALLOW_INTS;
285
286 /* We're now ready for the thread to begin. */
287 coop_yield();
288
289 /* Return to old dynamic context. */
290 scm_dowinds (old_winds, - scm_ilength (old_winds));
291 }
292
293 return thread;
294}
295
df366c26
MD
296/* This is the second thread spawning mechanism: threads from C */
297
0a1a92ab
MD
298typedef struct c_launch_data {
299 union {
300 SCM thread;
301 SCM rootcont;
302 } u;
df366c26
MD
303 scm_catch_body_t body;
304 void *body_data;
305 scm_catch_handler_t handler;
306 void *handler_data;
0a1a92ab
MD
307} c_launch_data;
308
309static SCM
39752bec 310c_body_bootstrip (c_launch_data* data)
0a1a92ab
MD
311{
312 /* First save the new root continuation */
313 data->u.rootcont = scm_root->rootcont;
39752bec 314 return (data->body) (data->body_data);
0a1a92ab
MD
315}
316
317static SCM
318c_handler_bootstrip (c_launch_data* data, SCM tag, SCM throw_args)
319{
320 scm_root->rootcont = data->u.rootcont;
321 return (data->handler) (data->handler_data, tag, throw_args);
322}
df366c26
MD
323
324static void
325c_launch_thread (void *p)
326{
0a1a92ab 327 register c_launch_data *data = (c_launch_data *) p;
df366c26 328 /* The thread object will be GC protected by being on this stack */
0a1a92ab 329 SCM thread = data->u.thread;
df366c26
MD
330 /* We must use the address of `thread', otherwise the compiler will
331 optimize it away. This is OK since the longest SCM_STACKITEM
332 also is a long. */
0a1a92ab
MD
333 scm_internal_cwdr ((scm_catch_body_t) c_body_bootstrip,
334 data,
335 (scm_catch_handler_t) c_handler_bootstrip,
336 data,
df366c26
MD
337 &thread);
338 scm_thread_count--;
0a1a92ab 339 scm_must_free ((char *) data);
df366c26
MD
340}
341
342SCM
343scm_spawn_thread (scm_catch_body_t body, void *body_data,
344 scm_catch_handler_t handler, void *handler_data)
345{
346 SCM thread;
347 coop_t *t;
348 SCM root, old_winds;
0a1a92ab
MD
349 c_launch_data *data = (c_launch_data *) scm_must_malloc (sizeof (*data),
350 "scm_spawn_thread");
df366c26
MD
351
352 /* Unwind wind chain. */
353 old_winds = scm_dynwinds;
354 scm_dowinds (SCM_EOL, scm_ilength (scm_root->dynwinds));
355
356 /* Allocate thread locals. */
357 root = scm_make_root (scm_root->handle);
358 /* Make thread. */
359 SCM_NEWCELL (thread);
360 SCM_DEFER_INTS;
361 SCM_SETCAR (thread, scm_tc16_thread);
362
0a1a92ab
MD
363 data->u.thread = thread;
364 data->body = body;
365 data->body_data = body_data;
366 data->handler = handler;
367 data->handler_data = handler_data;
df366c26 368
0a1a92ab 369 t = coop_create (c_launch_thread, (void *) data);
df366c26
MD
370
371 t->data = SCM_ROOT_STATE (root);
372 SCM_SETCDR (thread, t);
373 scm_thread_count++;
374 /* Note that the following statement also could cause coop_yield.*/
375 SCM_ALLOW_INTS;
376
377 /* We're now ready for the thread to begin. */
378 coop_yield();
379
380 /* Return to old dynamic context. */
381 scm_dowinds (old_winds, - scm_ilength (old_winds));
382
383 return thread;
384}
385
7bfd3b9e
JB
386#ifdef __STDC__
387SCM
388scm_join_thread (SCM t)
389#else
390SCM
391scm_join_thread (t)
392 SCM t;
393#endif
394{
395 SCM_ASSERT (SCM_NIMP (t) && SCM_THREADP (t), t, SCM_ARG1, s_join_thread);
396 coop_join (SCM_THREAD_DATA (t));
397 return SCM_BOOL_T;
398}
399
400#ifdef __STDC__
401SCM
402scm_yield ()
403#else
404SCM
405scm_yield ()
406#endif
407{
408 /* Yield early */
409 scm_switch_counter = SCM_THREAD_SWITCH_COUNT;
410 coop_yield();
411
412 return SCM_BOOL_T;
413}
414
415#ifdef __STDC__
416SCM
417scm_single_thread_p ()
418#else
419SCM
420scm_single_thread_p ()
421#endif
422{
423 return (coop_global_runq.tail == &coop_global_runq.t
424 ? SCM_BOOL_T
425 : SCM_BOOL_F);
426}
427
428#ifdef __STDC__
429SCM
430scm_make_mutex ()
431#else
432SCM
433scm_make_mutex ()
434#endif
435{
436 SCM m;
437 coop_m *data = (coop_m *) scm_must_malloc (sizeof (coop_m), "mutex");
23a62151
MD
438
439 SCM_NEWSMOB (m, scm_tc16_mutex, data);
7bfd3b9e
JB
440 coop_mutex_init (data);
441 return m;
442}
443
444#ifdef __STDC__
445SCM
446scm_lock_mutex (SCM m)
447#else
448SCM
449scm_lock_mutex (m)
450 SCM m;
451#endif
452{
453 SCM_ASSERT (SCM_NIMP (m) && SCM_MUTEXP (m), m, SCM_ARG1, s_lock_mutex);
454 coop_mutex_lock (SCM_MUTEX_DATA (m));
455 return SCM_BOOL_T;
456}
457
458#ifdef __STDC__
459SCM
460scm_unlock_mutex (SCM m)
461#else
462SCM
463scm_unlock_mutex (m)
464 SCM m;
465#endif
466{
467 SCM_ASSERT (SCM_NIMP (m) && SCM_MUTEXP (m), m, SCM_ARG1, s_unlock_mutex);
468 coop_mutex_unlock(SCM_MUTEX_DATA (m));
469
470 /* Yield early */
471 scm_switch_counter = SCM_THREAD_SWITCH_COUNT;
472 coop_yield();
473
474 return SCM_BOOL_T;
475}
476
477#ifdef __STDC__
478SCM
479scm_make_condition_variable ()
480#else
481SCM
482scm_make_condition_variable ()
483#endif
484{
485 SCM c;
486 coop_c *data = (coop_c *) scm_must_malloc (sizeof (coop_c), "condvar");
23a62151 487 SCM_NEWSMOB (c, scm_tc16_condvar, data);
7bfd3b9e
JB
488 coop_condition_variable_init (SCM_CONDVAR_DATA (c));
489 return c;
490}
491
492#ifdef __STDC__
493SCM
494scm_wait_condition_variable (SCM c, SCM m)
495#else
496SCM
497scm_wait_condition_variable (c, m)
498 SCM c;
499 SCM m;
500#endif
501{
502 SCM_ASSERT (SCM_NIMP (c) && SCM_CONDVARP (c),
503 c,
504 SCM_ARG1,
505 s_wait_condition_variable);
506 SCM_ASSERT (SCM_NIMP (m) && SCM_MUTEXP (m),
507 m,
508 SCM_ARG2,
509 s_wait_condition_variable);
c8bf4ecd
MD
510 coop_condition_variable_wait_mutex (SCM_CONDVAR_DATA (c),
511 SCM_MUTEX_DATA (m));
7bfd3b9e
JB
512 return SCM_BOOL_T;
513}
514
515#ifdef __STDC__
516SCM
517scm_signal_condition_variable (SCM c)
518#else
519SCM
520scm_signal_condition_variable (c)
521 SCM c;
522#endif
523{
524 SCM_ASSERT (SCM_NIMP (c) && SCM_CONDVARP (c),
525 c,
526 SCM_ARG1,
527 s_signal_condition_variable);
528 coop_condition_variable_signal (SCM_CONDVAR_DATA (c));
529 return SCM_BOOL_T;
530}