Commit | Line | Data |
---|---|---|
273b7b94 | 1 | /* Copyright (C) 1995, 96, 97, 98, 2000 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. */ |
1bbd0b84 GB |
41 | |
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
0f2d19dd JB |
45 | \f |
46 | ||
47 | #include <stdio.h> | |
48 | #include <signal.h> | |
49 | #include "_scm.h" | |
20e6290e JB |
50 | #include "eval.h" |
51 | #include "throw.h" | |
ba11fd4c | 52 | #include "root.h" |
20e6290e JB |
53 | #include "smob.h" |
54 | ||
b6791b2e | 55 | #include "validate.h" |
20e6290e | 56 | #include "async.h" |
0f2d19dd | 57 | |
95b88819 GH |
58 | #ifdef HAVE_STRING_H |
59 | #include <string.h> | |
60 | #endif | |
0f2d19dd JB |
61 | #ifdef HAVE_UNISTD_H |
62 | #include <unistd.h> | |
63 | #endif | |
64 | ||
65 | ||
66 | \f | |
67 | /* {Asynchronous Events} | |
68 | * | |
69 | * | |
70 | * Async == thunk + mark. | |
71 | * | |
72 | * Setting the mark guarantees future execution of the thunk. More | |
73 | * than one set may be satisfied by a single execution. | |
74 | * | |
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. | |
78 | * | |
79 | * If the clock reaches 0 when scm_mask_ints != 0, then reset the clock | |
80 | * to 1. | |
81 | * | |
82 | * If the clock reaches 0 any other time, run marked asyncs. | |
83 | * | |
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. | |
87 | * | |
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. | |
90 | * | |
91 | */ | |
92 | ||
a574455a GH |
93 | /* True between SCM_DEFER_INTS and SCM_ALLOW_INTS, and |
94 | * when the interpreter is not running at all. | |
95 | */ | |
96 | int scm_ints_disabled = 1; | |
0f2d19dd JB |
97 | |
98 | unsigned int scm_async_clock = 20; | |
99 | static unsigned int scm_async_rate = 20; | |
100 | unsigned int scm_mask_ints = 1; | |
101 | ||
102 | static unsigned int scm_tick_clock = 0; | |
103 | static unsigned int scm_tick_rate = 0; | |
104 | static unsigned int scm_desired_tick_rate = 0; | |
105 | static unsigned int scm_switch_clock = 0; | |
106 | static unsigned int scm_switch_rate = 0; | |
107 | static unsigned int scm_desired_switch_rate = 0; | |
108 | ||
0f2d19dd JB |
109 | static long scm_tc16_async; |
110 | ||
0f2d19dd JB |
111 | \f |
112 | ||
5e569ca8 MD |
113 | int |
114 | scm_asyncs_pending () | |
0f2d19dd JB |
115 | { |
116 | SCM pos; | |
117 | pos = scm_asyncs; | |
118 | while (pos != SCM_EOL) | |
119 | { | |
120 | SCM a; | |
121 | struct scm_async * it; | |
122 | a = SCM_CAR (pos); | |
123 | it = SCM_ASYNC (a); | |
124 | if (it->got_it) | |
125 | return 1; | |
126 | pos = SCM_CDR (pos); | |
127 | } | |
128 | return 0; | |
129 | } | |
130 | ||
9ea54cc6 | 131 | #if 0 |
9ea54cc6 | 132 | static SCM |
1bbd0b84 | 133 | scm_sys_tick_async_thunk (void) |
9ea54cc6 GH |
134 | { |
135 | scm_deliver_signal (SCM_TICK_SIGNAL); | |
136 | return SCM_BOOL_F; | |
137 | } | |
138 | #endif | |
1cc91f1b | 139 | |
0f2d19dd JB |
140 | void |
141 | scm_async_click () | |
0f2d19dd JB |
142 | { |
143 | int owe_switch; | |
144 | int owe_tick; | |
145 | ||
146 | if (!scm_switch_rate) | |
147 | { | |
148 | owe_switch = 0; | |
149 | scm_switch_clock = scm_switch_rate = scm_desired_switch_rate; | |
150 | scm_desired_switch_rate = 0; | |
151 | } | |
152 | else | |
153 | { | |
154 | owe_switch = (scm_async_rate >= scm_switch_clock); | |
155 | if (owe_switch) | |
156 | { | |
157 | if (scm_desired_switch_rate) | |
158 | { | |
159 | scm_switch_clock = scm_switch_rate = scm_desired_switch_rate; | |
160 | scm_desired_switch_rate = 0; | |
161 | } | |
162 | else | |
163 | scm_switch_clock = scm_switch_rate; | |
164 | } | |
165 | else | |
166 | { | |
167 | if (scm_desired_switch_rate) | |
168 | { | |
169 | scm_switch_clock = scm_switch_rate = scm_desired_switch_rate; | |
170 | scm_desired_switch_rate = 0; | |
171 | } | |
172 | else | |
173 | scm_switch_clock -= scm_async_rate; | |
174 | } | |
175 | } | |
176 | ||
177 | if (scm_mask_ints) | |
178 | { | |
179 | if (owe_switch) | |
180 | scm_switch (); | |
181 | scm_async_clock = 1; | |
182 | return;; | |
183 | } | |
184 | ||
185 | if (!scm_tick_rate) | |
186 | { | |
187 | unsigned int r; | |
188 | owe_tick = 0; | |
189 | r = scm_desired_tick_rate; | |
190 | if (r) | |
191 | { | |
192 | scm_desired_tick_rate = 0; | |
193 | scm_tick_rate = r; | |
194 | scm_tick_clock = r; | |
195 | } | |
196 | } | |
197 | else | |
198 | { | |
199 | owe_tick = (scm_async_rate >= scm_tick_clock); | |
200 | if (owe_tick) | |
201 | { | |
202 | scm_tick_clock = scm_tick_rate = scm_desired_tick_rate; | |
203 | scm_desired_tick_rate = 0; | |
204 | } | |
205 | else | |
206 | { | |
207 | if (scm_desired_tick_rate) | |
208 | { | |
209 | scm_tick_clock = scm_tick_rate = scm_desired_tick_rate; | |
210 | scm_desired_tick_rate = 0; | |
211 | } | |
212 | else | |
213 | scm_tick_clock -= scm_async_rate; | |
214 | } | |
215 | } | |
216 | ||
9ea54cc6 GH |
217 | /* |
218 | if (owe_tick) | |
219 | scm_async_mark (system_signal_asyncs[SCM_SIG_ORD(SCM_TICK_SIGNAL)]); | |
220 | */ | |
0f2d19dd JB |
221 | |
222 | SCM_DEFER_INTS; | |
223 | if (scm_tick_rate && scm_switch_rate) | |
224 | { | |
225 | scm_async_rate = min (scm_tick_clock, scm_switch_clock); | |
226 | scm_async_clock = scm_async_rate; | |
227 | } | |
228 | else if (scm_tick_rate) | |
229 | { | |
230 | scm_async_clock = scm_async_rate = scm_tick_clock; | |
231 | } | |
232 | else if (scm_switch_rate) | |
233 | { | |
234 | scm_async_clock = scm_async_rate = scm_switch_clock; | |
235 | } | |
236 | else | |
237 | scm_async_clock = scm_async_rate = 1 << 16; | |
238 | SCM_ALLOW_INTS_ONLY; | |
239 | ||
240 | tail: | |
241 | scm_run_asyncs (scm_asyncs); | |
242 | ||
243 | SCM_DEFER_INTS; | |
5e569ca8 | 244 | if (scm_asyncs_pending ()) |
0f2d19dd JB |
245 | { |
246 | SCM_ALLOW_INTS_ONLY; | |
247 | goto tail; | |
248 | } | |
249 | SCM_ALLOW_INTS; | |
250 | ||
251 | if (owe_switch) | |
252 | scm_switch (); | |
253 | } | |
254 | ||
255 | ||
256 | \f | |
257 | ||
1cc91f1b | 258 | |
0f2d19dd JB |
259 | void |
260 | scm_switch () | |
7ad737b6 MD |
261 | { |
262 | #if 0 /* Thread switching code should probably reside here, but the | |
263 | async switching code doesn't seem to work, so it's put in the | |
264 | SCM_ASYNC_TICK macro instead. /mdj */ | |
265 | SCM_THREAD_SWITCHING_CODE; | |
266 | #endif | |
267 | } | |
0f2d19dd | 268 | |
0f2d19dd JB |
269 | \f |
270 | ||
0f2d19dd | 271 | static SCM |
1bbd0b84 | 272 | mark_async (SCM obj) |
0f2d19dd JB |
273 | { |
274 | struct scm_async * it; | |
0f2d19dd JB |
275 | it = SCM_ASYNC (obj); |
276 | return it->thunk; | |
277 | } | |
278 | ||
0f2d19dd JB |
279 | \f |
280 | ||
3b3b36dd | 281 | SCM_DEFINE (scm_async, "async", 1, 0, 0, |
1bbd0b84 GB |
282 | (SCM thunk), |
283 | "") | |
284 | #define FUNC_NAME s_scm_async | |
0f2d19dd | 285 | { |
273b7b94 | 286 | SCM_RETURN_NEWSMOB2 (scm_tc16_async, 0, thunk); |
0f2d19dd | 287 | } |
1bbd0b84 | 288 | #undef FUNC_NAME |
0f2d19dd | 289 | |
3b3b36dd | 290 | SCM_DEFINE (scm_system_async, "system-async", 1, 0, 0, |
1bbd0b84 GB |
291 | (SCM thunk), |
292 | "") | |
293 | #define FUNC_NAME s_scm_system_async | |
0f2d19dd JB |
294 | { |
295 | SCM it; | |
296 | SCM list; | |
297 | ||
298 | it = scm_async (thunk); | |
273b7b94 MD |
299 | SCM_NEWCELL (list); |
300 | SCM_SETCAR (list, it); | |
301 | SCM_SETCDR (list, scm_asyncs); | |
0f2d19dd | 302 | scm_asyncs = list; |
0f2d19dd JB |
303 | return it; |
304 | } | |
1bbd0b84 | 305 | #undef FUNC_NAME |
0f2d19dd | 306 | |
3b3b36dd | 307 | SCM_DEFINE (scm_async_mark, "async-mark", 1, 0, 0, |
1bbd0b84 GB |
308 | (SCM a), |
309 | "") | |
310 | #define FUNC_NAME s_scm_async_mark | |
0f2d19dd JB |
311 | { |
312 | struct scm_async * it; | |
3b3b36dd | 313 | SCM_VALIDATE_ASYNC_COPY (1,a,it); |
0f2d19dd JB |
314 | it->got_it = 1; |
315 | return SCM_UNSPECIFIED; | |
316 | } | |
1bbd0b84 | 317 | #undef FUNC_NAME |
0f2d19dd JB |
318 | |
319 | ||
3b3b36dd | 320 | SCM_DEFINE (scm_system_async_mark, "system-async-mark", 1, 0, 0, |
1bbd0b84 GB |
321 | (SCM a), |
322 | "") | |
323 | #define FUNC_NAME s_scm_system_async_mark | |
0f2d19dd JB |
324 | { |
325 | struct scm_async * it; | |
3b3b36dd | 326 | SCM_VALIDATE_ASYNC_COPY (1,a,it); |
0f2d19dd JB |
327 | SCM_REDEFER_INTS; |
328 | it->got_it = 1; | |
329 | scm_async_rate = 1 + scm_async_rate - scm_async_clock; | |
330 | scm_async_clock = 1; | |
331 | SCM_REALLOW_INTS; | |
332 | return SCM_UNSPECIFIED; | |
333 | } | |
1bbd0b84 | 334 | #undef FUNC_NAME |
0f2d19dd JB |
335 | |
336 | ||
3b3b36dd | 337 | SCM_DEFINE (scm_run_asyncs, "run-asyncs", 1, 0, 0, |
1bbd0b84 GB |
338 | (SCM list_of_a), |
339 | "") | |
340 | #define FUNC_NAME s_scm_run_asyncs | |
0f2d19dd | 341 | { |
0f2d19dd JB |
342 | if (scm_mask_ints) |
343 | return SCM_BOOL_F; | |
1bbd0b84 | 344 | while (list_of_a != SCM_EOL) |
0f2d19dd JB |
345 | { |
346 | SCM a; | |
347 | struct scm_async * it; | |
3b3b36dd | 348 | SCM_VALIDATE_CONS (1,list_of_a); |
1bbd0b84 | 349 | a = SCM_CAR (list_of_a); |
5d2d2ffc | 350 | SCM_VALIDATE_ASYNC_COPY (SCM_ARG1,a,it); |
0f2d19dd JB |
351 | scm_mask_ints = 1; |
352 | if (it->got_it) | |
353 | { | |
354 | it->got_it = 0; | |
355 | scm_apply (it->thunk, SCM_EOL, SCM_EOL); | |
356 | } | |
357 | scm_mask_ints = 0; | |
1bbd0b84 | 358 | list_of_a = SCM_CDR (list_of_a); |
0f2d19dd JB |
359 | } |
360 | return SCM_BOOL_T; | |
361 | } | |
1bbd0b84 | 362 | #undef FUNC_NAME |
0f2d19dd JB |
363 | |
364 | \f | |
365 | ||
366 | ||
3b3b36dd | 367 | SCM_DEFINE (scm_noop, "noop", 0, 0, 1, |
1bbd0b84 GB |
368 | (SCM args), |
369 | "") | |
370 | #define FUNC_NAME s_scm_noop | |
0f2d19dd | 371 | { |
5d2d2ffc | 372 | return (SCM_NULLP (args) ? SCM_BOOL_F : SCM_CAR (args)); |
0f2d19dd | 373 | } |
1bbd0b84 | 374 | #undef FUNC_NAME |
0f2d19dd JB |
375 | |
376 | ||
377 | \f | |
378 | ||
3b3b36dd | 379 | SCM_DEFINE (scm_set_tick_rate, "set-tick-rate", 1, 0, 0, |
1bbd0b84 GB |
380 | (SCM n), |
381 | "") | |
382 | #define FUNC_NAME s_scm_set_tick_rate | |
0f2d19dd JB |
383 | { |
384 | unsigned int old_n; | |
c209c88e GB |
385 | |
386 | ||
3b3b36dd | 387 | SCM_VALIDATE_INUM (1,n); |
c209c88e | 388 | |
0f2d19dd | 389 | old_n = scm_tick_rate; |
c209c88e GB |
390 | |
391 | ||
f1267706 | 392 | scm_desired_tick_rate = SCM_UNPACK (SCM_INUM (n)); |
0f2d19dd JB |
393 | scm_async_rate = 1 + scm_async_rate - scm_async_clock; |
394 | scm_async_clock = 1; | |
395 | return SCM_MAKINUM (old_n); | |
396 | } | |
1bbd0b84 | 397 | #undef FUNC_NAME |
0f2d19dd JB |
398 | |
399 | \f | |
400 | ||
401 | ||
3b3b36dd | 402 | SCM_DEFINE (scm_set_switch_rate, "set-switch-rate", 1, 0, 0, |
1bbd0b84 GB |
403 | (SCM n), |
404 | "") | |
405 | #define FUNC_NAME s_scm_set_switch_rate | |
0f2d19dd JB |
406 | { |
407 | unsigned int old_n; | |
3b3b36dd | 408 | SCM_VALIDATE_INUM (1,n); |
0f2d19dd | 409 | old_n = scm_switch_rate; |
f1267706 | 410 | scm_desired_switch_rate = SCM_UNPACK (SCM_INUM (n)); |
0f2d19dd JB |
411 | scm_async_rate = 1 + scm_async_rate - scm_async_clock; |
412 | scm_async_clock = 1; | |
413 | return SCM_MAKINUM (old_n); | |
414 | } | |
1bbd0b84 | 415 | #undef FUNC_NAME |
0f2d19dd JB |
416 | |
417 | \f | |
418 | ||
9ea54cc6 GH |
419 | /* points to the GC system-async, so that scm_gc_end can find it. */ |
420 | SCM scm_gc_async; | |
1cc91f1b | 421 | |
9ea54cc6 GH |
422 | /* the vcell for gc-thunk. */ |
423 | static SCM scm_gc_vcell; | |
1cc91f1b | 424 | |
9ea54cc6 GH |
425 | /* the thunk installed in the GC system-async, which is marked at the |
426 | end of garbage collection. */ | |
0f2d19dd | 427 | static SCM |
9ea54cc6 | 428 | scm_sys_gc_async_thunk (void) |
0f2d19dd | 429 | { |
9ea54cc6 GH |
430 | if (SCM_NFALSEP (scm_gc_vcell)) |
431 | { | |
432 | SCM proc = SCM_CDR (scm_gc_vcell); | |
1cc91f1b | 433 | |
9ea54cc6 GH |
434 | if (SCM_NFALSEP (proc) && !SCM_UNBNDP (proc)) |
435 | scm_apply (proc, SCM_EOL, SCM_EOL); | |
436 | } | |
437 | return SCM_UNSPECIFIED; | |
0f2d19dd JB |
438 | } |
439 | ||
0f2d19dd JB |
440 | \f |
441 | ||
3b3b36dd | 442 | SCM_DEFINE (scm_unmask_signals, "unmask-signals", 0, 0, 0, |
1bbd0b84 GB |
443 | (), |
444 | "") | |
445 | #define FUNC_NAME s_scm_unmask_signals | |
0f2d19dd JB |
446 | { |
447 | scm_mask_ints = 0; | |
448 | return SCM_UNSPECIFIED; | |
449 | } | |
1bbd0b84 | 450 | #undef FUNC_NAME |
0f2d19dd JB |
451 | |
452 | ||
3b3b36dd | 453 | SCM_DEFINE (scm_mask_signals, "mask-signals", 0, 0, 0, |
1bbd0b84 GB |
454 | (), |
455 | "") | |
456 | #define FUNC_NAME s_scm_mask_signals | |
0f2d19dd JB |
457 | { |
458 | scm_mask_ints = 1; | |
459 | return SCM_UNSPECIFIED; | |
460 | } | |
1bbd0b84 | 461 | #undef FUNC_NAME |
0f2d19dd JB |
462 | |
463 | \f | |
464 | ||
0f2d19dd JB |
465 | void |
466 | scm_init_async () | |
0f2d19dd JB |
467 | { |
468 | SCM a_thunk; | |
273b7b94 | 469 | scm_tc16_async = scm_make_smob_type_mfpe ("async", 0, |
23a62151 | 470 | mark_async, NULL, NULL, NULL); |
9ea54cc6 | 471 | scm_gc_vcell = scm_sysintern ("gc-thunk", SCM_BOOL_F); |
0f2d19dd | 472 | a_thunk = scm_make_gsubr ("%gc-thunk", 0, 0, 0, scm_sys_gc_async_thunk); |
9ea54cc6 | 473 | scm_gc_async = scm_system_async (a_thunk); |
0f2d19dd | 474 | |
0f2d19dd JB |
475 | #include "async.x" |
476 | } |