1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/* Copyright (C) 2002 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "libguile/validate.h"
#include "libguile/root.h"
#include "libguile/stackchk.h"
#include "libguile/async.h"
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <stdio.h>
void *scm_null_threads_data;
static SCM main_thread;
typedef struct {
int level;
} scm_null_mutex;
typedef struct {
int signalled;
} scm_null_cond;
void
scm_threads_init (SCM_STACKITEM *i)
{
scm_tc16_thread = scm_make_smob_type ("thread", 0);
scm_tc16_mutex = scm_make_smob_type ("mutex", sizeof (scm_null_mutex));
scm_tc16_condvar = scm_make_smob_type ("condition-variable",
sizeof (scm_null_cond));
main_thread = scm_permanent_object (scm_cell (scm_tc16_thread, 0));
scm_null_threads_data = NULL;
}
#ifdef __ia64__
# define SCM_MARK_BACKING_STORE() do { \
ucontext_t ctx; \
SCM_STACKITEM * top, * bot; \
getcontext (&ctx); \
scm_mark_locations ((SCM_STACKITEM *) &ctx.uc_mcontext, \
((size_t) (sizeof (SCM_STACKITEM) - 1 + sizeof ctx.uc_mcontext) \
/ sizeof (SCM_STACKITEM))); \
bot = (SCM_STACKITEM *) __libc_ia64_register_backing_store_base; \
top = (SCM_STACKITEM *) ctx.uc_mcontext.sc_ar_bsp; \
scm_mark_locations (bot, top - bot); } while (0)
#else
# define SCM_MARK_BACKING_STORE()
#endif
void
scm_threads_mark_stacks (void)
{
/* Mark objects on the C stack. */
SCM_FLUSH_REGISTER_WINDOWS;
/* This assumes that all registers are saved into the jmp_buf */
setjmp (scm_save_regs_gc_mark);
scm_mark_locations ((SCM_STACKITEM *) scm_save_regs_gc_mark,
( (size_t) (sizeof (SCM_STACKITEM) - 1 +
sizeof scm_save_regs_gc_mark)
/ sizeof (SCM_STACKITEM)));
{
unsigned long stack_len = scm_stack_size (scm_stack_base);
#if SCM_STACK_GROWS_UP
scm_mark_locations (scm_stack_base, stack_len);
#else
scm_mark_locations (scm_stack_base - stack_len, stack_len);
#endif
}
SCM_MARK_BACKING_STORE();
}
SCM
scm_call_with_new_thread (SCM argl)
#define FUNC_NAME s_call_with_new_thread
{
SCM_MISC_ERROR ("threads are not supported in this version of Guile",
SCM_EOL);
return SCM_BOOL_F;
}
#undef FUNC_NAME
SCM
scm_spawn_thread (scm_t_catch_body body, void *body_data,
scm_t_catch_handler handler, void *handler_data)
{
scm_misc_error ("scm_spawn_thread",
"threads are not supported in this version of Guile",
SCM_EOL);
return SCM_BOOL_F;
}
SCM
scm_current_thread (void)
{
return main_thread;
}
SCM
scm_all_threads (void)
{
return scm_list_1 (main_thread);
}
scm_root_state *
scm_i_thread_root (SCM thread)
{
return (scm_root_state *)scm_null_threads_data;
}
SCM
scm_join_thread (SCM thread)
#define FUNC_NAME s_join_thread
{
SCM_MISC_ERROR ("threads are not supported in this version of Guile",
SCM_EOL);
return SCM_BOOL_F;
}
#undef FUNC_NAME
int
scm_c_thread_exited_p (SCM thread)
#define FUNC_NAME s_scm_thread_exited_p
{
return 0;
}
#undef FUNC_NAME
SCM
scm_yield (void)
{
return SCM_BOOL_T;
}
SCM
scm_make_mutex (void)
{
SCM m = scm_make_smob (scm_tc16_mutex);
scm_null_mutex *mx = SCM_MUTEX_DATA(m);
mx->level = 0;
return m;
}
SCM
scm_lock_mutex (SCM m)
{
scm_null_mutex *mx;
SCM_ASSERT (SCM_MUTEXP (m), m, SCM_ARG1, s_lock_mutex);
mx = SCM_MUTEX_DATA(m);
mx->level++;
return SCM_BOOL_T;
}
SCM
scm_try_mutex (SCM m)
{
return scm_lock_mutex (m); /* will always succeed right away. */
}
SCM
scm_unlock_mutex (SCM m)
{
scm_null_mutex *mx;
SCM_ASSERT (SCM_MUTEXP (m), m, SCM_ARG1, s_unlock_mutex);
mx = SCM_MUTEX_DATA(m);
if (mx->level == 0)
scm_misc_error (s_unlock_mutex, "mutex is not locked", SCM_EOL);
mx->level--;
return SCM_BOOL_T;
}
SCM
scm_make_condition_variable (void)
{
scm_null_cond *cv;
SCM c = scm_make_smob (scm_tc16_condvar);
cv = SCM_CONDVAR_DATA (c);
cv->signalled = 0;
return c;
}
/* Subtract the `struct timeval' values X and Y,
storing the result in RESULT. Might modify Y.
Return 1 if the difference is negative or zero, otherwise 0. */
static int
timeval_subtract (result, x, y)
struct timeval *result, *x, *y;
{
/* Perform the carry for the later subtraction by updating Y. */
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
/* Compute the time remaining to wait.
`tv_usec' is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
/* Return 1 if result is negative or zero. */
return x->tv_sec < y->tv_sec
|| (result->tv_sec == 0 && result->tv_usec == 0);
}
SCM
scm_timed_wait_condition_variable (SCM c, SCM m, SCM t)
#define FUNC_NAME s_wait_condition_variable
{
scm_null_cond *cv;
struct timeval waittime;
SCM_ASSERT (SCM_CONDVARP (c),
c,
SCM_ARG1,
s_wait_condition_variable);
SCM_ASSERT (SCM_MUTEXP (m),
m,
SCM_ARG2,
s_wait_condition_variable);
if (!SCM_UNBNDP (t))
{
if (SCM_CONSP (t))
{
SCM_VALIDATE_UINT_COPY (3, SCM_CAR(t), waittime.tv_sec);
SCM_VALIDATE_UINT_COPY (3, SCM_CDR(t), waittime.tv_usec);
}
else
{
SCM_VALIDATE_UINT_COPY (3, t, waittime.tv_sec);
waittime.tv_usec = 0;
}
}
cv = SCM_CONDVAR_DATA (c);
scm_unlock_mutex (m);
while (!cv->signalled)
{
if (SCM_UNBNDP (t))
select (0, NULL, NULL, NULL, NULL);
else
{
struct timeval now, then, diff;
then = waittime;
gettimeofday (&now, NULL);
if (timeval_subtract (&diff, &then, &now))
break;
select (0, NULL, NULL, NULL, &diff);
}
SCM_ASYNC_TICK;
}
scm_lock_mutex (m);
if (cv->signalled)
{
cv->signalled = 0;
return SCM_BOOL_T;
}
return SCM_BOOL_F;
}
#undef FUNC_NAME
SCM
scm_signal_condition_variable (SCM c)
{
scm_null_cond *cv;
SCM_ASSERT (SCM_CONDVARP (c),
c,
SCM_ARG1,
s_signal_condition_variable);
cv = SCM_CONDVAR_DATA (c);
cv->signalled = 1;
return SCM_BOOL_T;
}
SCM
scm_broadcast_condition_variable (SCM c)
{
return scm_signal_condition_variable (c); /* only one thread anyway. */
}
unsigned long
scm_thread_usleep (unsigned long usec)
{
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = usec;
select (0, NULL, NULL, NULL, &timeout);
return 0; /* Maybe we should calculate actual time slept,
but this is faster... :) */
}
unsigned long
scm_thread_sleep (unsigned long sec)
{
time_t now = time (NULL);
struct timeval timeout;
unsigned long slept;
timeout.tv_sec = sec;
timeout.tv_usec = 0;
select (0, NULL, NULL, NULL, &timeout);
slept = time (NULL) - now;
return slept > sec ? 0 : sec - slept;
}
/*
Local Variables:
c-file-style: "gnu"
End:
*/
|