1 /* Copyright (C) 1995,1996,1997,1998,2000,2001 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. */
46 #include "libguile/_scm.h"
47 #include "libguile/eval.h"
48 #include "libguile/throw.h"
49 #include "libguile/root.h"
50 #include "libguile/smob.h"
51 #include "libguile/lang.h"
53 #include "libguile/validate.h"
54 #include "libguile/async.h"
65 /* {Asynchronous Events}
68 * Async == thunk + mark.
70 * Setting the mark guarantees future execution of the thunk. More
71 * than one set may be satisfied by a single execution.
73 * scm_tick_clock decremented once per SCM_ALLOW_INTS.
74 * Async execution triggered by SCM_ALLOW_INTS when scm_tick_clock drops to 0.
75 * Async execution prevented by scm_mask_ints != 0.
77 * If the clock reaches 0 when scm_mask_ints != 0, then reset the clock
80 * If the clock reaches 0 any other time, run marked asyncs.
82 * From a unix signal handler, mark a corresponding async and set the clock
83 * to 1. Do SCM_REDEFER_INTS;/SCM_REALLOW_INTS so that if the signal handler is not
84 * called in the dynamic scope of a critical section, it is excecuted immediately.
86 * Overall, closely timed signals of a particular sort may be combined. Pending signals
87 * are delivered in a fixed priority order, regardless of arrival order.
91 /* True between SCM_DEFER_INTS and SCM_ALLOW_INTS, and
92 * when the interpreter is not running at all.
94 int scm_ints_disabled
= 1;
95 unsigned int scm_mask_ints
= 1;
97 #ifdef GUILE_OLD_ASYNC_CLICK
98 unsigned int scm_async_clock
= 20;
99 static unsigned int scm_async_rate
= 20;
101 static unsigned int scm_tick_clock
= 0;
102 static unsigned int scm_tick_rate
= 0;
103 static unsigned int scm_desired_tick_rate
= 0;
104 static unsigned int scm_switch_clock
= 0;
105 static unsigned int scm_switch_rate
= 0;
106 static unsigned int scm_desired_switch_rate
= 0;
108 int scm_asyncs_pending_p
= 0;
111 static scm_t_bits tc16_async
;
115 /* cmm: this has SCM_ prefix because SCM_MAKE_VALIDATE expects it.
117 #define SCM_ASYNCP(X) SCM_TYP16_PREDICATE (tc16_async, X)
118 #define VALIDATE_ASYNC(pos,a) SCM_MAKE_VALIDATE(pos, a, ASYNCP)
120 #define ASYNC_GOT_IT(X) (SCM_CELL_WORD_0 (X) >> 16)
121 #define SET_ASYNC_GOT_IT(X, V) (SCM_SET_CELL_WORD_0 ((X), SCM_TYP16 (X) | ((V) << 16)))
122 #define ASYNC_THUNK(X) SCM_CELL_OBJECT_1 (X)
126 #ifdef GUILE_OLD_ASYNC_CLICK
128 scm_asyncs_pending ()
132 while (!SCM_NULL_OR_NIL_P (pos
))
134 SCM a
= SCM_CAR (pos
);
135 if (ASYNC_GOT_IT (a
))
149 if (!scm_switch_rate
)
152 scm_switch_clock
= scm_switch_rate
= scm_desired_switch_rate
;
153 scm_desired_switch_rate
= 0;
157 owe_switch
= (scm_async_rate
>= scm_switch_clock
);
160 if (scm_desired_switch_rate
)
162 scm_switch_clock
= scm_switch_rate
= scm_desired_switch_rate
;
163 scm_desired_switch_rate
= 0;
166 scm_switch_clock
= scm_switch_rate
;
170 if (scm_desired_switch_rate
)
172 scm_switch_clock
= scm_switch_rate
= scm_desired_switch_rate
;
173 scm_desired_switch_rate
= 0;
176 scm_switch_clock
-= scm_async_rate
;
192 r
= scm_desired_tick_rate
;
195 scm_desired_tick_rate
= 0;
202 owe_tick
= (scm_async_rate
>= scm_tick_clock
);
205 scm_tick_clock
= scm_tick_rate
= scm_desired_tick_rate
;
206 scm_desired_tick_rate
= 0;
210 if (scm_desired_tick_rate
)
212 scm_tick_clock
= scm_tick_rate
= scm_desired_tick_rate
;
213 scm_desired_tick_rate
= 0;
216 scm_tick_clock
-= scm_async_rate
;
221 if (scm_tick_rate
&& scm_switch_rate
)
223 scm_async_rate
= min (scm_tick_clock
, scm_switch_clock
);
224 scm_async_clock
= scm_async_rate
;
226 else if (scm_tick_rate
)
228 scm_async_clock
= scm_async_rate
= scm_tick_clock
;
230 else if (scm_switch_rate
)
232 scm_async_clock
= scm_async_rate
= scm_switch_clock
;
235 scm_async_clock
= scm_async_rate
= 1 << 16;
239 scm_run_asyncs (scm_asyncs
);
242 if (scm_asyncs_pending ())
256 #if 0 /* Thread switching code should probably reside here, but the
257 async switching code doesn't seem to work, so it's put in the
258 SCM_DEFER_INTS macro instead. /mdj */
259 SCM_THREAD_SWITCHING_CODE
;
270 scm_run_asyncs (scm_asyncs
);
271 while (scm_asyncs_pending_p
);
282 return ASYNC_THUNK (obj
);
287 SCM_DEFINE (scm_async
, "async", 1, 0, 0,
289 "Create a new async for the procedure @var{thunk}.")
290 #define FUNC_NAME s_scm_async
292 SCM_RETURN_NEWSMOB (tc16_async
, SCM_UNPACK (thunk
));
296 SCM_DEFINE (scm_system_async
, "system-async", 1, 0, 0,
298 "Create a new async for the procedure @var{thunk}. Also\n"
299 "add it to the system's list of active async objects.")
300 #define FUNC_NAME s_scm_system_async
302 SCM it
= scm_async (thunk
);
303 scm_asyncs
= scm_cons (it
, scm_asyncs
);
308 SCM_DEFINE (scm_async_mark
, "async-mark", 1, 0, 0,
310 "Mark the async @var{a} for future execution.")
311 #define FUNC_NAME s_scm_async_mark
313 VALIDATE_ASYNC (1, a
);
314 #ifdef GUILE_OLD_ASYNC_CLICK
315 SET_ASYNC_GOT_IT (a
, 1);
317 SET_ASYNC_GOT_IT (a
, scm_asyncs_pending_p
= 1);
319 return SCM_UNSPECIFIED
;
324 SCM_DEFINE (scm_system_async_mark
, "system-async-mark", 1, 0, 0,
326 "Mark the async @var{a} for future execution.")
327 #define FUNC_NAME s_scm_system_async_mark
329 VALIDATE_ASYNC (1, a
);
331 #ifdef GUILE_OLD_ASYNC_CLICK
332 SET_ASYNC_GOT_IT (a
, 1);
333 scm_async_rate
= 1 + scm_async_rate
- scm_async_clock
;
336 SET_ASYNC_GOT_IT (a
, scm_asyncs_pending_p
= 1);
339 return SCM_UNSPECIFIED
;
344 scm_system_async_mark_from_signal_handler (SCM a
)
346 SET_ASYNC_GOT_IT (a
, scm_asyncs_pending_p
= 1);
349 SCM_DEFINE (scm_run_asyncs
, "run-asyncs", 1, 0, 0,
351 "Execute all thunks from the asyncs of the list @var{list_of_a}.")
352 #define FUNC_NAME s_scm_run_asyncs
354 #ifdef GUILE_OLD_ASYNC_CLICK
358 scm_asyncs_pending_p
= 0;
360 while (! SCM_NULL_OR_NIL_P (list_of_a
))
363 SCM_VALIDATE_CONS (1, list_of_a
);
364 a
= SCM_CAR (list_of_a
);
365 VALIDATE_ASYNC (SCM_ARG1
, a
);
367 if (ASYNC_GOT_IT (a
))
369 SET_ASYNC_GOT_IT (a
, 0);
370 scm_call_0 (ASYNC_THUNK (a
));
373 list_of_a
= SCM_CDR (list_of_a
);
382 SCM_DEFINE (scm_noop
, "noop", 0, 0, 1,
384 "Do nothing. When called without arguments, return @code{#f},\n"
385 "otherwise return the first argument.")
386 #define FUNC_NAME s_scm_noop
388 SCM_VALIDATE_REST_ARGUMENT (args
);
389 return (SCM_NULL_OR_NIL_P (args
) ? SCM_BOOL_F
: SCM_CAR (args
));
396 #ifdef GUILE_OLD_ASYNC_CLICK
398 SCM_DEFINE (scm_set_tick_rate
, "set-tick-rate", 1, 0, 0,
400 "Set the rate of async ticks to @var{n}. Return the old rate\n"
402 #define FUNC_NAME s_scm_set_tick_rate
404 unsigned int old_n
= scm_tick_rate
;
405 SCM_VALIDATE_INUM (1, n
);
406 scm_desired_tick_rate
= SCM_INUM (n
);
407 scm_async_rate
= 1 + scm_async_rate
- scm_async_clock
;
409 return SCM_MAKINUM (old_n
);
416 SCM_DEFINE (scm_set_switch_rate
, "set-switch-rate", 1, 0, 0,
418 "Set the async switch rate to @var{n}. Return the old value\n"
419 "of the switch rate.")
420 #define FUNC_NAME s_scm_set_switch_rate
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
;
427 return SCM_MAKINUM (old_n
);
435 SCM_DEFINE (scm_unmask_signals
, "unmask-signals", 0, 0, 0,
437 "Unmask signals. The returned value is not specified.")
438 #define FUNC_NAME s_scm_unmask_signals
441 return SCM_UNSPECIFIED
;
446 SCM_DEFINE (scm_mask_signals
, "mask-signals", 0, 0, 0,
448 "Mask signals. The returned value is not specified.")
449 #define FUNC_NAME s_scm_mask_signals
452 return SCM_UNSPECIFIED
;
461 scm_asyncs
= SCM_EOL
;
462 tc16_async
= scm_make_smob_type ("async", 0);
463 scm_set_smob_mark (tc16_async
, async_mark
);
465 #ifndef SCM_MAGIC_SNARFER
466 #include "libguile/async.x"