| 1 | /* Copyright (C) 1995, 96, 97, 98, 2000 Free Software Foundation, Inc. |
| 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 |
| 15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 16 | * Boston, MA 02111-1307 USA |
| 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. |
| 40 | * If you do not wish that, delete this exception notice. */ |
| 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 | |
| 45 | \f |
| 46 | |
| 47 | #include <stdio.h> |
| 48 | #include <signal.h> |
| 49 | #include "_scm.h" |
| 50 | #include "eval.h" |
| 51 | #include "throw.h" |
| 52 | #include "root.h" |
| 53 | #include "smob.h" |
| 54 | |
| 55 | #include "validate.h" |
| 56 | #include "async.h" |
| 57 | |
| 58 | #ifdef HAVE_STRING_H |
| 59 | #include <string.h> |
| 60 | #endif |
| 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 | |
| 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; |
| 97 | unsigned int scm_mask_ints = 1; |
| 98 | |
| 99 | #ifdef GUILE_OLD_ASYNC_CLICK |
| 100 | unsigned int scm_async_clock = 20; |
| 101 | static unsigned int scm_async_rate = 20; |
| 102 | |
| 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; |
| 109 | #else |
| 110 | int scm_asyncs_pending_p = 0; |
| 111 | #endif |
| 112 | |
| 113 | static long scm_tc16_async; |
| 114 | |
| 115 | \f |
| 116 | |
| 117 | #ifdef GUILE_OLD_ASYNC_CLICK |
| 118 | int |
| 119 | scm_asyncs_pending () |
| 120 | { |
| 121 | SCM pos; |
| 122 | pos = scm_asyncs; |
| 123 | while (pos != SCM_EOL) |
| 124 | { |
| 125 | SCM a; |
| 126 | struct scm_async * it; |
| 127 | a = SCM_CAR (pos); |
| 128 | it = SCM_ASYNC (a); |
| 129 | if (it->got_it) |
| 130 | return 1; |
| 131 | pos = SCM_CDR (pos); |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static SCM |
| 137 | scm_sys_tick_async_thunk (void) |
| 138 | { |
| 139 | scm_deliver_signal (SCM_TICK_SIGNAL); |
| 140 | return SCM_BOOL_F; |
| 141 | } |
| 142 | |
| 143 | void |
| 144 | scm_async_click () |
| 145 | { |
| 146 | int owe_switch; |
| 147 | int owe_tick; |
| 148 | |
| 149 | if (!scm_switch_rate) |
| 150 | { |
| 151 | owe_switch = 0; |
| 152 | scm_switch_clock = scm_switch_rate = scm_desired_switch_rate; |
| 153 | scm_desired_switch_rate = 0; |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | owe_switch = (scm_async_rate >= scm_switch_clock); |
| 158 | if (owe_switch) |
| 159 | { |
| 160 | if (scm_desired_switch_rate) |
| 161 | { |
| 162 | scm_switch_clock = scm_switch_rate = scm_desired_switch_rate; |
| 163 | scm_desired_switch_rate = 0; |
| 164 | } |
| 165 | else |
| 166 | scm_switch_clock = scm_switch_rate; |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | if (scm_desired_switch_rate) |
| 171 | { |
| 172 | scm_switch_clock = scm_switch_rate = scm_desired_switch_rate; |
| 173 | scm_desired_switch_rate = 0; |
| 174 | } |
| 175 | else |
| 176 | scm_switch_clock -= scm_async_rate; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (scm_mask_ints) |
| 181 | { |
| 182 | if (owe_switch) |
| 183 | scm_switch (); |
| 184 | scm_async_clock = 1; |
| 185 | return;; |
| 186 | } |
| 187 | |
| 188 | if (!scm_tick_rate) |
| 189 | { |
| 190 | unsigned int r; |
| 191 | owe_tick = 0; |
| 192 | r = scm_desired_tick_rate; |
| 193 | if (r) |
| 194 | { |
| 195 | scm_desired_tick_rate = 0; |
| 196 | scm_tick_rate = r; |
| 197 | scm_tick_clock = r; |
| 198 | } |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | owe_tick = (scm_async_rate >= scm_tick_clock); |
| 203 | if (owe_tick) |
| 204 | { |
| 205 | scm_tick_clock = scm_tick_rate = scm_desired_tick_rate; |
| 206 | scm_desired_tick_rate = 0; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | if (scm_desired_tick_rate) |
| 211 | { |
| 212 | scm_tick_clock = scm_tick_rate = scm_desired_tick_rate; |
| 213 | scm_desired_tick_rate = 0; |
| 214 | } |
| 215 | else |
| 216 | scm_tick_clock -= scm_async_rate; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | if (owe_tick) |
| 222 | scm_async_mark (system_signal_asyncs[SCM_SIG_ORD(SCM_TICK_SIGNAL)]); |
| 223 | */ |
| 224 | |
| 225 | SCM_DEFER_INTS; |
| 226 | if (scm_tick_rate && scm_switch_rate) |
| 227 | { |
| 228 | scm_async_rate = min (scm_tick_clock, scm_switch_clock); |
| 229 | scm_async_clock = scm_async_rate; |
| 230 | } |
| 231 | else if (scm_tick_rate) |
| 232 | { |
| 233 | scm_async_clock = scm_async_rate = scm_tick_clock; |
| 234 | } |
| 235 | else if (scm_switch_rate) |
| 236 | { |
| 237 | scm_async_clock = scm_async_rate = scm_switch_clock; |
| 238 | } |
| 239 | else |
| 240 | scm_async_clock = scm_async_rate = 1 << 16; |
| 241 | SCM_ALLOW_INTS_ONLY; |
| 242 | |
| 243 | tail: |
| 244 | scm_run_asyncs (scm_asyncs); |
| 245 | |
| 246 | SCM_DEFER_INTS; |
| 247 | if (scm_asyncs_pending ()) |
| 248 | { |
| 249 | SCM_ALLOW_INTS_ONLY; |
| 250 | goto tail; |
| 251 | } |
| 252 | SCM_ALLOW_INTS; |
| 253 | |
| 254 | if (owe_switch) |
| 255 | scm_switch (); |
| 256 | } |
| 257 | |
| 258 | #else |
| 259 | |
| 260 | void |
| 261 | scm_async_click () |
| 262 | { |
| 263 | if (!scm_mask_ints) |
| 264 | do |
| 265 | scm_run_asyncs (scm_asyncs); |
| 266 | while (scm_asyncs_pending_p); |
| 267 | } |
| 268 | #endif |
| 269 | |
| 270 | \f |
| 271 | |
| 272 | |
| 273 | #if 0 /* Thread switching code should probably reside here, but the |
| 274 | async switching code doesn't seem to work, so it's put in the |
| 275 | SCM_DEFER_INTS macro instead. /mdj */ |
| 276 | void |
| 277 | scm_switch () |
| 278 | { |
| 279 | SCM_THREAD_SWITCHING_CODE; |
| 280 | } |
| 281 | #endif |
| 282 | |
| 283 | \f |
| 284 | |
| 285 | static SCM |
| 286 | mark_async (SCM obj) |
| 287 | { |
| 288 | struct scm_async * it; |
| 289 | it = SCM_ASYNC (obj); |
| 290 | return it->thunk; |
| 291 | } |
| 292 | |
| 293 | \f |
| 294 | |
| 295 | SCM_DEFINE (scm_async, "async", 1, 0, 0, |
| 296 | (SCM thunk), |
| 297 | "") |
| 298 | #define FUNC_NAME s_scm_async |
| 299 | { |
| 300 | SCM_RETURN_NEWSMOB2 (scm_tc16_async, 0, thunk); |
| 301 | } |
| 302 | #undef FUNC_NAME |
| 303 | |
| 304 | SCM_DEFINE (scm_system_async, "system-async", 1, 0, 0, |
| 305 | (SCM thunk), |
| 306 | "") |
| 307 | #define FUNC_NAME s_scm_system_async |
| 308 | { |
| 309 | SCM it; |
| 310 | SCM list; |
| 311 | |
| 312 | it = scm_async (thunk); |
| 313 | SCM_NEWCELL (list); |
| 314 | SCM_SETCAR (list, it); |
| 315 | SCM_SETCDR (list, scm_asyncs); |
| 316 | scm_asyncs = list; |
| 317 | return it; |
| 318 | } |
| 319 | #undef FUNC_NAME |
| 320 | |
| 321 | SCM_DEFINE (scm_async_mark, "async-mark", 1, 0, 0, |
| 322 | (SCM a), |
| 323 | "") |
| 324 | #define FUNC_NAME s_scm_async_mark |
| 325 | { |
| 326 | struct scm_async * it; |
| 327 | SCM_VALIDATE_ASYNC_COPY (1,a,it); |
| 328 | #ifdef GUILE_OLD_ASYNC_CLICK |
| 329 | it->got_it = 1; |
| 330 | #else |
| 331 | scm_asyncs_pending_p = it->got_it = 1; |
| 332 | #endif |
| 333 | return SCM_UNSPECIFIED; |
| 334 | } |
| 335 | #undef FUNC_NAME |
| 336 | |
| 337 | |
| 338 | SCM_DEFINE (scm_system_async_mark, "system-async-mark", 1, 0, 0, |
| 339 | (SCM a), |
| 340 | "") |
| 341 | #define FUNC_NAME s_scm_system_async_mark |
| 342 | { |
| 343 | struct scm_async * it; |
| 344 | SCM_VALIDATE_ASYNC_COPY (1,a,it); |
| 345 | SCM_REDEFER_INTS; |
| 346 | #ifdef GUILE_OLD_ASYNC_CLICK |
| 347 | it->got_it = 1; |
| 348 | scm_async_rate = 1 + scm_async_rate - scm_async_clock; |
| 349 | scm_async_clock = 1; |
| 350 | #else |
| 351 | scm_asyncs_pending_p = it->got_it = 1; |
| 352 | #endif |
| 353 | SCM_REALLOW_INTS; |
| 354 | return SCM_UNSPECIFIED; |
| 355 | } |
| 356 | #undef FUNC_NAME |
| 357 | |
| 358 | |
| 359 | SCM_DEFINE (scm_run_asyncs, "run-asyncs", 1, 0, 0, |
| 360 | (SCM list_of_a), |
| 361 | "") |
| 362 | #define FUNC_NAME s_scm_run_asyncs |
| 363 | { |
| 364 | #ifdef GUILE_OLD_ASYNC_CLICK |
| 365 | if (scm_mask_ints) |
| 366 | return SCM_BOOL_F; |
| 367 | #endif |
| 368 | scm_asyncs_pending_p = 0; |
| 369 | while (list_of_a != SCM_EOL) |
| 370 | { |
| 371 | SCM a; |
| 372 | struct scm_async * it; |
| 373 | SCM_VALIDATE_CONS (1, list_of_a); |
| 374 | a = SCM_CAR (list_of_a); |
| 375 | SCM_VALIDATE_ASYNC_COPY (SCM_ARG1,a,it); |
| 376 | scm_mask_ints = 1; |
| 377 | if (it->got_it) |
| 378 | { |
| 379 | it->got_it = 0; |
| 380 | scm_apply (it->thunk, SCM_EOL, SCM_EOL); |
| 381 | } |
| 382 | scm_mask_ints = 0; |
| 383 | list_of_a = SCM_CDR (list_of_a); |
| 384 | } |
| 385 | return SCM_BOOL_T; |
| 386 | } |
| 387 | #undef FUNC_NAME |
| 388 | |
| 389 | \f |
| 390 | |
| 391 | |
| 392 | SCM_DEFINE (scm_noop, "noop", 0, 0, 1, |
| 393 | (SCM args), |
| 394 | "") |
| 395 | #define FUNC_NAME s_scm_noop |
| 396 | { |
| 397 | return (SCM_NULLP (args) ? SCM_BOOL_F : SCM_CAR (args)); |
| 398 | } |
| 399 | #undef FUNC_NAME |
| 400 | |
| 401 | |
| 402 | \f |
| 403 | |
| 404 | #ifdef GUILE_OLD_ASYNC_CLICK |
| 405 | |
| 406 | SCM_DEFINE (scm_set_tick_rate, "set-tick-rate", 1, 0, 0, |
| 407 | (SCM n), |
| 408 | "") |
| 409 | #define FUNC_NAME s_scm_set_tick_rate |
| 410 | { |
| 411 | unsigned int old_n = scm_tick_rate; |
| 412 | SCM_VALIDATE_INUM (1, n); |
| 413 | scm_desired_tick_rate = SCM_INUM (n); |
| 414 | scm_async_rate = 1 + scm_async_rate - scm_async_clock; |
| 415 | scm_async_clock = 1; |
| 416 | return SCM_MAKINUM (old_n); |
| 417 | } |
| 418 | #undef FUNC_NAME |
| 419 | |
| 420 | \f |
| 421 | |
| 422 | |
| 423 | SCM_DEFINE (scm_set_switch_rate, "set-switch-rate", 1, 0, 0, |
| 424 | (SCM n), |
| 425 | "") |
| 426 | #define FUNC_NAME s_scm_set_switch_rate |
| 427 | { |
| 428 | unsigned int old_n = scm_switch_rate; |
| 429 | SCM_VALIDATE_INUM (1, n); |
| 430 | scm_desired_switch_rate = SCM_INUM (n); |
| 431 | scm_async_rate = 1 + scm_async_rate - scm_async_clock; |
| 432 | scm_async_clock = 1; |
| 433 | return SCM_MAKINUM (old_n); |
| 434 | } |
| 435 | #undef FUNC_NAME |
| 436 | |
| 437 | #endif |
| 438 | \f |
| 439 | |
| 440 | /* points to the GC system-async, so that scm_gc_end can find it. */ |
| 441 | SCM scm_gc_async; |
| 442 | |
| 443 | /* the vcell for gc-thunk. */ |
| 444 | static SCM scm_gc_vcell; |
| 445 | |
| 446 | /* the thunk installed in the GC system-async, which is marked at the |
| 447 | end of garbage collection. */ |
| 448 | static SCM |
| 449 | scm_sys_gc_async_thunk (void) |
| 450 | { |
| 451 | if (SCM_NFALSEP (scm_gc_vcell)) |
| 452 | { |
| 453 | SCM proc = SCM_CDR (scm_gc_vcell); |
| 454 | |
| 455 | if (SCM_NFALSEP (proc) && !SCM_UNBNDP (proc)) |
| 456 | scm_apply (proc, SCM_EOL, SCM_EOL); |
| 457 | } |
| 458 | return SCM_UNSPECIFIED; |
| 459 | } |
| 460 | |
| 461 | \f |
| 462 | |
| 463 | SCM_DEFINE (scm_unmask_signals, "unmask-signals", 0, 0, 0, |
| 464 | (), |
| 465 | "") |
| 466 | #define FUNC_NAME s_scm_unmask_signals |
| 467 | { |
| 468 | scm_mask_ints = 0; |
| 469 | return SCM_UNSPECIFIED; |
| 470 | } |
| 471 | #undef FUNC_NAME |
| 472 | |
| 473 | |
| 474 | SCM_DEFINE (scm_mask_signals, "mask-signals", 0, 0, 0, |
| 475 | (), |
| 476 | "") |
| 477 | #define FUNC_NAME s_scm_mask_signals |
| 478 | { |
| 479 | scm_mask_ints = 1; |
| 480 | return SCM_UNSPECIFIED; |
| 481 | } |
| 482 | #undef FUNC_NAME |
| 483 | |
| 484 | \f |
| 485 | |
| 486 | void |
| 487 | scm_init_async () |
| 488 | { |
| 489 | SCM a_thunk; |
| 490 | scm_tc16_async = scm_make_smob_type_mfpe ("async", 0, |
| 491 | mark_async, NULL, NULL, NULL); |
| 492 | scm_gc_vcell = scm_sysintern ("gc-thunk", SCM_BOOL_F); |
| 493 | a_thunk = scm_make_gsubr ("%gc-thunk", 0, 0, 0, scm_sys_gc_async_thunk); |
| 494 | scm_gc_async = scm_system_async (a_thunk); |
| 495 | |
| 496 | #include "async.x" |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | Local Variables: |
| 501 | c-file-style: "gnu" |
| 502 | End: |
| 503 | */ |