]> git.donarmstrong.com Git - lilypond.git/blob - guile18/libguile/threads.h
New upstream version 2.19.65
[lilypond.git] / guile18 / 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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 #include "libguile/continuations.h"
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
41 \f
42
43 /* smob tags for the thread datatypes */
44 SCM_API scm_t_bits scm_tc16_thread;
45 SCM_API scm_t_bits scm_tc16_mutex;
46 SCM_API scm_t_bits scm_tc16_condvar;
47
48 typedef struct scm_i_thread {
49   struct scm_i_thread *next_thread;
50
51   SCM handle;
52   scm_i_pthread_t pthread;
53   
54   SCM join_queue;
55   SCM result;
56   int exited;
57
58   SCM sleep_object;
59   scm_i_pthread_mutex_t *sleep_mutex;
60   scm_i_pthread_cond_t sleep_cond;
61   int sleep_fd, sleep_pipe[2];
62
63   /* This mutex represents this threads right to access the heap.
64      That right can temporarily be taken away by the GC.  
65   */
66   scm_i_pthread_mutex_t heap_mutex;
67
68   /* The freelists of this thread.  Each thread has its own lists so
69      that they can all allocate concurrently.
70   */
71   SCM freelist, freelist2;
72   int clear_freelists_p; /* set if GC was done while thread was asleep */
73   int gc_running_p;      /* non-zero while this thread does GC or a
74                             sweep. */
75
76   /* Other thread local things.
77    */
78   SCM dynamic_state;
79   scm_t_debug_frame *last_debug_frame;
80   SCM dynwinds;
81
82   /* For system asyncs.
83    */
84   SCM active_asyncs;            /* The thunks to be run at the next
85                                    safe point */
86   unsigned int block_asyncs;    /* Non-zero means that asyncs should 
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. */
108   SCM_STACKITEM *base;
109   SCM_STACKITEM *top;
110   scm_i_jmp_buf regs;
111 #ifdef __ia64__
112   void *register_backing_store_base;
113   scm_t_contregs *pending_rbs_continuation;
114 #endif
115
116   /* Whether this thread is in a critical section. */
117   int critical_section_level;
118
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))
123
124 #define SCM_VALIDATE_THREAD(pos, a) \
125   scm_assert_smob_type (scm_tc16_thread, (a))
126 #define SCM_VALIDATE_MUTEX(pos, a) \
127   scm_assert_smob_type (scm_tc16_mutex, (a))
128 #define SCM_VALIDATE_CONDVAR(pos, a) \
129   scm_assert_smob_type (scm_tc16_condvar, (a))
130
131 SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
132                               scm_t_catch_handler handler, void *handler_data);
133
134 SCM_API void *scm_without_guile (void *(*func)(void *), void *data);
135 SCM_API void *scm_with_guile (void *(*func)(void *), void *data);
136
137 SCM_API void *scm_i_with_guile_and_parent (void *(*func)(void *), void *data,
138                                            SCM parent);
139
140
141 extern int scm_i_thread_go_to_sleep;
142
143 void scm_i_thread_put_to_sleep (void);
144 void scm_i_thread_wake_up (void);
145 void scm_i_thread_invalidate_freelists (void);
146 void scm_i_thread_sleep_for_gc (void);
147
148 void scm_threads_prehistory (SCM_STACKITEM *);
149 void scm_threads_init_first_thread (void);
150 SCM_API void scm_threads_mark_stacks (void);
151 SCM_API void scm_init_threads (void);
152 SCM_API void scm_init_thread_procs (void);
153 SCM_API void scm_init_threads_default_dynamic_state (void);
154
155
156 #define SCM_THREAD_SWITCHING_CODE \
157 do { \
158   if (scm_i_thread_go_to_sleep) \
159     scm_i_thread_sleep_for_gc (); \
160 } while (0)
161
162 SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler);
163 SCM_API SCM scm_yield (void);
164 SCM_API SCM scm_join_thread (SCM t);
165
166 SCM_API SCM scm_make_mutex (void);
167 SCM_API SCM scm_make_recursive_mutex (void);
168 SCM_API SCM scm_lock_mutex (SCM m);
169 SCM_API void scm_dynwind_lock_mutex (SCM mutex);
170 SCM_API SCM scm_try_mutex (SCM m);
171 SCM_API SCM scm_unlock_mutex (SCM m);
172
173 SCM_API SCM scm_make_condition_variable (void);
174 SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex);
175 SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex,
176                                                SCM abstime);
177 SCM_API SCM scm_signal_condition_variable (SCM cond);
178 SCM_API SCM scm_broadcast_condition_variable (SCM cond);
179
180 SCM_API SCM scm_current_thread (void);
181 SCM_API SCM scm_all_threads (void);
182
183 SCM_API int scm_c_thread_exited_p (SCM thread);
184 SCM_API SCM scm_thread_exited_p (SCM thread);
185
186 SCM_API void scm_dynwind_critical_section (SCM mutex);
187
188 #define SCM_I_CURRENT_THREAD \
189   ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key))
190 SCM_API scm_i_pthread_key_t scm_i_thread_key;
191
192 #define scm_i_dynwinds()         (SCM_I_CURRENT_THREAD->dynwinds)
193 #define scm_i_set_dynwinds(w)    (SCM_I_CURRENT_THREAD->dynwinds = (w))
194 #define scm_i_last_debug_frame() (SCM_I_CURRENT_THREAD->last_debug_frame)
195 #define scm_i_set_last_debug_frame(f) \
196                                  (SCM_I_CURRENT_THREAD->last_debug_frame = (f))
197
198 SCM_API scm_i_pthread_mutex_t scm_i_misc_mutex;
199
200 /* Convenience functions for working with the pthread API in guile
201    mode.
202 */
203
204 #if SCM_USE_PTHREAD_THREADS
205 SCM_API int scm_pthread_mutex_lock (pthread_mutex_t *mutex);
206 SCM_API void scm_dynwind_pthread_mutex_lock (pthread_mutex_t *mutex);
207 SCM_API int scm_pthread_cond_wait (pthread_cond_t *cond,
208                                    pthread_mutex_t *mutex);
209 SCM_API int scm_pthread_cond_timedwait (pthread_cond_t *cond,
210                                         pthread_mutex_t *mutex,
211                                         const scm_t_timespec *abstime);
212 #endif
213
214 /* More convenience functions.
215  */
216
217 SCM_API unsigned int scm_std_sleep (unsigned int);
218 SCM_API unsigned long scm_std_usleep (unsigned long);
219
220 #endif  /* SCM_THREADS_H */
221
222 /*
223   Local Variables:
224   c-file-style: "gnu"
225   End:
226 */