Merge commit 'feccd2d3100fd2964d4c2df58ab3da7ce4949a66' into vm-check
[bpt/guile.git] / libguile / threads.h
1 /* classes: h_files */
2
3 #ifndef SCM_THREADS_H
4 #define SCM_THREADS_H
5
6 /* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 \f
24
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"
32
33 #if SCM_USE_PTHREAD_THREADS
34 #include "libguile/pthread-threads.h"
35 #endif
36
37 #if SCM_USE_NULL_THREADS
38 #include "libguile/null-threads.h"
39 #endif
40
41 \f
42
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;
47
48 typedef struct scm_i_thread {
49 struct scm_i_thread *next_thread;
50
51 SCM handle;
52 scm_i_pthread_t pthread;
53
54 SCM cleanup_handler;
55 SCM join_queue;
56
57 scm_i_pthread_mutex_t admin_mutex;
58 SCM mutexes;
59 scm_i_pthread_mutex_t *held_mutex;
60
61 SCM result;
62 int canceled;
63 int exited;
64
65 SCM sleep_object;
66 scm_i_pthread_mutex_t *sleep_mutex;
67 scm_i_pthread_cond_t sleep_cond;
68 int sleep_fd, sleep_pipe[2];
69
70 /* This mutex represents this threads right to access the heap.
71 That right can temporarily be taken away by the GC.
72 */
73 scm_i_pthread_mutex_t heap_mutex;
74
75 /* Boolean tracking whether the above mutex is currently locked by
76 this thread. This is equivalent to whether or not the thread is
77 in "Guile mode". This field doesn't need any protection because
78 it is only ever set or tested by the owning thread.
79 */
80 int heap_mutex_locked_by_self;
81
82 /* The freelists of this thread. Each thread has its own lists so
83 that they can all allocate concurrently.
84 */
85 SCM freelist, freelist2;
86 int clear_freelists_p; /* set if GC was done while thread was asleep */
87 int gc_running_p; /* non-zero while this thread does GC or a
88 sweep. */
89
90 /* Other thread local things.
91 */
92 SCM dynamic_state;
93 scm_t_debug_frame *last_debug_frame;
94 SCM dynwinds;
95
96 /* For system asyncs.
97 */
98 SCM active_asyncs; /* The thunks to be run at the next
99 safe point */
100 unsigned int block_asyncs; /* Non-zero means that asyncs should
101 not be run. */
102 unsigned int pending_asyncs; /* Non-zero means that asyncs might be pending.
103 */
104
105 /* The current continuation root and the stack base for it.
106
107 The continuation root is an arbitrary but unique object that
108 identifies a dynamic extent. Continuations created during that
109 extent can also only be invoked during it.
110
111 We use pairs where the car is the thread handle and the cdr links
112 to the previous pair. This might be used for better error
113 messages but is not essential for identifying continuation roots.
114
115 The continuation base is the far end of the stack upto which it
116 needs to be copied.
117 */
118 SCM continuation_root;
119 SCM_STACKITEM *continuation_base;
120
121 /* For keeping track of the stack and registers. */
122 SCM vm;
123 SCM_STACKITEM *base;
124 SCM_STACKITEM *top;
125 jmp_buf regs;
126 #ifdef __ia64__
127 void *register_backing_store_base;
128 scm_t_contregs *pending_rbs_continuation;
129 #endif
130
131 } scm_i_thread;
132
133 #define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x)
134 #define SCM_I_THREAD_DATA(x) ((scm_i_thread *) SCM_SMOB_DATA (x))
135
136 #define SCM_VALIDATE_THREAD(pos, a) \
137 scm_assert_smob_type (scm_tc16_thread, (a))
138 #define SCM_VALIDATE_MUTEX(pos, a) \
139 scm_assert_smob_type (scm_tc16_mutex, (a))
140 #define SCM_VALIDATE_CONDVAR(pos, a) \
141 scm_assert_smob_type (scm_tc16_condvar, (a))
142
143 SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
144 scm_t_catch_handler handler, void *handler_data);
145
146 SCM_API void *scm_without_guile (void *(*func)(void *), void *data);
147 SCM_API void *scm_with_guile (void *(*func)(void *), void *data);
148
149 SCM_INTERNAL void *scm_i_with_guile_and_parent (void *(*func)(void *),
150 void *data, SCM parent);
151
152
153 extern int scm_i_thread_go_to_sleep;
154
155 SCM_INTERNAL void scm_i_thread_put_to_sleep (void);
156 SCM_INTERNAL void scm_i_thread_wake_up (void);
157 SCM_INTERNAL void scm_i_thread_invalidate_freelists (void);
158 void scm_i_thread_sleep_for_gc (void);
159
160 SCM_INTERNAL void scm_threads_prehistory (SCM_STACKITEM *);
161 SCM_INTERNAL void scm_threads_init_first_thread (void);
162 SCM_INTERNAL void scm_threads_mark_stacks (void);
163 SCM_INTERNAL void scm_init_threads (void);
164 SCM_INTERNAL void scm_init_thread_procs (void);
165 SCM_INTERNAL void scm_init_threads_default_dynamic_state (void);
166
167
168 #define SCM_THREAD_SWITCHING_CODE \
169 do { \
170 if (scm_i_thread_go_to_sleep) \
171 scm_i_thread_sleep_for_gc (); \
172 } while (0)
173
174 SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler);
175 SCM_API SCM scm_yield (void);
176 SCM_API SCM scm_cancel_thread (SCM t);
177 SCM_API SCM scm_set_thread_cleanup_x (SCM thread, SCM proc);
178 SCM_API SCM scm_thread_cleanup (SCM thread);
179 SCM_API SCM scm_join_thread (SCM t);
180 SCM_API SCM scm_join_thread_timed (SCM t, SCM timeout, SCM timeoutval);
181 SCM_API SCM scm_thread_p (SCM t);
182
183 SCM_API SCM scm_make_mutex (void);
184 SCM_API SCM scm_make_recursive_mutex (void);
185 SCM_API SCM scm_make_mutex_with_flags (SCM flags);
186 SCM_API SCM scm_lock_mutex (SCM m);
187 SCM_API SCM scm_lock_mutex_timed (SCM m, SCM timeout, SCM owner);
188 SCM_API void scm_dynwind_lock_mutex (SCM mutex);
189 SCM_API SCM scm_try_mutex (SCM m);
190 SCM_API SCM scm_unlock_mutex (SCM m);
191 SCM_API SCM scm_unlock_mutex_timed (SCM m, SCM cond, SCM timeout);
192 SCM_API SCM scm_mutex_p (SCM o);
193 SCM_API SCM scm_mutex_locked_p (SCM m);
194 SCM_API SCM scm_mutex_owner (SCM m);
195 SCM_API SCM scm_mutex_level (SCM m);
196
197 SCM_API SCM scm_make_condition_variable (void);
198 SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex);
199 SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex,
200 SCM abstime);
201 SCM_API SCM scm_signal_condition_variable (SCM cond);
202 SCM_API SCM scm_broadcast_condition_variable (SCM cond);
203 SCM_API SCM scm_condition_variable_p (SCM o);
204
205 SCM_API SCM scm_current_thread (void);
206 SCM_API SCM scm_all_threads (void);
207
208 SCM_API int scm_c_thread_exited_p (SCM thread);
209 SCM_API SCM scm_thread_exited_p (SCM thread);
210
211 SCM_API void scm_dynwind_critical_section (SCM mutex);
212
213 #define SCM_I_CURRENT_THREAD \
214 ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key))
215 SCM_API scm_i_pthread_key_t scm_i_thread_key;
216
217 #define scm_i_dynwinds() (SCM_I_CURRENT_THREAD->dynwinds)
218 #define scm_i_set_dynwinds(w) (SCM_I_CURRENT_THREAD->dynwinds = (w))
219 #define scm_i_last_debug_frame() (SCM_I_CURRENT_THREAD->last_debug_frame)
220 #define scm_i_set_last_debug_frame(f) \
221 (SCM_I_CURRENT_THREAD->last_debug_frame = (f))
222
223 SCM_INTERNAL scm_i_pthread_mutex_t scm_i_misc_mutex;
224
225 /* Convenience functions for working with the pthread API in guile
226 mode.
227 */
228
229 #if SCM_USE_PTHREAD_THREADS
230 SCM_API int scm_pthread_mutex_lock (pthread_mutex_t *mutex);
231 SCM_API void scm_dynwind_pthread_mutex_lock (pthread_mutex_t *mutex);
232 SCM_API int scm_pthread_cond_wait (pthread_cond_t *cond,
233 pthread_mutex_t *mutex);
234 SCM_API int scm_pthread_cond_timedwait (pthread_cond_t *cond,
235 pthread_mutex_t *mutex,
236 const scm_t_timespec *abstime);
237 #endif
238
239 /* More convenience functions.
240 */
241
242 SCM_API unsigned int scm_std_sleep (unsigned int);
243 SCM_API unsigned long scm_std_usleep (unsigned long);
244
245 #endif /* SCM_THREADS_H */
246
247 /*
248 Local Variables:
249 c-file-style: "gnu"
250 End:
251 */