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