* eval.c (scm_call_0, scm_call_1, scm_call_2, scm_call_3,
[bpt/guile.git] / libguile / async.c
CommitLineData
22a52da1 1/* Copyright (C) 1995,1996,1997,1998,2000,2001 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
0f2d19dd 47#include <signal.h>
a0599745
MD
48#include "libguile/_scm.h"
49#include "libguile/eval.h"
50#include "libguile/throw.h"
51#include "libguile/root.h"
52#include "libguile/smob.h"
20e6290e 53
a0599745
MD
54#include "libguile/validate.h"
55#include "libguile/async.h"
0f2d19dd 56
95b88819
GH
57#ifdef HAVE_STRING_H
58#include <string.h>
59#endif
0f2d19dd
JB
60#ifdef HAVE_UNISTD_H
61#include <unistd.h>
62#endif
63
64
65\f
66/* {Asynchronous Events}
67 *
68 *
69 * Async == thunk + mark.
70 *
71 * Setting the mark guarantees future execution of the thunk. More
72 * than one set may be satisfied by a single execution.
843e4e9d 73 *
0f2d19dd
JB
74 * scm_tick_clock decremented once per SCM_ALLOW_INTS.
75 * Async execution triggered by SCM_ALLOW_INTS when scm_tick_clock drops to 0.
76 * Async execution prevented by scm_mask_ints != 0.
77 *
78 * If the clock reaches 0 when scm_mask_ints != 0, then reset the clock
79 * to 1.
80 *
81 * If the clock reaches 0 any other time, run marked asyncs.
82 *
83 * From a unix signal handler, mark a corresponding async and set the clock
84 * to 1. Do SCM_REDEFER_INTS;/SCM_REALLOW_INTS so that if the signal handler is not
85 * called in the dynamic scope of a critical section, it is excecuted immediately.
86 *
87 * Overall, closely timed signals of a particular sort may be combined. Pending signals
88 * are delivered in a fixed priority order, regardless of arrival order.
89 *
90 */
91
a574455a
GH
92/* True between SCM_DEFER_INTS and SCM_ALLOW_INTS, and
93 * when the interpreter is not running at all.
94 */
95int scm_ints_disabled = 1;
9f0e55a6 96unsigned int scm_mask_ints = 1;
0f2d19dd 97
9f0e55a6 98#ifdef GUILE_OLD_ASYNC_CLICK
0f2d19dd
JB
99unsigned int scm_async_clock = 20;
100static unsigned int scm_async_rate = 20;
0f2d19dd
JB
101
102static unsigned int scm_tick_clock = 0;
103static unsigned int scm_tick_rate = 0;
104static unsigned int scm_desired_tick_rate = 0;
105static unsigned int scm_switch_clock = 0;
106static unsigned int scm_switch_rate = 0;
107static unsigned int scm_desired_switch_rate = 0;
9f0e55a6
MD
108#else
109int scm_asyncs_pending_p = 0;
110#endif
0f2d19dd 111
92c2555f 112static scm_t_bits tc16_async;
e94e3f21
ML
113
114\f
115
116/* cmm: this has SCM_ prefix because SCM_MAKE_VALIDATE expects it.
117 this is ugly. */
e841c3e0
KN
118#define SCM_ASYNCP(X) SCM_TYP16_PREDICATE (tc16_async, X)
119#define VALIDATE_ASYNC(pos,a) SCM_MAKE_VALIDATE(pos, a, ASYNCP)
e94e3f21
ML
120
121#define ASYNC_GOT_IT(X) (SCM_CELL_WORD_0 (X) >> 16)
d1ca2c64 122#define SET_ASYNC_GOT_IT(X, V) (SCM_SET_CELL_WORD_0 ((X), SCM_TYP16 (X) | ((V) << 16)))
e94e3f21 123#define ASYNC_THUNK(X) SCM_CELL_OBJECT_1 (X)
0f2d19dd 124
0f2d19dd
JB
125\f
126
9f0e55a6 127#ifdef GUILE_OLD_ASYNC_CLICK
5e569ca8
MD
128int
129scm_asyncs_pending ()
0f2d19dd
JB
130{
131 SCM pos;
132 pos = scm_asyncs;
22a52da1 133 while (!SCM_NULLP (pos))
0f2d19dd 134 {
843e4e9d 135 SCM a = SCM_CAR (pos);
e94e3f21 136 if (ASYNC_GOT_IT (a))
0f2d19dd
JB
137 return 1;
138 pos = SCM_CDR (pos);
139 }
140 return 0;
141}
142
1cc91f1b 143
0f2d19dd
JB
144void
145scm_async_click ()
0f2d19dd
JB
146{
147 int owe_switch;
148 int owe_tick;
149
150 if (!scm_switch_rate)
151 {
152 owe_switch = 0;
153 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
154 scm_desired_switch_rate = 0;
155 }
156 else
157 {
158 owe_switch = (scm_async_rate >= scm_switch_clock);
159 if (owe_switch)
160 {
161 if (scm_desired_switch_rate)
162 {
163 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
164 scm_desired_switch_rate = 0;
165 }
166 else
167 scm_switch_clock = scm_switch_rate;
168 }
169 else
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_async_rate;
178 }
179 }
180
181 if (scm_mask_ints)
182 {
183 if (owe_switch)
184 scm_switch ();
185 scm_async_clock = 1;
186 return;;
187 }
843e4e9d 188
0f2d19dd
JB
189 if (!scm_tick_rate)
190 {
191 unsigned int r;
192 owe_tick = 0;
193 r = scm_desired_tick_rate;
194 if (r)
195 {
196 scm_desired_tick_rate = 0;
197 scm_tick_rate = r;
198 scm_tick_clock = r;
199 }
200 }
201 else
202 {
203 owe_tick = (scm_async_rate >= scm_tick_clock);
204 if (owe_tick)
205 {
206 scm_tick_clock = scm_tick_rate = scm_desired_tick_rate;
207 scm_desired_tick_rate = 0;
208 }
209 else
210 {
211 if (scm_desired_tick_rate)
212 {
213 scm_tick_clock = scm_tick_rate = scm_desired_tick_rate;
214 scm_desired_tick_rate = 0;
215 }
216 else
217 scm_tick_clock -= scm_async_rate;
218 }
219 }
220
0f2d19dd
JB
221 SCM_DEFER_INTS;
222 if (scm_tick_rate && scm_switch_rate)
223 {
224 scm_async_rate = min (scm_tick_clock, scm_switch_clock);
225 scm_async_clock = scm_async_rate;
226 }
227 else if (scm_tick_rate)
228 {
229 scm_async_clock = scm_async_rate = scm_tick_clock;
230 }
231 else if (scm_switch_rate)
232 {
233 scm_async_clock = scm_async_rate = scm_switch_clock;
234 }
235 else
236 scm_async_clock = scm_async_rate = 1 << 16;
237 SCM_ALLOW_INTS_ONLY;
238
239 tail:
240 scm_run_asyncs (scm_asyncs);
241
242 SCM_DEFER_INTS;
5e569ca8 243 if (scm_asyncs_pending ())
0f2d19dd
JB
244 {
245 SCM_ALLOW_INTS_ONLY;
246 goto tail;
247 }
248 SCM_ALLOW_INTS;
249
250 if (owe_switch)
251 scm_switch ();
252}
253
8d924bfe
MD
254void
255scm_switch ()
256{
257#if 0 /* Thread switching code should probably reside here, but the
258 async switching code doesn't seem to work, so it's put in the
259 SCM_DEFER_INTS macro instead. /mdj */
260 SCM_THREAD_SWITCHING_CODE;
261#endif
262}
263
9f0e55a6
MD
264#else
265
266void
267scm_async_click ()
268{
269 if (!scm_mask_ints)
270 do
271 scm_run_asyncs (scm_asyncs);
272 while (scm_asyncs_pending_p);
273}
0f2d19dd 274
9f0e55a6 275#endif
0f2d19dd 276
8d924bfe 277
0f2d19dd
JB
278\f
279
0f2d19dd 280static SCM
e841c3e0 281async_mark (SCM obj)
0f2d19dd 282{
e94e3f21 283 return ASYNC_THUNK (obj);
0f2d19dd
JB
284}
285
0f2d19dd
JB
286\f
287
843e4e9d 288SCM_DEFINE (scm_async, "async", 1, 0, 0,
811cf846
MG
289 (SCM thunk),
290 "Create a new async for the procedure @var{thunk}.")
1bbd0b84 291#define FUNC_NAME s_scm_async
0f2d19dd 292{
e94e3f21 293 SCM_RETURN_NEWSMOB (tc16_async, SCM_UNPACK (thunk));
0f2d19dd 294}
1bbd0b84 295#undef FUNC_NAME
0f2d19dd 296
843e4e9d 297SCM_DEFINE (scm_system_async, "system-async", 1, 0, 0,
1bbd0b84 298 (SCM thunk),
811cf846
MG
299 "Create a new async for the procedure @var{thunk}. Also\n"
300 "add it to the system's list of active async objects.")
1bbd0b84 301#define FUNC_NAME s_scm_system_async
0f2d19dd 302{
22a52da1
DH
303 SCM it = scm_async (thunk);
304 scm_asyncs = scm_cons (it, scm_asyncs);
0f2d19dd
JB
305 return it;
306}
1bbd0b84 307#undef FUNC_NAME
0f2d19dd 308
843e4e9d 309SCM_DEFINE (scm_async_mark, "async-mark", 1, 0, 0,
1bbd0b84 310 (SCM a),
811cf846 311 "Mark the async @var{a} for future execution.")
1bbd0b84 312#define FUNC_NAME s_scm_async_mark
0f2d19dd 313{
e94e3f21 314 VALIDATE_ASYNC (1, a);
9f0e55a6 315#ifdef GUILE_OLD_ASYNC_CLICK
e94e3f21 316 SET_ASYNC_GOT_IT (a, 1);
9f0e55a6 317#else
e94e3f21 318 SET_ASYNC_GOT_IT (a, scm_asyncs_pending_p = 1);
9f0e55a6 319#endif
0f2d19dd
JB
320 return SCM_UNSPECIFIED;
321}
1bbd0b84 322#undef FUNC_NAME
0f2d19dd
JB
323
324
843e4e9d 325SCM_DEFINE (scm_system_async_mark, "system-async-mark", 1, 0, 0,
1bbd0b84 326 (SCM a),
811cf846 327 "Mark the async @var{a} for future execution.")
1bbd0b84 328#define FUNC_NAME s_scm_system_async_mark
0f2d19dd 329{
e94e3f21 330 VALIDATE_ASYNC (1, a);
0f2d19dd 331 SCM_REDEFER_INTS;
9f0e55a6 332#ifdef GUILE_OLD_ASYNC_CLICK
e94e3f21 333 SET_ASYNC_GOT_IT (a, 1);
0f2d19dd
JB
334 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
335 scm_async_clock = 1;
9f0e55a6 336#else
e94e3f21 337 SET_ASYNC_GOT_IT (a, scm_asyncs_pending_p = 1);
9f0e55a6 338#endif
0f2d19dd
JB
339 SCM_REALLOW_INTS;
340 return SCM_UNSPECIFIED;
341}
1bbd0b84 342#undef FUNC_NAME
0f2d19dd
JB
343
344
843e4e9d 345SCM_DEFINE (scm_run_asyncs, "run-asyncs", 1, 0, 0,
811cf846
MG
346 (SCM list_of_a),
347 "Execute all thunks from the asyncs of the list @var{list_of_a}.")
1bbd0b84 348#define FUNC_NAME s_scm_run_asyncs
0f2d19dd 349{
9f0e55a6 350#ifdef GUILE_OLD_ASYNC_CLICK
0f2d19dd
JB
351 if (scm_mask_ints)
352 return SCM_BOOL_F;
6587a966 353#else
9f0e55a6 354 scm_asyncs_pending_p = 0;
6587a966 355#endif
843524cc 356 while (! SCM_NULLP (list_of_a))
0f2d19dd
JB
357 {
358 SCM a;
9f0e55a6 359 SCM_VALIDATE_CONS (1, list_of_a);
1bbd0b84 360 a = SCM_CAR (list_of_a);
e94e3f21 361 VALIDATE_ASYNC (SCM_ARG1, a);
0f2d19dd 362 scm_mask_ints = 1;
e94e3f21 363 if (ASYNC_GOT_IT (a))
0f2d19dd 364 {
e94e3f21 365 SET_ASYNC_GOT_IT (a, 0);
fdc28395 366 scm_call_0 (ASYNC_THUNK (a));
0f2d19dd
JB
367 }
368 scm_mask_ints = 0;
1bbd0b84 369 list_of_a = SCM_CDR (list_of_a);
0f2d19dd
JB
370 }
371 return SCM_BOOL_T;
372}
1bbd0b84 373#undef FUNC_NAME
0f2d19dd
JB
374
375\f
376
377
843e4e9d 378SCM_DEFINE (scm_noop, "noop", 0, 0, 1,
811cf846
MG
379 (SCM args),
380 "Do nothing. When called without arguments, return @code{#f},\n"
381 "otherwise return the first argument.")
1bbd0b84 382#define FUNC_NAME s_scm_noop
0f2d19dd 383{
af45e3b0 384 SCM_VALIDATE_REST_ARGUMENT (args);
5d2d2ffc 385 return (SCM_NULLP (args) ? SCM_BOOL_F : SCM_CAR (args));
0f2d19dd 386}
1bbd0b84 387#undef FUNC_NAME
0f2d19dd
JB
388
389
390\f
391
9f0e55a6
MD
392#ifdef GUILE_OLD_ASYNC_CLICK
393
843e4e9d 394SCM_DEFINE (scm_set_tick_rate, "set-tick-rate", 1, 0, 0,
811cf846
MG
395 (SCM n),
396 "Set the rate of async ticks to @var{n}. Return the old rate\n"
397 "value.")
1bbd0b84 398#define FUNC_NAME s_scm_set_tick_rate
0f2d19dd 399{
9b139b4e
DH
400 unsigned int old_n = scm_tick_rate;
401 SCM_VALIDATE_INUM (1, n);
402 scm_desired_tick_rate = SCM_INUM (n);
0f2d19dd
JB
403 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
404 scm_async_clock = 1;
405 return SCM_MAKINUM (old_n);
406}
1bbd0b84 407#undef FUNC_NAME
0f2d19dd
JB
408
409\f
410
411
843e4e9d 412SCM_DEFINE (scm_set_switch_rate, "set-switch-rate", 1, 0, 0,
811cf846
MG
413 (SCM n),
414 "Set the async switch rate to @var{n}. Return the old value\n"
415 "of the switch rate.")
1bbd0b84 416#define FUNC_NAME s_scm_set_switch_rate
0f2d19dd 417{
9b139b4e
DH
418 unsigned int old_n = scm_switch_rate;
419 SCM_VALIDATE_INUM (1, n);
420 scm_desired_switch_rate = SCM_INUM (n);
0f2d19dd
JB
421 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
422 scm_async_clock = 1;
423 return SCM_MAKINUM (old_n);
424}
1bbd0b84 425#undef FUNC_NAME
0f2d19dd 426
9f0e55a6 427#endif
0f2d19dd 428
0f2d19dd
JB
429\f
430
843e4e9d 431SCM_DEFINE (scm_unmask_signals, "unmask-signals", 0, 0, 0,
811cf846
MG
432 (),
433 "Unmask signals. The returned value is not specified.")
1bbd0b84 434#define FUNC_NAME s_scm_unmask_signals
0f2d19dd
JB
435{
436 scm_mask_ints = 0;
437 return SCM_UNSPECIFIED;
438}
1bbd0b84 439#undef FUNC_NAME
0f2d19dd
JB
440
441
843e4e9d 442SCM_DEFINE (scm_mask_signals, "mask-signals", 0, 0, 0,
811cf846
MG
443 (),
444 "Mask signals. The returned value is not specified.")
1bbd0b84 445#define FUNC_NAME s_scm_mask_signals
0f2d19dd
JB
446{
447 scm_mask_ints = 1;
448 return SCM_UNSPECIFIED;
449}
1bbd0b84 450#undef FUNC_NAME
0f2d19dd
JB
451
452\f
453
0f2d19dd
JB
454void
455scm_init_async ()
0f2d19dd 456{
939794ce 457 scm_asyncs = SCM_EOL;
73ea78af 458 tc16_async = scm_make_smob_type ("async", 0);
e841c3e0 459 scm_set_smob_mark (tc16_async, async_mark);
73ea78af 460
8dc9439f 461#ifndef SCM_MAGIC_SNARFER
a0599745 462#include "libguile/async.x"
8dc9439f 463#endif
0f2d19dd 464}
89e00824
ML
465
466/*
467 Local Variables:
468 c-file-style: "gnu"
469 End:
470*/