1 /* Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
43 #include "coop-threads.h"
45 /* A counter of the current number of threads */
46 size_t scm_thread_count
= 0;
48 /* This is included rather than compiled separately in order
49 to simplify the configuration mechanism. */
52 /* A count-down counter used to determine when to switch
54 size_t scm_switch_counter
= SCM_THREAD_SWITCH_COUNT
;
56 coop_m scm_critical_section_mutex
;
60 scm_threads_free_thread (SCM t
)
63 scm_threads_free_thread (t
)
67 scm_must_free (SCM_THREAD_DATA (t
));
68 return sizeof (coop_t
);
73 scm_threads_free_mutex (SCM m
)
76 scm_threads_free_mutex (m
)
80 scm_must_free (SCM_MUTEX_DATA (m
));
81 return sizeof (coop_m
);
86 scm_threads_free_condvar (SCM c
)
89 scm_threads_free_condvar (c
)
93 scm_must_free (SCM_CONDVAR_DATA (c
));
94 return sizeof (coop_c
);
99 scm_threads_init (SCM_STACKITEM
*i
)
108 scm_thread_count
= 1;
110 coop_global_main
.sto
= i
;
111 coop_global_main
.base
= i
;
112 coop_global_curr
= &coop_global_main
;
113 coop_all_qput (&coop_global_allq
, coop_global_curr
);
115 coop_mutex_init (&scm_critical_section_mutex
);
117 coop_global_main
.data
= 0; /* Initialized in init.c */
122 scm_threads_mark_stacks ()
125 scm_threads_mark_stacks ()
130 for (thread
= coop_global_allq
.t
.all_next
;
131 thread
!= NULL
; thread
= thread
->all_next
)
133 if (thread
== coop_global_curr
)
136 /* stack_len is long rather than sizet in order to guarantee
137 that &stack_len is long aligned */
138 #ifdef STACK_GROWS_UP
139 long stack_len
= ((SCM_STACKITEM
*) (&thread
) -
140 (SCM_STACKITEM
*) thread
->base
);
142 /* Protect from the C stack. This must be the first marking
143 * done because it provides information about what objects
144 * are "in-use" by the C code. "in-use" objects are those
145 * for which the values from SCM_LENGTH and SCM_CHARS must remain
146 * usable. This requirement is stricter than a liveness
147 * requirement -- in particular, it constrains the implementation
150 SCM_FLUSH_REGISTER_WINDOWS
;
151 /* This assumes that all registers are saved into the jmp_buf */
152 setjmp (scm_save_regs_gc_mark
);
153 scm_mark_locations ((SCM_STACKITEM
*) scm_save_regs_gc_mark
,
154 ((scm_sizet
) sizeof scm_save_regs_gc_mark
155 / sizeof (SCM_STACKITEM
)));
157 scm_mark_locations (((size_t) thread
->base
,
160 long stack_len
= ((SCM_STACKITEM
*) thread
->base
-
161 (SCM_STACKITEM
*) (&thread
));
163 /* Protect from the C stack. This must be the first marking
164 * done because it provides information about what objects
165 * are "in-use" by the C code. "in-use" objects are those
166 * for which the values from SCM_LENGTH and SCM_CHARS must remain
167 * usable. This requirement is stricter than a liveness
168 * requirement -- in particular, it constrains the implementation
171 SCM_FLUSH_REGISTER_WINDOWS
;
172 /* This assumes that all registers are saved into the jmp_buf */
173 setjmp (scm_save_regs_gc_mark
);
174 scm_mark_locations ((SCM_STACKITEM
*) scm_save_regs_gc_mark
,
175 ((scm_sizet
) sizeof scm_save_regs_gc_mark
176 / sizeof (SCM_STACKITEM
)));
178 scm_mark_locations ((SCM_STACKITEM
*) &thread
,
184 /* Suspended thread */
185 #ifdef STACK_GROWS_UP
186 long stack_len
= ((SCM_STACKITEM
*) (thread
->sp
) -
187 (SCM_STACKITEM
*) thread
->base
);
189 scm_mark_locations ((size_t)thread
->base
,
192 long stack_len
= ((SCM_STACKITEM
*) thread
->base
-
193 (SCM_STACKITEM
*) (thread
->sp
));
195 /* Registers are already on the stack. No need to mark. */
197 scm_mark_locations ((SCM_STACKITEM
*) (size_t)thread
->sp
,
202 /* Mark this thread's root */
203 scm_gc_mark (((scm_root_state
*) thread
->data
) -> handle
);
207 /* NOTE: There are TWO mechanisms for starting a thread: The first one
208 is used when spawning a thread from Scheme, while the second one is
211 It might be argued that the first should be implemented in terms of
212 the second. The reason it isn't is that that would require an
213 extra unnecessary malloc (the thread_args structure). By providing
214 one pair of extra functions (c_launch_thread, scm_spawn_thread) the
215 Scheme threads are started more efficiently. */
217 /* This is the first thread spawning mechanism: threads from Scheme */
219 typedef struct scheme_launch_data
{
223 } scheme_launch_data
;
225 extern SCM
scm_apply (SCM
, SCM
, SCM
);
228 scheme_body_bootstrip (scheme_launch_data
* data
)
230 /* First save the new root continuation */
231 data
->rootcont
= scm_root
->rootcont
;
232 return scm_apply (data
->body
, SCM_EOL
, SCM_EOL
);
236 scheme_handler_bootstrip (scheme_launch_data
* data
, SCM tag
, SCM throw_args
)
238 scm_root
->rootcont
= data
->rootcont
;
239 return scm_apply (data
->handler
, scm_cons (tag
, throw_args
), SCM_EOL
);
243 scheme_launch_thread (void *p
)
245 /* The thread object will be GC protected by being a member of the
246 list given as argument to launch_thread. It will be marked
247 during the conservative sweep of the stack. */
248 register SCM argl
= (SCM
) p
;
249 SCM thread
= SCM_CAR (argl
);
250 scheme_launch_data data
;
251 data
.rootcont
= SCM_BOOL_F
;
252 data
.body
= SCM_CADR (argl
);
253 data
.handler
= SCM_CADDR (argl
);
254 scm_internal_cwdr ((scm_catch_body_t
) scheme_body_bootstrip
,
256 (scm_catch_handler_t
) scheme_handler_bootstrip
,
265 scm_call_with_new_thread (SCM argl
)
268 scm_call_with_new_thread (argl
)
274 /* Check arguments. */
276 register SCM args
= argl
;
278 SCM_ASSERT (SCM_NIMP (args
),
279 scm_makfrom0str (s_call_with_new_thread
),
281 thunk
= SCM_CAR (args
);
282 SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (thunk
)),
285 s_call_with_new_thread
);
286 args
= SCM_CDR (args
);
287 SCM_ASSERT (SCM_NIMP (args
),
288 scm_makfrom0str (s_call_with_new_thread
),
290 handler
= SCM_CAR (args
);
291 SCM_ASSERT (SCM_NFALSEP (scm_procedure_p (handler
)),
294 s_call_with_new_thread
);
295 SCM_ASSERT (SCM_NULLP (SCM_CDR (args
)),
296 scm_makfrom0str (s_call_with_new_thread
),
300 /* Make new thread. */
305 /* Unwind wind chain. */
306 old_winds
= scm_dynwinds
;
307 scm_dowinds (SCM_EOL
, scm_ilength (scm_root
->dynwinds
));
309 /* Allocate thread locals. */
310 root
= scm_make_root (scm_root
->handle
);
312 SCM_NEWCELL (thread
);
314 SCM_SETCAR (thread
, scm_tc16_thread
);
315 argl
= scm_cons (thread
, argl
);
316 /* Note that we couldn't pass a pointer to argl as data since the
317 argl variable may not exist in memory when the thread starts. */
318 t
= coop_create (scheme_launch_thread
, (void *) argl
);
319 t
->data
= SCM_ROOT_STATE (root
);
320 SCM_SETCDR (thread
, t
);
322 /* Note that the following statement also could cause coop_yield.*/
325 /* We're now ready for the thread to begin. */
328 /* Return to old dynamic context. */
329 scm_dowinds (old_winds
, - scm_ilength (old_winds
));
335 /* This is the second thread spawning mechanism: threads from C */
337 typedef struct c_launch_data
{
342 scm_catch_body_t body
;
344 scm_catch_handler_t handler
;
349 c_body_bootstrip (c_launch_data
* data
)
351 /* First save the new root continuation */
352 data
->u
.rootcont
= scm_root
->rootcont
;
353 return (data
->body
) (data
->body_data
);
357 c_handler_bootstrip (c_launch_data
* data
, SCM tag
, SCM throw_args
)
359 scm_root
->rootcont
= data
->u
.rootcont
;
360 return (data
->handler
) (data
->handler_data
, tag
, throw_args
);
364 c_launch_thread (void *p
)
366 register c_launch_data
*data
= (c_launch_data
*) p
;
367 /* The thread object will be GC protected by being on this stack */
368 SCM thread
= data
->u
.thread
;
369 /* We must use the address of `thread', otherwise the compiler will
370 optimize it away. This is OK since the longest SCM_STACKITEM
372 scm_internal_cwdr ((scm_catch_body_t
) c_body_bootstrip
,
374 (scm_catch_handler_t
) c_handler_bootstrip
,
378 scm_must_free ((char *) data
);
382 scm_spawn_thread (scm_catch_body_t body
, void *body_data
,
383 scm_catch_handler_t handler
, void *handler_data
)
388 c_launch_data
*data
= (c_launch_data
*) scm_must_malloc (sizeof (*data
),
391 /* Unwind wind chain. */
392 old_winds
= scm_dynwinds
;
393 scm_dowinds (SCM_EOL
, scm_ilength (scm_root
->dynwinds
));
395 /* Allocate thread locals. */
396 root
= scm_make_root (scm_root
->handle
);
398 SCM_NEWCELL (thread
);
400 SCM_SETCAR (thread
, scm_tc16_thread
);
402 data
->u
.thread
= thread
;
404 data
->body_data
= body_data
;
405 data
->handler
= handler
;
406 data
->handler_data
= handler_data
;
408 t
= coop_create (c_launch_thread
, (void *) data
);
410 t
->data
= SCM_ROOT_STATE (root
);
411 SCM_SETCDR (thread
, t
);
413 /* Note that the following statement also could cause coop_yield.*/
416 /* We're now ready for the thread to begin. */
419 /* Return to old dynamic context. */
420 scm_dowinds (old_winds
, - scm_ilength (old_winds
));
427 scm_join_thread (SCM t
)
434 SCM_ASSERT (SCM_NIMP (t
) && SCM_THREADP (t
), t
, SCM_ARG1
, s_join_thread
);
435 coop_join (SCM_THREAD_DATA (t
));
448 scm_switch_counter
= SCM_THREAD_SWITCH_COUNT
;
456 scm_single_thread_p ()
459 scm_single_thread_p ()
462 return (coop_global_runq
.tail
== &coop_global_runq
.t
476 coop_m
*data
= (coop_m
*) scm_must_malloc (sizeof (coop_m
), "mutex");
479 SCM_SETCAR (m
, scm_tc16_mutex
);
480 SCM_SETCDR (m
, data
);
482 coop_mutex_init (data
);
488 scm_lock_mutex (SCM m
)
495 SCM_ASSERT (SCM_NIMP (m
) && SCM_MUTEXP (m
), m
, SCM_ARG1
, s_lock_mutex
);
496 coop_mutex_lock (SCM_MUTEX_DATA (m
));
502 scm_unlock_mutex (SCM m
)
509 SCM_ASSERT (SCM_NIMP (m
) && SCM_MUTEXP (m
), m
, SCM_ARG1
, s_unlock_mutex
);
510 coop_mutex_unlock(SCM_MUTEX_DATA (m
));
513 scm_switch_counter
= SCM_THREAD_SWITCH_COUNT
;
521 scm_make_condition_variable ()
524 scm_make_condition_variable ()
528 coop_c
*data
= (coop_c
*) scm_must_malloc (sizeof (coop_c
), "condvar");
531 SCM_SETCAR (c
, scm_tc16_condvar
);
532 SCM_SETCDR (c
, data
);
534 coop_condition_variable_init (SCM_CONDVAR_DATA (c
));
540 scm_wait_condition_variable (SCM c
, SCM m
)
543 scm_wait_condition_variable (c
, m
)
548 SCM_ASSERT (SCM_NIMP (c
) && SCM_CONDVARP (c
),
551 s_wait_condition_variable
);
552 SCM_ASSERT (SCM_NIMP (m
) && SCM_MUTEXP (m
),
555 s_wait_condition_variable
);
556 coop_condition_variable_wait_mutex (SCM_CONDVAR_DATA (c
),
563 scm_signal_condition_variable (SCM c
)
566 scm_signal_condition_variable (c
)
570 SCM_ASSERT (SCM_NIMP (c
) && SCM_CONDVARP (c
),
573 s_signal_condition_variable
);
574 coop_condition_variable_signal (SCM_CONDVAR_DATA (c
));