Commit | Line | Data |
---|---|---|
7bfd3b9e JB |
1 | /* classes: h_files */ |
2 | ||
0527e687 DH |
3 | #ifndef SCM_THREADS_H |
4 | #define SCM_THREADS_H | |
7bfd3b9e | 5 | |
cde24ce1 | 6 | /* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2011 Free Software Foundation, Inc. |
0527e687 | 7 | * |
73be1d9e | 8 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
9 | * modify it under the terms of the GNU Lesser General Public License |
10 | * as published by the Free Software Foundation; either version 3 of | |
11 | * the License, or (at your option) any later version. | |
0527e687 | 12 | * |
53befeb7 NJ |
13 | * This library is distributed in the hope that it will be useful, but |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | * Lesser General Public License for more details. | |
0527e687 | 17 | * |
73be1d9e MV |
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 | |
53befeb7 NJ |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
21 | * 02110-1301 USA | |
73be1d9e | 22 | */ |
d3a6bc94 | 23 | |
7bfd3b9e JB |
24 | \f |
25 | ||
26 | #include "libguile/__scm.h" | |
27 | #include "libguile/procs.h" | |
1651c824 | 28 | #include "libguile/throw.h" |
f7eca35d | 29 | #include "libguile/root.h" |
d823b11b | 30 | #include "libguile/iselect.h" |
9de87eea | 31 | #include "libguile/dynwind.h" |
346e4402 | 32 | #include "libguile/continuations.h" |
9de87eea MV |
33 | |
34 | #if SCM_USE_PTHREAD_THREADS | |
35 | #include "libguile/pthread-threads.h" | |
36 | #endif | |
37 | ||
38 | #if SCM_USE_NULL_THREADS | |
39 | #include "libguile/null-threads.h" | |
40 | #endif | |
41 | ||
b2728432 DH |
42 | \f |
43 | ||
7bfd3b9e | 44 | /* smob tags for the thread datatypes */ |
33b001fd MV |
45 | SCM_API scm_t_bits scm_tc16_thread; |
46 | SCM_API scm_t_bits scm_tc16_mutex; | |
47 | SCM_API scm_t_bits scm_tc16_condvar; | |
7bfd3b9e | 48 | |
9de87eea MV |
49 | typedef struct scm_i_thread { |
50 | struct scm_i_thread *next_thread; | |
51 | ||
52 | SCM handle; | |
53 | scm_i_pthread_t pthread; | |
2e77f720 LC |
54 | |
55 | SCM cleanup_handler; | |
9de87eea | 56 | SCM join_queue; |
86a597f8 NJ |
57 | |
58 | scm_i_pthread_mutex_t admin_mutex; | |
6180e336 | 59 | SCM mutexes; |
d2a51087 | 60 | scm_i_pthread_mutex_t *held_mutex; |
86a597f8 | 61 | |
9de87eea | 62 | SCM result; |
2e77f720 | 63 | int canceled; |
9de87eea MV |
64 | int exited; |
65 | ||
72e6b608 LC |
66 | /* Boolean indicating whether the thread is in guile mode. */ |
67 | int guile_mode; | |
68 | ||
9de87eea MV |
69 | SCM sleep_object; |
70 | scm_i_pthread_mutex_t *sleep_mutex; | |
71 | scm_i_pthread_cond_t sleep_cond; | |
72 | int sleep_fd, sleep_pipe[2]; | |
73 | ||
378f2625 LC |
74 | /* Information about the Boehm-GC mark stack during the mark phase. This |
75 | is used by `scm_gc_mark ()'. */ | |
76 | void *current_mark_stack_ptr; | |
77 | void *current_mark_stack_limit; | |
78 | ||
9de87eea MV |
79 | /* Other thread local things. |
80 | */ | |
81 | SCM dynamic_state; | |
9de87eea MV |
82 | SCM dynwinds; |
83 | ||
84 | /* For system asyncs. | |
85 | */ | |
86 | SCM active_asyncs; /* The thunks to be run at the next | |
87 | safe point */ | |
74926120 | 88 | unsigned int block_asyncs; /* Non-zero means that asyncs should |
9de87eea MV |
89 | not be run. */ |
90 | unsigned int pending_asyncs; /* Non-zero means that asyncs might be pending. | |
91 | */ | |
92 | ||
93 | /* The current continuation root and the stack base for it. | |
94 | ||
95 | The continuation root is an arbitrary but unique object that | |
96 | identifies a dynamic extent. Continuations created during that | |
97 | extent can also only be invoked during it. | |
98 | ||
99 | We use pairs where the car is the thread handle and the cdr links | |
100 | to the previous pair. This might be used for better error | |
101 | messages but is not essential for identifying continuation roots. | |
102 | ||
103 | The continuation base is the far end of the stack upto which it | |
104 | needs to be copied. | |
105 | */ | |
106 | SCM continuation_root; | |
107 | SCM_STACKITEM *continuation_base; | |
108 | ||
109 | /* For keeping track of the stack and registers. */ | |
2bbe1533 | 110 | SCM vm; |
9de87eea | 111 | SCM_STACKITEM *base; |
a4dbe1ac | 112 | scm_i_jmp_buf regs; |
346e4402 NJ |
113 | #ifdef __ia64__ |
114 | void *register_backing_store_base; | |
115 | scm_t_contregs *pending_rbs_continuation; | |
116 | #endif | |
9de87eea | 117 | |
87f30eda NJ |
118 | /* Whether this thread is in a critical section. */ |
119 | int critical_section_level; | |
120 | ||
9de87eea MV |
121 | } scm_i_thread; |
122 | ||
123 | #define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x) | |
124 | #define SCM_I_THREAD_DATA(x) ((scm_i_thread *) SCM_SMOB_DATA (x)) | |
7bfd3b9e | 125 | |
d823b11b | 126 | #define SCM_VALIDATE_THREAD(pos, a) \ |
9de87eea | 127 | scm_assert_smob_type (scm_tc16_thread, (a)) |
d823b11b | 128 | #define SCM_VALIDATE_MUTEX(pos, a) \ |
9de87eea | 129 | scm_assert_smob_type (scm_tc16_mutex, (a)) |
d823b11b | 130 | #define SCM_VALIDATE_CONDVAR(pos, a) \ |
9de87eea | 131 | scm_assert_smob_type (scm_tc16_condvar, (a)) |
d823b11b | 132 | |
33b001fd MV |
133 | SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data, |
134 | scm_t_catch_handler handler, void *handler_data); | |
76da80e7 | 135 | |
9de87eea | 136 | SCM_API void *scm_without_guile (void *(*func)(void *), void *data); |
9de87eea | 137 | SCM_API void *scm_with_guile (void *(*func)(void *), void *data); |
98648121 | 138 | |
102dbb6f LC |
139 | SCM_INTERNAL void *scm_i_with_guile_and_parent (void *(*func)(void *), |
140 | void *data, SCM parent); | |
9bc4701c | 141 | |
d823b11b | 142 | |
9de87eea | 143 | void scm_threads_prehistory (SCM_STACKITEM *); |
9bc4701c | 144 | void scm_threads_init_first_thread (void); |
6bad09ba | 145 | |
102dbb6f LC |
146 | SCM_INTERNAL void scm_init_threads (void); |
147 | SCM_INTERNAL void scm_init_thread_procs (void); | |
148 | SCM_INTERNAL void scm_init_threads_default_dynamic_state (void); | |
9de87eea | 149 | |
d823b11b MV |
150 | |
151 | #define SCM_THREAD_SWITCHING_CODE \ | |
43adae30 | 152 | do { } while (0) |
b74f4728 | 153 | |
d823b11b | 154 | SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler); |
29717c89 | 155 | SCM_API SCM scm_yield (void); |
2e77f720 LC |
156 | SCM_API SCM scm_cancel_thread (SCM t); |
157 | SCM_API SCM scm_set_thread_cleanup_x (SCM thread, SCM proc); | |
158 | SCM_API SCM scm_thread_cleanup (SCM thread); | |
33b001fd | 159 | SCM_API SCM scm_join_thread (SCM t); |
6180e336 NJ |
160 | SCM_API SCM scm_join_thread_timed (SCM t, SCM timeout, SCM timeoutval); |
161 | SCM_API SCM scm_thread_p (SCM t); | |
9de87eea | 162 | |
33b001fd | 163 | SCM_API SCM scm_make_mutex (void); |
9de87eea | 164 | SCM_API SCM scm_make_recursive_mutex (void); |
6180e336 | 165 | SCM_API SCM scm_make_mutex_with_flags (SCM flags); |
33b001fd | 166 | SCM_API SCM scm_lock_mutex (SCM m); |
adc085f1 | 167 | SCM_API SCM scm_lock_mutex_timed (SCM m, SCM timeout, SCM owner); |
661ae7ab | 168 | SCM_API void scm_dynwind_lock_mutex (SCM mutex); |
5f05c406 | 169 | SCM_API SCM scm_try_mutex (SCM m); |
33b001fd | 170 | SCM_API SCM scm_unlock_mutex (SCM m); |
6180e336 NJ |
171 | SCM_API SCM scm_unlock_mutex_timed (SCM m, SCM cond, SCM timeout); |
172 | SCM_API SCM scm_mutex_p (SCM o); | |
adc085f1 JG |
173 | SCM_API SCM scm_mutex_locked_p (SCM m); |
174 | SCM_API SCM scm_mutex_owner (SCM m); | |
175 | SCM_API SCM scm_mutex_level (SCM m); | |
9de87eea | 176 | |
33b001fd MV |
177 | SCM_API SCM scm_make_condition_variable (void); |
178 | SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex); | |
5f05c406 MV |
179 | SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex, |
180 | SCM abstime); | |
33b001fd | 181 | SCM_API SCM scm_signal_condition_variable (SCM cond); |
5f05c406 | 182 | SCM_API SCM scm_broadcast_condition_variable (SCM cond); |
6180e336 | 183 | SCM_API SCM scm_condition_variable_p (SCM o); |
6d71500e | 184 | |
f7eca35d MV |
185 | SCM_API SCM scm_current_thread (void); |
186 | SCM_API SCM scm_all_threads (void); | |
187 | ||
5f05c406 MV |
188 | SCM_API int scm_c_thread_exited_p (SCM thread); |
189 | SCM_API SCM scm_thread_exited_p (SCM thread); | |
190 | ||
661ae7ab | 191 | SCM_API void scm_dynwind_critical_section (SCM mutex); |
9de87eea | 192 | |
46935a1f LC |
193 | #ifdef BUILDING_LIBGUILE |
194 | ||
f60a7648 AW |
195 | /* Though we don't need the key for SCM_I_CURRENT_THREAD if we have TLS, |
196 | we do use it for cleanup purposes. */ | |
197 | SCM_INTERNAL scm_i_pthread_key_t scm_i_thread_key; | |
198 | ||
705edb95 LC |
199 | # ifdef SCM_HAVE_THREAD_STORAGE_CLASS |
200 | ||
201 | SCM_INTERNAL SCM_THREAD_LOCAL scm_i_thread *scm_i_current_thread; | |
202 | # define SCM_I_CURRENT_THREAD (scm_i_current_thread) | |
203 | ||
204 | # else /* !SCM_HAVE_THREAD_STORAGE_CLASS */ | |
205 | ||
705edb95 LC |
206 | # define SCM_I_CURRENT_THREAD \ |
207 | ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key)) | |
208 | ||
209 | # endif /* !SCM_HAVE_THREAD_STORAGE_CLASS */ | |
9de87eea | 210 | |
46935a1f LC |
211 | # define scm_i_dynwinds() (SCM_I_CURRENT_THREAD->dynwinds) |
212 | # define scm_i_set_dynwinds(w) (SCM_I_CURRENT_THREAD->dynwinds = (w)) | |
46935a1f LC |
213 | |
214 | #endif /* BUILDING_LIBGUILE */ | |
215 | ||
f7eca35d | 216 | |
102dbb6f | 217 | SCM_INTERNAL scm_i_pthread_mutex_t scm_i_misc_mutex; |
9bc4701c | 218 | |
9de87eea MV |
219 | /* Convenience functions for working with the pthread API in guile |
220 | mode. | |
221 | */ | |
222 | ||
223 | #if SCM_USE_PTHREAD_THREADS | |
224 | SCM_API int scm_pthread_mutex_lock (pthread_mutex_t *mutex); | |
661ae7ab | 225 | SCM_API void scm_dynwind_pthread_mutex_lock (pthread_mutex_t *mutex); |
9de87eea MV |
226 | SCM_API int scm_pthread_cond_wait (pthread_cond_t *cond, |
227 | pthread_mutex_t *mutex); | |
228 | SCM_API int scm_pthread_cond_timedwait (pthread_cond_t *cond, | |
229 | pthread_mutex_t *mutex, | |
ab878b0f | 230 | const scm_t_timespec *abstime); |
9de87eea MV |
231 | #endif |
232 | ||
233 | /* More convenience functions. | |
234 | */ | |
d823b11b | 235 | |
9de87eea MV |
236 | SCM_API unsigned int scm_std_sleep (unsigned int); |
237 | SCM_API unsigned long scm_std_usleep (unsigned long); | |
50dc1840 | 238 | |
0527e687 | 239 | #endif /* SCM_THREADS_H */ |
89e00824 ML |
240 | |
241 | /* | |
242 | Local Variables: | |
243 | c-file-style: "gnu" | |
244 | End: | |
245 | */ |