6 /* Copyright (C) 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2006,
7 * 2007, 2008, 2009, 2011, 2013 Free Software Foundation, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 3 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include "libguile/__scm.h"
28 #include "libguile/procs.h"
29 #include "libguile/throw.h"
30 #include "libguile/root.h"
31 #include "libguile/iselect.h"
32 #include "libguile/dynwind.h"
33 #include "libguile/continuations.h"
35 #if SCM_USE_PTHREAD_THREADS
36 #include "libguile/pthread-threads.h"
39 #if SCM_USE_NULL_THREADS
40 #include "libguile/null-threads.h"
45 /* smob tags for the thread datatypes */
46 SCM_API scm_t_bits scm_tc16_thread
;
47 SCM_API scm_t_bits scm_tc16_mutex
;
48 SCM_API scm_t_bits scm_tc16_condvar
;
50 typedef struct scm_i_thread
{
51 struct scm_i_thread
*next_thread
;
54 scm_i_pthread_t pthread
;
59 scm_i_pthread_mutex_t admin_mutex
;
61 scm_i_pthread_mutex_t
*held_mutex
;
67 /* Boolean indicating whether the thread is in guile mode. */
71 scm_i_pthread_mutex_t
*sleep_mutex
;
72 scm_i_pthread_cond_t sleep_cond
;
73 int sleep_fd
, sleep_pipe
[2];
75 /* XXX: These two fields used to hold information about the BDW-GC
76 mark stack during the mark phase. They are no longer used. */
77 void *current_mark_stack_ptr
;
78 void *current_mark_stack_limit
;
80 /* Other thread local things.
87 SCM active_asyncs
; /* The thunks to be run at the next
89 unsigned int block_asyncs
; /* Non-zero means that asyncs should
91 unsigned int pending_asyncs
; /* Non-zero means that asyncs might be pending.
94 /* The current continuation root and the stack base for it.
96 The continuation root is an arbitrary but unique object that
97 identifies a dynamic extent. Continuations created during that
98 extent can also only be invoked during it.
100 We use pairs where the car is the thread handle and the cdr links
101 to the previous pair. This might be used for better error
102 messages but is not essential for identifying continuation roots.
104 The continuation base is the far end of the stack upto which it
107 SCM continuation_root
;
108 SCM_STACKITEM
*continuation_base
;
110 /* For keeping track of the stack and registers. */
115 void *register_backing_store_base
;
116 scm_t_contregs
*pending_rbs_continuation
;
119 /* Whether this thread is in a critical section. */
120 int critical_section_level
;
124 #define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x)
125 #define SCM_I_THREAD_DATA(x) ((scm_i_thread *) SCM_SMOB_DATA (x))
127 #define SCM_VALIDATE_THREAD(pos, a) \
128 scm_assert_smob_type (scm_tc16_thread, (a))
129 #define SCM_VALIDATE_MUTEX(pos, a) \
130 scm_assert_smob_type (scm_tc16_mutex, (a))
131 #define SCM_VALIDATE_CONDVAR(pos, a) \
132 scm_assert_smob_type (scm_tc16_condvar, (a))
134 SCM_API SCM
scm_spawn_thread (scm_t_catch_body body
, void *body_data
,
135 scm_t_catch_handler handler
, void *handler_data
);
137 SCM_API
void *scm_without_guile (void *(*func
)(void *), void *data
);
138 SCM_API
void *scm_with_guile (void *(*func
)(void *), void *data
);
140 SCM_INTERNAL
void scm_i_reset_fluid (size_t);
141 SCM_INTERNAL
void scm_threads_prehistory (void *);
142 SCM_INTERNAL
void scm_init_threads (void);
143 SCM_INTERNAL
void scm_init_thread_procs (void);
144 SCM_INTERNAL
void scm_init_threads_default_dynamic_state (void);
147 #define SCM_THREAD_SWITCHING_CODE \
150 SCM_API SCM
scm_call_with_new_thread (SCM thunk
, SCM handler
);
151 SCM_API SCM
scm_yield (void);
152 SCM_API SCM
scm_cancel_thread (SCM t
);
153 SCM_API SCM
scm_set_thread_cleanup_x (SCM thread
, SCM proc
);
154 SCM_API SCM
scm_thread_cleanup (SCM thread
);
155 SCM_API SCM
scm_join_thread (SCM t
);
156 SCM_API SCM
scm_join_thread_timed (SCM t
, SCM timeout
, SCM timeoutval
);
157 SCM_API SCM
scm_thread_p (SCM t
);
159 SCM_API SCM
scm_make_mutex (void);
160 SCM_API SCM
scm_make_recursive_mutex (void);
161 SCM_API SCM
scm_make_mutex_with_flags (SCM flags
);
162 SCM_API SCM
scm_lock_mutex (SCM m
);
163 SCM_API SCM
scm_lock_mutex_timed (SCM m
, SCM timeout
, SCM owner
);
164 SCM_API
void scm_dynwind_lock_mutex (SCM mutex
);
165 SCM_API SCM
scm_try_mutex (SCM m
);
166 SCM_API SCM
scm_unlock_mutex (SCM m
);
167 SCM_API SCM
scm_unlock_mutex_timed (SCM m
, SCM cond
, SCM timeout
);
168 SCM_API SCM
scm_mutex_p (SCM o
);
169 SCM_API SCM
scm_mutex_locked_p (SCM m
);
170 SCM_API SCM
scm_mutex_owner (SCM m
);
171 SCM_API SCM
scm_mutex_level (SCM m
);
173 SCM_API SCM
scm_make_condition_variable (void);
174 SCM_API SCM
scm_wait_condition_variable (SCM cond
, SCM mutex
);
175 SCM_API SCM
scm_timed_wait_condition_variable (SCM cond
, SCM mutex
,
177 SCM_API SCM
scm_signal_condition_variable (SCM cond
);
178 SCM_API SCM
scm_broadcast_condition_variable (SCM cond
);
179 SCM_API SCM
scm_condition_variable_p (SCM o
);
181 SCM_API SCM
scm_current_thread (void);
182 SCM_API SCM
scm_all_threads (void);
184 SCM_API
int scm_c_thread_exited_p (SCM thread
);
185 SCM_API SCM
scm_thread_exited_p (SCM thread
);
187 SCM_API
void scm_dynwind_critical_section (SCM mutex
);
189 #ifdef BUILDING_LIBGUILE
191 /* Though we don't need the key for SCM_I_CURRENT_THREAD if we have TLS,
192 we do use it for cleanup purposes. */
193 SCM_INTERNAL scm_i_pthread_key_t scm_i_thread_key
;
195 # ifdef SCM_HAVE_THREAD_STORAGE_CLASS
197 SCM_INTERNAL SCM_THREAD_LOCAL scm_i_thread
*scm_i_current_thread
;
198 # define SCM_I_CURRENT_THREAD (scm_i_current_thread)
200 # else /* !SCM_HAVE_THREAD_STORAGE_CLASS */
202 # define SCM_I_CURRENT_THREAD \
203 ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key))
205 # endif /* !SCM_HAVE_THREAD_STORAGE_CLASS */
207 # define scm_i_dynwinds() (SCM_I_CURRENT_THREAD->dynwinds)
208 # define scm_i_set_dynwinds(w) (SCM_I_CURRENT_THREAD->dynwinds = (w))
210 #endif /* BUILDING_LIBGUILE */
213 SCM_INTERNAL scm_i_pthread_mutex_t scm_i_misc_mutex
;
215 /* Convenience functions for working with the pthread API in guile
219 #if SCM_USE_PTHREAD_THREADS
220 SCM_API
int scm_pthread_mutex_lock (pthread_mutex_t
*mutex
);
221 SCM_API
void scm_dynwind_pthread_mutex_lock (pthread_mutex_t
*mutex
);
222 SCM_API
int scm_pthread_cond_wait (pthread_cond_t
*cond
,
223 pthread_mutex_t
*mutex
);
224 SCM_API
int scm_pthread_cond_timedwait (pthread_cond_t
*cond
,
225 pthread_mutex_t
*mutex
,
226 const scm_t_timespec
*abstime
);
229 /* More convenience functions.
232 SCM_API
unsigned int scm_std_sleep (unsigned int);
233 SCM_API
unsigned long scm_std_usleep (unsigned long);
235 SCM_API SCM
scm_total_processor_count (void);
236 SCM_API SCM
scm_current_processor_count (void);
238 #endif /* SCM_THREADS_H */