1 /* Copyright (C) 1995,1996 Free Software Foundation, Inc.
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)
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.
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, 675 Mass Ave, Cambridge, MA 02139, USA.
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
53 /* {Asynchronous Events}
56 * Async == thunk + mark.
58 * Setting the mark guarantees future execution of the thunk. More
59 * than one set may be satisfied by a single execution.
61 * scm_tick_clock decremented once per SCM_ALLOW_INTS.
62 * Async execution triggered by SCM_ALLOW_INTS when scm_tick_clock drops to 0.
63 * Async execution prevented by scm_mask_ints != 0.
65 * If the clock reaches 0 when scm_mask_ints != 0, then reset the clock
68 * If the clock reaches 0 any other time, run marked asyncs.
70 * From a unix signal handler, mark a corresponding async and set the clock
71 * to 1. Do SCM_REDEFER_INTS;/SCM_REALLOW_INTS so that if the signal handler is not
72 * called in the dynamic scope of a critical section, it is excecuted immediately.
74 * Overall, closely timed signals of a particular sort may be combined. Pending signals
75 * are delivered in a fixed priority order, regardless of arrival order.
80 #define min(A,B) ((A) < (B) ? (A) : (B))
83 unsigned int scm_async_clock
= 20;
84 static unsigned int scm_async_rate
= 20;
85 unsigned int scm_mask_ints
= 1;
87 static unsigned int scm_tick_clock
= 0;
88 static unsigned int scm_tick_rate
= 0;
89 static unsigned int scm_desired_tick_rate
= 0;
90 static unsigned int scm_switch_clock
= 0;
91 static unsigned int scm_switch_rate
= 0;
92 static unsigned int scm_desired_switch_rate
= 0;
94 static SCM system_signal_asyncs
[SCM_NUM_SIGS
];
95 static SCM handler_var
;
96 static SCM symbol_signal
;
101 int got_it
; /* needs to be delivered? */
102 SCM thunk
; /* the handler. */
106 static long scm_tc16_async
;
108 #define SCM_ASYNCP(X) (scm_tc16_async == SCM_GCTYP16 (X))
109 #define SCM_ASYNC(X) ((struct scm_async *)SCM_CDR (X))
118 asyncs_pending (void)
126 while (pos
!= SCM_EOL
)
129 struct scm_async
* it
;
142 scm_async_click (void)
151 if (!scm_switch_rate
)
154 scm_switch_clock
= scm_switch_rate
= scm_desired_switch_rate
;
155 scm_desired_switch_rate
= 0;
159 owe_switch
= (scm_async_rate
>= scm_switch_clock
);
162 if (scm_desired_switch_rate
)
164 scm_switch_clock
= scm_switch_rate
= scm_desired_switch_rate
;
165 scm_desired_switch_rate
= 0;
168 scm_switch_clock
= scm_switch_rate
;
172 if (scm_desired_switch_rate
)
174 scm_switch_clock
= scm_switch_rate
= scm_desired_switch_rate
;
175 scm_desired_switch_rate
= 0;
178 scm_switch_clock
-= scm_async_rate
;
194 r
= scm_desired_tick_rate
;
197 scm_desired_tick_rate
= 0;
204 owe_tick
= (scm_async_rate
>= scm_tick_clock
);
207 scm_tick_clock
= scm_tick_rate
= scm_desired_tick_rate
;
208 scm_desired_tick_rate
= 0;
212 if (scm_desired_tick_rate
)
214 scm_tick_clock
= scm_tick_rate
= scm_desired_tick_rate
;
215 scm_desired_tick_rate
= 0;
218 scm_tick_clock
-= scm_async_rate
;
223 scm_async_mark (system_signal_asyncs
[SCM_SIG_ORD(SCM_TICK_SIGNAL
)]);
226 if (scm_tick_rate
&& scm_switch_rate
)
228 scm_async_rate
= min (scm_tick_clock
, scm_switch_clock
);
229 scm_async_clock
= scm_async_rate
;
231 else if (scm_tick_rate
)
233 scm_async_clock
= scm_async_rate
= scm_tick_clock
;
235 else if (scm_switch_rate
)
237 scm_async_clock
= scm_async_rate
= scm_switch_clock
;
240 scm_async_clock
= scm_async_rate
= 1 << 16;
244 scm_run_asyncs (scm_asyncs
);
247 if (asyncs_pending ())
273 scm_deliver_signal (int num
)
276 scm_deliver_signal (num
)
281 handler
= SCM_CDR (handler_var
);
282 if (handler
!= SCM_BOOL_F
)
283 scm_apply (handler
, SCM_MAKINUM (num
), scm_listofnull
);
287 scm_throw (symbol_signal
,
288 scm_listify (SCM_MAKINUM (num
), SCM_UNDEFINED
));
297 print_async (SCM exp
, SCM port
, int writing
)
300 print_async (exp
, port
, writing
)
306 scm_gen_puts (scm_regular_string
, "#<async ", port
);
307 scm_intprint(exp
, 16, port
);
308 scm_gen_putc('>', port
);
321 struct scm_async
* it
;
322 if (SCM_GC8MARKP (obj
))
324 SCM_SETGC8MARK (obj
);
325 it
= SCM_ASYNC (obj
);
338 struct scm_async
* it
;
339 it
= SCM_ASYNC (obj
);
340 scm_must_free ((char *)it
);
341 return (sizeof (*it
));
345 static scm_smobfuns async_smob
=
356 SCM_PROC(s_async
, "async", 1, 0, 0, scm_async
);
359 scm_async (SCM thunk
)
367 struct scm_async
* async
;
371 SCM_SETCDR (it
, SCM_EOL
);
372 async
= (struct scm_async
*)scm_must_malloc (sizeof (*async
), s_async
);
374 async
->thunk
= thunk
;
375 SCM_SETCDR (it
, (SCM
)async
);
376 SCM_SETCAR (it
, (SCM
)scm_tc16_async
);
381 SCM_PROC(s_system_async
, "system-async", 1, 0, 0, scm_system_async
);
384 scm_system_async (SCM thunk
)
387 scm_system_async (thunk
)
394 it
= scm_async (thunk
);
397 SCM_SETCAR (list
, it
);
398 SCM_SETCDR (list
, scm_asyncs
);
404 SCM_PROC(s_async_mark
, "async-mark", 1, 0, 0, scm_async_mark
);
407 scm_async_mark (SCM a
)
414 struct scm_async
* it
;
415 SCM_ASSERT (SCM_NIMP (a
) && SCM_ASYNCP (a
), a
, SCM_ARG1
, s_async_mark
);
418 return SCM_UNSPECIFIED
;
422 SCM_PROC(s_system_async_mark
, "system-async-mark", 1, 0, 0, scm_system_async_mark
);
425 scm_system_async_mark (SCM a
)
428 scm_system_async_mark (a
)
432 struct scm_async
* it
;
433 SCM_ASSERT (SCM_NIMP (a
) && SCM_ASYNCP (a
), a
, SCM_ARG1
, s_async_mark
);
437 scm_async_rate
= 1 + scm_async_rate
- scm_async_clock
;
440 return SCM_UNSPECIFIED
;
444 SCM_PROC(s_run_asyncs
, "run-asyncs", 1, 0, 0, scm_run_asyncs
);
447 scm_run_asyncs (SCM list_of_a
)
450 scm_run_asyncs (list_of_a
)
459 while (pos
!= SCM_EOL
)
462 struct scm_async
* it
;
463 SCM_ASSERT (SCM_NIMP (pos
) && SCM_CONSP (pos
), pos
, SCM_ARG1
, s_run_asyncs
);
465 SCM_ASSERT (SCM_NIMP (a
) && SCM_ASYNCP (a
), a
, SCM_ARG1
, s_run_asyncs
);
471 scm_apply (it
->thunk
, SCM_EOL
, SCM_EOL
);
482 SCM_PROC(s_noop
, "noop", 0, 0, 1, scm_noop
);
492 return (SCM_NULLP (args
)
500 SCM_PROC(s_set_tick_rate
, "set-tick-rate", 1, 0, 0, scm_set_tick_rate
);
503 scm_set_tick_rate (SCM n
)
506 scm_set_tick_rate (n
)
511 SCM_ASSERT (SCM_INUMP (n
), n
, SCM_ARG1
, s_set_tick_rate
);
512 old_n
= scm_tick_rate
;
513 scm_desired_tick_rate
= SCM_INUM (n
);
514 scm_async_rate
= 1 + scm_async_rate
- scm_async_clock
;
516 return SCM_MAKINUM (old_n
);
522 SCM_PROC(s_set_switch_rate
, "set-switch-rate", 1, 0, 0, scm_set_switch_rate
);
525 scm_set_switch_rate (SCM n
)
528 scm_set_switch_rate (n
)
533 SCM_ASSERT (SCM_INUMP (n
), n
, SCM_ARG1
, s_set_switch_rate
);
534 old_n
= scm_switch_rate
;
535 scm_desired_switch_rate
= SCM_INUM (n
);
536 scm_async_rate
= 1 + scm_async_rate
- scm_async_clock
;
538 return SCM_MAKINUM (old_n
);
545 scm_sys_hup_async_thunk (void)
548 scm_sys_hup_async_thunk ()
551 scm_deliver_signal (SCM_HUP_SIGNAL
);
557 scm_sys_int_async_thunk (void)
560 scm_sys_int_async_thunk ()
563 scm_deliver_signal (SCM_INT_SIGNAL
);
569 scm_sys_fpe_async_thunk (void)
572 scm_sys_fpe_async_thunk ()
575 scm_deliver_signal (SCM_FPE_SIGNAL
);
581 scm_sys_bus_async_thunk (void)
584 scm_sys_bus_async_thunk ()
587 scm_deliver_signal (SCM_BUS_SIGNAL
);
593 scm_sys_segv_async_thunk (void)
596 scm_sys_segv_async_thunk ()
599 scm_deliver_signal (SCM_SEGV_SIGNAL
);
605 scm_sys_alrm_async_thunk (void)
608 scm_sys_alrm_async_thunk ()
611 scm_deliver_signal (SCM_ALRM_SIGNAL
);
617 scm_sys_gc_async_thunk (void)
620 scm_sys_gc_async_thunk ()
623 scm_deliver_signal (SCM_GC_SIGNAL
);
629 scm_sys_tick_async_thunk (void)
632 scm_sys_tick_async_thunk ()
635 scm_deliver_signal (SCM_TICK_SIGNAL
);
645 scm_take_signal (int n
)
653 if (!scm_ints_disabled
)
655 SCM_NEWCELL (ignored
); /* In case we interrupted SCM_NEWCELL,
656 * throw out the possibly already allocated
660 scm_system_async_mark (system_signal_asyncs
[SCM_SIG_ORD(n
)]);
666 SCM_PROC(s_unmask_signals
, "unmask-signals", 0, 0, 0, scm_unmask_signals
);
669 scm_unmask_signals (void)
672 scm_unmask_signals ()
676 return SCM_UNSPECIFIED
;
680 SCM_PROC(s_mask_signals
, "mask-signals", 0, 0, 0, scm_mask_signals
);
683 scm_mask_signals (void)
690 return SCM_UNSPECIFIED
;
697 scm_init_async (void)
704 scm_tc16_async
= scm_newsmob (&async_smob
);
705 symbol_signal
= SCM_CAR (scm_sysintern ("signal", strlen ("signal")));
706 scm_permanent_object (symbol_signal
);
708 /* These are in the opposite order of delivery priortity.
710 * Error conditions are given low priority:
712 a_thunk
= scm_make_gsubr ("%hup-thunk", 0, 0, 0, scm_sys_hup_async_thunk
);
713 system_signal_asyncs
[SCM_SIG_ORD(SCM_HUP_SIGNAL
)] = scm_system_async (a_thunk
);
714 a_thunk
= scm_make_gsubr ("%int-thunk", 0, 0, 0, scm_sys_int_async_thunk
);
715 system_signal_asyncs
[SCM_SIG_ORD(SCM_INT_SIGNAL
)] = scm_system_async (a_thunk
);
716 a_thunk
= scm_make_gsubr ("%fpe-thunk", 0, 0, 0, scm_sys_fpe_async_thunk
);
717 system_signal_asyncs
[SCM_SIG_ORD(SCM_FPE_SIGNAL
)] = scm_system_async (a_thunk
);
718 a_thunk
= scm_make_gsubr ("%bus-thunk", 0, 0, 0, scm_sys_bus_async_thunk
);
719 system_signal_asyncs
[SCM_SIG_ORD(SCM_BUS_SIGNAL
)] = scm_system_async (a_thunk
);
720 a_thunk
= scm_make_gsubr ("%segv-thunk", 0, 0, 0, scm_sys_segv_async_thunk
);
721 system_signal_asyncs
[SCM_SIG_ORD(SCM_SEGV_SIGNAL
)] = scm_system_async (a_thunk
);
724 a_thunk
= scm_make_gsubr ("%gc-thunk", 0, 0, 0, scm_sys_gc_async_thunk
);
725 system_signal_asyncs
[SCM_SIG_ORD(SCM_GC_SIGNAL
)] = scm_system_async (a_thunk
);
727 /* Clock and PC driven conditions are given highest priority. */
728 a_thunk
= scm_make_gsubr ("%tick-thunk", 0, 0, 0, scm_sys_tick_async_thunk
);
729 system_signal_asyncs
[SCM_SIG_ORD(SCM_TICK_SIGNAL
)] = scm_system_async (a_thunk
);
730 a_thunk
= scm_make_gsubr ("%alrm-thunk", 0, 0, 0, scm_sys_alrm_async_thunk
);
731 system_signal_asyncs
[SCM_SIG_ORD(SCM_ALRM_SIGNAL
)] = scm_system_async (a_thunk
);
733 handler_var
= scm_sysintern ("signal-handler", strlen ("signal"));
734 SCM_SETCDR (handler_var
, SCM_BOOL_F
);
735 scm_permanent_object (handler_var
);