Remove use of `scm_i_thread_put_to_sleep ()' in the string code.
[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
86a597f8 6/* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004, 2006, 2007, 2008 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 30#include "libguile/dynwind.h"
346e4402 31#include "libguile/continuations.h"
9de87eea
MV
32
33#if SCM_USE_PTHREAD_THREADS
34#include "libguile/pthread-threads.h"
35#endif
36
37#if SCM_USE_NULL_THREADS
38#include "libguile/null-threads.h"
39#endif
40
b2728432
DH
41\f
42
7bfd3b9e 43/* smob tags for the thread datatypes */
33b001fd
MV
44SCM_API scm_t_bits scm_tc16_thread;
45SCM_API scm_t_bits scm_tc16_mutex;
46SCM_API scm_t_bits scm_tc16_condvar;
7bfd3b9e 47
9de87eea
MV
48typedef struct scm_i_thread {
49 struct scm_i_thread *next_thread;
50
51 SCM handle;
52 scm_i_pthread_t pthread;
2e77f720
LC
53
54 SCM cleanup_handler;
9de87eea 55 SCM join_queue;
86a597f8
NJ
56
57 scm_i_pthread_mutex_t admin_mutex;
6180e336 58 SCM mutexes;
86a597f8 59
9de87eea 60 SCM result;
2e77f720 61 int canceled;
9de87eea
MV
62 int exited;
63
64 SCM sleep_object;
65 scm_i_pthread_mutex_t *sleep_mutex;
66 scm_i_pthread_cond_t sleep_cond;
67 int sleep_fd, sleep_pipe[2];
68
69 /* This mutex represents this threads right to access the heap.
74926120 70 That right can temporarily be taken away by the GC.
9de87eea
MV
71 */
72 scm_i_pthread_mutex_t heap_mutex;
73
74 /* The freelists of this thread. Each thread has its own lists so
75 that they can all allocate concurrently.
76 */
77 SCM freelist, freelist2;
78 int clear_freelists_p; /* set if GC was done while thread was asleep */
1a8fdd7e
MV
79 int gc_running_p; /* non-zero while this thread does GC or a
80 sweep. */
9de87eea 81
378f2625
LC
82 /* Information about the Boehm-GC mark stack during the mark phase. This
83 is used by `scm_gc_mark ()'. */
84 void *current_mark_stack_ptr;
85 void *current_mark_stack_limit;
86
9de87eea
MV
87 /* Other thread local things.
88 */
89 SCM dynamic_state;
90 scm_t_debug_frame *last_debug_frame;
91 SCM dynwinds;
92
93 /* For system asyncs.
94 */
95 SCM active_asyncs; /* The thunks to be run at the next
96 safe point */
74926120 97 unsigned int block_asyncs; /* Non-zero means that asyncs should
9de87eea
MV
98 not be run. */
99 unsigned int pending_asyncs; /* Non-zero means that asyncs might be pending.
100 */
101
102 /* The current continuation root and the stack base for it.
103
104 The continuation root is an arbitrary but unique object that
105 identifies a dynamic extent. Continuations created during that
106 extent can also only be invoked during it.
107
108 We use pairs where the car is the thread handle and the cdr links
109 to the previous pair. This might be used for better error
110 messages but is not essential for identifying continuation roots.
111
112 The continuation base is the far end of the stack upto which it
113 needs to be copied.
114 */
115 SCM continuation_root;
116 SCM_STACKITEM *continuation_base;
117
118 /* For keeping track of the stack and registers. */
119 SCM_STACKITEM *base;
120 SCM_STACKITEM *top;
121 jmp_buf regs;
346e4402
NJ
122#ifdef __ia64__
123 void *register_backing_store_base;
124 scm_t_contregs *pending_rbs_continuation;
125#endif
9de87eea
MV
126
127} scm_i_thread;
128
129#define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x)
130#define SCM_I_THREAD_DATA(x) ((scm_i_thread *) SCM_SMOB_DATA (x))
7bfd3b9e 131
d823b11b 132#define SCM_VALIDATE_THREAD(pos, a) \
9de87eea 133 scm_assert_smob_type (scm_tc16_thread, (a))
d823b11b 134#define SCM_VALIDATE_MUTEX(pos, a) \
9de87eea 135 scm_assert_smob_type (scm_tc16_mutex, (a))
d823b11b 136#define SCM_VALIDATE_CONDVAR(pos, a) \
9de87eea 137 scm_assert_smob_type (scm_tc16_condvar, (a))
d823b11b 138
33b001fd
MV
139SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
140 scm_t_catch_handler handler, void *handler_data);
76da80e7 141
9de87eea 142SCM_API void *scm_without_guile (void *(*func)(void *), void *data);
9de87eea 143SCM_API void *scm_with_guile (void *(*func)(void *), void *data);
98648121 144
102dbb6f
LC
145SCM_INTERNAL void *scm_i_with_guile_and_parent (void *(*func)(void *),
146 void *data, SCM parent);
9bc4701c 147
d823b11b 148
9bc4701c 149extern int scm_i_thread_go_to_sleep;
d823b11b 150
102dbb6f
LC
151SCM_INTERNAL void scm_i_thread_put_to_sleep (void);
152SCM_INTERNAL void scm_i_thread_wake_up (void);
153SCM_INTERNAL void scm_i_thread_invalidate_freelists (void);
9bc4701c 154void scm_i_thread_sleep_for_gc (void);
9de87eea
MV
155
156void scm_threads_prehistory (SCM_STACKITEM *);
9bc4701c 157void scm_threads_init_first_thread (void);
6bad09ba 158
102dbb6f
LC
159SCM_INTERNAL void scm_init_threads (void);
160SCM_INTERNAL void scm_init_thread_procs (void);
161SCM_INTERNAL void scm_init_threads_default_dynamic_state (void);
9de87eea 162
d823b11b
MV
163
164#define SCM_THREAD_SWITCHING_CODE \
165do { \
9bc4701c
MD
166 if (scm_i_thread_go_to_sleep) \
167 scm_i_thread_sleep_for_gc (); \
d823b11b 168} while (0)
b74f4728 169
d823b11b 170SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler);
29717c89 171SCM_API SCM scm_yield (void);
2e77f720
LC
172SCM_API SCM scm_cancel_thread (SCM t);
173SCM_API SCM scm_set_thread_cleanup_x (SCM thread, SCM proc);
174SCM_API SCM scm_thread_cleanup (SCM thread);
33b001fd 175SCM_API SCM scm_join_thread (SCM t);
6180e336
NJ
176SCM_API SCM scm_join_thread_timed (SCM t, SCM timeout, SCM timeoutval);
177SCM_API SCM scm_thread_p (SCM t);
9de87eea 178
33b001fd 179SCM_API SCM scm_make_mutex (void);
9de87eea 180SCM_API SCM scm_make_recursive_mutex (void);
6180e336 181SCM_API SCM scm_make_mutex_with_flags (SCM flags);
33b001fd 182SCM_API SCM scm_lock_mutex (SCM m);
adc085f1 183SCM_API SCM scm_lock_mutex_timed (SCM m, SCM timeout, SCM owner);
661ae7ab 184SCM_API void scm_dynwind_lock_mutex (SCM mutex);
5f05c406 185SCM_API SCM scm_try_mutex (SCM m);
33b001fd 186SCM_API SCM scm_unlock_mutex (SCM m);
6180e336
NJ
187SCM_API SCM scm_unlock_mutex_timed (SCM m, SCM cond, SCM timeout);
188SCM_API SCM scm_mutex_p (SCM o);
adc085f1
JG
189SCM_API SCM scm_mutex_locked_p (SCM m);
190SCM_API SCM scm_mutex_owner (SCM m);
191SCM_API SCM scm_mutex_level (SCM m);
9de87eea 192
33b001fd
MV
193SCM_API SCM scm_make_condition_variable (void);
194SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex);
5f05c406
MV
195SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex,
196 SCM abstime);
33b001fd 197SCM_API SCM scm_signal_condition_variable (SCM cond);
5f05c406 198SCM_API SCM scm_broadcast_condition_variable (SCM cond);
6180e336 199SCM_API SCM scm_condition_variable_p (SCM o);
6d71500e 200
f7eca35d
MV
201SCM_API SCM scm_current_thread (void);
202SCM_API SCM scm_all_threads (void);
203
5f05c406
MV
204SCM_API int scm_c_thread_exited_p (SCM thread);
205SCM_API SCM scm_thread_exited_p (SCM thread);
206
661ae7ab 207SCM_API void scm_dynwind_critical_section (SCM mutex);
9de87eea
MV
208
209#define SCM_I_CURRENT_THREAD \
210 ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key))
211SCM_API scm_i_pthread_key_t scm_i_thread_key;
212
213#define scm_i_dynwinds() (SCM_I_CURRENT_THREAD->dynwinds)
214#define scm_i_set_dynwinds(w) (SCM_I_CURRENT_THREAD->dynwinds = (w))
215#define scm_i_last_debug_frame() (SCM_I_CURRENT_THREAD->last_debug_frame)
216#define scm_i_set_last_debug_frame(f) \
217 (SCM_I_CURRENT_THREAD->last_debug_frame = (f))
f7eca35d 218
102dbb6f 219SCM_INTERNAL scm_i_pthread_mutex_t scm_i_misc_mutex;
9bc4701c 220
9de87eea
MV
221/* Convenience functions for working with the pthread API in guile
222 mode.
223*/
224
225#if SCM_USE_PTHREAD_THREADS
226SCM_API int scm_pthread_mutex_lock (pthread_mutex_t *mutex);
661ae7ab 227SCM_API void scm_dynwind_pthread_mutex_lock (pthread_mutex_t *mutex);
9de87eea
MV
228SCM_API int scm_pthread_cond_wait (pthread_cond_t *cond,
229 pthread_mutex_t *mutex);
230SCM_API int scm_pthread_cond_timedwait (pthread_cond_t *cond,
231 pthread_mutex_t *mutex,
232 const struct timespec *abstime);
233#endif
234
235/* More convenience functions.
236 */
d823b11b 237
9de87eea
MV
238SCM_API unsigned int scm_std_sleep (unsigned int);
239SCM_API unsigned long scm_std_usleep (unsigned long);
50dc1840 240
0527e687 241#endif /* SCM_THREADS_H */
89e00824
ML
242
243/*
244 Local Variables:
245 c-file-style: "gnu"
246 End:
247*/