*** empty log message ***
[bpt/guile.git] / libguile / async.c
CommitLineData
273b7b94 1/* Copyright (C) 1995, 96, 97, 98, 2000 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd
JB
45\f
46
47#include <stdio.h>
48#include <signal.h>
49#include "_scm.h"
20e6290e
JB
50#include "eval.h"
51#include "throw.h"
ba11fd4c 52#include "root.h"
20e6290e
JB
53#include "smob.h"
54
b6791b2e 55#include "validate.h"
20e6290e 56#include "async.h"
0f2d19dd 57
95b88819
GH
58#ifdef HAVE_STRING_H
59#include <string.h>
60#endif
0f2d19dd
JB
61#ifdef HAVE_UNISTD_H
62#include <unistd.h>
63#endif
64
65
66\f
67/* {Asynchronous Events}
68 *
69 *
70 * Async == thunk + mark.
71 *
72 * Setting the mark guarantees future execution of the thunk. More
73 * than one set may be satisfied by a single execution.
74 *
75 * scm_tick_clock decremented once per SCM_ALLOW_INTS.
76 * Async execution triggered by SCM_ALLOW_INTS when scm_tick_clock drops to 0.
77 * Async execution prevented by scm_mask_ints != 0.
78 *
79 * If the clock reaches 0 when scm_mask_ints != 0, then reset the clock
80 * to 1.
81 *
82 * If the clock reaches 0 any other time, run marked asyncs.
83 *
84 * From a unix signal handler, mark a corresponding async and set the clock
85 * to 1. Do SCM_REDEFER_INTS;/SCM_REALLOW_INTS so that if the signal handler is not
86 * called in the dynamic scope of a critical section, it is excecuted immediately.
87 *
88 * Overall, closely timed signals of a particular sort may be combined. Pending signals
89 * are delivered in a fixed priority order, regardless of arrival order.
90 *
91 */
92
a574455a
GH
93/* True between SCM_DEFER_INTS and SCM_ALLOW_INTS, and
94 * when the interpreter is not running at all.
95 */
96int scm_ints_disabled = 1;
9f0e55a6 97unsigned int scm_mask_ints = 1;
0f2d19dd 98
9f0e55a6 99#ifdef GUILE_OLD_ASYNC_CLICK
0f2d19dd
JB
100unsigned int scm_async_clock = 20;
101static unsigned int scm_async_rate = 20;
0f2d19dd
JB
102
103static unsigned int scm_tick_clock = 0;
104static unsigned int scm_tick_rate = 0;
105static unsigned int scm_desired_tick_rate = 0;
106static unsigned int scm_switch_clock = 0;
107static unsigned int scm_switch_rate = 0;
108static unsigned int scm_desired_switch_rate = 0;
9f0e55a6
MD
109#else
110int scm_asyncs_pending_p = 0;
111#endif
0f2d19dd 112
0f2d19dd
JB
113static long scm_tc16_async;
114
0f2d19dd
JB
115\f
116
9f0e55a6 117#ifdef GUILE_OLD_ASYNC_CLICK
5e569ca8
MD
118int
119scm_asyncs_pending ()
0f2d19dd
JB
120{
121 SCM pos;
122 pos = scm_asyncs;
123 while (pos != SCM_EOL)
124 {
125 SCM a;
126 struct scm_async * it;
127 a = SCM_CAR (pos);
128 it = SCM_ASYNC (a);
129 if (it->got_it)
130 return 1;
131 pos = SCM_CDR (pos);
132 }
133 return 0;
134}
135
9ea54cc6 136static SCM
1bbd0b84 137scm_sys_tick_async_thunk (void)
9ea54cc6
GH
138{
139 scm_deliver_signal (SCM_TICK_SIGNAL);
140 return SCM_BOOL_F;
141}
1cc91f1b 142
0f2d19dd
JB
143void
144scm_async_click ()
0f2d19dd
JB
145{
146 int owe_switch;
147 int owe_tick;
148
149 if (!scm_switch_rate)
150 {
151 owe_switch = 0;
152 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
153 scm_desired_switch_rate = 0;
154 }
155 else
156 {
157 owe_switch = (scm_async_rate >= scm_switch_clock);
158 if (owe_switch)
159 {
160 if (scm_desired_switch_rate)
161 {
162 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
163 scm_desired_switch_rate = 0;
164 }
165 else
166 scm_switch_clock = scm_switch_rate;
167 }
168 else
169 {
170 if (scm_desired_switch_rate)
171 {
172 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
173 scm_desired_switch_rate = 0;
174 }
175 else
176 scm_switch_clock -= scm_async_rate;
177 }
178 }
179
180 if (scm_mask_ints)
181 {
182 if (owe_switch)
183 scm_switch ();
184 scm_async_clock = 1;
185 return;;
186 }
187
188 if (!scm_tick_rate)
189 {
190 unsigned int r;
191 owe_tick = 0;
192 r = scm_desired_tick_rate;
193 if (r)
194 {
195 scm_desired_tick_rate = 0;
196 scm_tick_rate = r;
197 scm_tick_clock = r;
198 }
199 }
200 else
201 {
202 owe_tick = (scm_async_rate >= scm_tick_clock);
203 if (owe_tick)
204 {
205 scm_tick_clock = scm_tick_rate = scm_desired_tick_rate;
206 scm_desired_tick_rate = 0;
207 }
208 else
209 {
210 if (scm_desired_tick_rate)
211 {
212 scm_tick_clock = scm_tick_rate = scm_desired_tick_rate;
213 scm_desired_tick_rate = 0;
214 }
215 else
216 scm_tick_clock -= scm_async_rate;
217 }
218 }
219
9ea54cc6
GH
220 /*
221 if (owe_tick)
222 scm_async_mark (system_signal_asyncs[SCM_SIG_ORD(SCM_TICK_SIGNAL)]);
223 */
0f2d19dd
JB
224
225 SCM_DEFER_INTS;
226 if (scm_tick_rate && scm_switch_rate)
227 {
228 scm_async_rate = min (scm_tick_clock, scm_switch_clock);
229 scm_async_clock = scm_async_rate;
230 }
231 else if (scm_tick_rate)
232 {
233 scm_async_clock = scm_async_rate = scm_tick_clock;
234 }
235 else if (scm_switch_rate)
236 {
237 scm_async_clock = scm_async_rate = scm_switch_clock;
238 }
239 else
240 scm_async_clock = scm_async_rate = 1 << 16;
241 SCM_ALLOW_INTS_ONLY;
242
243 tail:
244 scm_run_asyncs (scm_asyncs);
245
246 SCM_DEFER_INTS;
5e569ca8 247 if (scm_asyncs_pending ())
0f2d19dd
JB
248 {
249 SCM_ALLOW_INTS_ONLY;
250 goto tail;
251 }
252 SCM_ALLOW_INTS;
253
254 if (owe_switch)
255 scm_switch ();
256}
257
9f0e55a6
MD
258#else
259
260void
261scm_async_click ()
262{
263 if (!scm_mask_ints)
264 do
265 scm_run_asyncs (scm_asyncs);
266 while (scm_asyncs_pending_p);
267}
268#endif
0f2d19dd
JB
269
270\f
271
1cc91f1b 272
9f0e55a6
MD
273#if 0 /* Thread switching code should probably reside here, but the
274 async switching code doesn't seem to work, so it's put in the
275 SCM_DEFER_INTS macro instead. /mdj */
0f2d19dd
JB
276void
277scm_switch ()
7ad737b6 278{
7ad737b6 279 SCM_THREAD_SWITCHING_CODE;
7ad737b6 280}
9f0e55a6 281#endif
0f2d19dd 282
0f2d19dd
JB
283\f
284
0f2d19dd 285static SCM
1bbd0b84 286mark_async (SCM obj)
0f2d19dd
JB
287{
288 struct scm_async * it;
0f2d19dd
JB
289 it = SCM_ASYNC (obj);
290 return it->thunk;
291}
292
0f2d19dd
JB
293\f
294
3b3b36dd 295SCM_DEFINE (scm_async, "async", 1, 0, 0,
1bbd0b84
GB
296 (SCM thunk),
297"")
298#define FUNC_NAME s_scm_async
0f2d19dd 299{
273b7b94 300 SCM_RETURN_NEWSMOB2 (scm_tc16_async, 0, thunk);
0f2d19dd 301}
1bbd0b84 302#undef FUNC_NAME
0f2d19dd 303
3b3b36dd 304SCM_DEFINE (scm_system_async, "system-async", 1, 0, 0,
1bbd0b84
GB
305 (SCM thunk),
306"")
307#define FUNC_NAME s_scm_system_async
0f2d19dd
JB
308{
309 SCM it;
310 SCM list;
311
312 it = scm_async (thunk);
273b7b94
MD
313 SCM_NEWCELL (list);
314 SCM_SETCAR (list, it);
315 SCM_SETCDR (list, scm_asyncs);
0f2d19dd 316 scm_asyncs = list;
0f2d19dd
JB
317 return it;
318}
1bbd0b84 319#undef FUNC_NAME
0f2d19dd 320
3b3b36dd 321SCM_DEFINE (scm_async_mark, "async-mark", 1, 0, 0,
1bbd0b84
GB
322 (SCM a),
323"")
324#define FUNC_NAME s_scm_async_mark
0f2d19dd
JB
325{
326 struct scm_async * it;
3b3b36dd 327 SCM_VALIDATE_ASYNC_COPY (1,a,it);
9f0e55a6 328#ifdef GUILE_OLD_ASYNC_CLICK
0f2d19dd 329 it->got_it = 1;
9f0e55a6
MD
330#else
331 scm_asyncs_pending_p = it->got_it = 1;
332#endif
0f2d19dd
JB
333 return SCM_UNSPECIFIED;
334}
1bbd0b84 335#undef FUNC_NAME
0f2d19dd
JB
336
337
3b3b36dd 338SCM_DEFINE (scm_system_async_mark, "system-async-mark", 1, 0, 0,
1bbd0b84
GB
339 (SCM a),
340"")
341#define FUNC_NAME s_scm_system_async_mark
0f2d19dd
JB
342{
343 struct scm_async * it;
3b3b36dd 344 SCM_VALIDATE_ASYNC_COPY (1,a,it);
0f2d19dd 345 SCM_REDEFER_INTS;
9f0e55a6 346#ifdef GUILE_OLD_ASYNC_CLICK
0f2d19dd
JB
347 it->got_it = 1;
348 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
349 scm_async_clock = 1;
9f0e55a6
MD
350#else
351 scm_asyncs_pending_p = it->got_it = 1;
352#endif
0f2d19dd
JB
353 SCM_REALLOW_INTS;
354 return SCM_UNSPECIFIED;
355}
1bbd0b84 356#undef FUNC_NAME
0f2d19dd
JB
357
358
3b3b36dd 359SCM_DEFINE (scm_run_asyncs, "run-asyncs", 1, 0, 0,
1bbd0b84
GB
360 (SCM list_of_a),
361"")
362#define FUNC_NAME s_scm_run_asyncs
0f2d19dd 363{
9f0e55a6 364#ifdef GUILE_OLD_ASYNC_CLICK
0f2d19dd
JB
365 if (scm_mask_ints)
366 return SCM_BOOL_F;
9f0e55a6
MD
367#endif
368 scm_asyncs_pending_p = 0;
1bbd0b84 369 while (list_of_a != SCM_EOL)
0f2d19dd
JB
370 {
371 SCM a;
372 struct scm_async * it;
9f0e55a6 373 SCM_VALIDATE_CONS (1, list_of_a);
1bbd0b84 374 a = SCM_CAR (list_of_a);
5d2d2ffc 375 SCM_VALIDATE_ASYNC_COPY (SCM_ARG1,a,it);
0f2d19dd
JB
376 scm_mask_ints = 1;
377 if (it->got_it)
378 {
379 it->got_it = 0;
380 scm_apply (it->thunk, SCM_EOL, SCM_EOL);
381 }
382 scm_mask_ints = 0;
1bbd0b84 383 list_of_a = SCM_CDR (list_of_a);
0f2d19dd
JB
384 }
385 return SCM_BOOL_T;
386}
1bbd0b84 387#undef FUNC_NAME
0f2d19dd
JB
388
389\f
390
391
3b3b36dd 392SCM_DEFINE (scm_noop, "noop", 0, 0, 1,
1bbd0b84
GB
393 (SCM args),
394"")
395#define FUNC_NAME s_scm_noop
0f2d19dd 396{
5d2d2ffc 397 return (SCM_NULLP (args) ? SCM_BOOL_F : SCM_CAR (args));
0f2d19dd 398}
1bbd0b84 399#undef FUNC_NAME
0f2d19dd
JB
400
401
402\f
403
9f0e55a6
MD
404#ifdef GUILE_OLD_ASYNC_CLICK
405
3b3b36dd 406SCM_DEFINE (scm_set_tick_rate, "set-tick-rate", 1, 0, 0,
1bbd0b84
GB
407 (SCM n),
408"")
409#define FUNC_NAME s_scm_set_tick_rate
0f2d19dd 410{
9b139b4e
DH
411 unsigned int old_n = scm_tick_rate;
412 SCM_VALIDATE_INUM (1, n);
413 scm_desired_tick_rate = SCM_INUM (n);
0f2d19dd
JB
414 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
415 scm_async_clock = 1;
416 return SCM_MAKINUM (old_n);
417}
1bbd0b84 418#undef FUNC_NAME
0f2d19dd
JB
419
420\f
421
422
3b3b36dd 423SCM_DEFINE (scm_set_switch_rate, "set-switch-rate", 1, 0, 0,
1bbd0b84
GB
424 (SCM n),
425"")
426#define FUNC_NAME s_scm_set_switch_rate
0f2d19dd 427{
9b139b4e
DH
428 unsigned int old_n = scm_switch_rate;
429 SCM_VALIDATE_INUM (1, n);
430 scm_desired_switch_rate = SCM_INUM (n);
0f2d19dd
JB
431 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
432 scm_async_clock = 1;
433 return SCM_MAKINUM (old_n);
434}
1bbd0b84 435#undef FUNC_NAME
0f2d19dd 436
9f0e55a6 437#endif
0f2d19dd
JB
438\f
439
9ea54cc6
GH
440/* points to the GC system-async, so that scm_gc_end can find it. */
441SCM scm_gc_async;
1cc91f1b 442
9ea54cc6
GH
443/* the vcell for gc-thunk. */
444static SCM scm_gc_vcell;
1cc91f1b 445
9ea54cc6
GH
446/* the thunk installed in the GC system-async, which is marked at the
447 end of garbage collection. */
0f2d19dd 448static SCM
9ea54cc6 449scm_sys_gc_async_thunk (void)
0f2d19dd 450{
9ea54cc6
GH
451 if (SCM_NFALSEP (scm_gc_vcell))
452 {
453 SCM proc = SCM_CDR (scm_gc_vcell);
1cc91f1b 454
9ea54cc6
GH
455 if (SCM_NFALSEP (proc) && !SCM_UNBNDP (proc))
456 scm_apply (proc, SCM_EOL, SCM_EOL);
457 }
458 return SCM_UNSPECIFIED;
0f2d19dd
JB
459}
460
0f2d19dd
JB
461\f
462
3b3b36dd 463SCM_DEFINE (scm_unmask_signals, "unmask-signals", 0, 0, 0,
1bbd0b84
GB
464 (),
465"")
466#define FUNC_NAME s_scm_unmask_signals
0f2d19dd
JB
467{
468 scm_mask_ints = 0;
469 return SCM_UNSPECIFIED;
470}
1bbd0b84 471#undef FUNC_NAME
0f2d19dd
JB
472
473
3b3b36dd 474SCM_DEFINE (scm_mask_signals, "mask-signals", 0, 0, 0,
1bbd0b84
GB
475 (),
476"")
477#define FUNC_NAME s_scm_mask_signals
0f2d19dd
JB
478{
479 scm_mask_ints = 1;
480 return SCM_UNSPECIFIED;
481}
1bbd0b84 482#undef FUNC_NAME
0f2d19dd
JB
483
484\f
485
0f2d19dd
JB
486void
487scm_init_async ()
0f2d19dd
JB
488{
489 SCM a_thunk;
273b7b94 490 scm_tc16_async = scm_make_smob_type_mfpe ("async", 0,
23a62151 491 mark_async, NULL, NULL, NULL);
9ea54cc6 492 scm_gc_vcell = scm_sysintern ("gc-thunk", SCM_BOOL_F);
0f2d19dd 493 a_thunk = scm_make_gsubr ("%gc-thunk", 0, 0, 0, scm_sys_gc_async_thunk);
9ea54cc6 494 scm_gc_async = scm_system_async (a_thunk);
0f2d19dd 495
0f2d19dd
JB
496#include "async.x"
497}
89e00824
ML
498
499/*
500 Local Variables:
501 c-file-style: "gnu"
502 End:
503*/