Merge remote-tracking branch 'origin/stable-2.0'
[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, 2009, 2011, 2012 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/dynstack.h"
31 #include "libguile/iselect.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 /* Boolean indicating whether the thread is in guile mode. */
67 int guile_mode;
68
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
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
79 /* Other thread local things.
80 */
81 SCM dynamic_state;
82
83 /* The dynamic stack. */
84 scm_t_dynstack dynstack;
85
86 /* For system asyncs.
87 */
88 SCM active_asyncs; /* The thunks to be run at the next
89 safe point */
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 vm;
113 SCM_STACKITEM *base;
114 scm_i_jmp_buf regs;
115 #ifdef __ia64__
116 void *register_backing_store_base;
117 scm_t_contregs *pending_rbs_continuation;
118 #endif
119
120 /* Whether this thread is in a critical section. */
121 int critical_section_level;
122
123 } scm_i_thread;
124
125 #define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x)
126 #define SCM_I_THREAD_DATA(x) ((scm_i_thread *) SCM_SMOB_DATA (x))
127
128 #define SCM_VALIDATE_THREAD(pos, a) \
129 scm_assert_smob_type (scm_tc16_thread, (a))
130 #define SCM_VALIDATE_MUTEX(pos, a) \
131 scm_assert_smob_type (scm_tc16_mutex, (a))
132 #define SCM_VALIDATE_CONDVAR(pos, a) \
133 scm_assert_smob_type (scm_tc16_condvar, (a))
134
135 SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
136 scm_t_catch_handler handler, void *handler_data);
137
138 SCM_API void *scm_without_guile (void *(*func)(void *), void *data);
139 SCM_API void *scm_with_guile (void *(*func)(void *), void *data);
140
141 SCM_INTERNAL void scm_i_reset_fluid (size_t);
142 SCM_INTERNAL void scm_threads_prehistory (void *);
143 SCM_INTERNAL void scm_init_threads (void);
144 SCM_INTERNAL void scm_init_thread_procs (void);
145 SCM_INTERNAL void scm_init_threads_default_dynamic_state (void);
146
147
148 SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler);
149 SCM_API SCM scm_yield (void);
150 SCM_API SCM scm_cancel_thread (SCM t);
151 SCM_API SCM scm_set_thread_cleanup_x (SCM thread, SCM proc);
152 SCM_API SCM scm_thread_cleanup (SCM thread);
153 SCM_API SCM scm_join_thread (SCM t);
154 SCM_API SCM scm_join_thread_timed (SCM t, SCM timeout, SCM timeoutval);
155 SCM_API SCM scm_thread_p (SCM t);
156
157 SCM_API SCM scm_make_mutex (void);
158 SCM_API SCM scm_make_recursive_mutex (void);
159 SCM_API SCM scm_make_mutex_with_flags (SCM flags);
160 SCM_API SCM scm_lock_mutex (SCM m);
161 SCM_API SCM scm_lock_mutex_timed (SCM m, SCM timeout, SCM owner);
162 SCM_API void scm_dynwind_lock_mutex (SCM mutex);
163 SCM_API SCM scm_try_mutex (SCM m);
164 SCM_API SCM scm_unlock_mutex (SCM m);
165 SCM_API SCM scm_unlock_mutex_timed (SCM m, SCM cond, SCM timeout);
166 SCM_API SCM scm_mutex_p (SCM o);
167 SCM_API SCM scm_mutex_locked_p (SCM m);
168 SCM_API SCM scm_mutex_owner (SCM m);
169 SCM_API SCM scm_mutex_level (SCM m);
170
171 SCM_API SCM scm_make_condition_variable (void);
172 SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex);
173 SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex,
174 SCM abstime);
175 SCM_API SCM scm_signal_condition_variable (SCM cond);
176 SCM_API SCM scm_broadcast_condition_variable (SCM cond);
177 SCM_API SCM scm_condition_variable_p (SCM o);
178
179 SCM_API SCM scm_current_thread (void);
180 SCM_API SCM scm_all_threads (void);
181
182 SCM_API int scm_c_thread_exited_p (SCM thread);
183 SCM_API SCM scm_thread_exited_p (SCM thread);
184
185 SCM_API void scm_dynwind_critical_section (SCM mutex);
186
187 #ifdef BUILDING_LIBGUILE
188
189 /* Though we don't need the key for SCM_I_CURRENT_THREAD if we have TLS,
190 we do use it for cleanup purposes. */
191 SCM_INTERNAL scm_i_pthread_key_t scm_i_thread_key;
192
193 # ifdef SCM_HAVE_THREAD_STORAGE_CLASS
194
195 SCM_INTERNAL SCM_THREAD_LOCAL scm_i_thread *scm_i_current_thread;
196 # define SCM_I_CURRENT_THREAD (scm_i_current_thread)
197
198 # else /* !SCM_HAVE_THREAD_STORAGE_CLASS */
199
200 # define SCM_I_CURRENT_THREAD \
201 ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key))
202
203 # endif /* !SCM_HAVE_THREAD_STORAGE_CLASS */
204
205 #endif /* BUILDING_LIBGUILE */
206
207 SCM_INTERNAL scm_i_pthread_mutex_t scm_i_misc_mutex;
208
209 /* Convenience functions for working with the pthread API in guile
210 mode.
211 */
212
213 #if SCM_USE_PTHREAD_THREADS
214 SCM_API int scm_pthread_mutex_lock (pthread_mutex_t *mutex);
215 SCM_API void scm_dynwind_pthread_mutex_lock (pthread_mutex_t *mutex);
216 SCM_API int scm_pthread_cond_wait (pthread_cond_t *cond,
217 pthread_mutex_t *mutex);
218 SCM_API int scm_pthread_cond_timedwait (pthread_cond_t *cond,
219 pthread_mutex_t *mutex,
220 const scm_t_timespec *abstime);
221 #endif
222
223 /* More convenience functions.
224 */
225
226 SCM_API unsigned int scm_std_sleep (unsigned int);
227 SCM_API unsigned long scm_std_usleep (unsigned long);
228
229 SCM_API SCM scm_total_processor_count (void);
230 SCM_API SCM scm_current_processor_count (void);
231
232 #endif /* SCM_THREADS_H */
233
234 /*
235 Local Variables:
236 c-file-style: "gnu"
237 End:
238 */