(scm_display_error, scm_set_print_params_x, scm_display_application,
[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>
a0599745
MD
49#include "libguile/_scm.h"
50#include "libguile/eval.h"
51#include "libguile/throw.h"
52#include "libguile/root.h"
53#include "libguile/smob.h"
20e6290e 54
a0599745
MD
55#include "libguile/validate.h"
56#include "libguile/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
e841c3e0 113static scm_bits_t tc16_async;
e94e3f21
ML
114
115\f
116
117/* cmm: this has SCM_ prefix because SCM_MAKE_VALIDATE expects it.
118 this is ugly. */
e841c3e0
KN
119#define SCM_ASYNCP(X) SCM_TYP16_PREDICATE (tc16_async, X)
120#define VALIDATE_ASYNC(pos,a) SCM_MAKE_VALIDATE(pos, a, ASYNCP)
e94e3f21
ML
121
122#define ASYNC_GOT_IT(X) (SCM_CELL_WORD_0 (X) >> 16)
d1ca2c64 123#define SET_ASYNC_GOT_IT(X, V) (SCM_SET_CELL_WORD_0 ((X), SCM_TYP16 (X) | ((V) << 16)))
e94e3f21 124#define ASYNC_THUNK(X) SCM_CELL_OBJECT_1 (X)
0f2d19dd 125
0f2d19dd
JB
126\f
127
9f0e55a6 128#ifdef GUILE_OLD_ASYNC_CLICK
5e569ca8
MD
129int
130scm_asyncs_pending ()
0f2d19dd
JB
131{
132 SCM pos;
133 pos = scm_asyncs;
134 while (pos != SCM_EOL)
135 {
843e4e9d 136 SCM a = SCM_CAR (pos);
e94e3f21 137 if (ASYNC_GOT_IT (a))
0f2d19dd
JB
138 return 1;
139 pos = SCM_CDR (pos);
140 }
141 return 0;
142}
143
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 }
843e4e9d 189
0f2d19dd
JB
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
0f2d19dd
JB
222 SCM_DEFER_INTS;
223 if (scm_tick_rate && scm_switch_rate)
224 {
225 scm_async_rate = min (scm_tick_clock, scm_switch_clock);
226 scm_async_clock = scm_async_rate;
227 }
228 else if (scm_tick_rate)
229 {
230 scm_async_clock = scm_async_rate = scm_tick_clock;
231 }
232 else if (scm_switch_rate)
233 {
234 scm_async_clock = scm_async_rate = scm_switch_clock;
235 }
236 else
237 scm_async_clock = scm_async_rate = 1 << 16;
238 SCM_ALLOW_INTS_ONLY;
239
240 tail:
241 scm_run_asyncs (scm_asyncs);
242
243 SCM_DEFER_INTS;
5e569ca8 244 if (scm_asyncs_pending ())
0f2d19dd
JB
245 {
246 SCM_ALLOW_INTS_ONLY;
247 goto tail;
248 }
249 SCM_ALLOW_INTS;
250
251 if (owe_switch)
252 scm_switch ();
253}
254
8d924bfe
MD
255void
256scm_switch ()
257{
258#if 0 /* Thread switching code should probably reside here, but the
259 async switching code doesn't seem to work, so it's put in the
260 SCM_DEFER_INTS macro instead. /mdj */
261 SCM_THREAD_SWITCHING_CODE;
262#endif
263}
264
9f0e55a6
MD
265#else
266
267void
268scm_async_click ()
269{
270 if (!scm_mask_ints)
271 do
272 scm_run_asyncs (scm_asyncs);
273 while (scm_asyncs_pending_p);
274}
0f2d19dd 275
9f0e55a6 276#endif
0f2d19dd 277
8d924bfe 278
0f2d19dd
JB
279\f
280
0f2d19dd 281static SCM
e841c3e0 282async_mark (SCM obj)
0f2d19dd 283{
e94e3f21 284 return ASYNC_THUNK (obj);
0f2d19dd
JB
285}
286
0f2d19dd
JB
287\f
288
843e4e9d 289SCM_DEFINE (scm_async, "async", 1, 0, 0,
1bbd0b84
GB
290 (SCM thunk),
291"")
292#define FUNC_NAME s_scm_async
0f2d19dd 293{
e94e3f21 294 SCM_RETURN_NEWSMOB (tc16_async, SCM_UNPACK (thunk));
0f2d19dd 295}
1bbd0b84 296#undef FUNC_NAME
0f2d19dd 297
843e4e9d 298SCM_DEFINE (scm_system_async, "system-async", 1, 0, 0,
1bbd0b84
GB
299 (SCM thunk),
300"")
301#define FUNC_NAME s_scm_system_async
0f2d19dd
JB
302{
303 SCM it;
304 SCM list;
305
306 it = scm_async (thunk);
273b7b94
MD
307 SCM_NEWCELL (list);
308 SCM_SETCAR (list, it);
309 SCM_SETCDR (list, scm_asyncs);
0f2d19dd 310 scm_asyncs = list;
0f2d19dd
JB
311 return it;
312}
1bbd0b84 313#undef FUNC_NAME
0f2d19dd 314
843e4e9d 315SCM_DEFINE (scm_async_mark, "async-mark", 1, 0, 0,
1bbd0b84
GB
316 (SCM a),
317"")
318#define FUNC_NAME s_scm_async_mark
0f2d19dd 319{
e94e3f21 320 VALIDATE_ASYNC (1, a);
9f0e55a6 321#ifdef GUILE_OLD_ASYNC_CLICK
e94e3f21 322 SET_ASYNC_GOT_IT (a, 1);
9f0e55a6 323#else
e94e3f21 324 SET_ASYNC_GOT_IT (a, scm_asyncs_pending_p = 1);
9f0e55a6 325#endif
0f2d19dd
JB
326 return SCM_UNSPECIFIED;
327}
1bbd0b84 328#undef FUNC_NAME
0f2d19dd
JB
329
330
843e4e9d 331SCM_DEFINE (scm_system_async_mark, "system-async-mark", 1, 0, 0,
1bbd0b84
GB
332 (SCM a),
333"")
334#define FUNC_NAME s_scm_system_async_mark
0f2d19dd 335{
e94e3f21 336 VALIDATE_ASYNC (1, a);
0f2d19dd 337 SCM_REDEFER_INTS;
9f0e55a6 338#ifdef GUILE_OLD_ASYNC_CLICK
e94e3f21 339 SET_ASYNC_GOT_IT (a, 1);
0f2d19dd
JB
340 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
341 scm_async_clock = 1;
9f0e55a6 342#else
e94e3f21 343 SET_ASYNC_GOT_IT (a, scm_asyncs_pending_p = 1);
9f0e55a6 344#endif
0f2d19dd
JB
345 SCM_REALLOW_INTS;
346 return SCM_UNSPECIFIED;
347}
1bbd0b84 348#undef FUNC_NAME
0f2d19dd
JB
349
350
843e4e9d 351SCM_DEFINE (scm_run_asyncs, "run-asyncs", 1, 0, 0,
1bbd0b84
GB
352 (SCM list_of_a),
353"")
354#define FUNC_NAME s_scm_run_asyncs
0f2d19dd 355{
9f0e55a6 356#ifdef GUILE_OLD_ASYNC_CLICK
0f2d19dd
JB
357 if (scm_mask_ints)
358 return SCM_BOOL_F;
6587a966 359#else
9f0e55a6 360 scm_asyncs_pending_p = 0;
6587a966 361#endif
843524cc 362 while (! SCM_NULLP (list_of_a))
0f2d19dd
JB
363 {
364 SCM a;
9f0e55a6 365 SCM_VALIDATE_CONS (1, list_of_a);
1bbd0b84 366 a = SCM_CAR (list_of_a);
e94e3f21 367 VALIDATE_ASYNC (SCM_ARG1, a);
0f2d19dd 368 scm_mask_ints = 1;
e94e3f21 369 if (ASYNC_GOT_IT (a))
0f2d19dd 370 {
e94e3f21
ML
371 SET_ASYNC_GOT_IT (a, 0);
372 scm_apply (ASYNC_THUNK (a), SCM_EOL, SCM_EOL);
0f2d19dd
JB
373 }
374 scm_mask_ints = 0;
1bbd0b84 375 list_of_a = SCM_CDR (list_of_a);
0f2d19dd
JB
376 }
377 return SCM_BOOL_T;
378}
1bbd0b84 379#undef FUNC_NAME
0f2d19dd
JB
380
381\f
382
383
843e4e9d 384SCM_DEFINE (scm_noop, "noop", 0, 0, 1,
1bbd0b84
GB
385 (SCM args),
386"")
387#define FUNC_NAME s_scm_noop
0f2d19dd 388{
af45e3b0 389 SCM_VALIDATE_REST_ARGUMENT (args);
5d2d2ffc 390 return (SCM_NULLP (args) ? SCM_BOOL_F : SCM_CAR (args));
0f2d19dd 391}
1bbd0b84 392#undef FUNC_NAME
0f2d19dd
JB
393
394
395\f
396
9f0e55a6
MD
397#ifdef GUILE_OLD_ASYNC_CLICK
398
843e4e9d 399SCM_DEFINE (scm_set_tick_rate, "set-tick-rate", 1, 0, 0,
1bbd0b84
GB
400 (SCM n),
401"")
402#define FUNC_NAME s_scm_set_tick_rate
0f2d19dd 403{
9b139b4e
DH
404 unsigned int old_n = scm_tick_rate;
405 SCM_VALIDATE_INUM (1, n);
406 scm_desired_tick_rate = SCM_INUM (n);
0f2d19dd
JB
407 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
408 scm_async_clock = 1;
409 return SCM_MAKINUM (old_n);
410}
1bbd0b84 411#undef FUNC_NAME
0f2d19dd
JB
412
413\f
414
415
843e4e9d 416SCM_DEFINE (scm_set_switch_rate, "set-switch-rate", 1, 0, 0,
1bbd0b84
GB
417 (SCM n),
418"")
419#define FUNC_NAME s_scm_set_switch_rate
0f2d19dd 420{
9b139b4e
DH
421 unsigned int old_n = scm_switch_rate;
422 SCM_VALIDATE_INUM (1, n);
423 scm_desired_switch_rate = SCM_INUM (n);
0f2d19dd
JB
424 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
425 scm_async_clock = 1;
426 return SCM_MAKINUM (old_n);
427}
1bbd0b84 428#undef FUNC_NAME
0f2d19dd 429
9f0e55a6 430#endif
0f2d19dd 431
0f2d19dd
JB
432\f
433
843e4e9d 434SCM_DEFINE (scm_unmask_signals, "unmask-signals", 0, 0, 0,
1bbd0b84
GB
435 (),
436"")
437#define FUNC_NAME s_scm_unmask_signals
0f2d19dd
JB
438{
439 scm_mask_ints = 0;
440 return SCM_UNSPECIFIED;
441}
1bbd0b84 442#undef FUNC_NAME
0f2d19dd
JB
443
444
843e4e9d 445SCM_DEFINE (scm_mask_signals, "mask-signals", 0, 0, 0,
1bbd0b84
GB
446 (),
447"")
448#define FUNC_NAME s_scm_mask_signals
0f2d19dd
JB
449{
450 scm_mask_ints = 1;
451 return SCM_UNSPECIFIED;
452}
1bbd0b84 453#undef FUNC_NAME
0f2d19dd
JB
454
455\f
456
0f2d19dd
JB
457void
458scm_init_async ()
0f2d19dd 459{
939794ce 460 scm_asyncs = SCM_EOL;
73ea78af 461 tc16_async = scm_make_smob_type ("async", 0);
e841c3e0 462 scm_set_smob_mark (tc16_async, async_mark);
73ea78af 463
8dc9439f 464#ifndef SCM_MAGIC_SNARFER
a0599745 465#include "libguile/async.x"
8dc9439f 466#endif
0f2d19dd 467}
89e00824
ML
468
469/*
470 Local Variables:
471 c-file-style: "gnu"
472 End:
473*/