* threads.h: Do not include "libguile/coop-defs.h". Include
[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 Free Software Foundation, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program 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
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this software; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21 * Boston, MA 02111-1307 USA
22 *
23 * As a special exception, the Free Software Foundation gives permission
24 * for additional uses of the text contained in its release of GUILE.
25 *
26 * The exception is that, if you link the GUILE library with other files
27 * to produce an executable, this does not by itself cause the
28 * resulting executable to be covered by the GNU General Public License.
29 * Your use of that executable is in no way restricted on account of
30 * linking the GUILE library code into it.
31 *
32 * This exception does not however invalidate any other reasons why
33 * the executable file might be covered by the GNU General Public License.
34 *
35 * This exception applies only to the code released by the
36 * Free Software Foundation under the name GUILE. If you copy
37 * code from other Free Software Foundation releases into a copy of
38 * GUILE, as the General Public License permits, the exception does
39 * not apply to the code that you add in this way. To avoid misleading
40 * anyone as to the status of such modified files, you must delete
41 * this exception notice from them.
42 *
43 * If you write modifications of your own for GUILE, it is your choice
44 * whether to permit this exception to apply to your modifications.
45 * If you do not wish that, delete this exception notice. */
46
47 \f
48
49 #include "libguile/__scm.h"
50 #include "libguile/procs.h"
51 #include "libguile/throw.h"
52 #include "libguile/root.h"
53 #include "libguile/iselect.h"
54 \f
55
56 /* smob tags for the thread datatypes */
57 SCM_API scm_t_bits scm_tc16_thread;
58 SCM_API scm_t_bits scm_tc16_mutex;
59 SCM_API scm_t_bits scm_tc16_condvar;
60
61 #define SCM_THREADP(x) SCM_TYP16_PREDICATE (scm_tc16_thread, x)
62 #define SCM_THREAD_DATA(x) ((void *) SCM_CELL_WORD_1 (x))
63
64 #define SCM_MUTEXP(x) SCM_TYP16_PREDICATE (scm_tc16_mutex, x)
65 #define SCM_MUTEX_DATA(x) ((void *) SCM_CELL_WORD_1 (x))
66
67 #define SCM_CONDVARP(x) SCM_TYP16_PREDICATE (scm_tc16_condvar, x)
68 #define SCM_CONDVAR_DATA(x) ((void *) SCM_CELL_WORD_1 (x))
69
70 #define SCM_VALIDATE_THREAD(pos, a) \
71 SCM_MAKE_VALIDATE_MSG (pos, a, THREADP, "thread")
72
73 #define SCM_VALIDATE_MUTEX(pos, a) \
74 SCM_MAKE_VALIDATE_MSG (pos, a, MUTEXP, "mutex")
75
76 #define SCM_VALIDATE_CONDVAR(pos, a) \
77 SCM_MAKE_VALIDATE_MSG (pos, a, CONDVARP, "condition variable")
78
79 SCM_API void scm_threads_mark_stacks (void);
80 SCM_API void scm_init_threads (SCM_STACKITEM *);
81 SCM_API void scm_init_thread_procs (void);
82
83 SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
84 scm_t_catch_handler handler, void *handler_data);
85
86 /* These are versions of the ordinary sleep and usleep functions
87 that play nicely with the thread system. */
88 SCM_API unsigned long scm_thread_sleep (unsigned long);
89 SCM_API unsigned long scm_thread_usleep (unsigned long);
90
91 /* Critical sections */
92
93 /* Since only one thread can be active anyway, we don't need to do
94 anything special around critical sections. In fact, that's the
95 reason we do only support cooperative threading: Guile's critical
96 regions have not been completely identified yet. (I think.) */
97
98 #define SCM_CRITICAL_SECTION_START
99 #define SCM_CRITICAL_SECTION_END
100
101 /* Switching */
102
103 SCM_API int scm_i_switch_counter;
104 #define SCM_I_THREAD_SWITCH_COUNT 50
105
106 #define SCM_THREAD_SWITCHING_CODE \
107 do { \
108 scm_i_switch_counter--; \
109 if (scm_i_switch_counter == 0) \
110 { \
111 scm_i_switch_counter = SCM_I_THREAD_SWITCH_COUNT; \
112 scm_yield(); \
113 } \
114 } while (0)
115
116 /* The C versions of the Scheme-visible thread functions. */
117 SCM_API SCM scm_yield (void);
118 SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler);
119 SCM_API SCM scm_join_thread (SCM t);
120 SCM_API SCM scm_make_mutex (void);
121 SCM_API SCM scm_lock_mutex (SCM m);
122 SCM_API SCM scm_try_mutex (SCM m);
123 SCM_API SCM scm_unlock_mutex (SCM m);
124 SCM_API SCM scm_make_condition_variable (void);
125 SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex);
126 SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex,
127 SCM abstime);
128 SCM_API SCM scm_signal_condition_variable (SCM cond);
129 SCM_API SCM scm_broadcast_condition_variable (SCM cond);
130
131 SCM_API SCM scm_current_thread (void);
132 SCM_API SCM scm_all_threads (void);
133
134 SCM_API int scm_c_thread_exited_p (SCM thread);
135 SCM_API SCM scm_thread_exited_p (SCM thread);
136
137 SCM_API scm_root_state *scm_i_thread_root (SCM thread);
138
139 SCM_API void *scm_i_thread_data;
140 SCM_API void scm_i_set_thread_data (void *);
141 #define SCM_THREAD_LOCAL_DATA scm_i_thread_data
142 #define SCM_SET_THREAD_LOCAL_DATA(x) scm_i_set_thread_data(x)
143
144 #ifndef HAVE_STRUCT_TIMESPEC
145 /* POSIX.4 structure for a time value. This is like a `struct timeval' but
146 has nanoseconds instead of microseconds. */
147 struct timespec
148 {
149 long int tv_sec; /* Seconds. */
150 long int tv_nsec; /* Nanoseconds. */
151 };
152 #endif
153
154 #ifdef USE_COPT_THREADS
155 #include "libguile/pthread-threads.h"
156 #else
157 #include "libguile/null-threads.h"
158 #endif
159
160 #endif /* SCM_THREADS_H */
161
162 /*
163 Local Variables:
164 c-file-style: "gnu"
165 End:
166 */