1 /* Copyright (C) 1995, 96, 97, 98, 2000 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, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
67 /* {Asynchronous Events}
70 * Async == thunk + mark.
72 * Setting the mark guarantees future execution of the thunk. More
73 * than one set may be satisfied by a single execution.
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.
79 * If the clock reaches 0 when scm_mask_ints != 0, then reset the clock
82 * If the clock reaches 0 any other time, run marked asyncs.
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.
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.
93 /* True between SCM_DEFER_INTS and SCM_ALLOW_INTS, and
94 * when the interpreter is not running at all.
96 int scm_ints_disabled
= 1;
97 unsigned int scm_mask_ints
= 1;
99 #ifdef GUILE_OLD_ASYNC_CLICK
100 unsigned int scm_async_clock
= 20;
101 static unsigned int scm_async_rate
= 20;
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;
110 int scm_asyncs_pending_p
= 0;
113 static long scm_tc16_async
;
117 #ifdef GUILE_OLD_ASYNC_CLICK
119 scm_asyncs_pending ()
123 while (pos
!= SCM_EOL
)
126 struct scm_async
* it
;
138 scm_sys_tick_async_thunk (void)
140 scm_deliver_signal (SCM_TICK_SIGNAL
);
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
;
224 scm_async_mark (system_signal_asyncs[SCM_SIG_ORD(SCM_TICK_SIGNAL)]);
228 if (scm_tick_rate
&& scm_switch_rate
)
230 scm_async_rate
= min (scm_tick_clock
, scm_switch_clock
);
231 scm_async_clock
= scm_async_rate
;
233 else if (scm_tick_rate
)
235 scm_async_clock
= scm_async_rate
= scm_tick_clock
;
237 else if (scm_switch_rate
)
239 scm_async_clock
= scm_async_rate
= scm_switch_clock
;
242 scm_async_clock
= scm_async_rate
= 1 << 16;
246 scm_run_asyncs (scm_asyncs
);
249 if (scm_asyncs_pending ())
263 #if 0 /* Thread switching code should probably reside here, but the
264 async switching code doesn't seem to work, so it's put in the
265 SCM_DEFER_INTS macro instead. /mdj */
266 SCM_THREAD_SWITCHING_CODE
;
277 scm_run_asyncs (scm_asyncs
);
278 while (scm_asyncs_pending_p
);
289 struct scm_async
* it
;
290 it
= SCM_ASYNC (obj
);
296 SCM_DEFINE (scm_async
, "async", 1, 0, 0,
299 #define FUNC_NAME s_scm_async
301 SCM_RETURN_NEWSMOB2 (scm_tc16_async
, 0, thunk
);
305 SCM_DEFINE (scm_system_async
, "system-async", 1, 0, 0,
308 #define FUNC_NAME s_scm_system_async
313 it
= scm_async (thunk
);
315 SCM_SETCAR (list
, it
);
316 SCM_SETCDR (list
, scm_asyncs
);
322 SCM_DEFINE (scm_async_mark
, "async-mark", 1, 0, 0,
325 #define FUNC_NAME s_scm_async_mark
327 struct scm_async
* it
;
328 SCM_VALIDATE_ASYNC_COPY (1,a
,it
);
329 #ifdef GUILE_OLD_ASYNC_CLICK
332 scm_asyncs_pending_p
= it
->got_it
= 1;
334 return SCM_UNSPECIFIED
;
339 SCM_DEFINE (scm_system_async_mark
, "system-async-mark", 1, 0, 0,
342 #define FUNC_NAME s_scm_system_async_mark
344 struct scm_async
*it
;
345 SCM_VALIDATE_ASYNC_COPY (1, a
, it
);
347 #ifdef GUILE_OLD_ASYNC_CLICK
349 scm_async_rate
= 1 + scm_async_rate
- scm_async_clock
;
352 scm_asyncs_pending_p
= it
->got_it
= 1;
355 return SCM_UNSPECIFIED
;
360 SCM_DEFINE (scm_run_asyncs
, "run-asyncs", 1, 0, 0,
363 #define FUNC_NAME s_scm_run_asyncs
365 #ifdef GUILE_OLD_ASYNC_CLICK
369 scm_asyncs_pending_p
= 0;
371 while (list_of_a
!= SCM_EOL
)
374 struct scm_async
* it
;
375 SCM_VALIDATE_CONS (1, list_of_a
);
376 a
= SCM_CAR (list_of_a
);
377 SCM_VALIDATE_ASYNC_COPY (SCM_ARG1
,a
,it
);
382 scm_apply (it
->thunk
, SCM_EOL
, SCM_EOL
);
385 list_of_a
= SCM_CDR (list_of_a
);
394 SCM_DEFINE (scm_noop
, "noop", 0, 0, 1,
397 #define FUNC_NAME s_scm_noop
399 return (SCM_NULLP (args
) ? SCM_BOOL_F
: SCM_CAR (args
));
406 #ifdef GUILE_OLD_ASYNC_CLICK
408 SCM_DEFINE (scm_set_tick_rate
, "set-tick-rate", 1, 0, 0,
411 #define FUNC_NAME s_scm_set_tick_rate
413 unsigned int old_n
= scm_tick_rate
;
414 SCM_VALIDATE_INUM (1, n
);
415 scm_desired_tick_rate
= SCM_INUM (n
);
416 scm_async_rate
= 1 + scm_async_rate
- scm_async_clock
;
418 return SCM_MAKINUM (old_n
);
425 SCM_DEFINE (scm_set_switch_rate
, "set-switch-rate", 1, 0, 0,
428 #define FUNC_NAME s_scm_set_switch_rate
430 unsigned int old_n
= scm_switch_rate
;
431 SCM_VALIDATE_INUM (1, n
);
432 scm_desired_switch_rate
= SCM_INUM (n
);
433 scm_async_rate
= 1 + scm_async_rate
- scm_async_clock
;
435 return SCM_MAKINUM (old_n
);
442 /* points to the GC system-async, so that scm_gc_end can find it. */
445 /* the vcell for gc-thunk. */
446 static SCM scm_gc_vcell
;
448 /* the thunk installed in the GC system-async, which is marked at the
449 end of garbage collection. */
451 scm_sys_gc_async_thunk (void)
453 if (SCM_NFALSEP (scm_gc_vcell
))
455 SCM proc
= SCM_CDR (scm_gc_vcell
);
457 if (SCM_NFALSEP (proc
) && !SCM_UNBNDP (proc
))
458 scm_apply (proc
, SCM_EOL
, SCM_EOL
);
460 return SCM_UNSPECIFIED
;
465 SCM_DEFINE (scm_unmask_signals
, "unmask-signals", 0, 0, 0,
468 #define FUNC_NAME s_scm_unmask_signals
471 return SCM_UNSPECIFIED
;
476 SCM_DEFINE (scm_mask_signals
, "mask-signals", 0, 0, 0,
479 #define FUNC_NAME s_scm_mask_signals
482 return SCM_UNSPECIFIED
;
492 scm_tc16_async
= scm_make_smob_type_mfpe ("async", 0,
493 mark_async
, NULL
, NULL
, NULL
);
494 scm_gc_vcell
= scm_sysintern ("gc-thunk", SCM_BOOL_F
);
495 a_thunk
= scm_make_gsubr ("%gc-thunk", 0, 0, 0, scm_sys_gc_async_thunk
);
496 scm_gc_async
= scm_system_async (a_thunk
);