* Wrapped deprecated code between #if (SCM_DEBUG_DEPRECATED == 0) #endif.
[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 #include "libguile/gc.h"
55
56 #include "libguile/validate.h"
57 #include "libguile/async.h"
58
59 #ifdef HAVE_STRING_H
60 #include <string.h>
61 #endif
62 #ifdef HAVE_UNISTD_H
63 #include <unistd.h>
64 #endif
65
66
67 \f
68 /* {Asynchronous Events}
69 *
70 *
71 * Async == thunk + mark.
72 *
73 * Setting the mark guarantees future execution of the thunk. More
74 * than one set may be satisfied by a single execution.
75 *
76 * scm_tick_clock decremented once per SCM_ALLOW_INTS.
77 * Async execution triggered by SCM_ALLOW_INTS when scm_tick_clock drops to 0.
78 * Async execution prevented by scm_mask_ints != 0.
79 *
80 * If the clock reaches 0 when scm_mask_ints != 0, then reset the clock
81 * to 1.
82 *
83 * If the clock reaches 0 any other time, run marked asyncs.
84 *
85 * From a unix signal handler, mark a corresponding async and set the clock
86 * to 1. Do SCM_REDEFER_INTS;/SCM_REALLOW_INTS so that if the signal handler is not
87 * called in the dynamic scope of a critical section, it is excecuted immediately.
88 *
89 * Overall, closely timed signals of a particular sort may be combined. Pending signals
90 * are delivered in a fixed priority order, regardless of arrival order.
91 *
92 */
93
94 /* True between SCM_DEFER_INTS and SCM_ALLOW_INTS, and
95 * when the interpreter is not running at all.
96 */
97 int scm_ints_disabled = 1;
98 unsigned int scm_mask_ints = 1;
99
100 #ifdef GUILE_OLD_ASYNC_CLICK
101 unsigned int scm_async_clock = 20;
102 static unsigned int scm_async_rate = 20;
103
104 static unsigned int scm_tick_clock = 0;
105 static unsigned int scm_tick_rate = 0;
106 static unsigned int scm_desired_tick_rate = 0;
107 static unsigned int scm_switch_clock = 0;
108 static unsigned int scm_switch_rate = 0;
109 static unsigned int scm_desired_switch_rate = 0;
110 #else
111 int scm_asyncs_pending_p = 0;
112 #endif
113
114 static long tc16_async;
115
116 \f
117
118 /* cmm: this has SCM_ prefix because SCM_MAKE_VALIDATE expects it.
119 this is ugly. */
120 #define SCM_ASYNCP(X) (SCM_NIMP(X) && (tc16_async == SCM_GCTYP16 (X)))
121
122 #define VALIDATE_ASYNC(pos,a) SCM_MAKE_VALIDATE(pos, a, ASYNCP)
123
124 #define ASYNC_GOT_IT(X) (SCM_CELL_WORD_0 (X) >> 16)
125 #define SET_ASYNC_GOT_IT(X, V) (SCM_SET_CELL_WORD_0 (X, (SCM_CELL_WORD_0 (X) & ((1 << 16) - 1)) | ((V) << 16)))
126 #define ASYNC_THUNK(X) SCM_CELL_OBJECT_1 (X)
127
128 \f
129
130 #ifdef GUILE_OLD_ASYNC_CLICK
131 int
132 scm_asyncs_pending ()
133 {
134 SCM pos;
135 pos = scm_asyncs;
136 while (pos != SCM_EOL)
137 {
138 SCM a = SCM_CAR (pos);
139 if (ASYNC_GOT_IT (a))
140 return 1;
141 pos = SCM_CDR (pos);
142 }
143 return 0;
144 }
145
146 #if 0
147 static SCM
148 scm_sys_tick_async_thunk (void)
149 {
150 scm_deliver_signal (SCM_TICK_SIGNAL);
151 return SCM_BOOL_F;
152 }
153 #endif
154
155 void
156 scm_async_click ()
157 {
158 int owe_switch;
159 int owe_tick;
160
161 if (!scm_switch_rate)
162 {
163 owe_switch = 0;
164 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
165 scm_desired_switch_rate = 0;
166 }
167 else
168 {
169 owe_switch = (scm_async_rate >= scm_switch_clock);
170 if (owe_switch)
171 {
172 if (scm_desired_switch_rate)
173 {
174 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
175 scm_desired_switch_rate = 0;
176 }
177 else
178 scm_switch_clock = scm_switch_rate;
179 }
180 else
181 {
182 if (scm_desired_switch_rate)
183 {
184 scm_switch_clock = scm_switch_rate = scm_desired_switch_rate;
185 scm_desired_switch_rate = 0;
186 }
187 else
188 scm_switch_clock -= scm_async_rate;
189 }
190 }
191
192 if (scm_mask_ints)
193 {
194 if (owe_switch)
195 scm_switch ();
196 scm_async_clock = 1;
197 return;;
198 }
199
200 if (!scm_tick_rate)
201 {
202 unsigned int r;
203 owe_tick = 0;
204 r = scm_desired_tick_rate;
205 if (r)
206 {
207 scm_desired_tick_rate = 0;
208 scm_tick_rate = r;
209 scm_tick_clock = r;
210 }
211 }
212 else
213 {
214 owe_tick = (scm_async_rate >= scm_tick_clock);
215 if (owe_tick)
216 {
217 scm_tick_clock = scm_tick_rate = scm_desired_tick_rate;
218 scm_desired_tick_rate = 0;
219 }
220 else
221 {
222 if (scm_desired_tick_rate)
223 {
224 scm_tick_clock = scm_tick_rate = scm_desired_tick_rate;
225 scm_desired_tick_rate = 0;
226 }
227 else
228 scm_tick_clock -= scm_async_rate;
229 }
230 }
231
232 /*
233 if (owe_tick)
234 scm_async_mark (system_signal_asyncs[SCM_SIG_ORD(SCM_TICK_SIGNAL)]); */
235
236 SCM_DEFER_INTS;
237 if (scm_tick_rate && scm_switch_rate)
238 {
239 scm_async_rate = min (scm_tick_clock, scm_switch_clock);
240 scm_async_clock = scm_async_rate;
241 }
242 else if (scm_tick_rate)
243 {
244 scm_async_clock = scm_async_rate = scm_tick_clock;
245 }
246 else if (scm_switch_rate)
247 {
248 scm_async_clock = scm_async_rate = scm_switch_clock;
249 }
250 else
251 scm_async_clock = scm_async_rate = 1 << 16;
252 SCM_ALLOW_INTS_ONLY;
253
254 tail:
255 scm_run_asyncs (scm_asyncs);
256
257 SCM_DEFER_INTS;
258 if (scm_asyncs_pending ())
259 {
260 SCM_ALLOW_INTS_ONLY;
261 goto tail;
262 }
263 SCM_ALLOW_INTS;
264
265 if (owe_switch)
266 scm_switch ();
267 }
268
269 void
270 scm_switch ()
271 {
272 #if 0 /* Thread switching code should probably reside here, but the
273 async switching code doesn't seem to work, so it's put in the
274 SCM_DEFER_INTS macro instead. /mdj */
275 SCM_THREAD_SWITCHING_CODE;
276 #endif
277 }
278
279 #else
280
281 void
282 scm_async_click ()
283 {
284 if (!scm_mask_ints)
285 do
286 scm_run_asyncs (scm_asyncs);
287 while (scm_asyncs_pending_p);
288 }
289
290 #endif
291
292
293 \f
294
295 static SCM
296 mark_async (SCM obj)
297 {
298 return ASYNC_THUNK (obj);
299 }
300
301 \f
302
303 SCM_DEFINE (scm_async, "async", 1, 0, 0,
304 (SCM thunk),
305 "")
306 #define FUNC_NAME s_scm_async
307 {
308 SCM_RETURN_NEWSMOB (tc16_async, SCM_UNPACK (thunk));
309 }
310 #undef FUNC_NAME
311
312 SCM_DEFINE (scm_system_async, "system-async", 1, 0, 0,
313 (SCM thunk),
314 "")
315 #define FUNC_NAME s_scm_system_async
316 {
317 SCM it;
318 SCM list;
319
320 it = scm_async (thunk);
321 SCM_NEWCELL (list);
322 SCM_SETCAR (list, it);
323 SCM_SETCDR (list, scm_asyncs);
324 scm_asyncs = list;
325 return it;
326 }
327 #undef FUNC_NAME
328
329 SCM_DEFINE (scm_async_mark, "async-mark", 1, 0, 0,
330 (SCM a),
331 "")
332 #define FUNC_NAME s_scm_async_mark
333 {
334 VALIDATE_ASYNC (1, a);
335 #ifdef GUILE_OLD_ASYNC_CLICK
336 SET_ASYNC_GOT_IT (a, 1);
337 #else
338 SET_ASYNC_GOT_IT (a, scm_asyncs_pending_p = 1);
339 #endif
340 return SCM_UNSPECIFIED;
341 }
342 #undef FUNC_NAME
343
344
345 SCM_DEFINE (scm_system_async_mark, "system-async-mark", 1, 0, 0,
346 (SCM a),
347 "")
348 #define FUNC_NAME s_scm_system_async_mark
349 {
350 VALIDATE_ASYNC (1, a);
351 SCM_REDEFER_INTS;
352 #ifdef GUILE_OLD_ASYNC_CLICK
353 SET_ASYNC_GOT_IT (a, 1);
354 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
355 scm_async_clock = 1;
356 #else
357 SET_ASYNC_GOT_IT (a, scm_asyncs_pending_p = 1);
358 #endif
359 SCM_REALLOW_INTS;
360 return SCM_UNSPECIFIED;
361 }
362 #undef FUNC_NAME
363
364
365 SCM_DEFINE (scm_run_asyncs, "run-asyncs", 1, 0, 0,
366 (SCM list_of_a),
367 "")
368 #define FUNC_NAME s_scm_run_asyncs
369 {
370 #ifdef GUILE_OLD_ASYNC_CLICK
371 if (scm_mask_ints)
372 return SCM_BOOL_F;
373 #else
374 scm_asyncs_pending_p = 0;
375 #endif
376 while (! SCM_NULLP (list_of_a))
377 {
378 SCM a;
379 SCM_VALIDATE_CONS (1, list_of_a);
380 a = SCM_CAR (list_of_a);
381 VALIDATE_ASYNC (SCM_ARG1, a);
382 scm_mask_ints = 1;
383 if (ASYNC_GOT_IT (a))
384 {
385 SET_ASYNC_GOT_IT (a, 0);
386 scm_apply (ASYNC_THUNK (a), SCM_EOL, SCM_EOL);
387 }
388 scm_mask_ints = 0;
389 list_of_a = SCM_CDR (list_of_a);
390 }
391 return SCM_BOOL_T;
392 }
393 #undef FUNC_NAME
394
395 \f
396
397
398 SCM_DEFINE (scm_noop, "noop", 0, 0, 1,
399 (SCM args),
400 "")
401 #define FUNC_NAME s_scm_noop
402 {
403 return (SCM_NULLP (args) ? SCM_BOOL_F : SCM_CAR (args));
404 }
405 #undef FUNC_NAME
406
407
408 \f
409
410 #ifdef GUILE_OLD_ASYNC_CLICK
411
412 SCM_DEFINE (scm_set_tick_rate, "set-tick-rate", 1, 0, 0,
413 (SCM n),
414 "")
415 #define FUNC_NAME s_scm_set_tick_rate
416 {
417 unsigned int old_n = scm_tick_rate;
418 SCM_VALIDATE_INUM (1, n);
419 scm_desired_tick_rate = SCM_INUM (n);
420 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
421 scm_async_clock = 1;
422 return SCM_MAKINUM (old_n);
423 }
424 #undef FUNC_NAME
425
426 \f
427
428
429 SCM_DEFINE (scm_set_switch_rate, "set-switch-rate", 1, 0, 0,
430 (SCM n),
431 "")
432 #define FUNC_NAME s_scm_set_switch_rate
433 {
434 unsigned int old_n = scm_switch_rate;
435 SCM_VALIDATE_INUM (1, n);
436 scm_desired_switch_rate = SCM_INUM (n);
437 scm_async_rate = 1 + scm_async_rate - scm_async_clock;
438 scm_async_clock = 1;
439 return SCM_MAKINUM (old_n);
440 }
441 #undef FUNC_NAME
442
443 #endif
444 \f
445
446 /* points to the GC system-async, so that scm_gc_end can find it. */
447 SCM scm_gc_async;
448
449 /* the vcell for gc-thunk. */
450 static SCM scm_gc_vcell;
451
452 /* the thunk installed in the GC system-async, which is marked at the
453 end of garbage collection. */
454 static SCM
455 scm_sys_gc_async_thunk (void)
456 {
457 scm_c_run_hook (scm_after_gc_hook, SCM_EOL);
458
459 #if (SCM_DEBUG_DEPRECATED == 0)
460
461 /* The following code will be removed in Guile 1.5. */
462 if (SCM_NFALSEP (scm_gc_vcell))
463 {
464 SCM proc = SCM_CDR (scm_gc_vcell);
465
466 if (SCM_NFALSEP (proc) && !SCM_UNBNDP (proc))
467 scm_apply (proc, SCM_EOL, SCM_EOL);
468 }
469
470 #endif /* SCM_DEBUG_DEPRECATED == 0 */
471
472 return SCM_UNSPECIFIED;
473 }
474
475 \f
476
477 SCM_DEFINE (scm_unmask_signals, "unmask-signals", 0, 0, 0,
478 (),
479 "")
480 #define FUNC_NAME s_scm_unmask_signals
481 {
482 scm_mask_ints = 0;
483 return SCM_UNSPECIFIED;
484 }
485 #undef FUNC_NAME
486
487
488 SCM_DEFINE (scm_mask_signals, "mask-signals", 0, 0, 0,
489 (),
490 "")
491 #define FUNC_NAME s_scm_mask_signals
492 {
493 scm_mask_ints = 1;
494 return SCM_UNSPECIFIED;
495 }
496 #undef FUNC_NAME
497
498 \f
499
500 void
501 scm_init_async ()
502 {
503 SCM a_thunk;
504 tc16_async = scm_make_smob_type_mfpe ("async", 0,
505 mark_async, NULL, NULL, NULL);
506 scm_gc_vcell = scm_sysintern ("gc-thunk", SCM_BOOL_F);
507 a_thunk = scm_make_gsubr ("%gc-thunk", 0, 0, 0, scm_sys_gc_async_thunk);
508 scm_gc_async = scm_system_async (a_thunk);
509
510 #include "libguile/async.x"
511 }
512
513 /*
514 Local Variables:
515 c-file-style: "gnu"
516 End:
517 */