Commit | Line | Data |
---|---|---|
7bfd3b9e JB |
1 | /* classes: h_files */ |
2 | ||
0527e687 DH |
3 | #ifndef SCM_THREADS_H |
4 | #define SCM_THREADS_H | |
7bfd3b9e | 5 | |
86a597f8 | 6 | /* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc. |
0527e687 | 7 | * |
73be1d9e MV |
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. | |
0527e687 | 12 | * |
73be1d9e | 13 | * This library is distributed in the hope that it will be useful, |
7bfd3b9e | 14 | * but 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 | |
92205699 | 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
73be1d9e | 21 | */ |
d3a6bc94 | 22 | |
7bfd3b9e JB |
23 | \f |
24 | ||
25 | #include "libguile/__scm.h" | |
26 | #include "libguile/procs.h" | |
1651c824 | 27 | #include "libguile/throw.h" |
f7eca35d | 28 | #include "libguile/root.h" |
d823b11b | 29 | #include "libguile/iselect.h" |
9de87eea MV |
30 | #include "libguile/dynwind.h" |
31 | ||
32 | #if SCM_USE_PTHREAD_THREADS | |
33 | #include "libguile/pthread-threads.h" | |
34 | #endif | |
35 | ||
36 | #if SCM_USE_NULL_THREADS | |
37 | #include "libguile/null-threads.h" | |
38 | #endif | |
39 | ||
b2728432 DH |
40 | \f |
41 | ||
7bfd3b9e | 42 | /* smob tags for the thread datatypes */ |
33b001fd MV |
43 | SCM_API scm_t_bits scm_tc16_thread; |
44 | SCM_API scm_t_bits scm_tc16_mutex; | |
45 | SCM_API scm_t_bits scm_tc16_condvar; | |
7bfd3b9e | 46 | |
9de87eea MV |
47 | typedef struct scm_i_thread { |
48 | struct scm_i_thread *next_thread; | |
49 | ||
50 | SCM handle; | |
51 | scm_i_pthread_t pthread; | |
2e77f720 LC |
52 | |
53 | SCM cleanup_handler; | |
9de87eea | 54 | SCM join_queue; |
86a597f8 NJ |
55 | |
56 | scm_i_pthread_mutex_t admin_mutex; | |
57 | ||
9de87eea | 58 | SCM result; |
2e77f720 | 59 | int canceled; |
9de87eea MV |
60 | int exited; |
61 | ||
62 | SCM sleep_object; | |
63 | scm_i_pthread_mutex_t *sleep_mutex; | |
64 | scm_i_pthread_cond_t sleep_cond; | |
65 | int sleep_fd, sleep_pipe[2]; | |
66 | ||
67 | /* This mutex represents this threads right to access the heap. | |
68 | That right can temporarily be taken away by the GC. | |
69 | */ | |
70 | scm_i_pthread_mutex_t heap_mutex; | |
71 | ||
72 | /* The freelists of this thread. Each thread has its own lists so | |
73 | that they can all allocate concurrently. | |
74 | */ | |
75 | SCM freelist, freelist2; | |
76 | int clear_freelists_p; /* set if GC was done while thread was asleep */ | |
1a8fdd7e MV |
77 | int gc_running_p; /* non-zero while this thread does GC or a |
78 | sweep. */ | |
9de87eea MV |
79 | |
80 | /* Other thread local things. | |
81 | */ | |
82 | SCM dynamic_state; | |
83 | scm_t_debug_frame *last_debug_frame; | |
84 | SCM dynwinds; | |
85 | ||
86 | /* For system asyncs. | |
87 | */ | |
88 | SCM active_asyncs; /* The thunks to be run at the next | |
89 | safe point */ | |
9de87eea MV |
90 | unsigned int block_asyncs; /* Non-zero means that asyncs should |
91 | not be run. */ | |
92 | unsigned int pending_asyncs; /* Non-zero means that asyncs might be pending. | |
93 | */ | |
94 | ||
95 | /* The current continuation root and the stack base for it. | |
96 | ||
97 | The continuation root is an arbitrary but unique object that | |
98 | identifies a dynamic extent. Continuations created during that | |
99 | extent can also only be invoked during it. | |
100 | ||
101 | We use pairs where the car is the thread handle and the cdr links | |
102 | to the previous pair. This might be used for better error | |
103 | messages but is not essential for identifying continuation roots. | |
104 | ||
105 | The continuation base is the far end of the stack upto which it | |
106 | needs to be copied. | |
107 | */ | |
108 | SCM continuation_root; | |
109 | SCM_STACKITEM *continuation_base; | |
110 | ||
111 | /* For keeping track of the stack and registers. */ | |
112 | SCM_STACKITEM *base; | |
113 | SCM_STACKITEM *top; | |
114 | jmp_buf regs; | |
115 | ||
116 | } scm_i_thread; | |
117 | ||
118 | #define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x) | |
119 | #define SCM_I_THREAD_DATA(x) ((scm_i_thread *) SCM_SMOB_DATA (x)) | |
7bfd3b9e | 120 | |
d823b11b | 121 | #define SCM_VALIDATE_THREAD(pos, a) \ |
9de87eea | 122 | scm_assert_smob_type (scm_tc16_thread, (a)) |
d823b11b | 123 | #define SCM_VALIDATE_MUTEX(pos, a) \ |
9de87eea | 124 | scm_assert_smob_type (scm_tc16_mutex, (a)) |
d823b11b | 125 | #define SCM_VALIDATE_CONDVAR(pos, a) \ |
9de87eea | 126 | scm_assert_smob_type (scm_tc16_condvar, (a)) |
d823b11b | 127 | |
33b001fd MV |
128 | SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data, |
129 | scm_t_catch_handler handler, void *handler_data); | |
76da80e7 | 130 | |
9de87eea | 131 | SCM_API void *scm_without_guile (void *(*func)(void *), void *data); |
9de87eea | 132 | SCM_API void *scm_with_guile (void *(*func)(void *), void *data); |
98648121 | 133 | |
9de87eea MV |
134 | SCM_API void *scm_i_with_guile_and_parent (void *(*func)(void *), void *data, |
135 | SCM parent); | |
9bc4701c | 136 | |
d823b11b | 137 | |
9bc4701c | 138 | extern int scm_i_thread_go_to_sleep; |
d823b11b | 139 | |
9bc4701c MD |
140 | void scm_i_thread_put_to_sleep (void); |
141 | void scm_i_thread_wake_up (void); | |
b0dc3d71 | 142 | void scm_i_thread_invalidate_freelists (void); |
9bc4701c | 143 | void scm_i_thread_sleep_for_gc (void); |
9de87eea MV |
144 | |
145 | void scm_threads_prehistory (SCM_STACKITEM *); | |
9bc4701c | 146 | void scm_threads_init_first_thread (void); |
9de87eea MV |
147 | SCM_API void scm_threads_mark_stacks (void); |
148 | SCM_API void scm_init_threads (void); | |
149 | SCM_API void scm_init_thread_procs (void); | |
150 | SCM_API void scm_init_threads_default_dynamic_state (void); | |
151 | ||
d823b11b MV |
152 | |
153 | #define SCM_THREAD_SWITCHING_CODE \ | |
154 | do { \ | |
9bc4701c MD |
155 | if (scm_i_thread_go_to_sleep) \ |
156 | scm_i_thread_sleep_for_gc (); \ | |
d823b11b | 157 | } while (0) |
b74f4728 | 158 | |
d823b11b | 159 | SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler); |
29717c89 | 160 | SCM_API SCM scm_yield (void); |
2e77f720 LC |
161 | SCM_API SCM scm_cancel_thread (SCM t); |
162 | SCM_API SCM scm_set_thread_cleanup_x (SCM thread, SCM proc); | |
163 | SCM_API SCM scm_thread_cleanup (SCM thread); | |
33b001fd | 164 | SCM_API SCM scm_join_thread (SCM t); |
9de87eea | 165 | |
33b001fd | 166 | SCM_API SCM scm_make_mutex (void); |
9de87eea | 167 | SCM_API SCM scm_make_recursive_mutex (void); |
33b001fd | 168 | SCM_API SCM scm_lock_mutex (SCM m); |
661ae7ab | 169 | SCM_API void scm_dynwind_lock_mutex (SCM mutex); |
5f05c406 | 170 | SCM_API SCM scm_try_mutex (SCM m); |
33b001fd | 171 | SCM_API SCM scm_unlock_mutex (SCM m); |
9de87eea | 172 | |
33b001fd MV |
173 | SCM_API SCM scm_make_condition_variable (void); |
174 | SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex); | |
5f05c406 MV |
175 | SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex, |
176 | SCM abstime); | |
33b001fd | 177 | SCM_API SCM scm_signal_condition_variable (SCM cond); |
5f05c406 | 178 | SCM_API SCM scm_broadcast_condition_variable (SCM cond); |
6d71500e | 179 | |
f7eca35d MV |
180 | SCM_API SCM scm_current_thread (void); |
181 | SCM_API SCM scm_all_threads (void); | |
182 | ||
5f05c406 MV |
183 | SCM_API int scm_c_thread_exited_p (SCM thread); |
184 | SCM_API SCM scm_thread_exited_p (SCM thread); | |
185 | ||
661ae7ab | 186 | SCM_API void scm_dynwind_critical_section (SCM mutex); |
9de87eea MV |
187 | |
188 | #define SCM_I_CURRENT_THREAD \ | |
189 | ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key)) | |
190 | SCM_API scm_i_pthread_key_t scm_i_thread_key; | |
191 | ||
192 | #define scm_i_dynwinds() (SCM_I_CURRENT_THREAD->dynwinds) | |
193 | #define scm_i_set_dynwinds(w) (SCM_I_CURRENT_THREAD->dynwinds = (w)) | |
194 | #define scm_i_last_debug_frame() (SCM_I_CURRENT_THREAD->last_debug_frame) | |
195 | #define scm_i_set_last_debug_frame(f) \ | |
196 | (SCM_I_CURRENT_THREAD->last_debug_frame = (f)) | |
f7eca35d | 197 | |
9de87eea | 198 | SCM_API scm_i_pthread_mutex_t scm_i_misc_mutex; |
9bc4701c | 199 | |
9de87eea MV |
200 | /* Convenience functions for working with the pthread API in guile |
201 | mode. | |
202 | */ | |
203 | ||
204 | #if SCM_USE_PTHREAD_THREADS | |
205 | SCM_API int scm_pthread_mutex_lock (pthread_mutex_t *mutex); | |
661ae7ab | 206 | SCM_API void scm_dynwind_pthread_mutex_lock (pthread_mutex_t *mutex); |
9de87eea MV |
207 | SCM_API int scm_pthread_cond_wait (pthread_cond_t *cond, |
208 | pthread_mutex_t *mutex); | |
209 | SCM_API int scm_pthread_cond_timedwait (pthread_cond_t *cond, | |
210 | pthread_mutex_t *mutex, | |
211 | const struct timespec *abstime); | |
212 | #endif | |
213 | ||
214 | /* More convenience functions. | |
215 | */ | |
d823b11b | 216 | |
9de87eea MV |
217 | SCM_API unsigned int scm_std_sleep (unsigned int); |
218 | SCM_API unsigned long scm_std_usleep (unsigned long); | |
50dc1840 | 219 | |
0527e687 | 220 | #endif /* SCM_THREADS_H */ |
89e00824 ML |
221 | |
222 | /* | |
223 | Local Variables: | |
224 | c-file-style: "gnu" | |
225 | End: | |
226 | */ |