* Use scm_tc3_* codes instead of hardcoded values.
[bpt/guile.git] / libguile / async.c
1 /* Copyright (C) 1995, 96, 97, 98, 2000 Free Software Foundation, Inc.
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
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
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.
40 * If you do not wish that, delete this exception notice. */
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
45 \f
46
47 #include <stdio.h>
48 #include <signal.h>
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"
54
55 #include "libguile/validate.h"
56 #include "libguile/async.h"
57
58 #ifdef HAVE_STRING_H
59 #include <string.h>
60 #endif
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.
74 *
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
93 /* True between SCM_DEFER_INTS and SCM_ALLOW_INTS, and
94 * when the interpreter is not running at all.
95 */
96 int scm_ints_disabled = 1;
97 unsigned int scm_mask_ints = 1;
98
99 #ifdef GUILE_OLD_ASYNC_CLICK
100 unsigned int scm_async_clock = 20;
101 static unsigned int scm_async_rate = 20;
102
103 static unsigned int scm_tick_clock = 0;
104 static unsigned int scm_tick_rate = 0;
105 static unsigned int scm_desired_tick_rate = 0;
106 static unsigned int scm_switch_clock = 0;
107 static unsigned int scm_switch_rate = 0;
108 static unsigned int scm_desired_switch_rate = 0;
109 #else
110 int scm_asyncs_pending_p = 0;
111 #endif
112
113 static long tc16_async;
114
115 \f
116
117 /* cmm: this has SCM_ prefix because SCM_MAKE_VALIDATE expects it.
118 this is ugly. */
119 #define SCM_ASYNCP(X) (SCM_NIMP(X) && (tc16_async == SCM_GCTYP16 (X)))
120
121 #define VALIDATE_ASYNC(pos,a) SCM_MAKE_VALIDATE(pos, a, ASYNCP)
122
123 #define ASYNC_GOT_IT(X) (SCM_CELL_WORD_0 (X) >> 16)
124 #define SET_ASYNC_GOT_IT(X, V) (SCM_SET_CELL_WORD_0 ((X), SCM_TYP16 (X) | ((V) << 16)))
125 #define ASYNC_THUNK(X) SCM_CELL_OBJECT_1 (X)
126
127 \f
128
129 #ifdef GUILE_OLD_ASYNC_CLICK
130 int
131 scm_asyncs_pending ()
132 {
133 SCM pos;
134 pos = scm_asyncs;
135 while (pos != SCM_EOL)
136 {
137 SCM a = SCM_CAR (pos);
138 if (ASYNC_GOT_IT (a))
139 return 1;
140 pos = SCM_CDR (pos);
141 }
142 return 0;
143 }
144
145
146 void
147 scm_async_click ()
148 {
149 int owe_switch;
150 int owe_tick;
151
152 if (!scm_switch_rate)
153 {
154 owe_switch = 0;
155 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
156 scm_desired_switch_rate = 0;
157 }
158 else
159 {
160 owe_switch = (scm_async_rate >= scm_switch_clock);
161 if (owe_switch)
162 {
163 if (scm_desired_switch_rate)
164 {
165 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
166 scm_desired_switch_rate = 0;
167 }
168 else
169 scm_switch_clock = scm_switch_rate;
170 }
171 else
172 {
173 if (scm_desired_switch_rate)
174 {
175 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
176 scm_desired_switch_rate = 0;
177 }
178 else
179 scm_switch_clock -= scm_async_rate;
180 }
181 }
182
183 if (scm_mask_ints)
184 {
185 if (owe_switch)
186 scm_switch ();
187 scm_async_clock = 1;
188 return;;
189 }
190
191 if (!scm_tick_rate)
192 {
193 unsigned int r;
194 owe_tick = 0;
195 r = scm_desired_tick_rate;
196 if (r)
197 {
198 scm_desired_tick_rate = 0;
199 scm_tick_rate = r;
200 scm_tick_clock = r;
201 }
202 }
203 else
204 {
205 owe_tick = (scm_async_rate >= scm_tick_clock);
206 if (owe_tick)
207 {
208 scm_tick_clock = scm_tick_rate = scm_desired_tick_rate;
209 scm_desired_tick_rate = 0;
210 }
211 else
212 {
213 if (scm_desired_tick_rate)
214 {
215 scm_tick_clock = scm_tick_rate = scm_desired_tick_rate;
216 scm_desired_tick_rate = 0;
217 }
218 else
219 scm_tick_clock -= scm_async_rate;
220 }
221 }
222
223 SCM_DEFER_INTS;
224 if (scm_tick_rate && scm_switch_rate)
225 {
226 scm_async_rate = min (scm_tick_clock, scm_switch_clock);
227 scm_async_clock = scm_async_rate;
228 }
229 else if (scm_tick_rate)
230 {
231 scm_async_clock = scm_async_rate = scm_tick_clock;
232 }
233 else if (scm_switch_rate)
234 {
235 scm_async_clock = scm_async_rate = scm_switch_clock;
236 }
237 else
238 scm_async_clock = scm_async_rate = 1 << 16;
239 SCM_ALLOW_INTS_ONLY;
240
241 tail:
242 scm_run_asyncs (scm_asyncs);
243
244 SCM_DEFER_INTS;
245 if (scm_asyncs_pending ())
246 {
247 SCM_ALLOW_INTS_ONLY;
248 goto tail;
249 }
250 SCM_ALLOW_INTS;
251
252 if (owe_switch)
253 scm_switch ();
254 }
255
256 void
257 scm_switch ()
258 {
259 #if 0 /* Thread switching code should probably reside here, but the
260 async switching code doesn't seem to work, so it's put in the
261 SCM_DEFER_INTS macro instead. /mdj */
262 SCM_THREAD_SWITCHING_CODE;
263 #endif
264 }
265
266 #else
267
268 void
269 scm_async_click ()
270 {
271 if (!scm_mask_ints)
272 do
273 scm_run_asyncs (scm_asyncs);
274 while (scm_asyncs_pending_p);
275 }
276
277 #endif
278
279
280 \f
281
282 static SCM
283 mark_async (SCM obj)
284 {
285 return ASYNC_THUNK (obj);
286 }
287
288 \f
289
290 SCM_DEFINE (scm_async, "async", 1, 0, 0,
291 (SCM thunk),
292 "")
293 #define FUNC_NAME s_scm_async
294 {
295 SCM_RETURN_NEWSMOB (tc16_async, SCM_UNPACK (thunk));
296 }
297 #undef FUNC_NAME
298
299 SCM_DEFINE (scm_system_async, "system-async", 1, 0, 0,
300 (SCM thunk),
301 "")
302 #define FUNC_NAME s_scm_system_async
303 {
304 SCM it;
305 SCM list;
306
307 it = scm_async (thunk);
308 SCM_NEWCELL (list);
309 SCM_SETCAR (list, it);
310 SCM_SETCDR (list, scm_asyncs);
311 scm_asyncs = list;
312 return it;
313 }
314 #undef FUNC_NAME
315
316 SCM_DEFINE (scm_async_mark, "async-mark", 1, 0, 0,
317 (SCM a),
318 "")
319 #define FUNC_NAME s_scm_async_mark
320 {
321 VALIDATE_ASYNC (1, a);
322 #ifdef GUILE_OLD_ASYNC_CLICK
323 SET_ASYNC_GOT_IT (a, 1);
324 #else
325 SET_ASYNC_GOT_IT (a, scm_asyncs_pending_p = 1);
326 #endif
327 return SCM_UNSPECIFIED;
328 }
329 #undef FUNC_NAME
330
331
332 SCM_DEFINE (scm_system_async_mark, "system-async-mark", 1, 0, 0,
333 (SCM a),
334 "")
335 #define FUNC_NAME s_scm_system_async_mark
336 {
337 VALIDATE_ASYNC (1, a);
338 SCM_REDEFER_INTS;
339 #ifdef GUILE_OLD_ASYNC_CLICK
340 SET_ASYNC_GOT_IT (a, 1);
341 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
342 scm_async_clock = 1;
343 #else
344 SET_ASYNC_GOT_IT (a, scm_asyncs_pending_p = 1);
345 #endif
346 SCM_REALLOW_INTS;
347 return SCM_UNSPECIFIED;
348 }
349 #undef FUNC_NAME
350
351
352 SCM_DEFINE (scm_run_asyncs, "run-asyncs", 1, 0, 0,
353 (SCM list_of_a),
354 "")
355 #define FUNC_NAME s_scm_run_asyncs
356 {
357 #ifdef GUILE_OLD_ASYNC_CLICK
358 if (scm_mask_ints)
359 return SCM_BOOL_F;
360 #else
361 scm_asyncs_pending_p = 0;
362 #endif
363 while (! SCM_NULLP (list_of_a))
364 {
365 SCM a;
366 SCM_VALIDATE_CONS (1, list_of_a);
367 a = SCM_CAR (list_of_a);
368 VALIDATE_ASYNC (SCM_ARG1, a);
369 scm_mask_ints = 1;
370 if (ASYNC_GOT_IT (a))
371 {
372 SET_ASYNC_GOT_IT (a, 0);
373 scm_apply (ASYNC_THUNK (a), SCM_EOL, SCM_EOL);
374 }
375 scm_mask_ints = 0;
376 list_of_a = SCM_CDR (list_of_a);
377 }
378 return SCM_BOOL_T;
379 }
380 #undef FUNC_NAME
381
382 \f
383
384
385 SCM_DEFINE (scm_noop, "noop", 0, 0, 1,
386 (SCM args),
387 "")
388 #define FUNC_NAME s_scm_noop
389 {
390 SCM_VALIDATE_REST_ARGUMENT (args);
391 return (SCM_NULLP (args) ? SCM_BOOL_F : SCM_CAR (args));
392 }
393 #undef FUNC_NAME
394
395
396 \f
397
398 #ifdef GUILE_OLD_ASYNC_CLICK
399
400 SCM_DEFINE (scm_set_tick_rate, "set-tick-rate", 1, 0, 0,
401 (SCM n),
402 "")
403 #define FUNC_NAME s_scm_set_tick_rate
404 {
405 unsigned int old_n = scm_tick_rate;
406 SCM_VALIDATE_INUM (1, n);
407 scm_desired_tick_rate = SCM_INUM (n);
408 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
409 scm_async_clock = 1;
410 return SCM_MAKINUM (old_n);
411 }
412 #undef FUNC_NAME
413
414 \f
415
416
417 SCM_DEFINE (scm_set_switch_rate, "set-switch-rate", 1, 0, 0,
418 (SCM n),
419 "")
420 #define FUNC_NAME s_scm_set_switch_rate
421 {
422 unsigned int old_n = scm_switch_rate;
423 SCM_VALIDATE_INUM (1, n);
424 scm_desired_switch_rate = SCM_INUM (n);
425 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
426 scm_async_clock = 1;
427 return SCM_MAKINUM (old_n);
428 }
429 #undef FUNC_NAME
430
431 #endif
432
433 \f
434
435 SCM_DEFINE (scm_unmask_signals, "unmask-signals", 0, 0, 0,
436 (),
437 "")
438 #define FUNC_NAME s_scm_unmask_signals
439 {
440 scm_mask_ints = 0;
441 return SCM_UNSPECIFIED;
442 }
443 #undef FUNC_NAME
444
445
446 SCM_DEFINE (scm_mask_signals, "mask-signals", 0, 0, 0,
447 (),
448 "")
449 #define FUNC_NAME s_scm_mask_signals
450 {
451 scm_mask_ints = 1;
452 return SCM_UNSPECIFIED;
453 }
454 #undef FUNC_NAME
455
456 \f
457
458 void
459 scm_init_async ()
460 {
461 scm_asyncs = SCM_EOL;
462 tc16_async = scm_make_smob_type ("async", 0);
463 scm_set_smob_mark (tc16_async, mark_async);
464
465 #ifndef SCM_MAGIC_SNARFER
466 #include "libguile/async.x"
467 #endif
468 }
469
470 /*
471 Local Variables:
472 c-file-style: "gnu"
473 End:
474 */