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