* coop-defs.h (struct coop_t): Removed dummy_mutex.
[bpt/guile.git] / libguile / coop-defs.h
1 /* classes: h_files */
2
3 #ifndef COOP_DEFSH
4 #define COOP_DEFSH
5
6 /* Copyright (C) 1996, 1997, 1998, 1999, 2000 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 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
48 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
49 \f
50
51 # ifdef TIME_WITH_SYS_TIME
52 # include <sys/time.h>
53 # include <time.h>
54 # else
55 # ifdef HAVE_SYS_TIME_H
56 # include <sys/time.h>
57 # else
58 # ifdef HAVE_TIME_H
59 # include <time.h>
60 # endif
61 # endif
62 # endif
63
64 #ifdef GUILE_ISELECT
65 #include "iselect.h"
66 #endif
67
68 #ifdef GUILE_PTHREAD_COMPAT
69 #include <pthread.h>
70 #endif
71
72 /* This file is included by threads.h, which, in turn, is included by
73 libguile.h while coop-threads.h only is included by
74 coop-threads.c. */
75
76 /* The coop_t struct must be declared here, since macros in this file
77 refer to the data member. */
78
79 /* The notion of a thread is merged with the notion of a queue.
80 Thread stuff: thread status (sp) and stuff to use during
81 (re)initialization. Queue stuff: next thread in the queue
82 (next). */
83
84 struct qt_t;
85
86 typedef struct coop_t {
87 struct qt_t *sp; /* QuickThreads handle. */
88 #ifndef GUILE_PTHREAD_COMPAT
89 void *sto; /* `malloc'-allocated stack. */
90 #endif
91
92 struct coop_t *next; /* Next thread in the queue. */
93
94 struct coop_t *all_next;
95 struct coop_t *all_prev;
96
97 void *data; /* Thread local data */
98 void **specific; /* Data associated with keys */
99 int n_keys; /* Upper limit for keys on this thread */
100
101 void *base; /* Base of stack */
102 void *top; /* Top of stack */
103
104 void *joining; /* A queue of threads waiting to join this
105 thread */
106
107 #ifdef GUILE_ISELECT
108 int nfds;
109 SELECT_TYPE *readfds;
110 SELECT_TYPE *writefds;
111 SELECT_TYPE *exceptfds;
112 int timeoutp;
113 struct timeval wakeup_time; /* Time to stop sleeping */
114 int _errno;
115 int retval;
116 #else
117 time_t wakeup_time; /* Time to stop sleeping */
118 #endif
119
120 #ifdef GUILE_PTHREAD_COMPAT
121 pthread_t dummy_thread;
122 pthread_mutex_t dummy_mutex;
123 #endif
124 } coop_t;
125
126 /* A queue is a circular list of threads. The queue head is a
127 designated list element. If this is a uniprocessor-only
128 implementation we can store the `main' thread in this, but in a
129 multiprocessor there are several `heavy' threads but only one run
130 queue. A fancier implementation might have private run queues,
131 which would lead to a simpler (trivial) implementation */
132
133 typedef struct coop_q_t {
134 coop_t t;
135 coop_t *tail;
136 } coop_q_t;
137
138 /* A Mutex variable is made up of a owner thread, and a queue of threads
139 waiting on the mutex */
140
141 typedef struct coop_m {
142 coop_t *owner; /* Mutex owner */
143 coop_q_t waiting; /* Queue of waiting threads */
144 } coop_m;
145
146 typedef int coop_mattr;
147
148 typedef coop_m scm_mutex_t;
149
150 extern int coop_mutex_init (coop_m*);
151 extern int coop_new_mutex_init (coop_m*, coop_mattr*);
152 extern int coop_mutex_lock (coop_m*);
153 extern int coop_mutex_trylock (coop_m*);
154 extern int coop_mutex_unlock (coop_m*);
155 extern int coop_mutex_destroy (coop_m*);
156 #define scm_mutex_init coop_mutex_init
157 #define scm_mutex_lock coop_mutex_lock
158 #define scm_mutex_trylock coop_mutex_lock
159 #define scm_mutex_unlock coop_mutex_unlock
160 #define scm_mutex_destroy coop_mutex_destroy
161
162 /* A Condition variable is made up of a list of threads waiting on the
163 condition. */
164
165 typedef struct coop_c {
166 coop_q_t waiting; /* Queue of waiting threads */
167 } coop_c;
168
169 typedef int coop_cattr;
170
171 typedef coop_c scm_cond_t;
172
173 #ifndef HAVE_STRUCT_TIMESPEC
174 /* POSIX.4 structure for a time value. This is like a `struct timeval' but
175 has nanoseconds instead of microseconds. */
176 struct timespec
177 {
178 long int tv_sec; /* Seconds. */
179 long int tv_nsec; /* Nanoseconds. */
180 };
181 #endif
182
183 extern int coop_condition_variable_init (coop_c*);
184 extern int coop_new_condition_variable_init (coop_c*, coop_cattr*);
185 extern int coop_condition_variable_wait_mutex (coop_c*, coop_m*);
186 extern int coop_condition_variable_timed_wait_mutex (coop_c*,
187 coop_m*,
188 const struct timespec *abstime);
189 extern int coop_condition_variable_signal (coop_c*);
190 extern int coop_condition_variable_destroy (coop_c*);
191 #define scm_cond_init coop_new_condition_variable_init
192 #define scm_cond_wait coop_condition_variable_wait_mutex
193 #define scm_cond_timedwait coop_condition_variable_timed_wait_mutex
194 #define scm_cond_signal coop_condition_variable_signal
195 #define scm_cond_broadcast coop_condition_variable_signal /* yes */
196 #define scm_cond_destroy coop_condition_variable_destroy
197
198 typedef int coop_k;
199
200 typedef coop_k scm_key_t;
201
202 extern int coop_key_create (coop_k *keyp, void (*destructor) (void *value));
203 extern int coop_setspecific (coop_k key, const void *value);
204 extern void *coop_getspecific (coop_k key);
205 extern int coop_key_delete (coop_k);
206 #define scm_key_create coop_key_create
207 #define scm_setspecific coop_setspecific
208 #define scm_getspecific coop_getspecific
209 #define scm_key_delete coop_key_delete
210
211 extern coop_t *coop_global_curr; /* Currently-executing thread. */
212
213 extern void coop_join (coop_t *t);
214 extern void coop_yield (void);
215
216 extern size_t scm_switch_counter;
217 extern size_t scm_thread_count;
218
219 \f
220 /* Some iselect functions. */
221
222 /* I'm not sure whether these three declarations should be here.
223 They're really defined in iselect.c, so you'd think they'd go in
224 iselect.h, but they use coop_t, defined above, which uses things
225 defined in iselect.h. Basically, we're making at best a flailing
226 (and failing) attempt at modularity here, and I don't have time to
227 rethink this at the moment. This code awaits a Hero. --JimB */
228 #ifdef GUILE_ISELECT
229 void coop_timeout_qinsert (coop_q_t *, coop_t *);
230 #endif
231 extern coop_t *coop_next_runnable_thread (void);
232 extern coop_t *coop_wait_for_runnable_thread_now (struct timeval *);
233 extern coop_t *coop_wait_for_runnable_thread (void);
234
235
236 \f
237
238 /* Cooperative threads don't need to have these defined */
239
240 #define SCM_THREAD_CRITICAL_SECTION_START
241 #define SCM_THREAD_CRITICAL_SECTION_END
242
243 \f
244
245 #define SCM_NO_CRITICAL_SECTION_OWNER 0
246 #define SCM_THREAD_SWITCH_COUNT 50 /* was 10 /mdj */
247
248 \f
249
250 #define SCM_THREAD_DEFER
251 #define SCM_THREAD_ALLOW
252 #define SCM_THREAD_REDEFER
253 #define SCM_THREAD_REALLOW_1
254 #define SCM_THREAD_REALLOW_2
255
256 #if 0
257 #define SCM_THREAD_SWITCHING_CODE \
258 do { \
259 if (scm_thread_count > 1) \
260 coop_yield(); \
261 } while (0)
262
263 #else
264 #define SCM_THREAD_SWITCHING_CODE \
265 do { \
266 if (scm_thread_count > 1) \
267 { \
268 scm_switch_counter--; \
269 if (scm_switch_counter == 0) \
270 { \
271 scm_switch_counter = SCM_THREAD_SWITCH_COUNT; \
272 coop_yield(); \
273 } \
274 } \
275 } while (0)
276
277 #endif
278
279 /* For pthreads, this is a value associated with a specific key.
280 * For coop, we use a special field for increased efficiency.
281 */
282 #define SCM_THREAD_LOCAL_DATA (coop_global_curr->data)
283 #define SCM_SET_THREAD_LOCAL_DATA(ptr) (coop_global_curr->data = (ptr))
284
285 #endif /* COOP_DEFSH */
286
287 /*
288 Local Variables:
289 c-file-style: "gnu"
290 End:
291 */