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 | ||
1cc91f1b JB |
107 | static int asyncs_pending SCM_P ((void)); |
108 | ||
5e569ca8 MD |
109 | int |
110 | scm_asyncs_pending () | |
0f2d19dd JB |
111 | { |
112 | SCM pos; | |
113 | pos = scm_asyncs; | |
114 | while (pos != SCM_EOL) | |
115 | { | |
116 | SCM a; | |
117 | struct scm_async * it; | |
118 | a = SCM_CAR (pos); | |
119 | it = SCM_ASYNC (a); | |
120 | if (it->got_it) | |
121 | return 1; | |
122 | pos = SCM_CDR (pos); | |
123 | } | |
124 | return 0; | |
125 | } | |
126 | ||
9ea54cc6 GH |
127 | #if 0 |
128 | static SCM scm_sys_tick_async_thunk SCM_P ((void)); | |
129 | static SCM | |
130 | scm_sys_tick_async_thunk () | |
131 | { | |
132 | scm_deliver_signal (SCM_TICK_SIGNAL); | |
133 | return SCM_BOOL_F; | |
134 | } | |
135 | #endif | |
1cc91f1b | 136 | |
0f2d19dd JB |
137 | void |
138 | scm_async_click () | |
0f2d19dd JB |
139 | { |
140 | int owe_switch; | |
141 | int owe_tick; | |
142 | ||
143 | if (!scm_switch_rate) | |
144 | { | |
145 | owe_switch = 0; | |
146 | scm_switch_clock = scm_switch_rate = scm_desired_switch_rate; | |
147 | scm_desired_switch_rate = 0; | |
148 | } | |
149 | else | |
150 | { | |
151 | owe_switch = (scm_async_rate >= scm_switch_clock); | |
152 | if (owe_switch) | |
153 | { | |
154 | if (scm_desired_switch_rate) | |
155 | { | |
156 | scm_switch_clock = scm_switch_rate = scm_desired_switch_rate; | |
157 | scm_desired_switch_rate = 0; | |
158 | } | |
159 | else | |
160 | scm_switch_clock = scm_switch_rate; | |
161 | } | |
162 | else | |
163 | { | |
164 | if (scm_desired_switch_rate) | |
165 | { | |
166 | scm_switch_clock = scm_switch_rate = scm_desired_switch_rate; | |
167 | scm_desired_switch_rate = 0; | |
168 | } | |
169 | else | |
170 | scm_switch_clock -= scm_async_rate; | |
171 | } | |
172 | } | |
173 | ||
174 | if (scm_mask_ints) | |
175 | { | |
176 | if (owe_switch) | |
177 | scm_switch (); | |
178 | scm_async_clock = 1; | |
179 | return;; | |
180 | } | |
181 | ||
182 | if (!scm_tick_rate) | |
183 | { | |
184 | unsigned int r; | |
185 | owe_tick = 0; | |
186 | r = scm_desired_tick_rate; | |
187 | if (r) | |
188 | { | |
189 | scm_desired_tick_rate = 0; | |
190 | scm_tick_rate = r; | |
191 | scm_tick_clock = r; | |
192 | } | |
193 | } | |
194 | else | |
195 | { | |
196 | owe_tick = (scm_async_rate >= scm_tick_clock); | |
197 | if (owe_tick) | |
198 | { | |
199 | scm_tick_clock = scm_tick_rate = scm_desired_tick_rate; | |
200 | scm_desired_tick_rate = 0; | |
201 | } | |
202 | else | |
203 | { | |
204 | if (scm_desired_tick_rate) | |
205 | { | |
206 | scm_tick_clock = scm_tick_rate = scm_desired_tick_rate; | |
207 | scm_desired_tick_rate = 0; | |
208 | } | |
209 | else | |
210 | scm_tick_clock -= scm_async_rate; | |
211 | } | |
212 | } | |
213 | ||
9ea54cc6 GH |
214 | /* |
215 | if (owe_tick) | |
216 | scm_async_mark (system_signal_asyncs[SCM_SIG_ORD(SCM_TICK_SIGNAL)]); | |
217 | */ | |
0f2d19dd JB |
218 | |
219 | SCM_DEFER_INTS; | |
220 | if (scm_tick_rate && scm_switch_rate) | |
221 | { | |
222 | scm_async_rate = min (scm_tick_clock, scm_switch_clock); | |
223 | scm_async_clock = scm_async_rate; | |
224 | } | |
225 | else if (scm_tick_rate) | |
226 | { | |
227 | scm_async_clock = scm_async_rate = scm_tick_clock; | |
228 | } | |
229 | else if (scm_switch_rate) | |
230 | { | |
231 | scm_async_clock = scm_async_rate = scm_switch_clock; | |
232 | } | |
233 | else | |
234 | scm_async_clock = scm_async_rate = 1 << 16; | |
235 | SCM_ALLOW_INTS_ONLY; | |
236 | ||
237 | tail: | |
238 | scm_run_asyncs (scm_asyncs); | |
239 | ||
240 | SCM_DEFER_INTS; | |
5e569ca8 | 241 | if (scm_asyncs_pending ()) |
0f2d19dd JB |
242 | { |
243 | SCM_ALLOW_INTS_ONLY; | |
244 | goto tail; | |
245 | } | |
246 | SCM_ALLOW_INTS; | |
247 | ||
248 | if (owe_switch) | |
249 | scm_switch (); | |
250 | } | |
251 | ||
252 | ||
253 | \f | |
254 | ||
1cc91f1b | 255 | |
0f2d19dd JB |
256 | void |
257 | scm_switch () | |
7ad737b6 MD |
258 | { |
259 | #if 0 /* Thread switching code should probably reside here, but the | |
260 | async switching code doesn't seem to work, so it's put in the | |
261 | SCM_ASYNC_TICK macro instead. /mdj */ | |
262 | SCM_THREAD_SWITCHING_CODE; | |
263 | #endif | |
264 | } | |
0f2d19dd | 265 | |
0f2d19dd JB |
266 | \f |
267 | ||
1cc91f1b JB |
268 | static int print_async SCM_P ((SCM exp, SCM port, scm_print_state *pstate)); |
269 | ||
0f2d19dd | 270 | static int |
9882ea19 | 271 | print_async (exp, port, pstate) |
0f2d19dd JB |
272 | SCM exp; |
273 | SCM port; | |
9882ea19 | 274 | scm_print_state *pstate; |
0f2d19dd | 275 | { |
b7f3516f | 276 | scm_puts ("#<async ", port); |
0f2d19dd | 277 | scm_intprint(exp, 16, port); |
b7f3516f | 278 | scm_putc('>', port); |
0f2d19dd JB |
279 | return 1; |
280 | } | |
281 | ||
1cc91f1b JB |
282 | |
283 | static SCM mark_async SCM_P ((SCM obj)); | |
284 | ||
0f2d19dd JB |
285 | static SCM |
286 | mark_async (obj) | |
287 | SCM obj; | |
0f2d19dd JB |
288 | { |
289 | struct scm_async * it; | |
290 | if (SCM_GC8MARKP (obj)) | |
291 | return SCM_BOOL_F; | |
292 | SCM_SETGC8MARK (obj); | |
293 | it = SCM_ASYNC (obj); | |
294 | return it->thunk; | |
295 | } | |
296 | ||
1cc91f1b JB |
297 | |
298 | static scm_sizet free_async SCM_P ((SCM obj)); | |
299 | ||
0f2d19dd | 300 | static scm_sizet |
1cc91f1b | 301 | free_async (obj) |
0f2d19dd | 302 | SCM obj; |
0f2d19dd JB |
303 | { |
304 | struct scm_async * it; | |
305 | it = SCM_ASYNC (obj); | |
306 | scm_must_free ((char *)it); | |
307 | return (sizeof (*it)); | |
308 | } | |
309 | ||
310 | ||
311 | static scm_smobfuns async_smob = | |
312 | { | |
313 | mark_async, | |
314 | free_async, | |
315 | print_async, | |
316 | 0 | |
317 | }; | |
318 | ||
319 | ||
320 | \f | |
321 | ||
322 | SCM_PROC(s_async, "async", 1, 0, 0, scm_async); | |
1cc91f1b | 323 | |
0f2d19dd JB |
324 | SCM |
325 | scm_async (thunk) | |
326 | SCM thunk; | |
0f2d19dd JB |
327 | { |
328 | SCM it; | |
329 | struct scm_async * async; | |
330 | ||
331 | SCM_NEWCELL (it); | |
332 | SCM_DEFER_INTS; | |
333 | SCM_SETCDR (it, SCM_EOL); | |
334 | async = (struct scm_async *)scm_must_malloc (sizeof (*async), s_async); | |
335 | async->got_it = 0; | |
336 | async->thunk = thunk; | |
337 | SCM_SETCDR (it, (SCM)async); | |
338 | SCM_SETCAR (it, (SCM)scm_tc16_async); | |
339 | SCM_ALLOW_INTS; | |
340 | return it; | |
341 | } | |
342 | ||
343 | SCM_PROC(s_system_async, "system-async", 1, 0, 0, scm_system_async); | |
1cc91f1b | 344 | |
0f2d19dd JB |
345 | SCM |
346 | scm_system_async (thunk) | |
347 | SCM thunk; | |
0f2d19dd JB |
348 | { |
349 | SCM it; | |
350 | SCM list; | |
351 | ||
352 | it = scm_async (thunk); | |
353 | SCM_NEWCELL (list); | |
354 | SCM_DEFER_INTS; | |
355 | SCM_SETCAR (list, it); | |
356 | SCM_SETCDR (list, scm_asyncs); | |
357 | scm_asyncs = list; | |
358 | SCM_ALLOW_INTS; | |
359 | return it; | |
360 | } | |
361 | ||
362 | SCM_PROC(s_async_mark, "async-mark", 1, 0, 0, scm_async_mark); | |
1cc91f1b | 363 | |
0f2d19dd JB |
364 | SCM |
365 | scm_async_mark (a) | |
366 | SCM a; | |
0f2d19dd JB |
367 | { |
368 | struct scm_async * it; | |
369 | SCM_ASSERT (SCM_NIMP (a) && SCM_ASYNCP (a), a, SCM_ARG1, s_async_mark); | |
370 | it = SCM_ASYNC (a); | |
371 | it->got_it = 1; | |
372 | return SCM_UNSPECIFIED; | |
373 | } | |
374 | ||
375 | ||
376 | SCM_PROC(s_system_async_mark, "system-async-mark", 1, 0, 0, scm_system_async_mark); | |
1cc91f1b | 377 | |
0f2d19dd JB |
378 | SCM |
379 | scm_system_async_mark (a) | |
380 | SCM a; | |
0f2d19dd JB |
381 | { |
382 | struct scm_async * it; | |
383 | SCM_ASSERT (SCM_NIMP (a) && SCM_ASYNCP (a), a, SCM_ARG1, s_async_mark); | |
384 | it = SCM_ASYNC (a); | |
385 | SCM_REDEFER_INTS; | |
386 | it->got_it = 1; | |
387 | scm_async_rate = 1 + scm_async_rate - scm_async_clock; | |
388 | scm_async_clock = 1; | |
389 | SCM_REALLOW_INTS; | |
390 | return SCM_UNSPECIFIED; | |
391 | } | |
392 | ||
393 | ||
394 | SCM_PROC(s_run_asyncs, "run-asyncs", 1, 0, 0, scm_run_asyncs); | |
1cc91f1b | 395 | |
0f2d19dd JB |
396 | SCM |
397 | scm_run_asyncs (list_of_a) | |
398 | SCM list_of_a; | |
0f2d19dd JB |
399 | { |
400 | SCM pos; | |
401 | ||
402 | if (scm_mask_ints) | |
403 | return SCM_BOOL_F; | |
404 | pos = list_of_a; | |
405 | while (pos != SCM_EOL) | |
406 | { | |
407 | SCM a; | |
408 | struct scm_async * it; | |
409 | SCM_ASSERT (SCM_NIMP (pos) && SCM_CONSP (pos), pos, SCM_ARG1, s_run_asyncs); | |
410 | a = SCM_CAR (pos); | |
411 | SCM_ASSERT (SCM_NIMP (a) && SCM_ASYNCP (a), a, SCM_ARG1, s_run_asyncs); | |
412 | it = SCM_ASYNC (a); | |
413 | scm_mask_ints = 1; | |
414 | if (it->got_it) | |
415 | { | |
416 | it->got_it = 0; | |
417 | scm_apply (it->thunk, SCM_EOL, SCM_EOL); | |
418 | } | |
419 | scm_mask_ints = 0; | |
420 | pos = SCM_CDR (pos); | |
421 | } | |
422 | return SCM_BOOL_T; | |
423 | } | |
424 | ||
425 | \f | |
426 | ||
427 | ||
428 | SCM_PROC(s_noop, "noop", 0, 0, 1, scm_noop); | |
1cc91f1b | 429 | |
0f2d19dd JB |
430 | SCM |
431 | scm_noop (args) | |
432 | SCM args; | |
0f2d19dd JB |
433 | { |
434 | return (SCM_NULLP (args) | |
435 | ? SCM_BOOL_F | |
436 | : SCM_CAR (args)); | |
437 | } | |
438 | ||
439 | ||
440 | \f | |
441 | ||
442 | SCM_PROC(s_set_tick_rate, "set-tick-rate", 1, 0, 0, scm_set_tick_rate); | |
1cc91f1b | 443 | |
0f2d19dd JB |
444 | SCM |
445 | scm_set_tick_rate (n) | |
446 | SCM n; | |
0f2d19dd JB |
447 | { |
448 | unsigned int old_n; | |
449 | SCM_ASSERT (SCM_INUMP (n), n, SCM_ARG1, s_set_tick_rate); | |
450 | old_n = scm_tick_rate; | |
451 | scm_desired_tick_rate = SCM_INUM (n); | |
452 | scm_async_rate = 1 + scm_async_rate - scm_async_clock; | |
453 | scm_async_clock = 1; | |
454 | return SCM_MAKINUM (old_n); | |
455 | } | |
456 | ||
457 | \f | |
458 | ||
459 | ||
460 | SCM_PROC(s_set_switch_rate, "set-switch-rate", 1, 0, 0, scm_set_switch_rate); | |
1cc91f1b | 461 | |
0f2d19dd JB |
462 | SCM |
463 | scm_set_switch_rate (n) | |
464 | SCM n; | |
0f2d19dd JB |
465 | { |
466 | unsigned int old_n; | |
467 | SCM_ASSERT (SCM_INUMP (n), n, SCM_ARG1, s_set_switch_rate); | |
468 | old_n = scm_switch_rate; | |
469 | scm_desired_switch_rate = SCM_INUM (n); | |
470 | scm_async_rate = 1 + scm_async_rate - scm_async_clock; | |
471 | scm_async_clock = 1; | |
472 | return SCM_MAKINUM (old_n); | |
473 | } | |
474 | ||
475 | \f | |
476 | ||
9ea54cc6 GH |
477 | /* points to the GC system-async, so that scm_gc_end can find it. */ |
478 | SCM scm_gc_async; | |
1cc91f1b | 479 | |
9ea54cc6 GH |
480 | /* the vcell for gc-thunk. */ |
481 | static SCM scm_gc_vcell; | |
1cc91f1b | 482 | |
9ea54cc6 GH |
483 | /* the thunk installed in the GC system-async, which is marked at the |
484 | end of garbage collection. */ | |
0f2d19dd | 485 | static SCM |
9ea54cc6 | 486 | scm_sys_gc_async_thunk (void) |
0f2d19dd | 487 | { |
9ea54cc6 GH |
488 | if (SCM_NFALSEP (scm_gc_vcell)) |
489 | { | |
490 | SCM proc = SCM_CDR (scm_gc_vcell); | |
1cc91f1b | 491 | |
9ea54cc6 GH |
492 | if (SCM_NFALSEP (proc) && !SCM_UNBNDP (proc)) |
493 | scm_apply (proc, SCM_EOL, SCM_EOL); | |
494 | } | |
495 | return SCM_UNSPECIFIED; | |
0f2d19dd JB |
496 | } |
497 | ||
0f2d19dd JB |
498 | \f |
499 | ||
0f2d19dd | 500 | SCM_PROC(s_unmask_signals, "unmask-signals", 0, 0, 0, scm_unmask_signals); |
1cc91f1b | 501 | |
0f2d19dd JB |
502 | SCM |
503 | scm_unmask_signals () | |
0f2d19dd JB |
504 | { |
505 | scm_mask_ints = 0; | |
506 | return SCM_UNSPECIFIED; | |
507 | } | |
508 | ||
509 | ||
510 | SCM_PROC(s_mask_signals, "mask-signals", 0, 0, 0, scm_mask_signals); | |
1cc91f1b | 511 | |
0f2d19dd JB |
512 | SCM |
513 | scm_mask_signals () | |
0f2d19dd JB |
514 | { |
515 | scm_mask_ints = 1; | |
516 | return SCM_UNSPECIFIED; | |
517 | } | |
518 | ||
519 | \f | |
520 | ||
0f2d19dd JB |
521 | void |
522 | scm_init_async () | |
0f2d19dd JB |
523 | { |
524 | SCM a_thunk; | |
525 | scm_tc16_async = scm_newsmob (&async_smob); | |
0f2d19dd | 526 | |
9ea54cc6 | 527 | scm_gc_vcell = scm_sysintern ("gc-thunk", SCM_BOOL_F); |
0f2d19dd | 528 | a_thunk = scm_make_gsubr ("%gc-thunk", 0, 0, 0, scm_sys_gc_async_thunk); |
9ea54cc6 | 529 | scm_gc_async = scm_system_async (a_thunk); |
0f2d19dd | 530 | |
0f2d19dd JB |
531 | #include "async.x" |
532 | } |