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 | |
79cd5b8e | 6 | /* Copyright (C) 1996,1997,1998,1999,2000,2001, 2002 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 | ||
ec81cb0b | 49 | #include "libguile/__scm.h" |
a0599745 | 50 | #include "libguile/iselect.h" |
44e8413c | 51 | |
277ee0fa | 52 | #if SCM_HAVE_WINSOCK2_H |
ec81cb0b | 53 | # include <winsock2.h> |
8f99e3f3 SJ |
54 | #endif |
55 | ||
32e738bb MD |
56 | #ifdef GUILE_PTHREAD_COMPAT |
57 | #include <pthread.h> | |
58 | #endif | |
59 | ||
7bfd3b9e JB |
60 | /* This file is included by threads.h, which, in turn, is included by |
61 | libguile.h while coop-threads.h only is included by | |
62 | coop-threads.c. */ | |
63 | ||
64 | /* The coop_t struct must be declared here, since macros in this file | |
65 | refer to the data member. */ | |
66 | ||
67 | /* The notion of a thread is merged with the notion of a queue. | |
68 | Thread stuff: thread status (sp) and stuff to use during | |
69 | (re)initialization. Queue stuff: next thread in the queue | |
70 | (next). */ | |
71 | ||
72 | struct qt_t; | |
73 | ||
74 | typedef struct coop_t { | |
75 | struct qt_t *sp; /* QuickThreads handle. */ | |
76 | void *sto; /* `malloc'-allocated stack. */ | |
77 | ||
78 | struct coop_t *next; /* Next thread in the queue. */ | |
79 | ||
80 | struct coop_t *all_next; | |
81 | struct coop_t *all_prev; | |
82 | ||
83 | void *data; /* Thread local data */ | |
32e738bb MD |
84 | void **specific; /* Data associated with keys */ |
85 | int n_keys; /* Upper limit for keys on this thread */ | |
7bfd3b9e JB |
86 | |
87 | void *base; /* Base of stack */ | |
88 | void *top; /* Top of stack */ | |
89 | ||
90 | void *joining; /* A queue of threads waiting to join this | |
91 | thread */ | |
92 | ||
9997213b MV |
93 | SCM handle; /* SCM handle, protected via scm_all_threads. */ |
94 | ||
44e8413c MD |
95 | int nfds; |
96 | SELECT_TYPE *readfds; | |
97 | SELECT_TYPE *writefds; | |
98 | SELECT_TYPE *exceptfds; | |
99 | int timeoutp; | |
100 | struct timeval wakeup_time; /* Time to stop sleeping */ | |
c44bfbc9 | 101 | int _errno; |
44e8413c | 102 | int retval; |
7bfd3b9e | 103 | |
32e738bb MD |
104 | #ifdef GUILE_PTHREAD_COMPAT |
105 | pthread_t dummy_thread; | |
106 | pthread_mutex_t dummy_mutex; | |
32e738bb | 107 | #endif |
7bfd3b9e JB |
108 | } coop_t; |
109 | ||
c8bf4ecd MD |
110 | /* A queue is a circular list of threads. The queue head is a |
111 | designated list element. If this is a uniprocessor-only | |
112 | implementation we can store the `main' thread in this, but in a | |
113 | multiprocessor there are several `heavy' threads but only one run | |
114 | queue. A fancier implementation might have private run queues, | |
115 | which would lead to a simpler (trivial) implementation */ | |
116 | ||
117 | typedef struct coop_q_t { | |
118 | coop_t t; | |
119 | coop_t *tail; | |
120 | } coop_q_t; | |
121 | ||
122 | /* A Mutex variable is made up of a owner thread, and a queue of threads | |
123 | waiting on the mutex */ | |
124 | ||
125 | typedef struct coop_m { | |
126 | coop_t *owner; /* Mutex owner */ | |
79cd5b8e | 127 | int level; /* for recursive locks. */ |
c8bf4ecd MD |
128 | coop_q_t waiting; /* Queue of waiting threads */ |
129 | } coop_m; | |
130 | ||
32e738bb MD |
131 | typedef int coop_mattr; |
132 | ||
33b001fd MV |
133 | SCM_API int coop_mutex_init (coop_m*); |
134 | SCM_API int coop_new_mutex_init (coop_m*, coop_mattr*); | |
135 | SCM_API int coop_mutex_lock (coop_m*); | |
136 | SCM_API int coop_mutex_trylock (coop_m*); | |
137 | SCM_API int coop_mutex_unlock (coop_m*); | |
138 | SCM_API int coop_mutex_destroy (coop_m*); | |
c8bf4ecd MD |
139 | |
140 | /* A Condition variable is made up of a list of threads waiting on the | |
141 | condition. */ | |
142 | ||
143 | typedef struct coop_c { | |
144 | coop_q_t waiting; /* Queue of waiting threads */ | |
145 | } coop_c; | |
146 | ||
32e738bb MD |
147 | typedef int coop_cattr; |
148 | ||
33b001fd MV |
149 | SCM_API int coop_condition_variable_init (coop_c*); |
150 | SCM_API int coop_new_condition_variable_init (coop_c*, coop_cattr*); | |
151 | SCM_API int coop_condition_variable_wait_mutex (coop_c*, coop_m*); | |
152 | SCM_API int coop_condition_variable_timed_wait_mutex (coop_c*, | |
153 | coop_m*, | |
ec81cb0b | 154 | const scm_t_timespec *abstime); |
33b001fd | 155 | SCM_API int coop_condition_variable_signal (coop_c*); |
79cd5b8e | 156 | SCM_API int coop_condition_variable_broadcast (coop_c*); |
33b001fd | 157 | SCM_API int coop_condition_variable_destroy (coop_c*); |
c8bf4ecd | 158 | |
32e738bb MD |
159 | typedef int coop_k; |
160 | ||
92c2555f | 161 | typedef coop_k scm_t_key; |
32e738bb | 162 | |
33b001fd MV |
163 | SCM_API int coop_key_create (coop_k *keyp, void (*destruktor) (void *value)); |
164 | SCM_API int coop_setspecific (coop_k key, const void *value); | |
165 | SCM_API void *coop_getspecific (coop_k key); | |
166 | SCM_API int coop_key_delete (coop_k); | |
32e738bb MD |
167 | #define scm_key_create coop_key_create |
168 | #define scm_setspecific coop_setspecific | |
169 | #define scm_getspecific coop_getspecific | |
170 | #define scm_key_delete coop_key_delete | |
171 | ||
33b001fd | 172 | SCM_API coop_t *coop_global_curr; /* Currently-executing thread. */ |
7bfd3b9e | 173 | |
33b001fd MV |
174 | SCM_API void coop_join (coop_t *t); |
175 | SCM_API void coop_yield (void); | |
7bfd3b9e | 176 | |
33b001fd MV |
177 | SCM_API size_t scm_switch_counter; |
178 | SCM_API size_t scm_thread_count; | |
7bfd3b9e JB |
179 | |
180 | \f | |
6d71500e JB |
181 | /* Some iselect functions. */ |
182 | ||
183 | /* I'm not sure whether these three declarations should be here. | |
184 | They're really defined in iselect.c, so you'd think they'd go in | |
185 | iselect.h, but they use coop_t, defined above, which uses things | |
186 | defined in iselect.h. Basically, we're making at best a flailing | |
187 | (and failing) attempt at modularity here, and I don't have time to | |
3d7f708f MV |
188 | rethink this at the moment. This code awaits a Hero. --JimB |
189 | */ | |
33b001fd | 190 | SCM_API void coop_timeout_qinsert (coop_q_t *, coop_t *); |
33b001fd MV |
191 | SCM_API coop_t *coop_next_runnable_thread (void); |
192 | SCM_API coop_t *coop_wait_for_runnable_thread_now (struct timeval *); | |
193 | SCM_API coop_t *coop_wait_for_runnable_thread (void); | |
6d71500e JB |
194 | |
195 | ||
196 | \f | |
7bfd3b9e JB |
197 | |
198 | /* Cooperative threads don't need to have these defined */ | |
199 | ||
216eedfc DH |
200 | #define SCM_CRITICAL_SECTION_START |
201 | #define SCM_CRITICAL_SECTION_END | |
7bfd3b9e JB |
202 | |
203 | \f | |
204 | ||
205 | #define SCM_NO_CRITICAL_SECTION_OWNER 0 | |
206 | #define SCM_THREAD_SWITCH_COUNT 50 /* was 10 /mdj */ | |
207 | ||
208 | \f | |
209 | ||
7bfd3b9e JB |
210 | #if 0 |
211 | #define SCM_THREAD_SWITCHING_CODE \ | |
d3a6bc94 | 212 | do { \ |
7bfd3b9e JB |
213 | if (scm_thread_count > 1) \ |
214 | coop_yield(); \ | |
d3a6bc94 | 215 | } while (0) |
7bfd3b9e JB |
216 | |
217 | #else | |
218 | #define SCM_THREAD_SWITCHING_CODE \ | |
d3a6bc94 | 219 | do { \ |
7bfd3b9e JB |
220 | if (scm_thread_count > 1) \ |
221 | { \ | |
222 | scm_switch_counter--; \ | |
223 | if (scm_switch_counter == 0) \ | |
224 | { \ | |
225 | scm_switch_counter = SCM_THREAD_SWITCH_COUNT; \ | |
226 | coop_yield(); \ | |
227 | } \ | |
228 | } \ | |
d3a6bc94 | 229 | } while (0) |
7bfd3b9e JB |
230 | |
231 | #endif | |
232 | ||
32e738bb MD |
233 | /* For pthreads, this is a value associated with a specific key. |
234 | * For coop, we use a special field for increased efficiency. | |
235 | */ | |
7bfd3b9e JB |
236 | #define SCM_THREAD_LOCAL_DATA (coop_global_curr->data) |
237 | #define SCM_SET_THREAD_LOCAL_DATA(ptr) (coop_global_curr->data = (ptr)) | |
238 | ||
0527e687 | 239 | #endif /* SCM_COOP_DEFS_H */ |
89e00824 ML |
240 | |
241 | /* | |
242 | Local Variables: | |
243 | c-file-style: "gnu" | |
244 | End: | |
245 | */ |