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