(scm_i_frame_single_threaded): Removed.
[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 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/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
40 \f
41
42 /* smob tags for the thread datatypes */
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;
46
47 typedef struct scm_i_thread {
48 struct scm_i_thread *next_thread;
49
50 SCM handle;
51 scm_i_pthread_t pthread;
52
53 SCM join_queue;
54 SCM result;
55 int exited;
56
57 SCM sleep_object;
58 scm_i_pthread_mutex_t *sleep_mutex;
59 scm_i_pthread_cond_t sleep_cond;
60 int sleep_fd, sleep_pipe[2];
61
62 /* This mutex represents this threads right to access the heap.
63 That right can temporarily be taken away by the GC.
64 */
65 scm_i_pthread_mutex_t heap_mutex;
66
67 /* The freelists of this thread. Each thread has its own lists so
68 that they can all allocate concurrently.
69 */
70 SCM freelist, freelist2;
71 int clear_freelists_p; /* set if GC was done while thread was asleep */
72
73 /* Other thread local things.
74 */
75 SCM dynamic_state;
76 scm_t_debug_frame *last_debug_frame;
77 SCM dynwinds;
78
79 /* For system asyncs.
80 */
81 SCM active_asyncs; /* The thunks to be run at the next
82 safe point */
83 unsigned int block_asyncs; /* Non-zero means that asyncs should
84 not be run. */
85 unsigned int pending_asyncs; /* Non-zero means that asyncs might be pending.
86 */
87
88 /* The current continuation root and the stack base for it.
89
90 The continuation root is an arbitrary but unique object that
91 identifies a dynamic extent. Continuations created during that
92 extent can also only be invoked during it.
93
94 We use pairs where the car is the thread handle and the cdr links
95 to the previous pair. This might be used for better error
96 messages but is not essential for identifying continuation roots.
97
98 The continuation base is the far end of the stack upto which it
99 needs to be copied.
100 */
101 SCM continuation_root;
102 SCM_STACKITEM *continuation_base;
103
104 /* For keeping track of the stack and registers. */
105 SCM_STACKITEM *base;
106 SCM_STACKITEM *top;
107 jmp_buf regs;
108
109 } scm_i_thread;
110
111 #define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x)
112 #define SCM_I_THREAD_DATA(x) ((scm_i_thread *) SCM_SMOB_DATA (x))
113
114 #define SCM_VALIDATE_THREAD(pos, a) \
115 scm_assert_smob_type (scm_tc16_thread, (a))
116 #define SCM_VALIDATE_MUTEX(pos, a) \
117 scm_assert_smob_type (scm_tc16_mutex, (a))
118 #define SCM_VALIDATE_CONDVAR(pos, a) \
119 scm_assert_smob_type (scm_tc16_condvar, (a))
120
121 SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
122 scm_t_catch_handler handler, void *handler_data);
123
124 typedef void *scm_t_guile_ticket;
125 SCM_API void scm_enter_guile (scm_t_guile_ticket ticket);
126 SCM_API scm_t_guile_ticket scm_leave_guile (void);
127 SCM_API void *scm_without_guile (void *(*func)(void *), void *data);
128
129 SCM_API void *scm_with_guile (void *(*func)(void *), void *data);
130 SCM_API void *scm_i_with_guile_and_parent (void *(*func)(void *), void *data,
131 SCM parent);
132
133
134 extern int scm_i_thread_go_to_sleep;
135
136 void scm_i_thread_put_to_sleep (void);
137 void scm_i_thread_wake_up (void);
138 void scm_i_thread_invalidate_freelists (void);
139 void scm_i_thread_sleep_for_gc (void);
140
141 void scm_threads_prehistory (SCM_STACKITEM *);
142 void scm_threads_init_first_thread (void);
143 SCM_API void scm_threads_mark_stacks (void);
144 SCM_API void scm_init_threads (void);
145 SCM_API void scm_init_thread_procs (void);
146 SCM_API void scm_init_threads_default_dynamic_state (void);
147
148
149 #define SCM_THREAD_SWITCHING_CODE \
150 do { \
151 if (scm_i_thread_go_to_sleep) \
152 scm_i_thread_sleep_for_gc (); \
153 } while (0)
154
155 SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler);
156 SCM_API SCM scm_yield (void);
157 SCM_API SCM scm_join_thread (SCM t);
158
159 SCM_API SCM scm_make_mutex (void);
160 SCM_API SCM scm_make_recursive_mutex (void);
161 SCM_API SCM scm_lock_mutex (SCM m);
162 SCM_API void scm_frame_lock_mutex (SCM mutex);
163 SCM_API SCM scm_try_mutex (SCM m);
164 SCM_API SCM scm_unlock_mutex (SCM m);
165
166 SCM_API SCM scm_make_condition_variable (void);
167 SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex);
168 SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex,
169 SCM abstime);
170 SCM_API SCM scm_signal_condition_variable (SCM cond);
171 SCM_API SCM scm_broadcast_condition_variable (SCM cond);
172
173 SCM_API SCM scm_current_thread (void);
174 SCM_API SCM scm_all_threads (void);
175
176 SCM_API int scm_c_thread_exited_p (SCM thread);
177 SCM_API SCM scm_thread_exited_p (SCM thread);
178
179 SCM_API void scm_frame_critical_section (SCM mutex);
180
181 #define SCM_I_CURRENT_THREAD \
182 ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key))
183 SCM_API scm_i_pthread_key_t scm_i_thread_key;
184
185 #define scm_i_dynwinds() (SCM_I_CURRENT_THREAD->dynwinds)
186 #define scm_i_set_dynwinds(w) (SCM_I_CURRENT_THREAD->dynwinds = (w))
187 #define scm_i_last_debug_frame() (SCM_I_CURRENT_THREAD->last_debug_frame)
188 #define scm_i_set_last_debug_frame(f) \
189 (SCM_I_CURRENT_THREAD->last_debug_frame = (f))
190
191 SCM_API scm_i_pthread_mutex_t scm_i_misc_mutex;
192
193 /* Convenience functions for working with the pthread API in guile
194 mode.
195 */
196
197 #if SCM_USE_PTHREAD_THREADS
198 SCM_API int scm_pthread_mutex_lock (pthread_mutex_t *mutex);
199 SCM_API void scm_frame_pthread_mutex_lock (pthread_mutex_t *mutex);
200 SCM_API int scm_pthread_cond_wait (pthread_cond_t *cond,
201 pthread_mutex_t *mutex);
202 SCM_API int scm_pthread_cond_timedwait (pthread_cond_t *cond,
203 pthread_mutex_t *mutex,
204 const struct timespec *abstime);
205 #endif
206
207 /* More convenience functions.
208 */
209
210 SCM_API unsigned int scm_std_sleep (unsigned int);
211 SCM_API unsigned long scm_std_usleep (unsigned long);
212
213 #endif /* SCM_THREADS_H */
214
215 /*
216 Local Variables:
217 c-file-style: "gnu"
218 End:
219 */