6 /* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libguile/__scm.h"
26 #include "libguile/procs.h"
27 #include "libguile/throw.h"
28 #include "libguile/root.h"
29 #include "libguile/iselect.h"
30 #include "libguile/dynwind.h"
31 #include "libguile/continuations.h"
33 #if SCM_USE_PTHREAD_THREADS
34 #include "libguile/pthread-threads.h"
37 #if SCM_USE_NULL_THREADS
38 #include "libguile/null-threads.h"
43 /* smob tags for the thread datatypes */
44 SCM_API scm_t_bits scm_tc16_thread
;
45 SCM_API scm_t_bits scm_tc16_mutex
;
46 SCM_API scm_t_bits scm_tc16_condvar
;
48 typedef struct scm_i_thread
{
49 struct scm_i_thread
*next_thread
;
52 scm_i_pthread_t pthread
;
57 scm_i_pthread_mutex_t admin_mutex
;
65 scm_i_pthread_mutex_t
*sleep_mutex
;
66 scm_i_pthread_cond_t sleep_cond
;
67 int sleep_fd
, sleep_pipe
[2];
69 /* Information about the Boehm-GC mark stack during the mark phase. This
70 is used by `scm_gc_mark ()'. */
71 void *current_mark_stack_ptr
;
72 void *current_mark_stack_limit
;
74 /* Other thread local things.
77 scm_t_debug_frame
*last_debug_frame
;
82 SCM active_asyncs
; /* The thunks to be run at the next
84 unsigned int block_asyncs
; /* Non-zero means that asyncs should
86 unsigned int pending_asyncs
; /* Non-zero means that asyncs might be pending.
89 /* The current continuation root and the stack base for it.
91 The continuation root is an arbitrary but unique object that
92 identifies a dynamic extent. Continuations created during that
93 extent can also only be invoked during it.
95 We use pairs where the car is the thread handle and the cdr links
96 to the previous pair. This might be used for better error
97 messages but is not essential for identifying continuation roots.
99 The continuation base is the far end of the stack upto which it
102 SCM continuation_root
;
103 SCM_STACKITEM
*continuation_base
;
105 /* For keeping track of the stack and registers. */
110 void *register_backing_store_base
;
111 scm_t_contregs
*pending_rbs_continuation
;
116 #define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x)
117 #define SCM_I_THREAD_DATA(x) ((scm_i_thread *) SCM_SMOB_DATA (x))
119 #define SCM_VALIDATE_THREAD(pos, a) \
120 scm_assert_smob_type (scm_tc16_thread, (a))
121 #define SCM_VALIDATE_MUTEX(pos, a) \
122 scm_assert_smob_type (scm_tc16_mutex, (a))
123 #define SCM_VALIDATE_CONDVAR(pos, a) \
124 scm_assert_smob_type (scm_tc16_condvar, (a))
126 SCM_API SCM
scm_spawn_thread (scm_t_catch_body body
, void *body_data
,
127 scm_t_catch_handler handler
, void *handler_data
);
129 SCM_API
void *scm_without_guile (void *(*func
)(void *), void *data
);
130 SCM_API
void *scm_with_guile (void *(*func
)(void *), void *data
);
132 SCM_INTERNAL
void *scm_i_with_guile_and_parent (void *(*func
)(void *),
133 void *data
, SCM parent
);
136 void scm_threads_prehistory (SCM_STACKITEM
*);
137 void scm_threads_init_first_thread (void);
139 SCM_INTERNAL
void scm_init_threads (void);
140 SCM_INTERNAL
void scm_init_thread_procs (void);
141 SCM_INTERNAL
void scm_init_threads_default_dynamic_state (void);
144 #define SCM_THREAD_SWITCHING_CODE \
147 SCM_API SCM
scm_call_with_new_thread (SCM thunk
, SCM handler
);
148 SCM_API SCM
scm_yield (void);
149 SCM_API SCM
scm_cancel_thread (SCM t
);
150 SCM_API SCM
scm_set_thread_cleanup_x (SCM thread
, SCM proc
);
151 SCM_API SCM
scm_thread_cleanup (SCM thread
);
152 SCM_API SCM
scm_join_thread (SCM t
);
153 SCM_API SCM
scm_join_thread_timed (SCM t
, SCM timeout
, SCM timeoutval
);
154 SCM_API SCM
scm_thread_p (SCM t
);
156 SCM_API SCM
scm_make_mutex (void);
157 SCM_API SCM
scm_make_recursive_mutex (void);
158 SCM_API SCM
scm_make_mutex_with_flags (SCM flags
);
159 SCM_API SCM
scm_lock_mutex (SCM m
);
160 SCM_API SCM
scm_lock_mutex_timed (SCM m
, SCM timeout
, SCM owner
);
161 SCM_API
void scm_dynwind_lock_mutex (SCM mutex
);
162 SCM_API SCM
scm_try_mutex (SCM m
);
163 SCM_API SCM
scm_unlock_mutex (SCM m
);
164 SCM_API SCM
scm_unlock_mutex_timed (SCM m
, SCM cond
, SCM timeout
);
165 SCM_API SCM
scm_mutex_p (SCM o
);
166 SCM_API SCM
scm_mutex_locked_p (SCM m
);
167 SCM_API SCM
scm_mutex_owner (SCM m
);
168 SCM_API SCM
scm_mutex_level (SCM m
);
170 SCM_API SCM
scm_make_condition_variable (void);
171 SCM_API SCM
scm_wait_condition_variable (SCM cond
, SCM mutex
);
172 SCM_API SCM
scm_timed_wait_condition_variable (SCM cond
, SCM mutex
,
174 SCM_API SCM
scm_signal_condition_variable (SCM cond
);
175 SCM_API SCM
scm_broadcast_condition_variable (SCM cond
);
176 SCM_API SCM
scm_condition_variable_p (SCM o
);
178 SCM_API SCM
scm_current_thread (void);
179 SCM_API SCM
scm_all_threads (void);
181 SCM_API
int scm_c_thread_exited_p (SCM thread
);
182 SCM_API SCM
scm_thread_exited_p (SCM thread
);
184 SCM_API
void scm_dynwind_critical_section (SCM mutex
);
186 #define SCM_I_CURRENT_THREAD \
187 ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key))
188 SCM_API scm_i_pthread_key_t scm_i_thread_key
;
190 #define scm_i_dynwinds() (SCM_I_CURRENT_THREAD->dynwinds)
191 #define scm_i_set_dynwinds(w) (SCM_I_CURRENT_THREAD->dynwinds = (w))
192 #define scm_i_last_debug_frame() (SCM_I_CURRENT_THREAD->last_debug_frame)
193 #define scm_i_set_last_debug_frame(f) \
194 (SCM_I_CURRENT_THREAD->last_debug_frame = (f))
196 SCM_INTERNAL scm_i_pthread_mutex_t scm_i_misc_mutex
;
198 /* Convenience functions for working with the pthread API in guile
202 #if SCM_USE_PTHREAD_THREADS
203 SCM_API
int scm_pthread_mutex_lock (pthread_mutex_t
*mutex
);
204 SCM_API
void scm_dynwind_pthread_mutex_lock (pthread_mutex_t
*mutex
);
205 SCM_API
int scm_pthread_cond_wait (pthread_cond_t
*cond
,
206 pthread_mutex_t
*mutex
);
207 SCM_API
int scm_pthread_cond_timedwait (pthread_cond_t
*cond
,
208 pthread_mutex_t
*mutex
,
209 const struct timespec
*abstime
);
212 /* More convenience functions.
215 SCM_API
unsigned int scm_std_sleep (unsigned int);
216 SCM_API
unsigned long scm_std_usleep (unsigned long);
218 #endif /* SCM_THREADS_H */