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