Remove #include <stdio.h>. Add #include <string.h>.
[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
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
e841c3e0 112static scm_bits_t 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;
133 while (pos != SCM_EOL)
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
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 316 (SCM a),
811cf846 317 "Mark the async @var{a} for future execution.")
1bbd0b84 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 332 (SCM a),
811cf846 333 "Mark the async @var{a} for future execution.")
1bbd0b84 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,
811cf846
MG
352 (SCM list_of_a),
353 "Execute all thunks from the asyncs of the list @var{list_of_a}.")
1bbd0b84 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,
811cf846
MG
385 (SCM args),
386 "Do nothing. When called without arguments, return @code{#f},\n"
387 "otherwise return the first argument.")
1bbd0b84 388#define FUNC_NAME s_scm_noop
0f2d19dd 389{
af45e3b0 390 SCM_VALIDATE_REST_ARGUMENT (args);
5d2d2ffc 391 return (SCM_NULLP (args) ? SCM_BOOL_F : SCM_CAR (args));
0f2d19dd 392}
1bbd0b84 393#undef FUNC_NAME
0f2d19dd
JB
394
395
396\f
397
9f0e55a6
MD
398#ifdef GUILE_OLD_ASYNC_CLICK
399
843e4e9d 400SCM_DEFINE (scm_set_tick_rate, "set-tick-rate", 1, 0, 0,
811cf846
MG
401 (SCM n),
402 "Set the rate of async ticks to @var{n}. Return the old rate\n"
403 "value.")
1bbd0b84 404#define FUNC_NAME s_scm_set_tick_rate
0f2d19dd 405{
9b139b4e
DH
406 unsigned int old_n = scm_tick_rate;
407 SCM_VALIDATE_INUM (1, n);
408 scm_desired_tick_rate = SCM_INUM (n);
0f2d19dd
JB
409 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
410 scm_async_clock = 1;
411 return SCM_MAKINUM (old_n);
412}
1bbd0b84 413#undef FUNC_NAME
0f2d19dd
JB
414
415\f
416
417
843e4e9d 418SCM_DEFINE (scm_set_switch_rate, "set-switch-rate", 1, 0, 0,
811cf846
MG
419 (SCM n),
420 "Set the async switch rate to @var{n}. Return the old value\n"
421 "of the switch rate.")
1bbd0b84 422#define FUNC_NAME s_scm_set_switch_rate
0f2d19dd 423{
9b139b4e
DH
424 unsigned int old_n = scm_switch_rate;
425 SCM_VALIDATE_INUM (1, n);
426 scm_desired_switch_rate = SCM_INUM (n);
0f2d19dd
JB
427 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
428 scm_async_clock = 1;
429 return SCM_MAKINUM (old_n);
430}
1bbd0b84 431#undef FUNC_NAME
0f2d19dd 432
9f0e55a6 433#endif
0f2d19dd 434
0f2d19dd
JB
435\f
436
843e4e9d 437SCM_DEFINE (scm_unmask_signals, "unmask-signals", 0, 0, 0,
811cf846
MG
438 (),
439 "Unmask signals. The returned value is not specified.")
1bbd0b84 440#define FUNC_NAME s_scm_unmask_signals
0f2d19dd
JB
441{
442 scm_mask_ints = 0;
443 return SCM_UNSPECIFIED;
444}
1bbd0b84 445#undef FUNC_NAME
0f2d19dd
JB
446
447
843e4e9d 448SCM_DEFINE (scm_mask_signals, "mask-signals", 0, 0, 0,
811cf846
MG
449 (),
450 "Mask signals. The returned value is not specified.")
1bbd0b84 451#define FUNC_NAME s_scm_mask_signals
0f2d19dd
JB
452{
453 scm_mask_ints = 1;
454 return SCM_UNSPECIFIED;
455}
1bbd0b84 456#undef FUNC_NAME
0f2d19dd
JB
457
458\f
459
0f2d19dd
JB
460void
461scm_init_async ()
0f2d19dd 462{
939794ce 463 scm_asyncs = SCM_EOL;
73ea78af 464 tc16_async = scm_make_smob_type ("async", 0);
e841c3e0 465 scm_set_smob_mark (tc16_async, async_mark);
73ea78af 466
8dc9439f 467#ifndef SCM_MAGIC_SNARFER
a0599745 468#include "libguile/async.x"
8dc9439f 469#endif
0f2d19dd 470}
89e00824
ML
471
472/*
473 Local Variables:
474 c-file-style: "gnu"
475 End:
476*/