(read_without_guile): New.
[bpt/guile.git] / libguile / threads.h
CommitLineData
7bfd3b9e
JB
1/* classes: h_files */
2
0527e687
DH
3#ifndef SCM_THREADS_H
4#define SCM_THREADS_H
7bfd3b9e 5
50dc1840 6/* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004 Free Software Foundation, Inc.
0527e687 7 *
73be1d9e
MV
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.
0527e687 12 *
73be1d9e 13 * This library is distributed in the hope that it will be useful,
7bfd3b9e 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
0527e687 17 *
73be1d9e
MV
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
92205699 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 21 */
d3a6bc94 22
7bfd3b9e
JB
23\f
24
25#include "libguile/__scm.h"
26#include "libguile/procs.h"
1651c824 27#include "libguile/throw.h"
f7eca35d 28#include "libguile/root.h"
d823b11b 29#include "libguile/iselect.h"
9de87eea
MV
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
b2728432
DH
40\f
41
7bfd3b9e 42/* smob tags for the thread datatypes */
33b001fd
MV
43SCM_API scm_t_bits scm_tc16_thread;
44SCM_API scm_t_bits scm_tc16_mutex;
45SCM_API scm_t_bits scm_tc16_condvar;
7bfd3b9e 46
9de87eea
MV
47typedef 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 */
1a8fdd7e
MV
72 int gc_running_p; /* non-zero while this thread does GC or a
73 sweep. */
9de87eea
MV
74
75 /* Other thread local things.
76 */
77 SCM dynamic_state;
78 scm_t_debug_frame *last_debug_frame;
79 SCM dynwinds;
80
81 /* For system asyncs.
82 */
83 SCM active_asyncs; /* The thunks to be run at the next
84 safe point */
9de87eea
MV
85 unsigned int block_asyncs; /* Non-zero means that asyncs should
86 not be run. */
87 unsigned int pending_asyncs; /* Non-zero means that asyncs might be pending.
88 */
89
90 /* The current continuation root and the stack base for it.
91
92 The continuation root is an arbitrary but unique object that
93 identifies a dynamic extent. Continuations created during that
94 extent can also only be invoked during it.
95
96 We use pairs where the car is the thread handle and the cdr links
97 to the previous pair. This might be used for better error
98 messages but is not essential for identifying continuation roots.
99
100 The continuation base is the far end of the stack upto which it
101 needs to be copied.
102 */
103 SCM continuation_root;
104 SCM_STACKITEM *continuation_base;
105
106 /* For keeping track of the stack and registers. */
107 SCM_STACKITEM *base;
108 SCM_STACKITEM *top;
109 jmp_buf regs;
110
111} scm_i_thread;
112
113#define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x)
114#define SCM_I_THREAD_DATA(x) ((scm_i_thread *) SCM_SMOB_DATA (x))
7bfd3b9e 115
d823b11b 116#define SCM_VALIDATE_THREAD(pos, a) \
9de87eea 117 scm_assert_smob_type (scm_tc16_thread, (a))
d823b11b 118#define SCM_VALIDATE_MUTEX(pos, a) \
9de87eea 119 scm_assert_smob_type (scm_tc16_mutex, (a))
d823b11b 120#define SCM_VALIDATE_CONDVAR(pos, a) \
9de87eea 121 scm_assert_smob_type (scm_tc16_condvar, (a))
d823b11b 122
33b001fd
MV
123SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
124 scm_t_catch_handler handler, void *handler_data);
76da80e7 125
9de87eea
MV
126typedef void *scm_t_guile_ticket;
127SCM_API void scm_enter_guile (scm_t_guile_ticket ticket);
128SCM_API scm_t_guile_ticket scm_leave_guile (void);
129SCM_API void *scm_without_guile (void *(*func)(void *), void *data);
76da80e7 130
9de87eea
MV
131SCM_API void *scm_with_guile (void *(*func)(void *), void *data);
132SCM_API void *scm_i_with_guile_and_parent (void *(*func)(void *), void *data,
133 SCM parent);
9bc4701c 134
d823b11b 135
9bc4701c 136extern int scm_i_thread_go_to_sleep;
d823b11b 137
9bc4701c
MD
138void scm_i_thread_put_to_sleep (void);
139void scm_i_thread_wake_up (void);
b0dc3d71 140void scm_i_thread_invalidate_freelists (void);
9bc4701c 141void scm_i_thread_sleep_for_gc (void);
9de87eea
MV
142
143void scm_threads_prehistory (SCM_STACKITEM *);
9bc4701c 144void scm_threads_init_first_thread (void);
9de87eea
MV
145SCM_API void scm_threads_mark_stacks (void);
146SCM_API void scm_init_threads (void);
147SCM_API void scm_init_thread_procs (void);
148SCM_API void scm_init_threads_default_dynamic_state (void);
149
d823b11b
MV
150
151#define SCM_THREAD_SWITCHING_CODE \
152do { \
9bc4701c
MD
153 if (scm_i_thread_go_to_sleep) \
154 scm_i_thread_sleep_for_gc (); \
d823b11b 155} while (0)
b74f4728 156
d823b11b 157SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler);
29717c89 158SCM_API SCM scm_yield (void);
33b001fd 159SCM_API SCM scm_join_thread (SCM t);
9de87eea 160
33b001fd 161SCM_API SCM scm_make_mutex (void);
9de87eea 162SCM_API SCM scm_make_recursive_mutex (void);
33b001fd 163SCM_API SCM scm_lock_mutex (SCM m);
a4d106c7 164SCM_API void scm_frame_lock_mutex (SCM mutex);
5f05c406 165SCM_API SCM scm_try_mutex (SCM m);
33b001fd 166SCM_API SCM scm_unlock_mutex (SCM m);
9de87eea 167
33b001fd
MV
168SCM_API SCM scm_make_condition_variable (void);
169SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex);
5f05c406
MV
170SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex,
171 SCM abstime);
33b001fd 172SCM_API SCM scm_signal_condition_variable (SCM cond);
5f05c406 173SCM_API SCM scm_broadcast_condition_variable (SCM cond);
6d71500e 174
f7eca35d
MV
175SCM_API SCM scm_current_thread (void);
176SCM_API SCM scm_all_threads (void);
177
5f05c406
MV
178SCM_API int scm_c_thread_exited_p (SCM thread);
179SCM_API SCM scm_thread_exited_p (SCM thread);
180
a4d106c7 181SCM_API void scm_frame_critical_section (SCM mutex);
9de87eea
MV
182
183#define SCM_I_CURRENT_THREAD \
184 ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key))
185SCM_API scm_i_pthread_key_t scm_i_thread_key;
186
187#define scm_i_dynwinds() (SCM_I_CURRENT_THREAD->dynwinds)
188#define scm_i_set_dynwinds(w) (SCM_I_CURRENT_THREAD->dynwinds = (w))
189#define scm_i_last_debug_frame() (SCM_I_CURRENT_THREAD->last_debug_frame)
190#define scm_i_set_last_debug_frame(f) \
191 (SCM_I_CURRENT_THREAD->last_debug_frame = (f))
f7eca35d 192
9de87eea 193SCM_API scm_i_pthread_mutex_t scm_i_misc_mutex;
9bc4701c 194
9de87eea
MV
195/* Convenience functions for working with the pthread API in guile
196 mode.
197*/
198
199#if SCM_USE_PTHREAD_THREADS
200SCM_API int scm_pthread_mutex_lock (pthread_mutex_t *mutex);
201SCM_API void scm_frame_pthread_mutex_lock (pthread_mutex_t *mutex);
202SCM_API int scm_pthread_cond_wait (pthread_cond_t *cond,
203 pthread_mutex_t *mutex);
204SCM_API int scm_pthread_cond_timedwait (pthread_cond_t *cond,
205 pthread_mutex_t *mutex,
206 const struct timespec *abstime);
207#endif
208
209/* More convenience functions.
210 */
d823b11b 211
9de87eea
MV
212SCM_API unsigned int scm_std_sleep (unsigned int);
213SCM_API unsigned long scm_std_usleep (unsigned long);
50dc1840 214
0527e687 215#endif /* SCM_THREADS_H */
89e00824
ML
216
217/*
218 Local Variables:
219 c-file-style: "gnu"
220 End:
221*/