Merge branch 'stable-2.0'
[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
01b69e79 6/* Copyright (C) 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2006,
26d14806 7 * 2007, 2008, 2009, 2011, 2012, 2013 Free Software Foundation, Inc.
0527e687 8 *
73be1d9e 9 * This library is free software; you can redistribute it and/or
53befeb7
NJ
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 3 of
12 * the License, or (at your option) any later version.
0527e687 13 *
53befeb7
NJ
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
0527e687 18 *
73be1d9e
MV
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
53befeb7
NJ
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301 USA
73be1d9e 23 */
d3a6bc94 24
7bfd3b9e
JB
25\f
26
27#include "libguile/__scm.h"
28#include "libguile/procs.h"
1651c824 29#include "libguile/throw.h"
f7eca35d 30#include "libguile/root.h"
9ede013f 31#include "libguile/dynstack.h"
d823b11b 32#include "libguile/iselect.h"
346e4402 33#include "libguile/continuations.h"
9de87eea
MV
34
35#if SCM_USE_PTHREAD_THREADS
36#include "libguile/pthread-threads.h"
37#endif
38
39#if SCM_USE_NULL_THREADS
40#include "libguile/null-threads.h"
41#endif
42
b2728432
DH
43\f
44
7bfd3b9e 45/* smob tags for the thread datatypes */
33b001fd
MV
46SCM_API scm_t_bits scm_tc16_thread;
47SCM_API scm_t_bits scm_tc16_mutex;
48SCM_API scm_t_bits scm_tc16_condvar;
7bfd3b9e 49
9de87eea
MV
50typedef struct scm_i_thread {
51 struct scm_i_thread *next_thread;
52
53 SCM handle;
54 scm_i_pthread_t pthread;
2e77f720
LC
55
56 SCM cleanup_handler;
9de87eea 57 SCM join_queue;
86a597f8
NJ
58
59 scm_i_pthread_mutex_t admin_mutex;
6180e336 60 SCM mutexes;
d2a51087 61 scm_i_pthread_mutex_t *held_mutex;
86a597f8 62
9de87eea 63 SCM result;
2e77f720 64 int canceled;
9de87eea
MV
65 int exited;
66
72e6b608
LC
67 /* Boolean indicating whether the thread is in guile mode. */
68 int guile_mode;
69
9de87eea
MV
70 SCM sleep_object;
71 scm_i_pthread_mutex_t *sleep_mutex;
72 scm_i_pthread_cond_t sleep_cond;
73 int sleep_fd, sleep_pipe[2];
74
9de87eea
MV
75 /* Other thread local things.
76 */
77 SCM dynamic_state;
9ede013f
AW
78
79 /* The dynamic stack. */
80 scm_t_dynstack dynstack;
9de87eea
MV
81
82 /* For system asyncs.
83 */
84 SCM active_asyncs; /* The thunks to be run at the next
85 safe point */
74926120 86 unsigned int block_asyncs; /* Non-zero means that asyncs should
9de87eea
MV
87 not be run. */
88 unsigned int pending_asyncs; /* Non-zero means that asyncs might be pending.
89 */
90
91 /* The current continuation root and the stack base for it.
92
93 The continuation root is an arbitrary but unique object that
94 identifies a dynamic extent. Continuations created during that
95 extent can also only be invoked during it.
96
97 We use pairs where the car is the thread handle and the cdr links
98 to the previous pair. This might be used for better error
99 messages but is not essential for identifying continuation roots.
100
101 The continuation base is the far end of the stack upto which it
102 needs to be copied.
103 */
104 SCM continuation_root;
105 SCM_STACKITEM *continuation_base;
106
107 /* For keeping track of the stack and registers. */
3506b152 108 struct scm_vm *vp;
9de87eea 109 SCM_STACKITEM *base;
a4dbe1ac 110 scm_i_jmp_buf regs;
346e4402
NJ
111#ifdef __ia64__
112 void *register_backing_store_base;
113 scm_t_contregs *pending_rbs_continuation;
114#endif
9de87eea 115
87f30eda
NJ
116 /* Whether this thread is in a critical section. */
117 int critical_section_level;
118
9de87eea
MV
119} scm_i_thread;
120
121#define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x)
122#define SCM_I_THREAD_DATA(x) ((scm_i_thread *) SCM_SMOB_DATA (x))
7bfd3b9e 123
d823b11b 124#define SCM_VALIDATE_THREAD(pos, a) \
9de87eea 125 scm_assert_smob_type (scm_tc16_thread, (a))
d823b11b 126#define SCM_VALIDATE_MUTEX(pos, a) \
9de87eea 127 scm_assert_smob_type (scm_tc16_mutex, (a))
d823b11b 128#define SCM_VALIDATE_CONDVAR(pos, a) \
9de87eea 129 scm_assert_smob_type (scm_tc16_condvar, (a))
d823b11b 130
33b001fd
MV
131SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
132 scm_t_catch_handler handler, void *handler_data);
76da80e7 133
9de87eea 134SCM_API void *scm_without_guile (void *(*func)(void *), void *data);
9de87eea 135SCM_API void *scm_with_guile (void *(*func)(void *), void *data);
98648121 136
aafb4ed7 137SCM_INTERNAL void scm_i_reset_fluid (size_t);
12c1d861 138SCM_INTERNAL void scm_threads_prehistory (void *);
102dbb6f
LC
139SCM_INTERNAL void scm_init_threads (void);
140SCM_INTERNAL void scm_init_thread_procs (void);
141SCM_INTERNAL void scm_init_threads_default_dynamic_state (void);
9de87eea 142
e676a4c3 143SCM_INTERNAL void scm_i_dynwind_pthread_mutex_lock_block_asyncs (scm_i_pthread_mutex_t *mutex);
d823b11b 144
d823b11b 145SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler);
29717c89 146SCM_API SCM scm_yield (void);
2e77f720
LC
147SCM_API SCM scm_cancel_thread (SCM t);
148SCM_API SCM scm_set_thread_cleanup_x (SCM thread, SCM proc);
149SCM_API SCM scm_thread_cleanup (SCM thread);
33b001fd 150SCM_API SCM scm_join_thread (SCM t);
6180e336
NJ
151SCM_API SCM scm_join_thread_timed (SCM t, SCM timeout, SCM timeoutval);
152SCM_API SCM scm_thread_p (SCM t);
9de87eea 153
33b001fd 154SCM_API SCM scm_make_mutex (void);
9de87eea 155SCM_API SCM scm_make_recursive_mutex (void);
6180e336 156SCM_API SCM scm_make_mutex_with_flags (SCM flags);
33b001fd 157SCM_API SCM scm_lock_mutex (SCM m);
adc085f1 158SCM_API SCM scm_lock_mutex_timed (SCM m, SCM timeout, SCM owner);
661ae7ab 159SCM_API void scm_dynwind_lock_mutex (SCM mutex);
5f05c406 160SCM_API SCM scm_try_mutex (SCM m);
33b001fd 161SCM_API SCM scm_unlock_mutex (SCM m);
6180e336
NJ
162SCM_API SCM scm_unlock_mutex_timed (SCM m, SCM cond, SCM timeout);
163SCM_API SCM scm_mutex_p (SCM o);
adc085f1
JG
164SCM_API SCM scm_mutex_locked_p (SCM m);
165SCM_API SCM scm_mutex_owner (SCM m);
166SCM_API SCM scm_mutex_level (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);
6180e336 174SCM_API SCM scm_condition_variable_p (SCM o);
6d71500e 175
f7eca35d
MV
176SCM_API SCM scm_current_thread (void);
177SCM_API SCM scm_all_threads (void);
178
5f05c406
MV
179SCM_API int scm_c_thread_exited_p (SCM thread);
180SCM_API SCM scm_thread_exited_p (SCM thread);
181
661ae7ab 182SCM_API void scm_dynwind_critical_section (SCM mutex);
9de87eea 183
46935a1f
LC
184#ifdef BUILDING_LIBGUILE
185
f60a7648
AW
186/* Though we don't need the key for SCM_I_CURRENT_THREAD if we have TLS,
187 we do use it for cleanup purposes. */
188SCM_INTERNAL scm_i_pthread_key_t scm_i_thread_key;
189
705edb95
LC
190# ifdef SCM_HAVE_THREAD_STORAGE_CLASS
191
192SCM_INTERNAL SCM_THREAD_LOCAL scm_i_thread *scm_i_current_thread;
193# define SCM_I_CURRENT_THREAD (scm_i_current_thread)
194
195# else /* !SCM_HAVE_THREAD_STORAGE_CLASS */
196
705edb95
LC
197# define SCM_I_CURRENT_THREAD \
198 ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key))
199
200# endif /* !SCM_HAVE_THREAD_STORAGE_CLASS */
9de87eea 201
46935a1f
LC
202#endif /* BUILDING_LIBGUILE */
203
102dbb6f 204SCM_INTERNAL scm_i_pthread_mutex_t scm_i_misc_mutex;
9bc4701c 205
9de87eea
MV
206/* Convenience functions for working with the pthread API in guile
207 mode.
208*/
209
210#if SCM_USE_PTHREAD_THREADS
211SCM_API int scm_pthread_mutex_lock (pthread_mutex_t *mutex);
661ae7ab 212SCM_API void scm_dynwind_pthread_mutex_lock (pthread_mutex_t *mutex);
9de87eea
MV
213SCM_API int scm_pthread_cond_wait (pthread_cond_t *cond,
214 pthread_mutex_t *mutex);
215SCM_API int scm_pthread_cond_timedwait (pthread_cond_t *cond,
216 pthread_mutex_t *mutex,
ab878b0f 217 const scm_t_timespec *abstime);
9de87eea
MV
218#endif
219
220/* More convenience functions.
221 */
d823b11b 222
9de87eea
MV
223SCM_API unsigned int scm_std_sleep (unsigned int);
224SCM_API unsigned long scm_std_usleep (unsigned long);
50dc1840 225
d20912e6
LC
226SCM_API SCM scm_total_processor_count (void);
227SCM_API SCM scm_current_processor_count (void);
228
0527e687 229#endif /* SCM_THREADS_H */
89e00824
ML
230
231/*
232 Local Variables:
233 c-file-style: "gnu"
234 End:
235*/