*.[ch]: Replace GUILE_PROC w/ SCM_DEFINE.
[bpt/guile.git] / libguile / async.c
CommitLineData
7dc6e754 1/* Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
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.
7 *
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.
12 *
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"
52#include "smob.h"
53
1bbd0b84 54#include "scm_validate.h"
20e6290e 55#include "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.
73 *
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;
0f2d19dd
JB
96
97unsigned int scm_async_clock = 20;
98static unsigned int scm_async_rate = 20;
99unsigned int scm_mask_ints = 1;
100
101static unsigned int scm_tick_clock = 0;
102static unsigned int scm_tick_rate = 0;
103static unsigned int scm_desired_tick_rate = 0;
104static unsigned int scm_switch_clock = 0;
105static unsigned int scm_switch_rate = 0;
106static unsigned int scm_desired_switch_rate = 0;
107
0f2d19dd
JB
108static long scm_tc16_async;
109
0f2d19dd
JB
110\f
111
5e569ca8
MD
112int
113scm_asyncs_pending ()
0f2d19dd
JB
114{
115 SCM pos;
116 pos = scm_asyncs;
117 while (pos != SCM_EOL)
118 {
119 SCM a;
120 struct scm_async * it;
121 a = SCM_CAR (pos);
122 it = SCM_ASYNC (a);
123 if (it->got_it)
124 return 1;
125 pos = SCM_CDR (pos);
126 }
127 return 0;
128}
129
9ea54cc6 130#if 0
9ea54cc6 131static SCM
1bbd0b84 132scm_sys_tick_async_thunk (void)
9ea54cc6
GH
133{
134 scm_deliver_signal (SCM_TICK_SIGNAL);
135 return SCM_BOOL_F;
136}
137#endif
1cc91f1b 138
0f2d19dd
JB
139void
140scm_async_click ()
0f2d19dd
JB
141{
142 int owe_switch;
143 int owe_tick;
144
145 if (!scm_switch_rate)
146 {
147 owe_switch = 0;
148 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
149 scm_desired_switch_rate = 0;
150 }
151 else
152 {
153 owe_switch = (scm_async_rate >= scm_switch_clock);
154 if (owe_switch)
155 {
156 if (scm_desired_switch_rate)
157 {
158 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
159 scm_desired_switch_rate = 0;
160 }
161 else
162 scm_switch_clock = scm_switch_rate;
163 }
164 else
165 {
166 if (scm_desired_switch_rate)
167 {
168 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
169 scm_desired_switch_rate = 0;
170 }
171 else
172 scm_switch_clock -= scm_async_rate;
173 }
174 }
175
176 if (scm_mask_ints)
177 {
178 if (owe_switch)
179 scm_switch ();
180 scm_async_clock = 1;
181 return;;
182 }
183
184 if (!scm_tick_rate)
185 {
186 unsigned int r;
187 owe_tick = 0;
188 r = scm_desired_tick_rate;
189 if (r)
190 {
191 scm_desired_tick_rate = 0;
192 scm_tick_rate = r;
193 scm_tick_clock = r;
194 }
195 }
196 else
197 {
198 owe_tick = (scm_async_rate >= scm_tick_clock);
199 if (owe_tick)
200 {
201 scm_tick_clock = scm_tick_rate = scm_desired_tick_rate;
202 scm_desired_tick_rate = 0;
203 }
204 else
205 {
206 if (scm_desired_tick_rate)
207 {
208 scm_tick_clock = scm_tick_rate = scm_desired_tick_rate;
209 scm_desired_tick_rate = 0;
210 }
211 else
212 scm_tick_clock -= scm_async_rate;
213 }
214 }
215
9ea54cc6
GH
216 /*
217 if (owe_tick)
218 scm_async_mark (system_signal_asyncs[SCM_SIG_ORD(SCM_TICK_SIGNAL)]);
219 */
0f2d19dd
JB
220
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
254
255\f
256
1cc91f1b 257
0f2d19dd
JB
258void
259scm_switch ()
7ad737b6
MD
260{
261#if 0 /* Thread switching code should probably reside here, but the
262 async switching code doesn't seem to work, so it's put in the
263 SCM_ASYNC_TICK macro instead. /mdj */
264 SCM_THREAD_SWITCHING_CODE;
265#endif
266}
0f2d19dd 267
0f2d19dd
JB
268\f
269
0f2d19dd 270static SCM
1bbd0b84 271mark_async (SCM obj)
0f2d19dd
JB
272{
273 struct scm_async * it;
0f2d19dd
JB
274 it = SCM_ASYNC (obj);
275 return it->thunk;
276}
277
0f2d19dd
JB
278\f
279
a1ec6916 280SCM_DEFINE(scm_async, "async", 1, 0, 0,
1bbd0b84
GB
281 (SCM thunk),
282"")
283#define FUNC_NAME s_scm_async
0f2d19dd 284{
4801c311 285 struct scm_async * async
1bbd0b84 286 = (struct scm_async *) scm_must_malloc (sizeof (*async), FUNC_NAME);
0f2d19dd
JB
287 async->got_it = 0;
288 async->thunk = thunk;
23a62151 289 SCM_RETURN_NEWSMOB (scm_tc16_async, async);
0f2d19dd 290}
1bbd0b84 291#undef FUNC_NAME
0f2d19dd 292
a1ec6916 293SCM_DEFINE(scm_system_async, "system-async", 1, 0, 0,
1bbd0b84
GB
294 (SCM thunk),
295"")
296#define FUNC_NAME s_scm_system_async
0f2d19dd
JB
297{
298 SCM it;
299 SCM list;
300
301 it = scm_async (thunk);
23a62151 302 SCM_NEWSMOB (list, it, scm_asyncs);
0f2d19dd 303 scm_asyncs = list;
0f2d19dd
JB
304 return it;
305}
1bbd0b84 306#undef FUNC_NAME
0f2d19dd 307
a1ec6916 308SCM_DEFINE(scm_async_mark, "async-mark", 1, 0, 0,
1bbd0b84
GB
309 (SCM a),
310"")
311#define FUNC_NAME s_scm_async_mark
0f2d19dd
JB
312{
313 struct scm_async * it;
1bbd0b84 314 SCM_VALIDATE_ASYNC_COPY(1,a,it);
0f2d19dd
JB
315 it->got_it = 1;
316 return SCM_UNSPECIFIED;
317}
1bbd0b84 318#undef FUNC_NAME
0f2d19dd
JB
319
320
a1ec6916 321SCM_DEFINE(scm_system_async_mark, "system-async-mark", 1, 0, 0,
1bbd0b84
GB
322 (SCM a),
323"")
324#define FUNC_NAME s_scm_system_async_mark
0f2d19dd
JB
325{
326 struct scm_async * it;
1bbd0b84 327 SCM_VALIDATE_ASYNC_COPY(1,a,it);
0f2d19dd
JB
328 SCM_REDEFER_INTS;
329 it->got_it = 1;
330 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
331 scm_async_clock = 1;
332 SCM_REALLOW_INTS;
333 return SCM_UNSPECIFIED;
334}
1bbd0b84 335#undef FUNC_NAME
0f2d19dd
JB
336
337
a1ec6916 338SCM_DEFINE(scm_run_asyncs, "run-asyncs", 1, 0, 0,
1bbd0b84
GB
339 (SCM list_of_a),
340"")
341#define FUNC_NAME s_scm_run_asyncs
0f2d19dd 342{
0f2d19dd
JB
343 if (scm_mask_ints)
344 return SCM_BOOL_F;
1bbd0b84 345 while (list_of_a != SCM_EOL)
0f2d19dd
JB
346 {
347 SCM a;
348 struct scm_async * it;
30939477 349 SCM_VALIDATE_CONS(1,list_of_a);
1bbd0b84 350 a = SCM_CAR (list_of_a);
0c95b57d 351 SCM_ASSERT (SCM_ASYNCP (a), a, SCM_ARG1, FUNC_NAME);
0f2d19dd
JB
352 it = SCM_ASYNC (a);
353 scm_mask_ints = 1;
354 if (it->got_it)
355 {
356 it->got_it = 0;
357 scm_apply (it->thunk, SCM_EOL, SCM_EOL);
358 }
359 scm_mask_ints = 0;
1bbd0b84 360 list_of_a = SCM_CDR (list_of_a);
0f2d19dd
JB
361 }
362 return SCM_BOOL_T;
363}
1bbd0b84 364#undef FUNC_NAME
0f2d19dd
JB
365
366\f
367
368
a1ec6916 369SCM_DEFINE(scm_noop, "noop", 0, 0, 1,
1bbd0b84
GB
370 (SCM args),
371"")
372#define FUNC_NAME s_scm_noop
0f2d19dd
JB
373{
374 return (SCM_NULLP (args)
375 ? SCM_BOOL_F
376 : SCM_CAR (args));
377}
1bbd0b84 378#undef FUNC_NAME
0f2d19dd
JB
379
380
381\f
382
a1ec6916 383SCM_DEFINE(scm_set_tick_rate, "set-tick-rate", 1, 0, 0,
1bbd0b84
GB
384 (SCM n),
385"")
386#define FUNC_NAME s_scm_set_tick_rate
0f2d19dd
JB
387{
388 unsigned int old_n;
47c6b75e 389 SCM_VALIDATE_INUM(1,n);
0f2d19dd
JB
390 old_n = scm_tick_rate;
391 scm_desired_tick_rate = SCM_INUM (n);
392 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
393 scm_async_clock = 1;
394 return SCM_MAKINUM (old_n);
395}
1bbd0b84 396#undef FUNC_NAME
0f2d19dd
JB
397
398\f
399
400
a1ec6916 401SCM_DEFINE(scm_set_switch_rate, "set-switch-rate", 1, 0, 0,
1bbd0b84
GB
402 (SCM n),
403"")
404#define FUNC_NAME s_scm_set_switch_rate
0f2d19dd
JB
405{
406 unsigned int old_n;
47c6b75e 407 SCM_VALIDATE_INUM(1,n);
0f2d19dd
JB
408 old_n = scm_switch_rate;
409 scm_desired_switch_rate = SCM_INUM (n);
410 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
411 scm_async_clock = 1;
412 return SCM_MAKINUM (old_n);
413}
1bbd0b84 414#undef FUNC_NAME
0f2d19dd
JB
415
416\f
417
9ea54cc6
GH
418/* points to the GC system-async, so that scm_gc_end can find it. */
419SCM scm_gc_async;
1cc91f1b 420
9ea54cc6
GH
421/* the vcell for gc-thunk. */
422static SCM scm_gc_vcell;
1cc91f1b 423
9ea54cc6
GH
424/* the thunk installed in the GC system-async, which is marked at the
425 end of garbage collection. */
0f2d19dd 426static SCM
9ea54cc6 427scm_sys_gc_async_thunk (void)
0f2d19dd 428{
9ea54cc6
GH
429 if (SCM_NFALSEP (scm_gc_vcell))
430 {
431 SCM proc = SCM_CDR (scm_gc_vcell);
1cc91f1b 432
9ea54cc6
GH
433 if (SCM_NFALSEP (proc) && !SCM_UNBNDP (proc))
434 scm_apply (proc, SCM_EOL, SCM_EOL);
435 }
436 return SCM_UNSPECIFIED;
0f2d19dd
JB
437}
438
0f2d19dd
JB
439\f
440
a1ec6916 441SCM_DEFINE(scm_unmask_signals, "unmask-signals", 0, 0, 0,
1bbd0b84
GB
442 (),
443"")
444#define FUNC_NAME s_scm_unmask_signals
0f2d19dd
JB
445{
446 scm_mask_ints = 0;
447 return SCM_UNSPECIFIED;
448}
1bbd0b84 449#undef FUNC_NAME
0f2d19dd
JB
450
451
a1ec6916 452SCM_DEFINE(scm_mask_signals, "mask-signals", 0, 0, 0,
1bbd0b84
GB
453 (),
454"")
455#define FUNC_NAME s_scm_mask_signals
0f2d19dd
JB
456{
457 scm_mask_ints = 1;
458 return SCM_UNSPECIFIED;
459}
1bbd0b84 460#undef FUNC_NAME
0f2d19dd
JB
461
462\f
463
0f2d19dd
JB
464void
465scm_init_async ()
0f2d19dd
JB
466{
467 SCM a_thunk;
23a62151
MD
468 scm_tc16_async = scm_make_smob_type_mfpe ("async", sizeof (struct scm_async),
469 mark_async, NULL, NULL, NULL);
9ea54cc6 470 scm_gc_vcell = scm_sysintern ("gc-thunk", SCM_BOOL_F);
0f2d19dd 471 a_thunk = scm_make_gsubr ("%gc-thunk", 0, 0, 0, scm_sys_gc_async_thunk);
9ea54cc6 472 scm_gc_async = scm_system_async (a_thunk);
0f2d19dd 473
0f2d19dd
JB
474#include "async.x"
475}