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