cebdc7b690a920991776b3fc9121a70ce37afcb7
[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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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/threads-plugin.h"
31 \f
32
33 /* smob tags for the thread datatypes */
34 SCM_API scm_t_bits scm_tc16_thread;
35 SCM_API scm_t_bits scm_tc16_mutex;
36 SCM_API scm_t_bits scm_tc16_fair_mutex;
37 SCM_API scm_t_bits scm_tc16_condvar;
38 SCM_API scm_t_bits scm_tc16_fair_condvar;
39
40 #define SCM_THREADP(x) SCM_TYP16_PREDICATE (scm_tc16_thread, x)
41 #define SCM_THREAD_DATA(x) ((scm_thread *) SCM_CELL_WORD_1 (x))
42
43 #define SCM_MUTEXP(x) SCM_TYP16_PREDICATE (scm_tc16_mutex, x)
44 #define SCM_FAIR_MUTEX_P(x) SCM_TYP16_PREDICATE (scm_tc16_fair_mutex, x)
45 #define SCM_MUTEX_DATA(x) ((void *) SCM_CELL_WORD_1 (x))
46
47 #define SCM_CONDVARP(x) SCM_TYP16_PREDICATE (scm_tc16_condvar, x)
48 #define SCM_FAIR_CONDVAR_P(x) SCM_TYP16_PREDICATE (scm_tc16_fair_condvar, x)
49 #define SCM_CONDVAR_DATA(x) ((void *) SCM_CELL_WORD_1 (x))
50
51 #define SCM_VALIDATE_THREAD(pos, a) \
52 SCM_MAKE_VALIDATE_MSG (pos, a, THREADP, "thread")
53
54 #define SCM_VALIDATE_MUTEX(pos, a) \
55 SCM_ASSERT_TYPE (SCM_MUTEXP (a) || SCM_FAIR_MUTEX_P (a), \
56 a, pos, FUNC_NAME, "mutex");
57
58 #define SCM_VALIDATE_CONDVAR(pos, a) \
59 SCM_ASSERT_TYPE (SCM_CONDVARP (a) || SCM_FAIR_CONDVAR_P (a), \
60 a, pos, FUNC_NAME, "condition variable");
61
62 SCM_API void scm_threads_mark_stacks (void);
63 SCM_API void scm_init_threads (SCM_STACKITEM *);
64 SCM_API void scm_init_thread_procs (void);
65
66 /*----------------------------------------------------------------------*/
67 /* Low-level C API */
68
69 /* The purpose of this API is seamless, simple and thread package
70 independent interaction with Guile threads from the application.
71
72 Note that Guile also uses it to implement itself, just like
73 with the rest of the application API.
74 */
75
76 /* MDJ 021209 <djurfeldt@nada.kth.se>:
77 The separation of the plugin interface (currently in
78 pthread-threads.h and null-threads.h) and the low-level C API needs
79 to be completed in a sensible way.
80 */
81
82 /* Deprecate this name and rename to scm_thread_create?
83 Introduce the other two arguments in pthread_create to prepare for
84 the future?
85 */
86 SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
87 scm_t_catch_handler handler, void *handler_data);
88
89 #define scm_thread_join scm_i_plugin_thread_join
90 #define scm_thread_detach scm_i_plugin_thread_detach
91 #define scm_thread_self scm_i_plugin_thread_self
92 #define scm_thread_yield scm_i_plugin_thread_yield
93
94 #define scm_mutex_init scm_i_plugin_mutex_init
95 #define scm_mutex_destroy scm_i_plugin_mutex_destroy
96 SCM_API int scm_mutex_lock (scm_t_mutex *m);
97 #define scm_mutex_trylock scm_i_plugin_mutex_trylock
98 #define scm_mutex_unlock scm_i_plugin_mutex_unlock
99
100 /* Guile itself needs recursive mutexes. See for example the
101 implentation of scm_force in eval.c.
102
103 Note that scm_rec_mutex_lock et al can be replaced by direct usage
104 of the corresponding pthread functions if we use the pthread
105 debugging API to access the stack top (in which case there is no
106 longer any need to save the top of the stack before blocking).
107
108 It's therefore highly motivated to use these calls in situations
109 where Guile or the application needs recursive mutexes.
110 */
111 #define scm_rec_mutex_init scm_i_plugin_rec_mutex_init
112 #define scm_rec_mutex_destroy scm_i_plugin_rec_mutex_destroy
113 /* It's a safer bet to use the following functions.
114 The future of the _init functions is uncertain.
115 */
116 SCM_API scm_t_rec_mutex *scm_make_rec_mutex (void);
117 SCM_API void scm_rec_mutex_free (scm_t_rec_mutex *);
118 SCM_API int scm_rec_mutex_lock (scm_t_rec_mutex *m);
119 #define scm_rec_mutex_trylock scm_i_plugin_rec_mutex_trylock
120 #define scm_rec_mutex_unlock scm_i_plugin_rec_mutex_unlock
121
122 #define scm_cond_init scm_i_plugin_cond_init
123 #define scm_cond_destroy scm_i_plugin_cond_destroy
124 SCM_API int scm_cond_wait (scm_t_cond *c, scm_t_mutex *m);
125 SCM_API int scm_cond_timedwait (scm_t_cond *c,
126 scm_t_mutex *m,
127 const scm_t_timespec *t);
128 #define scm_cond_signal scm_i_plugin_cond_signal
129 #define scm_cond_broadcast scm_i_plugin_cond_broadcast
130
131 #define scm_key_create scm_i_plugin_key_create
132 #define scm_key_delete scm_i_plugin_key_delete
133 #define scm_setspecific scm_i_plugin_setspecific
134 #define scm_getspecific scm_i_plugin_getspecific
135
136 #define scm_thread_select scm_internal_select
137
138 /* The application must scm_leave_guile() before entering any piece of
139 code which can
140 1. block, or
141 2. execute for any longer period of time without calling SCM_TICK
142
143 Note, though, that it is *not* necessary to use these calls
144 together with any call in this API.
145 */
146
147 SCM_API void scm_enter_guile (void);
148 SCM_API void scm_leave_guile (void);
149
150 /* Better versions (although we need the former ones also in order to
151 avoid forcing code restructuring in existing applications): */
152 /*fixme* Not implemented yet! */
153 SCM_API void *scm_in_guile (void (*func) (void*), void *data);
154 SCM_API void *scm_outside_guile (void (*func) (void*), void *data);
155
156 /* These are versions of the ordinary sleep and usleep functions
157 that play nicely with the thread system. */
158 SCM_API unsigned long scm_thread_sleep (unsigned long);
159 SCM_API unsigned long scm_thread_usleep (unsigned long);
160
161 /* End of low-level C API */
162 /*----------------------------------------------------------------------*/
163
164 typedef struct scm_thread scm_thread;
165
166 SCM_API void scm_i_enter_guile (scm_thread *t);
167 SCM_API scm_thread *scm_i_leave_guile (void);
168
169 /* Critical sections */
170
171 /* This is the generic critical section for places where we are too
172 lazy to allocate a specific mutex. */
173 extern scm_t_mutex scm_i_critical_section_mutex;
174
175 #define SCM_CRITICAL_SECTION_START \
176 scm_mutex_lock (&scm_i_critical_section_mutex)
177 #define SCM_CRITICAL_SECTION_END \
178 scm_mutex_unlock (&scm_i_critical_section_mutex)
179
180 /* This is the temporary support for the old ALLOW/DEFER ints sections */
181 extern scm_t_rec_mutex scm_i_defer_mutex;
182
183 extern int scm_i_thread_go_to_sleep;
184
185 void scm_i_thread_put_to_sleep (void);
186 void scm_i_thread_wake_up (void);
187 void scm_i_thread_invalidate_freelists (void);
188 void scm_i_thread_sleep_for_gc (void);
189 void scm_threads_prehistory (void);
190 void scm_threads_init_first_thread (void);
191
192 #define SCM_THREAD_SWITCHING_CODE \
193 do { \
194 if (scm_i_thread_go_to_sleep) \
195 scm_i_thread_sleep_for_gc (); \
196 } while (0)
197
198 SCM scm_i_create_thread (scm_t_catch_body body, void *body_data,
199 scm_t_catch_handler handler, void *handler_data,
200 SCM protects);
201
202 /* The C versions of the Scheme-visible thread functions. */
203 SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler);
204 SCM_API SCM scm_yield (void);
205 SCM_API SCM scm_join_thread (SCM t);
206 SCM_API SCM scm_make_mutex (void);
207 SCM_API SCM scm_make_fair_mutex (void);
208 SCM_API SCM scm_lock_mutex (SCM m);
209 SCM_API SCM scm_try_mutex (SCM m);
210 SCM_API SCM scm_unlock_mutex (SCM m);
211 SCM_API SCM scm_make_condition_variable (void);
212 SCM_API SCM scm_make_fair_condition_variable (void);
213 SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex);
214 SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex,
215 SCM abstime);
216 SCM_API SCM scm_signal_condition_variable (SCM cond);
217 SCM_API SCM scm_broadcast_condition_variable (SCM cond);
218
219 SCM_API SCM scm_current_thread (void);
220 SCM_API SCM scm_all_threads (void);
221
222 SCM_API int scm_c_thread_exited_p (SCM thread);
223 SCM_API SCM scm_thread_exited_p (SCM thread);
224
225 SCM_API scm_root_state *scm_i_thread_root (SCM thread);
226
227 #if SCM_USE_PTHREAD_THREADS
228 # include "libguile/pthread-threads.h"
229 #else
230 # include "libguile/null-threads.h"
231 #endif
232
233 #define SCM_CURRENT_THREAD \
234 ((scm_thread *) scm_i_plugin_getspecific (scm_i_thread_key))
235 extern scm_t_key scm_i_thread_key;
236
237 /* These macros have confusing names.
238 They really refer to the root state of the running thread. */
239 #define SCM_THREAD_LOCAL_DATA (scm_i_plugin_getspecific (scm_i_root_state_key))
240 #define SCM_SET_THREAD_LOCAL_DATA(x) scm_i_set_thread_data(x)
241 SCM_API scm_t_key scm_i_root_state_key;
242 SCM_API void scm_i_set_thread_data (void *);
243
244 #endif /* SCM_THREADS_H */
245
246 /*
247 Local Variables:
248 c-file-style: "gnu"
249 End:
250 */