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