1 /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * 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.
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
25 #include "libguile/bdw-gc.h"
26 #include "libguile/_scm.h"
34 #include <string.h> /* for memset used by FD_ZERO on Solaris 10 */
45 #include "libguile/validate.h"
46 #include "libguile/root.h"
47 #include "libguile/eval.h"
48 #include "libguile/async.h"
49 #include "libguile/ports.h"
50 #include "libguile/threads.h"
51 #include "libguile/dynwind.h"
52 #include "libguile/iselect.h"
53 #include "libguile/fluids.h"
54 #include "libguile/continuations.h"
55 #include "libguile/gc.h"
56 #include "libguile/init.h"
57 #include "libguile/scmsigs.h"
58 #include "libguile/strings.h"
59 #include "libguile/weaks.h"
61 #include <full-read.h>
66 /* First some libgc shims. */
68 /* Make sure GC_fn_type is defined; it is missing from the public
69 headers of GC 7.1 and earlier. */
70 #ifndef HAVE_GC_FN_TYPE
71 typedef void * (* GC_fn_type
) (void *);
79 #ifndef GC_UNIMPLEMENTED
80 #define GC_UNIMPLEMENTED 3
83 /* Likewise struct GC_stack_base is missing before 7.1. */
84 #ifndef HAVE_GC_STACK_BASE
85 struct GC_stack_base
{
86 void * mem_base
; /* Base of memory stack. */
88 void * reg_base
; /* Base of separate register stack. */
93 GC_register_my_thread (struct GC_stack_base
*stack_base
)
95 return GC_UNIMPLEMENTED
;
99 GC_unregister_my_thread ()
103 #if !SCM_USE_PTHREAD_THREADS
104 /* No threads; we can just use GC_stackbottom. */
106 get_thread_stack_base ()
108 return GC_stackbottom
;
111 #elif defined HAVE_PTHREAD_ATTR_GETSTACK && defined HAVE_PTHREAD_GETATTR_NP \
112 && defined PTHREAD_ATTR_GETSTACK_WORKS
113 /* This method for GNU/Linux and perhaps some other systems.
114 It's not for MacOS X or Solaris 10, since pthread_getattr_np is not
115 available on them. */
117 get_thread_stack_base ()
123 pthread_getattr_np (pthread_self (), &attr
);
124 pthread_attr_getstack (&attr
, &start
, &size
);
125 end
= (char *)start
+ size
;
127 #if SCM_STACK_GROWS_UP
134 #elif defined HAVE_PTHREAD_GET_STACKADDR_NP
135 /* This method for MacOS X.
136 It'd be nice if there was some documentation on pthread_get_stackaddr_np,
137 but as of 2006 there's nothing obvious at apple.com. */
139 get_thread_stack_base ()
141 return pthread_get_stackaddr_np (pthread_self ());
145 #error Threads enabled with old BDW-GC, but missing get_thread_stack_base impl. Please upgrade to libgc >= 7.1.
149 GC_get_stack_base (struct GC_stack_base
*stack_base
)
151 stack_base
->mem_base
= get_thread_stack_base ();
153 /* Calculate and store off the base of this thread's register
154 backing store (RBS). Unfortunately our implementation(s) of
155 scm_ia64_register_backing_store_base are only reliable for the
156 main thread. For other threads, therefore, find out the current
157 top of the RBS, and use that as a maximum. */
158 stack_base
->reg_base
= scm_ia64_register_backing_store_base ();
163 bsp
= scm_ia64_ar_bsp (&ctx
);
164 if (stack_base
->reg_base
> bsp
)
165 stack_base
->reg_base
= bsp
;
172 GC_call_with_stack_base(void * (*fn
) (struct GC_stack_base
*, void*), void *arg
)
174 struct GC_stack_base stack_base
;
176 stack_base
.mem_base
= (void*)&stack_base
;
178 /* FIXME: Untested. */
182 stack_base
.reg_base
= scm_ia64_ar_bsp (&ctx
);
186 return fn (&stack_base
, arg
);
188 #endif /* HAVE_GC_STACK_BASE */
191 /* Now define with_gc_active and with_gc_inactive. */
193 #if (defined(HAVE_GC_DO_BLOCKING) && defined (HAVE_DECL_GC_DO_BLOCKING) && defined (HAVE_GC_CALL_WITH_GC_ACTIVE))
195 /* We have a sufficiently new libgc (7.2 or newer). */
198 with_gc_inactive (GC_fn_type func
, void *data
)
200 return GC_do_blocking (func
, data
);
204 with_gc_active (GC_fn_type func
, void *data
)
206 return GC_call_with_gc_active (func
, data
);
211 /* libgc not new enough, so never actually deactivate GC.
213 Note that though GC 7.1 does have a GC_do_blocking, it doesn't have
214 GC_call_with_gc_active. */
217 with_gc_inactive (GC_fn_type func
, void *data
)
223 with_gc_active (GC_fn_type func
, void *data
)
228 #endif /* HAVE_GC_DO_BLOCKING */
233 to_timespec (SCM t
, scm_t_timespec
*waittime
)
237 waittime
->tv_sec
= scm_to_ulong (SCM_CAR (t
));
238 waittime
->tv_nsec
= scm_to_ulong (SCM_CDR (t
)) * 1000;
242 double time
= scm_to_double (t
);
243 double sec
= scm_c_truncate (time
);
245 waittime
->tv_sec
= (long) sec
;
246 waittime
->tv_nsec
= (long) ((time
- sec
) * 1000000000);
253 /* Note: We annotate with "GC-robust" assignments whose purpose is to avoid
254 the risk of false references leading to unbounded retained space as
255 described in "Bounding Space Usage of Conservative Garbage Collectors",
258 /* Make an empty queue data structure.
263 return scm_cons (SCM_EOL
, SCM_EOL
);
266 /* Put T at the back of Q and return a handle that can be used with
267 remqueue to remove T from Q again.
270 enqueue (SCM q
, SCM t
)
272 SCM c
= scm_cons (t
, SCM_EOL
);
273 SCM_CRITICAL_SECTION_START
;
274 if (scm_is_null (SCM_CDR (q
)))
277 SCM_SETCDR (SCM_CAR (q
), c
);
279 SCM_CRITICAL_SECTION_END
;
283 /* Remove the element that the handle C refers to from the queue Q. C
284 must have been returned from a call to enqueue. The return value
285 is zero when the element referred to by C has already been removed.
286 Otherwise, 1 is returned.
289 remqueue (SCM q
, SCM c
)
292 SCM_CRITICAL_SECTION_START
;
293 for (p
= SCM_CDR (q
); !scm_is_null (p
); p
= SCM_CDR (p
))
295 if (scm_is_eq (p
, c
))
297 if (scm_is_eq (c
, SCM_CAR (q
)))
298 SCM_SETCAR (q
, SCM_CDR (c
));
299 SCM_SETCDR (prev
, SCM_CDR (c
));
302 SCM_SETCDR (c
, SCM_EOL
);
304 SCM_CRITICAL_SECTION_END
;
309 SCM_CRITICAL_SECTION_END
;
313 /* Remove the front-most element from the queue Q and return it.
314 Return SCM_BOOL_F when Q is empty.
320 SCM_CRITICAL_SECTION_START
;
324 SCM_CRITICAL_SECTION_END
;
329 SCM_SETCDR (q
, SCM_CDR (c
));
330 if (scm_is_null (SCM_CDR (q
)))
331 SCM_SETCAR (q
, SCM_EOL
);
332 SCM_CRITICAL_SECTION_END
;
335 SCM_SETCDR (c
, SCM_EOL
);
341 /*** Thread smob routines */
345 thread_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
347 /* On a Gnu system pthread_t is an unsigned long, but on mingw it's a
348 struct. A cast like "(unsigned long) t->pthread" is a syntax error in
349 the struct case, hence we go via a union, and extract according to the
350 size of pthread_t. */
358 scm_i_thread
*t
= SCM_I_THREAD_DATA (exp
);
359 scm_i_pthread_t p
= t
->pthread
;
362 if (sizeof (p
) == sizeof (unsigned short))
364 else if (sizeof (p
) == sizeof (unsigned int))
366 else if (sizeof (p
) == sizeof (unsigned long))
371 scm_puts ("#<thread ", port
);
372 scm_uintprint (id
, 10, port
);
373 scm_puts (" (", port
);
374 scm_uintprint ((scm_t_bits
)t
, 16, port
);
375 scm_puts (")>", port
);
380 /*** Blocking on queues. */
382 /* See also scm_i_queue_async_cell for how such a block is
386 /* Put the current thread on QUEUE and go to sleep, waiting for it to
387 be woken up by a call to 'unblock_from_queue', or to be
388 interrupted. Upon return of this function, the current thread is
389 no longer on QUEUE, even when the sleep has been interrupted.
391 The caller of block_self must hold MUTEX. It will be atomically
392 unlocked while sleeping, just as with scm_i_pthread_cond_wait.
394 SLEEP_OBJECT is an arbitrary SCM value that is kept alive as long
397 When WAITTIME is not NULL, the sleep will be aborted at that time.
399 The return value of block_self is an errno value. It will be zero
400 when the sleep has been successfully completed by a call to
401 unblock_from_queue, EINTR when it has been interrupted by the
402 delivery of a system async, and ETIMEDOUT when the timeout has
405 The system asyncs themselves are not executed by block_self.
408 block_self (SCM queue
, SCM sleep_object
, scm_i_pthread_mutex_t
*mutex
,
409 const scm_t_timespec
*waittime
)
411 scm_i_thread
*t
= SCM_I_CURRENT_THREAD
;
415 if (scm_i_setup_sleep (t
, sleep_object
, mutex
, -1))
420 q_handle
= enqueue (queue
, t
->handle
);
421 if (waittime
== NULL
)
422 err
= scm_i_scm_pthread_cond_wait (&t
->sleep_cond
, mutex
);
424 err
= scm_i_scm_pthread_cond_timedwait (&t
->sleep_cond
, mutex
, waittime
);
426 /* When we are still on QUEUE, we have been interrupted. We
427 report this only when no other error (such as a timeout) has
430 if (remqueue (queue
, q_handle
) && err
== 0)
433 scm_i_reset_sleep (t
);
439 /* Wake up the first thread on QUEUE, if any. The awoken thread is
440 returned, or #f if the queue was empty.
443 unblock_from_queue (SCM queue
)
445 SCM thread
= dequeue (queue
);
446 if (scm_is_true (thread
))
447 scm_i_pthread_cond_signal (&SCM_I_THREAD_DATA(thread
)->sleep_cond
);
452 /* Getting into and out of guile mode.
455 /* Key used to attach a cleanup handler to a given thread. Also, if
456 thread-local storage is unavailable, this key is used to retrieve the
457 current thread with `pthread_getspecific ()'. */
458 scm_i_pthread_key_t scm_i_thread_key
;
461 #ifdef SCM_HAVE_THREAD_STORAGE_CLASS
463 /* When thread-local storage (TLS) is available, a pointer to the
464 current-thread object is kept in TLS. Note that storing the thread-object
465 itself in TLS (rather than a pointer to some malloc'd memory) is not
466 possible since thread objects may live longer than the actual thread they
468 SCM_THREAD_LOCAL scm_i_thread
*scm_i_current_thread
= NULL
;
470 #endif /* SCM_HAVE_THREAD_STORAGE_CLASS */
473 static scm_i_pthread_mutex_t thread_admin_mutex
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
474 static scm_i_thread
*all_threads
= NULL
;
475 static int thread_count
;
477 static SCM scm_i_default_dynamic_state
;
479 /* Run when a fluid is collected. */
481 scm_i_reset_fluid (size_t n
, SCM val
)
485 scm_i_pthread_mutex_lock (&thread_admin_mutex
);
486 for (t
= all_threads
; t
; t
= t
->next_thread
)
487 if (SCM_I_DYNAMIC_STATE_P (t
->dynamic_state
))
489 SCM v
= SCM_I_DYNAMIC_STATE_FLUIDS (t
->dynamic_state
);
491 if (n
< SCM_SIMPLE_VECTOR_LENGTH (v
))
492 SCM_SIMPLE_VECTOR_SET (v
, n
, val
);
494 scm_i_pthread_mutex_unlock (&thread_admin_mutex
);
497 /* Perform first stage of thread initialisation, in non-guile mode.
500 guilify_self_1 (struct GC_stack_base
*base
)
504 /* We must arrange for SCM_I_CURRENT_THREAD to point to a valid value
505 before allocating anything in this thread, because allocation could
506 cause GC to run, and GC could cause finalizers, which could invoke
507 Scheme functions, which need the current thread to be set. */
509 t
.pthread
= scm_i_pthread_self ();
510 t
.handle
= SCM_BOOL_F
;
511 t
.result
= SCM_BOOL_F
;
512 t
.cleanup_handler
= SCM_BOOL_F
;
515 t
.join_queue
= SCM_EOL
;
516 t
.dynamic_state
= SCM_BOOL_F
;
517 t
.dynwinds
= SCM_EOL
;
518 t
.active_asyncs
= SCM_EOL
;
520 t
.pending_asyncs
= 1;
521 t
.critical_section_level
= 0;
522 t
.base
= base
->mem_base
;
524 t
.register_backing_store_base
= base
->reg
-base
;
526 t
.continuation_root
= SCM_EOL
;
527 t
.continuation_base
= t
.base
;
528 scm_i_pthread_cond_init (&t
.sleep_cond
, NULL
);
529 t
.sleep_mutex
= NULL
;
530 t
.sleep_object
= SCM_BOOL_F
;
533 if (pipe2 (t
.sleep_pipe
, O_CLOEXEC
) != 0)
534 /* FIXME: Error conditions during the initialization phase are handled
535 gracelessly since public functions such as `scm_init_guile ()'
536 currently have type `void'. */
539 scm_i_pthread_mutex_init (&t
.admin_mutex
, NULL
);
540 t
.current_mark_stack_ptr
= NULL
;
541 t
.current_mark_stack_limit
= NULL
;
546 /* The switcheroo. */
548 scm_i_thread
*t_ptr
= &t
;
551 t_ptr
= GC_malloc (sizeof (scm_i_thread
));
552 memcpy (t_ptr
, &t
, sizeof t
);
554 scm_i_pthread_setspecific (scm_i_thread_key
, t_ptr
);
556 #ifdef SCM_HAVE_THREAD_STORAGE_CLASS
557 /* Cache the current thread in TLS for faster lookup. */
558 scm_i_current_thread
= t_ptr
;
561 scm_i_pthread_mutex_lock (&thread_admin_mutex
);
562 t_ptr
->next_thread
= all_threads
;
565 scm_i_pthread_mutex_unlock (&thread_admin_mutex
);
571 /* Perform second stage of thread initialisation, in guile mode.
574 guilify_self_2 (SCM parent
)
576 scm_i_thread
*t
= SCM_I_CURRENT_THREAD
;
580 SCM_NEWSMOB (t
->handle
, scm_tc16_thread
, t
);
582 t
->continuation_root
= scm_cons (t
->handle
, SCM_EOL
);
583 t
->continuation_base
= t
->base
;
586 if (scm_is_true (parent
))
587 t
->dynamic_state
= scm_make_dynamic_state (parent
);
589 t
->dynamic_state
= scm_i_make_initial_dynamic_state ();
591 t
->join_queue
= make_queue ();
598 /* We implement our own mutex type since we want them to be 'fair', we
599 want to do fancy things while waiting for them (like running
600 asyncs) and we might want to add things that are nice for
605 scm_i_pthread_mutex_t lock
;
607 int level
; /* how much the owner owns us. <= 1 for non-recursive mutexes */
609 int recursive
; /* allow recursive locking? */
610 int unchecked_unlock
; /* is it an error to unlock an unlocked mutex? */
611 int allow_external_unlock
; /* is it an error to unlock a mutex that is not
612 owned by the current thread? */
614 SCM waiting
; /* the threads waiting for this mutex. */
617 #define SCM_MUTEXP(x) SCM_SMOB_PREDICATE (scm_tc16_mutex, x)
618 #define SCM_MUTEX_DATA(x) ((fat_mutex *) SCM_SMOB_DATA (x))
621 call_cleanup (void *data
)
624 return scm_call_0 (*proc_p
);
627 /* Perform thread tear-down, in guile mode.
630 do_thread_exit (void *v
)
632 scm_i_thread
*t
= (scm_i_thread
*) v
;
634 /* Ensure the signal handling thread has been launched, because we might be
635 shutting it down. This needs to be done in Guile mode. */
636 scm_i_ensure_signal_delivery_thread ();
638 if (!scm_is_false (t
->cleanup_handler
))
640 SCM ptr
= t
->cleanup_handler
;
642 t
->cleanup_handler
= SCM_BOOL_F
;
643 t
->result
= scm_internal_catch (SCM_BOOL_T
,
645 scm_handle_by_message_noexit
, NULL
);
648 scm_i_scm_pthread_mutex_lock (&t
->admin_mutex
);
651 close (t
->sleep_pipe
[0]);
652 close (t
->sleep_pipe
[1]);
653 while (scm_is_true (unblock_from_queue (t
->join_queue
)))
656 while (!scm_is_null (t
->mutexes
))
658 SCM mutex
= SCM_WEAK_PAIR_CAR (t
->mutexes
);
660 if (!SCM_UNBNDP (mutex
))
662 fat_mutex
*m
= SCM_MUTEX_DATA (mutex
);
664 scm_i_pthread_mutex_lock (&m
->lock
);
666 /* Since MUTEX is in `t->mutexes', T must be its owner. */
667 assert (scm_is_eq (m
->owner
, t
->handle
));
669 unblock_from_queue (m
->waiting
);
671 scm_i_pthread_mutex_unlock (&m
->lock
);
674 t
->mutexes
= SCM_WEAK_PAIR_CDR (t
->mutexes
);
677 scm_i_pthread_mutex_unlock (&t
->admin_mutex
);
683 do_thread_exit_trampoline (struct GC_stack_base
*sb
, void *v
)
685 /* Won't hurt if we are already registered. */
686 #if SCM_USE_PTHREAD_THREADS
687 GC_register_my_thread (sb
);
690 return scm_with_guile (do_thread_exit
, v
);
694 on_thread_exit (void *v
)
696 /* This handler is executed in non-guile mode. */
697 scm_i_thread
*t
= (scm_i_thread
*) v
, **tp
;
699 /* If this thread was cancelled while doing a cond wait, it will
700 still have a mutex locked, so we unlock it here. */
703 scm_i_pthread_mutex_unlock (t
->held_mutex
);
704 t
->held_mutex
= NULL
;
707 /* Reinstate the current thread for purposes of scm_with_guile
708 guile-mode cleanup handlers. Only really needed in the non-TLS
709 case but it doesn't hurt to be consistent. */
710 scm_i_pthread_setspecific (scm_i_thread_key
, t
);
712 /* Scheme-level thread finalizers and other cleanup needs to happen in
714 GC_call_with_stack_base (do_thread_exit_trampoline
, t
);
716 /* Removing ourself from the list of all threads needs to happen in
717 non-guile mode since all SCM values on our stack become
718 unprotected once we are no longer in the list. */
719 scm_i_pthread_mutex_lock (&thread_admin_mutex
);
720 for (tp
= &all_threads
; *tp
; tp
= &(*tp
)->next_thread
)
723 *tp
= t
->next_thread
;
726 t
->next_thread
= NULL
;
732 /* If there's only one other thread, it could be the signal delivery
733 thread, so we need to notify it to shut down by closing its read pipe.
734 If it's not the signal delivery thread, then closing the read pipe isn't
736 if (thread_count
<= 1)
737 scm_i_close_signal_pipe ();
739 scm_i_pthread_mutex_unlock (&thread_admin_mutex
);
741 scm_i_pthread_setspecific (scm_i_thread_key
, NULL
);
743 #if SCM_USE_PTHREAD_THREADS
744 GC_unregister_my_thread ();
748 static scm_i_pthread_once_t init_thread_key_once
= SCM_I_PTHREAD_ONCE_INIT
;
751 init_thread_key (void)
753 scm_i_pthread_key_create (&scm_i_thread_key
, on_thread_exit
);
756 /* Perform any initializations necessary to make the current thread
757 known to Guile (via SCM_I_CURRENT_THREAD), initializing Guile itself,
760 BASE is the stack base to use with GC.
762 PARENT is the dynamic state to use as the parent, ot SCM_BOOL_F in
763 which case the default dynamic state is used.
765 Returns zero when the thread was known to guile already; otherwise
768 Note that it could be the case that the thread was known
769 to Guile, but not in guile mode (because we are within a
770 scm_without_guile call). Check SCM_I_CURRENT_THREAD->guile_mode to
771 be sure. New threads are put into guile mode implicitly. */
774 scm_i_init_thread_for_guile (struct GC_stack_base
*base
, SCM parent
)
776 scm_i_pthread_once (&init_thread_key_once
, init_thread_key
);
778 if (SCM_I_CURRENT_THREAD
)
780 /* Thread is already known to Guile.
786 /* This thread has not been guilified yet.
789 scm_i_pthread_mutex_lock (&scm_i_init_mutex
);
790 if (scm_initialized_p
== 0)
792 /* First thread ever to enter Guile. Run the full
795 scm_i_init_guile (base
);
797 #if defined (HAVE_GC_ALLOW_REGISTER_THREADS) && SCM_USE_PTHREAD_THREADS
798 /* Allow other threads to come in later. */
799 GC_allow_register_threads ();
802 scm_i_pthread_mutex_unlock (&scm_i_init_mutex
);
806 /* Guile is already initialized, but this thread enters it for
807 the first time. Only initialize this thread.
809 scm_i_pthread_mutex_unlock (&scm_i_init_mutex
);
811 /* Register this thread with libgc. */
812 #if SCM_USE_PTHREAD_THREADS
813 GC_register_my_thread (base
);
816 guilify_self_1 (base
);
817 guilify_self_2 (parent
);
826 struct GC_stack_base stack_base
;
828 if (GC_get_stack_base (&stack_base
) == GC_SUCCESS
)
829 scm_i_init_thread_for_guile (&stack_base
,
830 scm_i_default_dynamic_state
);
833 fprintf (stderr
, "Failed to get stack base for current thread.\n");
838 SCM_UNUSED
static void
839 scm_leave_guile_cleanup (void *x
)
841 on_thread_exit (SCM_I_CURRENT_THREAD
);
844 struct with_guile_args
852 with_guile_trampoline (void *data
)
854 struct with_guile_args
*args
= data
;
856 return scm_c_with_continuation_barrier (args
->func
, args
->data
);
860 with_guile_and_parent (struct GC_stack_base
*base
, void *data
)
865 struct with_guile_args
*args
= data
;
867 new_thread
= scm_i_init_thread_for_guile (base
, args
->parent
);
868 t
= SCM_I_CURRENT_THREAD
;
871 /* We are in Guile mode. */
872 assert (t
->guile_mode
);
874 res
= scm_c_with_continuation_barrier (args
->func
, args
->data
);
876 /* Leave Guile mode. */
879 else if (t
->guile_mode
)
881 /* Already in Guile mode. */
882 res
= scm_c_with_continuation_barrier (args
->func
, args
->data
);
886 /* We are not in Guile mode, either because we are not within a
887 scm_with_guile, or because we are within a scm_without_guile.
889 This call to scm_with_guile() could happen from anywhere on the
890 stack, and in particular lower on the stack than when it was
891 when this thread was first guilified. Thus, `base' must be
893 #if SCM_STACK_GROWS_UP
894 if (SCM_STACK_PTR (base
->mem_base
) < t
->base
)
895 t
->base
= SCM_STACK_PTR (base
->mem_base
);
897 if (SCM_STACK_PTR (base
->mem_base
) > t
->base
)
898 t
->base
= SCM_STACK_PTR (base
->mem_base
);
902 res
= with_gc_active (with_guile_trampoline
, args
);
909 scm_i_with_guile_and_parent (void *(*func
)(void *), void *data
, SCM parent
)
911 struct with_guile_args args
;
915 args
.parent
= parent
;
917 return GC_call_with_stack_base (with_guile_and_parent
, &args
);
921 scm_with_guile (void *(*func
)(void *), void *data
)
923 return scm_i_with_guile_and_parent (func
, data
,
924 scm_i_default_dynamic_state
);
928 scm_without_guile (void *(*func
)(void *), void *data
)
931 scm_i_thread
*t
= SCM_I_CURRENT_THREAD
;
935 SCM_I_CURRENT_THREAD
->guile_mode
= 0;
936 result
= with_gc_inactive (func
, data
);
937 SCM_I_CURRENT_THREAD
->guile_mode
= 1;
940 /* Otherwise we're not in guile mode, so nothing to do. */
941 result
= func (data
);
947 /*** Thread creation */
954 scm_i_pthread_mutex_t mutex
;
955 scm_i_pthread_cond_t cond
;
959 really_launch (void *d
)
961 launch_data
*data
= (launch_data
*)d
;
962 SCM thunk
= data
->thunk
, handler
= data
->handler
;
965 t
= SCM_I_CURRENT_THREAD
;
967 scm_i_scm_pthread_mutex_lock (&data
->mutex
);
968 data
->thread
= scm_current_thread ();
969 scm_i_pthread_cond_signal (&data
->cond
);
970 scm_i_pthread_mutex_unlock (&data
->mutex
);
972 if (SCM_UNBNDP (handler
))
973 t
->result
= scm_call_0 (thunk
);
975 t
->result
= scm_catch (SCM_BOOL_T
, thunk
, handler
);
981 launch_thread (void *d
)
983 launch_data
*data
= (launch_data
*)d
;
984 scm_i_pthread_detach (scm_i_pthread_self ());
985 scm_i_with_guile_and_parent (really_launch
, d
, data
->parent
);
989 SCM_DEFINE (scm_call_with_new_thread
, "call-with-new-thread", 1, 1, 0,
990 (SCM thunk
, SCM handler
),
991 "Call @code{thunk} in a new thread and with a new dynamic state,\n"
992 "returning a new thread object representing the thread. The procedure\n"
993 "@var{thunk} is called via @code{with-continuation-barrier}.\n"
995 "When @var{handler} is specified, then @var{thunk} is called from\n"
996 "within a @code{catch} with tag @code{#t} that has @var{handler} as its\n"
997 "handler. This catch is established inside the continuation barrier.\n"
999 "Once @var{thunk} or @var{handler} returns, the return value is made\n"
1000 "the @emph{exit value} of the thread and the thread is terminated.")
1001 #define FUNC_NAME s_scm_call_with_new_thread
1007 SCM_ASSERT (scm_is_true (scm_thunk_p (thunk
)), thunk
, SCM_ARG1
, FUNC_NAME
);
1008 SCM_ASSERT (SCM_UNBNDP (handler
) || scm_is_true (scm_procedure_p (handler
)),
1009 handler
, SCM_ARG2
, FUNC_NAME
);
1011 data
.parent
= scm_current_dynamic_state ();
1013 data
.handler
= handler
;
1014 data
.thread
= SCM_BOOL_F
;
1015 scm_i_pthread_mutex_init (&data
.mutex
, NULL
);
1016 scm_i_pthread_cond_init (&data
.cond
, NULL
);
1018 scm_i_scm_pthread_mutex_lock (&data
.mutex
);
1019 err
= scm_i_pthread_create (&id
, NULL
, launch_thread
, &data
);
1022 scm_i_pthread_mutex_unlock (&data
.mutex
);
1024 scm_syserror (NULL
);
1026 scm_i_scm_pthread_cond_wait (&data
.cond
, &data
.mutex
);
1027 scm_i_pthread_mutex_unlock (&data
.mutex
);
1035 scm_t_catch_body body
;
1037 scm_t_catch_handler handler
;
1040 scm_i_pthread_mutex_t mutex
;
1041 scm_i_pthread_cond_t cond
;
1045 really_spawn (void *d
)
1047 spawn_data
*data
= (spawn_data
*)d
;
1048 scm_t_catch_body body
= data
->body
;
1049 void *body_data
= data
->body_data
;
1050 scm_t_catch_handler handler
= data
->handler
;
1051 void *handler_data
= data
->handler_data
;
1052 scm_i_thread
*t
= SCM_I_CURRENT_THREAD
;
1054 scm_i_scm_pthread_mutex_lock (&data
->mutex
);
1055 data
->thread
= scm_current_thread ();
1056 scm_i_pthread_cond_signal (&data
->cond
);
1057 scm_i_pthread_mutex_unlock (&data
->mutex
);
1059 if (handler
== NULL
)
1060 t
->result
= body (body_data
);
1062 t
->result
= scm_internal_catch (SCM_BOOL_T
,
1064 handler
, handler_data
);
1070 spawn_thread (void *d
)
1072 spawn_data
*data
= (spawn_data
*)d
;
1073 scm_i_pthread_detach (scm_i_pthread_self ());
1074 scm_i_with_guile_and_parent (really_spawn
, d
, data
->parent
);
1079 scm_spawn_thread (scm_t_catch_body body
, void *body_data
,
1080 scm_t_catch_handler handler
, void *handler_data
)
1086 data
.parent
= scm_current_dynamic_state ();
1088 data
.body_data
= body_data
;
1089 data
.handler
= handler
;
1090 data
.handler_data
= handler_data
;
1091 data
.thread
= SCM_BOOL_F
;
1092 scm_i_pthread_mutex_init (&data
.mutex
, NULL
);
1093 scm_i_pthread_cond_init (&data
.cond
, NULL
);
1095 scm_i_scm_pthread_mutex_lock (&data
.mutex
);
1096 err
= scm_i_pthread_create (&id
, NULL
, spawn_thread
, &data
);
1099 scm_i_pthread_mutex_unlock (&data
.mutex
);
1101 scm_syserror (NULL
);
1103 scm_i_scm_pthread_cond_wait (&data
.cond
, &data
.mutex
);
1104 scm_i_pthread_mutex_unlock (&data
.mutex
);
1109 SCM_DEFINE (scm_yield
, "yield", 0, 0, 0,
1111 "Move the calling thread to the end of the scheduling queue.")
1112 #define FUNC_NAME s_scm_yield
1114 return scm_from_bool (scm_i_sched_yield ());
1118 SCM_DEFINE (scm_cancel_thread
, "cancel-thread", 1, 0, 0,
1120 "Asynchronously force the target @var{thread} to terminate. @var{thread} "
1121 "cannot be the current thread, and if @var{thread} has already terminated or "
1122 "been signaled to terminate, this function is a no-op.")
1123 #define FUNC_NAME s_scm_cancel_thread
1125 scm_i_thread
*t
= NULL
;
1127 SCM_VALIDATE_THREAD (1, thread
);
1128 t
= SCM_I_THREAD_DATA (thread
);
1129 scm_i_scm_pthread_mutex_lock (&t
->admin_mutex
);
1133 scm_i_pthread_mutex_unlock (&t
->admin_mutex
);
1134 scm_i_pthread_cancel (t
->pthread
);
1137 scm_i_pthread_mutex_unlock (&t
->admin_mutex
);
1139 return SCM_UNSPECIFIED
;
1143 SCM_DEFINE (scm_set_thread_cleanup_x
, "set-thread-cleanup!", 2, 0, 0,
1144 (SCM thread
, SCM proc
),
1145 "Set the thunk @var{proc} as the cleanup handler for the thread @var{thread}. "
1146 "This handler will be called when the thread exits.")
1147 #define FUNC_NAME s_scm_set_thread_cleanup_x
1151 SCM_VALIDATE_THREAD (1, thread
);
1152 if (!scm_is_false (proc
))
1153 SCM_VALIDATE_THUNK (2, proc
);
1155 t
= SCM_I_THREAD_DATA (thread
);
1156 scm_i_pthread_mutex_lock (&t
->admin_mutex
);
1158 if (!(t
->exited
|| t
->canceled
))
1159 t
->cleanup_handler
= proc
;
1161 scm_i_pthread_mutex_unlock (&t
->admin_mutex
);
1163 return SCM_UNSPECIFIED
;
1167 SCM_DEFINE (scm_thread_cleanup
, "thread-cleanup", 1, 0, 0,
1169 "Return the cleanup handler installed for the thread @var{thread}.")
1170 #define FUNC_NAME s_scm_thread_cleanup
1175 SCM_VALIDATE_THREAD (1, thread
);
1177 t
= SCM_I_THREAD_DATA (thread
);
1178 scm_i_pthread_mutex_lock (&t
->admin_mutex
);
1179 ret
= (t
->exited
|| t
->canceled
) ? SCM_BOOL_F
: t
->cleanup_handler
;
1180 scm_i_pthread_mutex_unlock (&t
->admin_mutex
);
1186 SCM
scm_join_thread (SCM thread
)
1188 return scm_join_thread_timed (thread
, SCM_UNDEFINED
, SCM_UNDEFINED
);
1191 SCM_DEFINE (scm_join_thread_timed
, "join-thread", 1, 2, 0,
1192 (SCM thread
, SCM timeout
, SCM timeoutval
),
1193 "Suspend execution of the calling thread until the target @var{thread} "
1194 "terminates, unless the target @var{thread} has already terminated. ")
1195 #define FUNC_NAME s_scm_join_thread_timed
1198 scm_t_timespec ctimeout
, *timeout_ptr
= NULL
;
1199 SCM res
= SCM_BOOL_F
;
1201 if (! (SCM_UNBNDP (timeoutval
)))
1204 SCM_VALIDATE_THREAD (1, thread
);
1205 if (scm_is_eq (scm_current_thread (), thread
))
1206 SCM_MISC_ERROR ("cannot join the current thread", SCM_EOL
);
1208 t
= SCM_I_THREAD_DATA (thread
);
1209 scm_i_scm_pthread_mutex_lock (&t
->admin_mutex
);
1211 if (! SCM_UNBNDP (timeout
))
1213 to_timespec (timeout
, &ctimeout
);
1214 timeout_ptr
= &ctimeout
;
1223 int err
= block_self (t
->join_queue
, thread
, &t
->admin_mutex
,
1233 else if (err
== ETIMEDOUT
)
1236 scm_i_pthread_mutex_unlock (&t
->admin_mutex
);
1238 scm_i_scm_pthread_mutex_lock (&t
->admin_mutex
);
1240 /* Check for exit again, since we just released and
1241 reacquired the admin mutex, before the next block_self
1242 call (which would block forever if t has already
1252 scm_i_pthread_mutex_unlock (&t
->admin_mutex
);
1258 SCM_DEFINE (scm_thread_p
, "thread?", 1, 0, 0,
1260 "Return @code{#t} if @var{obj} is a thread.")
1261 #define FUNC_NAME s_scm_thread_p
1263 return SCM_I_IS_THREAD(obj
) ? SCM_BOOL_T
: SCM_BOOL_F
;
1269 fat_mutex_free (SCM mx
)
1271 fat_mutex
*m
= SCM_MUTEX_DATA (mx
);
1272 scm_i_pthread_mutex_destroy (&m
->lock
);
1277 fat_mutex_print (SCM mx
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
1279 fat_mutex
*m
= SCM_MUTEX_DATA (mx
);
1280 scm_puts ("#<mutex ", port
);
1281 scm_uintprint ((scm_t_bits
)m
, 16, port
);
1282 scm_puts (">", port
);
1287 make_fat_mutex (int recursive
, int unchecked_unlock
, int external_unlock
)
1292 m
= scm_gc_malloc (sizeof (fat_mutex
), "mutex");
1293 scm_i_pthread_mutex_init (&m
->lock
, NULL
);
1294 m
->owner
= SCM_BOOL_F
;
1297 m
->recursive
= recursive
;
1298 m
->unchecked_unlock
= unchecked_unlock
;
1299 m
->allow_external_unlock
= external_unlock
;
1301 m
->waiting
= SCM_EOL
;
1302 SCM_NEWSMOB (mx
, scm_tc16_mutex
, (scm_t_bits
) m
);
1303 m
->waiting
= make_queue ();
1307 SCM
scm_make_mutex (void)
1309 return scm_make_mutex_with_flags (SCM_EOL
);
1312 SCM_SYMBOL (unchecked_unlock_sym
, "unchecked-unlock");
1313 SCM_SYMBOL (allow_external_unlock_sym
, "allow-external-unlock");
1314 SCM_SYMBOL (recursive_sym
, "recursive");
1316 SCM_DEFINE (scm_make_mutex_with_flags
, "make-mutex", 0, 0, 1,
1318 "Create a new mutex. ")
1319 #define FUNC_NAME s_scm_make_mutex_with_flags
1321 int unchecked_unlock
= 0, external_unlock
= 0, recursive
= 0;
1324 while (! scm_is_null (ptr
))
1326 SCM flag
= SCM_CAR (ptr
);
1327 if (scm_is_eq (flag
, unchecked_unlock_sym
))
1328 unchecked_unlock
= 1;
1329 else if (scm_is_eq (flag
, allow_external_unlock_sym
))
1330 external_unlock
= 1;
1331 else if (scm_is_eq (flag
, recursive_sym
))
1334 SCM_MISC_ERROR ("unsupported mutex option: ~a", scm_list_1 (flag
));
1335 ptr
= SCM_CDR (ptr
);
1337 return make_fat_mutex (recursive
, unchecked_unlock
, external_unlock
);
1341 SCM_DEFINE (scm_make_recursive_mutex
, "make-recursive-mutex", 0, 0, 0,
1343 "Create a new recursive mutex. ")
1344 #define FUNC_NAME s_scm_make_recursive_mutex
1346 return make_fat_mutex (1, 0, 0);
1350 SCM_SYMBOL (scm_abandoned_mutex_error_key
, "abandoned-mutex-error");
1353 fat_mutex_lock (SCM mutex
, scm_t_timespec
*timeout
, SCM owner
, int *ret
)
1355 fat_mutex
*m
= SCM_MUTEX_DATA (mutex
);
1357 SCM new_owner
= SCM_UNBNDP (owner
) ? scm_current_thread() : owner
;
1358 SCM err
= SCM_BOOL_F
;
1360 struct timeval current_time
;
1362 scm_i_scm_pthread_mutex_lock (&m
->lock
);
1368 m
->owner
= new_owner
;
1371 if (SCM_I_IS_THREAD (new_owner
))
1373 scm_i_thread
*t
= SCM_I_THREAD_DATA (new_owner
);
1374 scm_i_pthread_mutex_lock (&t
->admin_mutex
);
1376 /* Only keep a weak reference to MUTEX so that it's not
1377 retained when not referenced elsewhere (bug #27450).
1378 The weak pair itself is eventually removed when MUTEX
1379 is unlocked. Note that `t->mutexes' lists mutexes
1380 currently held by T, so it should be small. */
1381 t
->mutexes
= scm_weak_car_pair (mutex
, t
->mutexes
);
1383 scm_i_pthread_mutex_unlock (&t
->admin_mutex
);
1388 else if (SCM_I_IS_THREAD (m
->owner
) && scm_c_thread_exited_p (m
->owner
))
1390 m
->owner
= new_owner
;
1391 err
= scm_cons (scm_abandoned_mutex_error_key
,
1392 scm_from_locale_string ("lock obtained on abandoned "
1397 else if (scm_is_eq (m
->owner
, new_owner
))
1406 err
= scm_cons (scm_misc_error_key
,
1407 scm_from_locale_string ("mutex already locked "
1415 if (timeout
!= NULL
)
1417 gettimeofday (¤t_time
, NULL
);
1418 if (current_time
.tv_sec
> timeout
->tv_sec
||
1419 (current_time
.tv_sec
== timeout
->tv_sec
&&
1420 current_time
.tv_usec
* 1000 > timeout
->tv_nsec
))
1426 block_self (m
->waiting
, mutex
, &m
->lock
, timeout
);
1427 scm_i_pthread_mutex_unlock (&m
->lock
);
1429 scm_i_scm_pthread_mutex_lock (&m
->lock
);
1432 scm_i_pthread_mutex_unlock (&m
->lock
);
1436 SCM
scm_lock_mutex (SCM mx
)
1438 return scm_lock_mutex_timed (mx
, SCM_UNDEFINED
, SCM_UNDEFINED
);
1441 SCM_DEFINE (scm_lock_mutex_timed
, "lock-mutex", 1, 2, 0,
1442 (SCM m
, SCM timeout
, SCM owner
),
1443 "Lock @var{mutex}. If the mutex is already locked, the calling thread "
1444 "blocks until the mutex becomes available. The function returns when "
1445 "the calling thread owns the lock on @var{mutex}. Locking a mutex that "
1446 "a thread already owns will succeed right away and will not block the "
1447 "thread. That is, Guile's mutexes are @emph{recursive}. ")
1448 #define FUNC_NAME s_scm_lock_mutex_timed
1452 scm_t_timespec cwaittime
, *waittime
= NULL
;
1454 SCM_VALIDATE_MUTEX (1, m
);
1456 if (! SCM_UNBNDP (timeout
) && ! scm_is_false (timeout
))
1458 to_timespec (timeout
, &cwaittime
);
1459 waittime
= &cwaittime
;
1462 exception
= fat_mutex_lock (m
, waittime
, owner
, &ret
);
1463 if (!scm_is_false (exception
))
1464 scm_ithrow (SCM_CAR (exception
), scm_list_1 (SCM_CDR (exception
)), 1);
1465 return ret
? SCM_BOOL_T
: SCM_BOOL_F
;
1470 lock_mutex_return_void (SCM mx
)
1472 (void) scm_lock_mutex (mx
);
1476 unlock_mutex_return_void (SCM mx
)
1478 (void) scm_unlock_mutex (mx
);
1482 scm_dynwind_lock_mutex (SCM mutex
)
1484 scm_dynwind_unwind_handler_with_scm (unlock_mutex_return_void
, mutex
,
1485 SCM_F_WIND_EXPLICITLY
);
1486 scm_dynwind_rewind_handler_with_scm (lock_mutex_return_void
, mutex
,
1487 SCM_F_WIND_EXPLICITLY
);
1490 SCM_DEFINE (scm_try_mutex
, "try-mutex", 1, 0, 0,
1492 "Try to lock @var{mutex}. If the mutex is already locked by someone "
1493 "else, return @code{#f}. Else lock the mutex and return @code{#t}. ")
1494 #define FUNC_NAME s_scm_try_mutex
1498 scm_t_timespec cwaittime
, *waittime
= NULL
;
1500 SCM_VALIDATE_MUTEX (1, mutex
);
1502 to_timespec (scm_from_int(0), &cwaittime
);
1503 waittime
= &cwaittime
;
1505 exception
= fat_mutex_lock (mutex
, waittime
, SCM_UNDEFINED
, &ret
);
1506 if (!scm_is_false (exception
))
1507 scm_ithrow (SCM_CAR (exception
), scm_list_1 (SCM_CDR (exception
)), 1);
1508 return ret
? SCM_BOOL_T
: SCM_BOOL_F
;
1512 /*** Fat condition variables */
1515 scm_i_pthread_mutex_t lock
;
1516 SCM waiting
; /* the threads waiting for this condition. */
1519 #define SCM_CONDVARP(x) SCM_SMOB_PREDICATE (scm_tc16_condvar, x)
1520 #define SCM_CONDVAR_DATA(x) ((fat_cond *) SCM_SMOB_DATA (x))
1523 fat_mutex_unlock (SCM mutex
, SCM cond
,
1524 const scm_t_timespec
*waittime
, int relock
)
1527 fat_mutex
*m
= SCM_MUTEX_DATA (mutex
);
1529 scm_i_thread
*t
= SCM_I_CURRENT_THREAD
;
1530 int err
= 0, ret
= 0;
1532 scm_i_scm_pthread_mutex_lock (&m
->lock
);
1536 if (!scm_is_eq (owner
, t
->handle
))
1540 if (!m
->unchecked_unlock
)
1542 scm_i_pthread_mutex_unlock (&m
->lock
);
1543 scm_misc_error (NULL
, "mutex not locked", SCM_EOL
);
1547 else if (!m
->allow_external_unlock
)
1549 scm_i_pthread_mutex_unlock (&m
->lock
);
1550 scm_misc_error (NULL
, "mutex not locked by current thread", SCM_EOL
);
1554 if (! (SCM_UNBNDP (cond
)))
1556 c
= SCM_CONDVAR_DATA (cond
);
1565 /* Change the owner of MUTEX. */
1566 t
->mutexes
= scm_delq_x (mutex
, t
->mutexes
);
1567 m
->owner
= unblock_from_queue (m
->waiting
);
1572 err
= block_self (c
->waiting
, cond
, &m
->lock
, waittime
);
1573 scm_i_pthread_mutex_unlock (&m
->lock
);
1580 else if (err
== ETIMEDOUT
)
1585 else if (err
!= EINTR
)
1588 scm_syserror (NULL
);
1594 scm_lock_mutex_timed (mutex
, SCM_UNDEFINED
, owner
);
1602 scm_remember_upto_here_2 (cond
, mutex
);
1604 scm_i_scm_pthread_mutex_lock (&m
->lock
);
1613 /* Change the owner of MUTEX. */
1614 t
->mutexes
= scm_delq_x (mutex
, t
->mutexes
);
1615 m
->owner
= unblock_from_queue (m
->waiting
);
1618 scm_i_pthread_mutex_unlock (&m
->lock
);
1625 SCM
scm_unlock_mutex (SCM mx
)
1627 return scm_unlock_mutex_timed (mx
, SCM_UNDEFINED
, SCM_UNDEFINED
);
1630 SCM_DEFINE (scm_unlock_mutex_timed
, "unlock-mutex", 1, 2, 0,
1631 (SCM mx
, SCM cond
, SCM timeout
),
1632 "Unlocks @var{mutex} if the calling thread owns the lock on "
1633 "@var{mutex}. Calling unlock-mutex on a mutex not owned by the current "
1634 "thread results in undefined behaviour. Once a mutex has been unlocked, "
1635 "one thread blocked on @var{mutex} is awakened and grabs the mutex "
1636 "lock. Every call to @code{lock-mutex} by this thread must be matched "
1637 "with a call to @code{unlock-mutex}. Only the last call to "
1638 "@code{unlock-mutex} will actually unlock the mutex. ")
1639 #define FUNC_NAME s_scm_unlock_mutex_timed
1641 scm_t_timespec cwaittime
, *waittime
= NULL
;
1643 SCM_VALIDATE_MUTEX (1, mx
);
1644 if (! (SCM_UNBNDP (cond
)))
1646 SCM_VALIDATE_CONDVAR (2, cond
);
1648 if (! (SCM_UNBNDP (timeout
)))
1650 to_timespec (timeout
, &cwaittime
);
1651 waittime
= &cwaittime
;
1655 return fat_mutex_unlock (mx
, cond
, waittime
, 0) ? SCM_BOOL_T
: SCM_BOOL_F
;
1659 SCM_DEFINE (scm_mutex_p
, "mutex?", 1, 0, 0,
1661 "Return @code{#t} if @var{obj} is a mutex.")
1662 #define FUNC_NAME s_scm_mutex_p
1664 return SCM_MUTEXP (obj
) ? SCM_BOOL_T
: SCM_BOOL_F
;
1668 SCM_DEFINE (scm_mutex_owner
, "mutex-owner", 1, 0, 0,
1670 "Return the thread owning @var{mx}, or @code{#f}.")
1671 #define FUNC_NAME s_scm_mutex_owner
1674 fat_mutex
*m
= NULL
;
1676 SCM_VALIDATE_MUTEX (1, mx
);
1677 m
= SCM_MUTEX_DATA (mx
);
1678 scm_i_pthread_mutex_lock (&m
->lock
);
1680 scm_i_pthread_mutex_unlock (&m
->lock
);
1686 SCM_DEFINE (scm_mutex_level
, "mutex-level", 1, 0, 0,
1688 "Return the lock level of mutex @var{mx}.")
1689 #define FUNC_NAME s_scm_mutex_level
1691 SCM_VALIDATE_MUTEX (1, mx
);
1692 return scm_from_int (SCM_MUTEX_DATA(mx
)->level
);
1696 SCM_DEFINE (scm_mutex_locked_p
, "mutex-locked?", 1, 0, 0,
1698 "Returns @code{#t} if the mutex @var{mx} is locked.")
1699 #define FUNC_NAME s_scm_mutex_locked_p
1701 SCM_VALIDATE_MUTEX (1, mx
);
1702 return SCM_MUTEX_DATA (mx
)->level
> 0 ? SCM_BOOL_T
: SCM_BOOL_F
;
1707 fat_cond_print (SCM cv
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
1709 fat_cond
*c
= SCM_CONDVAR_DATA (cv
);
1710 scm_puts ("#<condition-variable ", port
);
1711 scm_uintprint ((scm_t_bits
)c
, 16, port
);
1712 scm_puts (">", port
);
1716 SCM_DEFINE (scm_make_condition_variable
, "make-condition-variable", 0, 0, 0,
1718 "Make a new condition variable.")
1719 #define FUNC_NAME s_scm_make_condition_variable
1724 c
= scm_gc_malloc (sizeof (fat_cond
), "condition variable");
1725 c
->waiting
= SCM_EOL
;
1726 SCM_NEWSMOB (cv
, scm_tc16_condvar
, (scm_t_bits
) c
);
1727 c
->waiting
= make_queue ();
1732 SCM_DEFINE (scm_timed_wait_condition_variable
, "wait-condition-variable", 2, 1, 0,
1733 (SCM cv
, SCM mx
, SCM t
),
1734 "Wait until @var{cond-var} has been signalled. While waiting, "
1735 "@var{mutex} is atomically unlocked (as with @code{unlock-mutex}) and "
1736 "is locked again when this function returns. When @var{time} is given, "
1737 "it specifies a point in time where the waiting should be aborted. It "
1738 "can be either a integer as returned by @code{current-time} or a pair "
1739 "as returned by @code{gettimeofday}. When the waiting is aborted the "
1740 "mutex is locked and @code{#f} is returned. When the condition "
1741 "variable is in fact signalled, the mutex is also locked and @code{#t} "
1743 #define FUNC_NAME s_scm_timed_wait_condition_variable
1745 scm_t_timespec waittime
, *waitptr
= NULL
;
1747 SCM_VALIDATE_CONDVAR (1, cv
);
1748 SCM_VALIDATE_MUTEX (2, mx
);
1750 if (!SCM_UNBNDP (t
))
1752 to_timespec (t
, &waittime
);
1753 waitptr
= &waittime
;
1756 return fat_mutex_unlock (mx
, cv
, waitptr
, 1) ? SCM_BOOL_T
: SCM_BOOL_F
;
1761 fat_cond_signal (fat_cond
*c
)
1763 unblock_from_queue (c
->waiting
);
1766 SCM_DEFINE (scm_signal_condition_variable
, "signal-condition-variable", 1, 0, 0,
1768 "Wake up one thread that is waiting for @var{cv}")
1769 #define FUNC_NAME s_scm_signal_condition_variable
1771 SCM_VALIDATE_CONDVAR (1, cv
);
1772 fat_cond_signal (SCM_CONDVAR_DATA (cv
));
1778 fat_cond_broadcast (fat_cond
*c
)
1780 while (scm_is_true (unblock_from_queue (c
->waiting
)))
1784 SCM_DEFINE (scm_broadcast_condition_variable
, "broadcast-condition-variable", 1, 0, 0,
1786 "Wake up all threads that are waiting for @var{cv}. ")
1787 #define FUNC_NAME s_scm_broadcast_condition_variable
1789 SCM_VALIDATE_CONDVAR (1, cv
);
1790 fat_cond_broadcast (SCM_CONDVAR_DATA (cv
));
1795 SCM_DEFINE (scm_condition_variable_p
, "condition-variable?", 1, 0, 0,
1797 "Return @code{#t} if @var{obj} is a condition variable.")
1798 #define FUNC_NAME s_scm_condition_variable_p
1800 return SCM_CONDVARP(obj
) ? SCM_BOOL_T
: SCM_BOOL_F
;
1811 SELECT_TYPE
*read_fds
;
1812 SELECT_TYPE
*write_fds
;
1813 SELECT_TYPE
*except_fds
;
1814 struct timeval
*timeout
;
1821 do_std_select (void *args
)
1823 struct select_args
*select_args
;
1825 select_args
= (struct select_args
*) args
;
1827 select_args
->result
=
1828 select (select_args
->nfds
,
1829 select_args
->read_fds
, select_args
->write_fds
,
1830 select_args
->except_fds
, select_args
->timeout
);
1831 select_args
->errno_value
= errno
;
1837 scm_std_select (int nfds
,
1838 SELECT_TYPE
*readfds
,
1839 SELECT_TYPE
*writefds
,
1840 SELECT_TYPE
*exceptfds
,
1841 struct timeval
*timeout
)
1844 int res
, eno
, wakeup_fd
;
1845 scm_i_thread
*t
= SCM_I_CURRENT_THREAD
;
1846 struct select_args args
;
1848 if (readfds
== NULL
)
1850 FD_ZERO (&my_readfds
);
1851 readfds
= &my_readfds
;
1854 while (scm_i_setup_sleep (t
, SCM_BOOL_F
, NULL
, t
->sleep_pipe
[1]))
1857 wakeup_fd
= t
->sleep_pipe
[0];
1858 FD_SET (wakeup_fd
, readfds
);
1859 if (wakeup_fd
>= nfds
)
1863 args
.read_fds
= readfds
;
1864 args
.write_fds
= writefds
;
1865 args
.except_fds
= exceptfds
;
1866 args
.timeout
= timeout
;
1868 /* Explicitly cooperate with the GC. */
1869 scm_without_guile (do_std_select
, &args
);
1872 eno
= args
.errno_value
;
1875 scm_i_reset_sleep (t
);
1877 if (res
> 0 && FD_ISSET (wakeup_fd
, readfds
))
1880 full_read (wakeup_fd
, &dummy
, 1);
1882 FD_CLR (wakeup_fd
, readfds
);
1894 /* Convenience API for blocking while in guile mode. */
1896 #if SCM_USE_PTHREAD_THREADS
1898 /* It seems reasonable to not run procedures related to mutex and condition
1899 variables within `GC_do_blocking ()' since, (i) the GC can operate even
1900 without it, and (ii) the only potential gain would be GC latency. See
1901 http://thread.gmane.org/gmane.comp.programming.garbage-collection.boehmgc/2245/focus=2251
1902 for a discussion of the pros and cons. */
1905 scm_pthread_mutex_lock (scm_i_pthread_mutex_t
*mutex
)
1907 int res
= scm_i_pthread_mutex_lock (mutex
);
1912 do_unlock (void *data
)
1914 scm_i_pthread_mutex_unlock ((scm_i_pthread_mutex_t
*)data
);
1918 scm_dynwind_pthread_mutex_lock (scm_i_pthread_mutex_t
*mutex
)
1920 scm_i_scm_pthread_mutex_lock (mutex
);
1921 scm_dynwind_unwind_handler (do_unlock
, mutex
, SCM_F_WIND_EXPLICITLY
);
1925 scm_pthread_cond_wait (scm_i_pthread_cond_t
*cond
, scm_i_pthread_mutex_t
*mutex
)
1928 scm_i_thread
*t
= SCM_I_CURRENT_THREAD
;
1930 t
->held_mutex
= mutex
;
1931 res
= scm_i_pthread_cond_wait (cond
, mutex
);
1932 t
->held_mutex
= NULL
;
1938 scm_pthread_cond_timedwait (scm_i_pthread_cond_t
*cond
,
1939 scm_i_pthread_mutex_t
*mutex
,
1940 const scm_t_timespec
*wt
)
1943 scm_i_thread
*t
= SCM_I_CURRENT_THREAD
;
1945 t
->held_mutex
= mutex
;
1946 res
= scm_i_pthread_cond_timedwait (cond
, mutex
, wt
);
1947 t
->held_mutex
= NULL
;
1955 scm_std_usleep (unsigned long usecs
)
1958 tv
.tv_usec
= usecs
% 1000000;
1959 tv
.tv_sec
= usecs
/ 1000000;
1960 scm_std_select (0, NULL
, NULL
, NULL
, &tv
);
1961 return tv
.tv_sec
* 1000000 + tv
.tv_usec
;
1965 scm_std_sleep (unsigned int secs
)
1970 scm_std_select (0, NULL
, NULL
, NULL
, &tv
);
1976 SCM_DEFINE (scm_current_thread
, "current-thread", 0, 0, 0,
1978 "Return the thread that called this function.")
1979 #define FUNC_NAME s_scm_current_thread
1981 return SCM_I_CURRENT_THREAD
->handle
;
1986 scm_c_make_list (size_t n
, SCM fill
)
1990 res
= scm_cons (fill
, res
);
1994 SCM_DEFINE (scm_all_threads
, "all-threads", 0, 0, 0,
1996 "Return a list of all threads.")
1997 #define FUNC_NAME s_scm_all_threads
1999 /* We can not allocate while holding the thread_admin_mutex because
2000 of the way GC is done.
2002 int n
= thread_count
;
2004 SCM list
= scm_c_make_list (n
, SCM_UNSPECIFIED
), *l
;
2006 scm_i_pthread_mutex_lock (&thread_admin_mutex
);
2008 for (t
= all_threads
; t
&& n
> 0; t
= t
->next_thread
)
2010 if (t
!= scm_i_signal_delivery_thread
)
2012 SCM_SETCAR (*l
, t
->handle
);
2013 l
= SCM_CDRLOC (*l
);
2018 scm_i_pthread_mutex_unlock (&thread_admin_mutex
);
2023 SCM_DEFINE (scm_thread_exited_p
, "thread-exited?", 1, 0, 0,
2025 "Return @code{#t} iff @var{thread} has exited.\n")
2026 #define FUNC_NAME s_scm_thread_exited_p
2028 return scm_from_bool (scm_c_thread_exited_p (thread
));
2033 scm_c_thread_exited_p (SCM thread
)
2034 #define FUNC_NAME s_scm_thread_exited_p
2037 SCM_VALIDATE_THREAD (1, thread
);
2038 t
= SCM_I_THREAD_DATA (thread
);
2043 SCM_DEFINE (scm_total_processor_count
, "total-processor-count", 0, 0, 0,
2045 "Return the total number of processors of the machine, which\n"
2046 "is guaranteed to be at least 1. A ``processor'' here is a\n"
2047 "thread execution unit, which can be either:\n\n"
2049 "@item an execution core in a (possibly multi-core) chip, in a\n"
2050 " (possibly multi- chip) module, in a single computer, or\n"
2051 "@item a thread execution unit inside a core in the case of\n"
2052 " @dfn{hyper-threaded} CPUs.\n"
2054 "Which of the two definitions is used, is unspecified.\n")
2055 #define FUNC_NAME s_scm_total_processor_count
2057 return scm_from_ulong (num_processors (NPROC_ALL
));
2061 SCM_DEFINE (scm_current_processor_count
, "current-processor-count", 0, 0, 0,
2063 "Like @code{total-processor-count}, but return the number of\n"
2064 "processors available to the current process. See\n"
2065 "@code{setaffinity} and @code{getaffinity} for more\n"
2067 #define FUNC_NAME s_scm_current_processor_count
2069 return scm_from_ulong (num_processors (NPROC_CURRENT
));
2076 static scm_i_pthread_cond_t wake_up_cond
;
2077 static int threads_initialized_p
= 0;
2080 /* This mutex is used by SCM_CRITICAL_SECTION_START/END.
2082 scm_i_pthread_mutex_t scm_i_critical_section_mutex
;
2084 static SCM dynwind_critical_section_mutex
;
2087 scm_dynwind_critical_section (SCM mutex
)
2089 if (scm_is_false (mutex
))
2090 mutex
= dynwind_critical_section_mutex
;
2091 scm_dynwind_lock_mutex (mutex
);
2092 scm_dynwind_block_asyncs ();
2095 /*** Initialization */
2097 scm_i_pthread_mutex_t scm_i_misc_mutex
;
2099 #if SCM_USE_PTHREAD_THREADS
2100 pthread_mutexattr_t scm_i_pthread_mutexattr_recursive
[1];
2104 scm_threads_prehistory (void *base
)
2106 #if SCM_USE_PTHREAD_THREADS
2107 pthread_mutexattr_init (scm_i_pthread_mutexattr_recursive
);
2108 pthread_mutexattr_settype (scm_i_pthread_mutexattr_recursive
,
2109 PTHREAD_MUTEX_RECURSIVE
);
2112 scm_i_pthread_mutex_init (&scm_i_critical_section_mutex
,
2113 scm_i_pthread_mutexattr_recursive
);
2114 scm_i_pthread_mutex_init (&scm_i_misc_mutex
, NULL
);
2115 scm_i_pthread_cond_init (&wake_up_cond
, NULL
);
2117 guilify_self_1 ((struct GC_stack_base
*) base
);
2120 scm_t_bits scm_tc16_thread
;
2121 scm_t_bits scm_tc16_mutex
;
2122 scm_t_bits scm_tc16_condvar
;
2127 scm_tc16_thread
= scm_make_smob_type ("thread", sizeof (scm_i_thread
));
2128 scm_set_smob_print (scm_tc16_thread
, thread_print
);
2130 scm_tc16_mutex
= scm_make_smob_type ("mutex", sizeof (fat_mutex
));
2131 scm_set_smob_print (scm_tc16_mutex
, fat_mutex_print
);
2132 scm_set_smob_free (scm_tc16_mutex
, fat_mutex_free
);
2134 scm_tc16_condvar
= scm_make_smob_type ("condition-variable",
2136 scm_set_smob_print (scm_tc16_condvar
, fat_cond_print
);
2138 scm_i_default_dynamic_state
= SCM_BOOL_F
;
2139 guilify_self_2 (SCM_BOOL_F
);
2140 threads_initialized_p
= 1;
2142 dynwind_critical_section_mutex
= scm_make_recursive_mutex ();
2146 scm_init_threads_default_dynamic_state ()
2148 SCM state
= scm_make_dynamic_state (scm_current_dynamic_state ());
2149 scm_i_default_dynamic_state
= state
;
2153 scm_init_thread_procs ()
2155 #include "libguile/threads.x"
2159 /* IA64-specific things. */
2163 # include <sys/param.h>
2164 # include <sys/pstat.h>
2166 scm_ia64_register_backing_store_base (void)
2168 struct pst_vm_status vm_status
;
2170 while (pstat_getprocvm (&vm_status
, sizeof (vm_status
), 0, i
++) == 1)
2171 if (vm_status
.pst_type
== PS_RSESTACK
)
2172 return (void *) vm_status
.pst_vaddr
;
2176 scm_ia64_ar_bsp (const void *ctx
)
2179 __uc_get_ar_bsp (ctx
, &bsp
);
2180 return (void *) bsp
;
2184 # include <ucontext.h>
2186 scm_ia64_register_backing_store_base (void)
2188 extern void *__libc_ia64_register_backing_store_base
;
2189 return __libc_ia64_register_backing_store_base
;
2192 scm_ia64_ar_bsp (const void *opaque
)
2194 const ucontext_t
*ctx
= opaque
;
2195 return (void *) ctx
->uc_mcontext
.sc_ar_bsp
;
2198 #endif /* __ia64__ */