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.
56 /* {Asynchronous Events}
59 * Async == thunk + mark.
61 * Setting the mark guarantees future execution of the thunk. More
62 * than one set may be satisfied by a single execution.
64 * scm_tick_clock decremented once per SCM_ALLOW_INTS.
65 * Async execution triggered by SCM_ALLOW_INTS when scm_tick_clock drops to 0.
66 * Async execution prevented by scm_mask_ints != 0.
68 * If the clock reaches 0 when scm_mask_ints != 0, then reset the clock
71 * If the clock reaches 0 any other time, run marked asyncs.
73 * From a unix signal handler, mark a corresponding async and set the clock
74 * to 1. Do SCM_REDEFER_INTS;/SCM_REALLOW_INTS so that if the signal handler is not
75 * called in the dynamic scope of a critical section, it is excecuted immediately.
77 * Overall, closely timed signals of a particular sort may be combined. Pending signals
78 * are delivered in a fixed priority order, regardless of arrival order.
83 #define min(A,B) ((A) < (B) ? (A) : (B))
86 unsigned int scm_async_clock
= 20;
87 static unsigned int scm_async_rate
= 20;
88 unsigned int scm_mask_ints
= 1;
90 static unsigned int scm_tick_clock
= 0;
91 static unsigned int scm_tick_rate
= 0;
92 static unsigned int scm_desired_tick_rate
= 0;
93 static unsigned int scm_switch_clock
= 0;
94 static unsigned int scm_switch_rate
= 0;
95 static unsigned int scm_desired_switch_rate
= 0;
97 static SCM system_signal_asyncs
[SCM_NUM_SIGS
];
98 static SCM handler_var
;
99 static SCM symbol_signal
;
104 int got_it
; /* needs to be delivered? */
105 SCM thunk
; /* the handler. */
109 static long scm_tc16_async
;
111 #define SCM_ASYNCP(X) (scm_tc16_async == SCM_GCTYP16 (X))
112 #define SCM_ASYNC(X) ((struct scm_async *)SCM_CDR (X))
121 asyncs_pending (void)
129 while (pos
!= SCM_EOL
)
132 struct scm_async
* it
;
145 scm_async_click (void)
154 if (!scm_switch_rate
)
157 scm_switch_clock
= scm_switch_rate
= scm_desired_switch_rate
;
158 scm_desired_switch_rate
= 0;
162 owe_switch
= (scm_async_rate
>= scm_switch_clock
);
165 if (scm_desired_switch_rate
)
167 scm_switch_clock
= scm_switch_rate
= scm_desired_switch_rate
;
168 scm_desired_switch_rate
= 0;
171 scm_switch_clock
= scm_switch_rate
;
175 if (scm_desired_switch_rate
)
177 scm_switch_clock
= scm_switch_rate
= scm_desired_switch_rate
;
178 scm_desired_switch_rate
= 0;
181 scm_switch_clock
-= scm_async_rate
;
197 r
= scm_desired_tick_rate
;
200 scm_desired_tick_rate
= 0;
207 owe_tick
= (scm_async_rate
>= scm_tick_clock
);
210 scm_tick_clock
= scm_tick_rate
= scm_desired_tick_rate
;
211 scm_desired_tick_rate
= 0;
215 if (scm_desired_tick_rate
)
217 scm_tick_clock
= scm_tick_rate
= scm_desired_tick_rate
;
218 scm_desired_tick_rate
= 0;
221 scm_tick_clock
-= scm_async_rate
;
226 scm_async_mark (system_signal_asyncs
[SCM_SIG_ORD(SCM_TICK_SIGNAL
)]);
229 if (scm_tick_rate
&& scm_switch_rate
)
231 scm_async_rate
= min (scm_tick_clock
, scm_switch_clock
);
232 scm_async_clock
= scm_async_rate
;
234 else if (scm_tick_rate
)
236 scm_async_clock
= scm_async_rate
= scm_tick_clock
;
238 else if (scm_switch_rate
)
240 scm_async_clock
= scm_async_rate
= scm_switch_clock
;
243 scm_async_clock
= scm_async_rate
= 1 << 16;
247 scm_run_asyncs (scm_asyncs
);
250 if (asyncs_pending ())
276 scm_deliver_signal (int num
)
279 scm_deliver_signal (num
)
284 handler
= SCM_CDR (handler_var
);
285 if (handler
!= SCM_BOOL_F
)
286 scm_apply (handler
, SCM_MAKINUM (num
), scm_listofnull
);
290 scm_throw (symbol_signal
,
291 scm_listify (SCM_MAKINUM (num
), SCM_UNDEFINED
));
300 print_async (SCM exp
, SCM port
, int writing
)
303 print_async (exp
, port
, writing
)
309 scm_gen_puts (scm_regular_string
, "#<async ", port
);
310 scm_intprint(exp
, 16, port
);
311 scm_gen_putc('>', port
);
324 struct scm_async
* it
;
325 if (SCM_GC8MARKP (obj
))
327 SCM_SETGC8MARK (obj
);
328 it
= SCM_ASYNC (obj
);
341 struct scm_async
* it
;
342 it
= SCM_ASYNC (obj
);
343 scm_must_free ((char *)it
);
344 return (sizeof (*it
));
348 static scm_smobfuns async_smob
=
359 SCM_PROC(s_async
, "async", 1, 0, 0, scm_async
);
362 scm_async (SCM thunk
)
370 struct scm_async
* async
;
374 SCM_SETCDR (it
, SCM_EOL
);
375 async
= (struct scm_async
*)scm_must_malloc (sizeof (*async
), s_async
);
377 async
->thunk
= thunk
;
378 SCM_SETCDR (it
, (SCM
)async
);
379 SCM_SETCAR (it
, (SCM
)scm_tc16_async
);
384 SCM_PROC(s_system_async
, "system-async", 1, 0, 0, scm_system_async
);
387 scm_system_async (SCM thunk
)
390 scm_system_async (thunk
)
397 it
= scm_async (thunk
);
400 SCM_SETCAR (list
, it
);
401 SCM_SETCDR (list
, scm_asyncs
);
407 SCM_PROC(s_async_mark
, "async-mark", 1, 0, 0, scm_async_mark
);
410 scm_async_mark (SCM a
)
417 struct scm_async
* it
;
418 SCM_ASSERT (SCM_NIMP (a
) && SCM_ASYNCP (a
), a
, SCM_ARG1
, s_async_mark
);
421 return SCM_UNSPECIFIED
;
425 SCM_PROC(s_system_async_mark
, "system-async-mark", 1, 0, 0, scm_system_async_mark
);
428 scm_system_async_mark (SCM a
)
431 scm_system_async_mark (a
)
435 struct scm_async
* it
;
436 SCM_ASSERT (SCM_NIMP (a
) && SCM_ASYNCP (a
), a
, SCM_ARG1
, s_async_mark
);
440 scm_async_rate
= 1 + scm_async_rate
- scm_async_clock
;
443 return SCM_UNSPECIFIED
;
447 SCM_PROC(s_run_asyncs
, "run-asyncs", 1, 0, 0, scm_run_asyncs
);
450 scm_run_asyncs (SCM list_of_a
)
453 scm_run_asyncs (list_of_a
)
462 while (pos
!= SCM_EOL
)
465 struct scm_async
* it
;
466 SCM_ASSERT (SCM_NIMP (pos
) && SCM_CONSP (pos
), pos
, SCM_ARG1
, s_run_asyncs
);
468 SCM_ASSERT (SCM_NIMP (a
) && SCM_ASYNCP (a
), a
, SCM_ARG1
, s_run_asyncs
);
474 scm_apply (it
->thunk
, SCM_EOL
, SCM_EOL
);
485 SCM_PROC(s_noop
, "noop", 0, 0, 1, scm_noop
);
495 return (SCM_NULLP (args
)
503 SCM_PROC(s_set_tick_rate
, "set-tick-rate", 1, 0, 0, scm_set_tick_rate
);
506 scm_set_tick_rate (SCM n
)
509 scm_set_tick_rate (n
)
514 SCM_ASSERT (SCM_INUMP (n
), n
, SCM_ARG1
, s_set_tick_rate
);
515 old_n
= scm_tick_rate
;
516 scm_desired_tick_rate
= SCM_INUM (n
);
517 scm_async_rate
= 1 + scm_async_rate
- scm_async_clock
;
519 return SCM_MAKINUM (old_n
);
525 SCM_PROC(s_set_switch_rate
, "set-switch-rate", 1, 0, 0, scm_set_switch_rate
);
528 scm_set_switch_rate (SCM n
)
531 scm_set_switch_rate (n
)
536 SCM_ASSERT (SCM_INUMP (n
), n
, SCM_ARG1
, s_set_switch_rate
);
537 old_n
= scm_switch_rate
;
538 scm_desired_switch_rate
= SCM_INUM (n
);
539 scm_async_rate
= 1 + scm_async_rate
- scm_async_clock
;
541 return SCM_MAKINUM (old_n
);
548 scm_sys_hup_async_thunk (void)
551 scm_sys_hup_async_thunk ()
554 scm_deliver_signal (SCM_HUP_SIGNAL
);
560 scm_sys_int_async_thunk (void)
563 scm_sys_int_async_thunk ()
566 scm_deliver_signal (SCM_INT_SIGNAL
);
572 scm_sys_fpe_async_thunk (void)
575 scm_sys_fpe_async_thunk ()
578 scm_deliver_signal (SCM_FPE_SIGNAL
);
584 scm_sys_bus_async_thunk (void)
587 scm_sys_bus_async_thunk ()
590 scm_deliver_signal (SCM_BUS_SIGNAL
);
596 scm_sys_segv_async_thunk (void)
599 scm_sys_segv_async_thunk ()
602 scm_deliver_signal (SCM_SEGV_SIGNAL
);
608 scm_sys_alrm_async_thunk (void)
611 scm_sys_alrm_async_thunk ()
614 scm_deliver_signal (SCM_ALRM_SIGNAL
);
620 scm_sys_gc_async_thunk (void)
623 scm_sys_gc_async_thunk ()
626 scm_deliver_signal (SCM_GC_SIGNAL
);
632 scm_sys_tick_async_thunk (void)
635 scm_sys_tick_async_thunk ()
638 scm_deliver_signal (SCM_TICK_SIGNAL
);
648 scm_take_signal (int n
)
656 if (!scm_ints_disabled
)
658 SCM_NEWCELL (ignored
); /* In case we interrupted SCM_NEWCELL,
659 * throw out the possibly already allocated
663 scm_system_async_mark (system_signal_asyncs
[SCM_SIG_ORD(n
)]);
669 SCM_PROC(s_unmask_signals
, "unmask-signals", 0, 0, 0, scm_unmask_signals
);
672 scm_unmask_signals (void)
675 scm_unmask_signals ()
679 return SCM_UNSPECIFIED
;
683 SCM_PROC(s_mask_signals
, "mask-signals", 0, 0, 0, scm_mask_signals
);
686 scm_mask_signals (void)
693 return SCM_UNSPECIFIED
;
700 scm_init_async (void)
707 scm_tc16_async
= scm_newsmob (&async_smob
);
708 symbol_signal
= SCM_CAR (scm_sysintern ("signal", strlen ("signal")));
709 scm_permanent_object (symbol_signal
);
711 /* These are in the opposite order of delivery priortity.
713 * Error conditions are given low priority:
715 a_thunk
= scm_make_gsubr ("%hup-thunk", 0, 0, 0, scm_sys_hup_async_thunk
);
716 system_signal_asyncs
[SCM_SIG_ORD(SCM_HUP_SIGNAL
)] = scm_system_async (a_thunk
);
717 a_thunk
= scm_make_gsubr ("%int-thunk", 0, 0, 0, scm_sys_int_async_thunk
);
718 system_signal_asyncs
[SCM_SIG_ORD(SCM_INT_SIGNAL
)] = scm_system_async (a_thunk
);
719 a_thunk
= scm_make_gsubr ("%fpe-thunk", 0, 0, 0, scm_sys_fpe_async_thunk
);
720 system_signal_asyncs
[SCM_SIG_ORD(SCM_FPE_SIGNAL
)] = scm_system_async (a_thunk
);
721 a_thunk
= scm_make_gsubr ("%bus-thunk", 0, 0, 0, scm_sys_bus_async_thunk
);
722 system_signal_asyncs
[SCM_SIG_ORD(SCM_BUS_SIGNAL
)] = scm_system_async (a_thunk
);
723 a_thunk
= scm_make_gsubr ("%segv-thunk", 0, 0, 0, scm_sys_segv_async_thunk
);
724 system_signal_asyncs
[SCM_SIG_ORD(SCM_SEGV_SIGNAL
)] = scm_system_async (a_thunk
);
727 a_thunk
= scm_make_gsubr ("%gc-thunk", 0, 0, 0, scm_sys_gc_async_thunk
);
728 system_signal_asyncs
[SCM_SIG_ORD(SCM_GC_SIGNAL
)] = scm_system_async (a_thunk
);
730 /* Clock and PC driven conditions are given highest priority. */
731 a_thunk
= scm_make_gsubr ("%tick-thunk", 0, 0, 0, scm_sys_tick_async_thunk
);
732 system_signal_asyncs
[SCM_SIG_ORD(SCM_TICK_SIGNAL
)] = scm_system_async (a_thunk
);
733 a_thunk
= scm_make_gsubr ("%alrm-thunk", 0, 0, 0, scm_sys_alrm_async_thunk
);
734 system_signal_asyncs
[SCM_SIG_ORD(SCM_ALRM_SIGNAL
)] = scm_system_async (a_thunk
);
736 handler_var
= scm_sysintern ("signal-handler", strlen ("signal"));
737 SCM_SETCDR (handler_var
, SCM_BOOL_F
);
738 scm_permanent_object (handler_var
);