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