Oops
[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
6587a966 136#if 0
9ea54cc6 137static SCM
1bbd0b84 138scm_sys_tick_async_thunk (void)
9ea54cc6
GH
139{
140 scm_deliver_signal (SCM_TICK_SIGNAL);
141 return SCM_BOOL_F;
142}
6587a966 143#endif
1cc91f1b 144
0f2d19dd
JB
145void
146scm_async_click ()
0f2d19dd
JB
147{
148 int owe_switch;
149 int owe_tick;
150
151 if (!scm_switch_rate)
152 {
153 owe_switch = 0;
154 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
155 scm_desired_switch_rate = 0;
156 }
157 else
158 {
159 owe_switch = (scm_async_rate >= scm_switch_clock);
160 if (owe_switch)
161 {
162 if (scm_desired_switch_rate)
163 {
164 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
165 scm_desired_switch_rate = 0;
166 }
167 else
168 scm_switch_clock = scm_switch_rate;
169 }
170 else
171 {
172 if (scm_desired_switch_rate)
173 {
174 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
175 scm_desired_switch_rate = 0;
176 }
177 else
178 scm_switch_clock -= scm_async_rate;
179 }
180 }
181
182 if (scm_mask_ints)
183 {
184 if (owe_switch)
185 scm_switch ();
186 scm_async_clock = 1;
187 return;;
188 }
189
190 if (!scm_tick_rate)
191 {
192 unsigned int r;
193 owe_tick = 0;
194 r = scm_desired_tick_rate;
195 if (r)
196 {
197 scm_desired_tick_rate = 0;
198 scm_tick_rate = r;
199 scm_tick_clock = r;
200 }
201 }
202 else
203 {
204 owe_tick = (scm_async_rate >= scm_tick_clock);
205 if (owe_tick)
206 {
207 scm_tick_clock = scm_tick_rate = scm_desired_tick_rate;
208 scm_desired_tick_rate = 0;
209 }
210 else
211 {
212 if (scm_desired_tick_rate)
213 {
214 scm_tick_clock = scm_tick_rate = scm_desired_tick_rate;
215 scm_desired_tick_rate = 0;
216 }
217 else
218 scm_tick_clock -= scm_async_rate;
219 }
220 }
221
9ea54cc6
GH
222 /*
223 if (owe_tick)
224 scm_async_mark (system_signal_asyncs[SCM_SIG_ORD(SCM_TICK_SIGNAL)]);
225 */
0f2d19dd
JB
226
227 SCM_DEFER_INTS;
228 if (scm_tick_rate && scm_switch_rate)
229 {
230 scm_async_rate = min (scm_tick_clock, scm_switch_clock);
231 scm_async_clock = scm_async_rate;
232 }
233 else if (scm_tick_rate)
234 {
235 scm_async_clock = scm_async_rate = scm_tick_clock;
236 }
237 else if (scm_switch_rate)
238 {
239 scm_async_clock = scm_async_rate = scm_switch_clock;
240 }
241 else
242 scm_async_clock = scm_async_rate = 1 << 16;
243 SCM_ALLOW_INTS_ONLY;
244
245 tail:
246 scm_run_asyncs (scm_asyncs);
247
248 SCM_DEFER_INTS;
5e569ca8 249 if (scm_asyncs_pending ())
0f2d19dd
JB
250 {
251 SCM_ALLOW_INTS_ONLY;
252 goto tail;
253 }
254 SCM_ALLOW_INTS;
255
256 if (owe_switch)
257 scm_switch ();
258}
259
9f0e55a6
MD
260#else
261
262void
263scm_async_click ()
264{
265 if (!scm_mask_ints)
266 do
267 scm_run_asyncs (scm_asyncs);
268 while (scm_asyncs_pending_p);
269}
270#endif
0f2d19dd
JB
271
272\f
273
1cc91f1b 274
9f0e55a6
MD
275#if 0 /* Thread switching code should probably reside here, but the
276 async switching code doesn't seem to work, so it's put in the
277 SCM_DEFER_INTS macro instead. /mdj */
0f2d19dd
JB
278void
279scm_switch ()
7ad737b6 280{
7ad737b6 281 SCM_THREAD_SWITCHING_CODE;
7ad737b6 282}
9f0e55a6 283#endif
0f2d19dd 284
0f2d19dd
JB
285\f
286
0f2d19dd 287static SCM
1bbd0b84 288mark_async (SCM obj)
0f2d19dd
JB
289{
290 struct scm_async * it;
0f2d19dd
JB
291 it = SCM_ASYNC (obj);
292 return it->thunk;
293}
294
0f2d19dd
JB
295\f
296
3b3b36dd 297SCM_DEFINE (scm_async, "async", 1, 0, 0,
1bbd0b84
GB
298 (SCM thunk),
299"")
300#define FUNC_NAME s_scm_async
0f2d19dd 301{
273b7b94 302 SCM_RETURN_NEWSMOB2 (scm_tc16_async, 0, thunk);
0f2d19dd 303}
1bbd0b84 304#undef FUNC_NAME
0f2d19dd 305
3b3b36dd 306SCM_DEFINE (scm_system_async, "system-async", 1, 0, 0,
1bbd0b84
GB
307 (SCM thunk),
308"")
309#define FUNC_NAME s_scm_system_async
0f2d19dd
JB
310{
311 SCM it;
312 SCM list;
313
314 it = scm_async (thunk);
273b7b94
MD
315 SCM_NEWCELL (list);
316 SCM_SETCAR (list, it);
317 SCM_SETCDR (list, scm_asyncs);
0f2d19dd 318 scm_asyncs = list;
0f2d19dd
JB
319 return it;
320}
1bbd0b84 321#undef FUNC_NAME
0f2d19dd 322
3b3b36dd 323SCM_DEFINE (scm_async_mark, "async-mark", 1, 0, 0,
1bbd0b84
GB
324 (SCM a),
325"")
326#define FUNC_NAME s_scm_async_mark
0f2d19dd
JB
327{
328 struct scm_async * it;
3b3b36dd 329 SCM_VALIDATE_ASYNC_COPY (1,a,it);
9f0e55a6 330#ifdef GUILE_OLD_ASYNC_CLICK
0f2d19dd 331 it->got_it = 1;
9f0e55a6
MD
332#else
333 scm_asyncs_pending_p = it->got_it = 1;
334#endif
0f2d19dd
JB
335 return SCM_UNSPECIFIED;
336}
1bbd0b84 337#undef FUNC_NAME
0f2d19dd
JB
338
339
3b3b36dd 340SCM_DEFINE (scm_system_async_mark, "system-async-mark", 1, 0, 0,
1bbd0b84
GB
341 (SCM a),
342"")
343#define FUNC_NAME s_scm_system_async_mark
0f2d19dd
JB
344{
345 struct scm_async * it;
3b3b36dd 346 SCM_VALIDATE_ASYNC_COPY (1,a,it);
0f2d19dd 347 SCM_REDEFER_INTS;
9f0e55a6 348#ifdef GUILE_OLD_ASYNC_CLICK
0f2d19dd
JB
349 it->got_it = 1;
350 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
351 scm_async_clock = 1;
9f0e55a6
MD
352#else
353 scm_asyncs_pending_p = it->got_it = 1;
354#endif
0f2d19dd
JB
355 SCM_REALLOW_INTS;
356 return SCM_UNSPECIFIED;
357}
1bbd0b84 358#undef FUNC_NAME
0f2d19dd
JB
359
360
3b3b36dd 361SCM_DEFINE (scm_run_asyncs, "run-asyncs", 1, 0, 0,
1bbd0b84
GB
362 (SCM list_of_a),
363"")
364#define FUNC_NAME s_scm_run_asyncs
0f2d19dd 365{
9f0e55a6 366#ifdef GUILE_OLD_ASYNC_CLICK
0f2d19dd
JB
367 if (scm_mask_ints)
368 return SCM_BOOL_F;
6587a966 369#else
9f0e55a6 370 scm_asyncs_pending_p = 0;
6587a966 371#endif
1bbd0b84 372 while (list_of_a != SCM_EOL)
0f2d19dd
JB
373 {
374 SCM a;
375 struct scm_async * it;
9f0e55a6 376 SCM_VALIDATE_CONS (1, list_of_a);
1bbd0b84 377 a = SCM_CAR (list_of_a);
5d2d2ffc 378 SCM_VALIDATE_ASYNC_COPY (SCM_ARG1,a,it);
0f2d19dd
JB
379 scm_mask_ints = 1;
380 if (it->got_it)
381 {
382 it->got_it = 0;
383 scm_apply (it->thunk, SCM_EOL, SCM_EOL);
384 }
385 scm_mask_ints = 0;
1bbd0b84 386 list_of_a = SCM_CDR (list_of_a);
0f2d19dd
JB
387 }
388 return SCM_BOOL_T;
389}
1bbd0b84 390#undef FUNC_NAME
0f2d19dd
JB
391
392\f
393
394
3b3b36dd 395SCM_DEFINE (scm_noop, "noop", 0, 0, 1,
1bbd0b84
GB
396 (SCM args),
397"")
398#define FUNC_NAME s_scm_noop
0f2d19dd 399{
5d2d2ffc 400 return (SCM_NULLP (args) ? SCM_BOOL_F : SCM_CAR (args));
0f2d19dd 401}
1bbd0b84 402#undef FUNC_NAME
0f2d19dd
JB
403
404
405\f
406
9f0e55a6
MD
407#ifdef GUILE_OLD_ASYNC_CLICK
408
3b3b36dd 409SCM_DEFINE (scm_set_tick_rate, "set-tick-rate", 1, 0, 0,
1bbd0b84
GB
410 (SCM n),
411"")
412#define FUNC_NAME s_scm_set_tick_rate
0f2d19dd 413{
9b139b4e
DH
414 unsigned int old_n = scm_tick_rate;
415 SCM_VALIDATE_INUM (1, n);
416 scm_desired_tick_rate = SCM_INUM (n);
0f2d19dd
JB
417 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
418 scm_async_clock = 1;
419 return SCM_MAKINUM (old_n);
420}
1bbd0b84 421#undef FUNC_NAME
0f2d19dd
JB
422
423\f
424
425
3b3b36dd 426SCM_DEFINE (scm_set_switch_rate, "set-switch-rate", 1, 0, 0,
1bbd0b84
GB
427 (SCM n),
428"")
429#define FUNC_NAME s_scm_set_switch_rate
0f2d19dd 430{
9b139b4e
DH
431 unsigned int old_n = scm_switch_rate;
432 SCM_VALIDATE_INUM (1, n);
433 scm_desired_switch_rate = SCM_INUM (n);
0f2d19dd
JB
434 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
435 scm_async_clock = 1;
436 return SCM_MAKINUM (old_n);
437}
1bbd0b84 438#undef FUNC_NAME
0f2d19dd 439
9f0e55a6 440#endif
0f2d19dd
JB
441\f
442
9ea54cc6
GH
443/* points to the GC system-async, so that scm_gc_end can find it. */
444SCM scm_gc_async;
1cc91f1b 445
9ea54cc6
GH
446/* the vcell for gc-thunk. */
447static SCM scm_gc_vcell;
1cc91f1b 448
9ea54cc6
GH
449/* the thunk installed in the GC system-async, which is marked at the
450 end of garbage collection. */
0f2d19dd 451static SCM
9ea54cc6 452scm_sys_gc_async_thunk (void)
0f2d19dd 453{
9ea54cc6
GH
454 if (SCM_NFALSEP (scm_gc_vcell))
455 {
456 SCM proc = SCM_CDR (scm_gc_vcell);
1cc91f1b 457
9ea54cc6
GH
458 if (SCM_NFALSEP (proc) && !SCM_UNBNDP (proc))
459 scm_apply (proc, SCM_EOL, SCM_EOL);
460 }
461 return SCM_UNSPECIFIED;
0f2d19dd
JB
462}
463
0f2d19dd
JB
464\f
465
3b3b36dd 466SCM_DEFINE (scm_unmask_signals, "unmask-signals", 0, 0, 0,
1bbd0b84
GB
467 (),
468"")
469#define FUNC_NAME s_scm_unmask_signals
0f2d19dd
JB
470{
471 scm_mask_ints = 0;
472 return SCM_UNSPECIFIED;
473}
1bbd0b84 474#undef FUNC_NAME
0f2d19dd
JB
475
476
3b3b36dd 477SCM_DEFINE (scm_mask_signals, "mask-signals", 0, 0, 0,
1bbd0b84
GB
478 (),
479"")
480#define FUNC_NAME s_scm_mask_signals
0f2d19dd
JB
481{
482 scm_mask_ints = 1;
483 return SCM_UNSPECIFIED;
484}
1bbd0b84 485#undef FUNC_NAME
0f2d19dd
JB
486
487\f
488
0f2d19dd
JB
489void
490scm_init_async ()
0f2d19dd
JB
491{
492 SCM a_thunk;
273b7b94 493 scm_tc16_async = scm_make_smob_type_mfpe ("async", 0,
23a62151 494 mark_async, NULL, NULL, NULL);
9ea54cc6 495 scm_gc_vcell = scm_sysintern ("gc-thunk", SCM_BOOL_F);
0f2d19dd 496 a_thunk = scm_make_gsubr ("%gc-thunk", 0, 0, 0, scm_sys_gc_async_thunk);
9ea54cc6 497 scm_gc_async = scm_system_async (a_thunk);
0f2d19dd 498
0f2d19dd
JB
499#include "async.x"
500}
89e00824
ML
501
502/*
503 Local Variables:
504 c-file-style: "gnu"
505 End:
506*/