event.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /* Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. *
  3. * Authors: Mike Sperber, Robert Ransom
  4. */
  5. #define _WIN32_WINNT 0x0400/* for SetWaitableTimer */
  6. #include <windows.h>
  7. #include <signal.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <errno.h> /* for errno, (ANSI) */
  11. #include "c-mods.h"
  12. #include "scheme48vm.h"
  13. #include "event.h"
  14. static void
  15. set_signal_catcher(int signum, void (*catcher)(int))
  16. {
  17. signal(signum, catcher);
  18. }
  19. static long keyboard_interrupt_count = 0;
  20. static VOID CALLBACK
  21. keyboard_interrupt_callback(DWORD dwParam)
  22. {
  23. keyboard_interrupt_count += 1;
  24. NOTE_EVENT;
  25. }
  26. HANDLE s48_main_thread;
  27. when_keyboard_interrupt(int ign)
  28. {
  29. if (!QueueUserAPC(keyboard_interrupt_callback,
  30. s48_main_thread,
  31. (DWORD) 0))
  32. {
  33. fprintf(stderr, "QueueUserAPC failed\n");
  34. exit(-1);
  35. }
  36. }
  37. static void
  38. start_control_c_interrupts()
  39. {
  40. set_signal_catcher(SIGINT, when_keyboard_interrupt);
  41. }
  42. /* ticks since last timer-interrupt request
  43. (except for some types, identical to Unix code) */
  44. long s48_current_time = 0;
  45. static long alarm_time = -1;
  46. static long poll_time = -1;
  47. static long poll_interval = 5;
  48. static void
  49. when_alarm_interrupt()
  50. {
  51. s48_current_time += 1;
  52. /* fprintf(stderr, "[tick %ld]", s48_current_time); */
  53. if ((alarm_time >= 0 && alarm_time <= s48_current_time) ||
  54. (poll_time >= 0 && poll_time <= s48_current_time))
  55. NOTE_EVENT;
  56. }
  57. #define USEC_PER_POLL (1000000 / POLLS_PER_SECOND)
  58. /* delta is in ticks, 0 cancels current alarm */
  59. long
  60. s48_schedule_alarm_interrupt(long delta)
  61. {
  62. long old;
  63. /* fprintf(stderr, "<scheduling alarm for %ld + %ld>\n", s48_current_time,
  64. delta/TICKS_PER_POLL); */
  65. /* get remaining time */
  66. if (alarm_time == -1)
  67. old = -1;
  68. else
  69. old = (alarm_time - s48_current_time) * TICKS_PER_POLL;
  70. /* decrement poll_time and reset current_time */
  71. if (poll_time != -1)
  72. poll_time -= s48_current_time;
  73. s48_current_time = 0;
  74. /* set alarm_time */
  75. if (delta == 0)
  76. {
  77. NOTE_EVENT;
  78. alarm_time = 0;
  79. }
  80. else
  81. alarm_time = delta / TICKS_PER_POLL;
  82. return old;
  83. }
  84. /* The next two procedures return times in seconds and ticks
  85. (also from Lars Bergstrom's version) */
  86. static DWORD startup_real_time_ticks;
  87. long s48_real_time(long *ticks)
  88. {
  89. DWORD now;
  90. now = GetTickCount();
  91. *ticks = ((now - startup_real_time_ticks) % 1000) * (TICKS_PER_SECOND / 1000);
  92. return (now - startup_real_time_ticks) / 1000;
  93. }
  94. long s48_run_time(long *ticks)
  95. {
  96. FILETIME create, exit, user, kernel;
  97. SYSTEMTIME systime;
  98. HANDLE this_process;
  99. BOOL status;
  100. this_process = GetCurrentProcess();
  101. status = GetProcessTimes(this_process, &create, &exit, &user, &kernel);
  102. status = FileTimeToSystemTime(&user, &systime);
  103. /* go from 100ns to 1ms resolution */
  104. *ticks = systime.wMilliseconds * (TICKS_PER_SECOND / 1000);
  105. return systime.wSecond;
  106. }
  107. static HANDLE alarm_thread = NULL;
  108. DWORD
  109. alarm_thread_func(LPDWORD id)
  110. {
  111. for (;;)
  112. {
  113. Sleep(USEC_PER_POLL / 1000);
  114. when_alarm_interrupt();
  115. }
  116. return 0; /* shouldn't get here */
  117. }
  118. /*
  119. * If it's being called for the first time, create the thread
  120. * If it's being called again, that means that it's been suspended
  121. * and should be resumed.
  122. */
  123. void
  124. s48_start_alarm_interrupts(void)
  125. {
  126. if (alarm_thread == NULL)
  127. {
  128. DWORD alarm_thread_id;
  129. alarm_thread = CreateThread(NULL, /* no security attributes */
  130. 0, /* default stack size */
  131. (LPTHREAD_START_ROUTINE) alarm_thread_func,
  132. NULL, /* argument to thread -- ignored */
  133. 0, /* default creation flags */
  134. &alarm_thread_id);
  135. if (alarm_thread == NULL)
  136. {
  137. fprintf(stderr, "failure creating alarm timer thread\n");
  138. exit(-1);
  139. }
  140. }
  141. else
  142. ResumeThread(alarm_thread);
  143. }
  144. void
  145. s48_stop_alarm_interrupts(void)
  146. {
  147. if (alarm_thread != NULL)
  148. SuspendThread(alarm_thread);
  149. }
  150. /*
  151. * We keep two queues of ports: those that have a pending operation, and
  152. * those whose operation has completed. Periodically, we call select() on
  153. * the pending ports and move any that are ready onto the other queue and
  154. * signal an event.
  155. */
  156. #define FD_QUIESCENT 0 /* idle */
  157. #define FD_READY 1 /* I/O ready to be performed */
  158. #define FD_PENDING 2 /* waiting */
  159. typedef struct fd_struct {
  160. int fd, /* file descriptor */
  161. status; /* one of the FD_* constants */
  162. long os_status; /* characters processed or error code */
  163. psbool has_error;
  164. psbool is_input; /* iff input */
  165. struct fd_struct *next; /* next on same queue */
  166. } fd_struct;
  167. /*
  168. * A queue of fd_structs is empty iff the first field is NULL. In
  169. * that case, lastp points to first.
  170. */
  171. typedef struct fdque
  172. {
  173. fd_struct *first, **lastp;
  174. } fdque;
  175. static fd_struct *fds[FD_SETSIZE];
  176. static fdque ready = {
  177. NULL,
  178. &ready.first
  179. };
  180. /*
  181. * Given a pointer to the link of a fd_struct, and a pointer to
  182. * the queue it is on, remove the entry from the queue.
  183. * The entry removed is returned.
  184. */
  185. static fd_struct *
  186. rmque(fd_struct **link, fdque *que)
  187. {
  188. fd_struct *res;
  189. res = *link;
  190. *link = res->next;
  191. if (res->next == NULL)
  192. que->lastp = link;
  193. return (res);
  194. }
  195. /*
  196. * Find a fd_struct in a queue, and remove it.
  197. */
  198. static void
  199. findrm(fd_struct *entry, fdque *que)
  200. {
  201. fd_struct **fp,
  202. *f;
  203. for (fp = &que->first; (f = *fp) != entry; fp = &f->next)
  204. if (f == NULL) {
  205. fprintf(stderr, "ERROR: findrm fd %d, status %d not on queue.\n",
  206. entry->fd, entry->status);
  207. return;
  208. }
  209. rmque(fp, que);
  210. }
  211. /*
  212. * Add a fd_struct to a queue.
  213. */
  214. static void
  215. addque(fd_struct *entry, fdque *que)
  216. {
  217. *que->lastp = entry;
  218. entry->next = NULL;
  219. que->lastp = &entry->next;
  220. }
  221. static psbool
  222. there_are_ready_ports(void)
  223. {
  224. return (ready.first != NULL);
  225. }
  226. static int
  227. next_ready_port(long* os_status, psbool* has_error)
  228. {
  229. fd_struct *p;
  230. p = rmque(&ready.first, &ready);
  231. p->status = FD_QUIESCENT;
  232. *os_status = p->os_status;
  233. *has_error = p->has_error;
  234. return (p->fd);
  235. }
  236. /*
  237. * Put fd on to the queue of ports with ready operations.
  238. * Return PSTRUE if successful, and PSFALSE otherwise.
  239. */
  240. psbool
  241. s48_add_ready_fd(long fd, psbool is_input, psbool has_error, long os_status)
  242. {
  243. fd_struct* data = fds[fd]; /* we created this before */
  244. data->is_input = is_input;
  245. data->os_status = os_status;
  246. data->has_error = has_error;
  247. if (data->status == FD_READY)
  248. return (PSTRUE); /* fd is already ready */
  249. data->status = FD_READY;
  250. addque(data, &ready);
  251. return PSTRUE;
  252. }
  253. /*
  254. * Add a new fd_struct for fd.
  255. */
  256. static fd_struct *
  257. add_fd(long fd, psbool is_input)
  258. {
  259. struct fd_struct *new;
  260. new = (struct fd_struct *)malloc(sizeof(*new));
  261. if (new != NULL) {
  262. new->fd = fd;
  263. new->status = FD_QUIESCENT;
  264. new->is_input = is_input;
  265. new->next = NULL;
  266. fds[fd] = new;
  267. }
  268. return (new);
  269. }
  270. static fd_struct *
  271. get_or_create_fd_struct(long fd, psbool is_input)
  272. {
  273. if (fds[fd] == NULL)
  274. return add_fd(fd, is_input);
  275. else
  276. return fds[fd];
  277. }
  278. psbool
  279. s48_add_pending_fd(int fd, psbool is_input)
  280. {
  281. fd_struct* data = get_or_create_fd_struct(fd, is_input);
  282. if (data)
  283. {
  284. if (data->status != FD_PENDING)
  285. {
  286. data->status = FD_PENDING;
  287. if (poll_time == -1)
  288. poll_time = s48_current_time + poll_interval;
  289. }
  290. return PSTRUE;
  291. }
  292. else
  293. return PSFALSE;
  294. }
  295. psbool
  296. s48_is_pending(long fd)
  297. {
  298. return (fds[fd] != NULL) && (fds[fd]->status == FD_PENDING);
  299. }
  300. /*
  301. * Remove fd from any queues it is on. Returns true if the FD was on a queue
  302. * and false if it wasn't.
  303. */
  304. psbool
  305. s48_remove_fd(int fd)
  306. {
  307. struct fd_struct *data;
  308. if (! (0 <= fd && fd < FD_SETSIZE)) {
  309. fprintf(stderr, "ERROR: s48_remove_fd fd %d not in [0, %d)\n",
  310. fd,
  311. FD_SETSIZE);
  312. return PSFALSE;
  313. }
  314. data = fds[fd];
  315. if (data == NULL)
  316. return PSFALSE;
  317. if (data->status == FD_PENDING) {
  318. /* the callback will see this and no-op */
  319. data->status = FD_QUIESCENT;
  320. /*#### if (pending.first == NULL)
  321. poll_time = -1; */
  322. } else if (data->status == FD_READY)
  323. findrm(data, &ready);
  324. free((void *)data);
  325. fds[fd] = NULL;
  326. return TRUE;
  327. }
  328. HANDLE
  329. s48_create_mutex_semaphore()
  330. {
  331. HANDLE handle = CreateSemaphore(NULL, /* lpSemaphoreAttributes */
  332. 0, /* lInitialCount */
  333. 1, /* lMaximumCount */
  334. NULL); /* lpName */
  335. if (handle == NULL)
  336. {
  337. fprintf(stderr, "error in CreateSemaphore\n");
  338. exit(-1);
  339. }
  340. return handle;
  341. }
  342. static HANDLE external_event_mutex;
  343. #define LOCK_EXTERNAL_EVENTS WaitForSingleObject(external_event_mutex, INFINITE)
  344. #define UNLOCK_EXTERNAL_EVENTS ReleaseSemaphore(external_event_mutex, 1, NULL)
  345. long
  346. s48_dequeue_external_event(char* readyp)
  347. {
  348. long retval;
  349. LOCK_EXTERNAL_EVENTS;
  350. retval = s48_dequeue_external_eventBUunsafe(readyp);
  351. UNLOCK_EXTERNAL_EVENTS;
  352. return retval;
  353. }
  354. static char
  355. external_event_pending()
  356. {
  357. char retval;
  358. LOCK_EXTERNAL_EVENTS;
  359. retval = s48_external_event_pendingPUunsafe();
  360. UNLOCK_EXTERNAL_EVENTS;
  361. return retval;
  362. }
  363. /* no side effect */
  364. static char
  365. external_event_ready()
  366. {
  367. char retval;
  368. LOCK_EXTERNAL_EVENTS;
  369. retval = s48_external_event_readyPUunsafe();
  370. UNLOCK_EXTERNAL_EVENTS;
  371. return retval;
  372. }
  373. VOID CALLBACK
  374. s48_when_external_event_interrupt(DWORD dwParam)
  375. {
  376. /* do nothing, except possibly interrupt the running SleepEx */
  377. }
  378. void
  379. s48_note_external_event(long uid)
  380. {
  381. LOCK_EXTERNAL_EVENTS;
  382. s48_note_external_eventBUunsafe(uid);
  383. UNLOCK_EXTERNAL_EVENTS;
  384. NOTE_EVENT;
  385. if (!QueueUserAPC(s48_when_external_event_interrupt,
  386. s48_main_thread,
  387. (DWORD) 0))
  388. {
  389. fprintf(stderr, "QueueUserAPC failed\n");
  390. exit(-1);
  391. }
  392. }
  393. /*
  394. * ; Scheme version of the get-next-event procedure
  395. * ;
  396. * ; 1. If there has been a keyboard interrupt, return it.
  397. * ; 2. Check for ready ports if enough time has passed since the last check.
  398. * ; 3. If there is a ready port, return it.
  399. * ; 4. If an alarm is due, return it.
  400. * ; 5. If no events are pending, clear the event flags.
  401. * (define (get-next-event)
  402. * (cond ((> *keyboard-interrupt-count* 0)
  403. * (without-interrupts
  404. * (lambda ()
  405. * (set! *keyboard-interrupt-count*
  406. * (- *keyboard-interrupt-count* 1))))
  407. * (values (enum event-type keyboard-interrupt) #f #f))
  408. * (else
  409. * (cond ((>= *current_time* *poll-time*)
  410. * (queue-ready-ports)
  411. * (set! *poll-time* (+ *time* *poll-interval*))))
  412. * (cond ((not (queue-empty? ready-ports))
  413. * (values (enum event-type i/o-completion)
  414. * (dequeue! ready-ports)))
  415. * ((>= *current_time* *alarm-time*)
  416. * (set! *alarm-time* max-integer)
  417. * (values (enum event-type alarm-interrupt) #f))
  418. * (else
  419. * (without-interrupts
  420. * (lambda ()
  421. * (if (and (= *keyboard-interrupt-count* 0)
  422. * (> *alarm-time* *current_time*)
  423. * (> *poll-time* *current_time*))
  424. * (set! *pending-event?* #f))))
  425. * (values (enum event-type no-event) #f))))))
  426. */
  427. int
  428. s48_get_next_event(long *ready_fd, long *status)
  429. {
  430. /* extern int s48_os_signal_pending(void); */
  431. /*
  432. fprintf(stderr, "[poll at %d (waiting for %d)]\n", s48_current_time, alarm_time);
  433. */
  434. if (keyboard_interrupt_count > 0) {
  435. --keyboard_interrupt_count;
  436. /* fprintf(stderr, "[keyboard interrupt]\n"); */
  437. return (KEYBOARD_INTERRUPT_EVENT);
  438. }
  439. if (poll_time != -1 && s48_current_time >= poll_time) {
  440. SleepEx(0, TRUE);
  441. poll_time = s48_current_time + poll_interval;
  442. }
  443. if (there_are_ready_ports()) {
  444. psbool has_error;
  445. *ready_fd = next_ready_port(status, &has_error);
  446. if (has_error)
  447. {
  448. /* fprintf(stderr, "[i/o error on port %ld, status %ld]\n", *ready_fd, *status); */
  449. return (IO_ERROR_EVENT);
  450. }
  451. else
  452. {
  453. /* fprintf(stderr, "[i/o completion on port %ld, status %ld]\n", *ready_fd, *status); */
  454. return (IO_COMPLETION_EVENT);
  455. }
  456. }
  457. if (alarm_time != -1 && s48_current_time >= alarm_time) {
  458. alarm_time = -1;
  459. /* fprintf(stderr, "[alarm %ld]\n", s48_current_time); */
  460. return (ALARM_EVENT);
  461. }
  462. /*
  463. if (s48_os_signal_pending())
  464. return (OS_SIGNAL_EVENT);
  465. */
  466. if (external_event_pending())
  467. return (EXTERNAL_EVENT);
  468. if ((keyboard_interrupt_count == 0)
  469. && (alarm_time == -1 || s48_current_time < alarm_time)
  470. && (poll_time == -1 || s48_current_time < poll_time))
  471. s48_Spending_eventsPS = PSFALSE;
  472. return (NO_EVENT);
  473. }
  474. int
  475. s48_wait_for_event(long max_wait, psbool is_minutes)
  476. {
  477. /* fprintf(stderr, "[waiting]\n"); */
  478. DWORD dwMilliseconds;
  479. s48_stop_alarm_interrupts();
  480. if (max_wait == -1)
  481. dwMilliseconds = INFINITE;
  482. else if (is_minutes)
  483. dwMilliseconds = max_wait * 60 * 1000;
  484. else
  485. dwMilliseconds = max_wait * (1000 / TICKS_PER_SECOND);
  486. SleepEx(dwMilliseconds,
  487. TRUE);
  488. if (there_are_ready_ports()
  489. || external_event_ready())
  490. NOTE_EVENT;
  491. s48_start_alarm_interrupts();
  492. return NO_ERRORS;
  493. }
  494. void
  495. s48_sysdep_init(void)
  496. {
  497. startup_real_time_ticks = GetTickCount();
  498. /* Yes, this is the official hoopla to get at an absolute handle for
  499. the current thread. GetCurrentThread() returns a *constant*. */
  500. if (!DuplicateHandle(GetCurrentProcess(),
  501. GetCurrentThread(), GetCurrentProcess(),
  502. &s48_main_thread,
  503. THREAD_ALL_ACCESS, FALSE, 0))
  504. {
  505. fprintf(stderr, "DuplicateHandle failed\n");
  506. exit(-1);
  507. }
  508. external_event_mutex = s48_create_mutex_semaphore();
  509. UNLOCK_EXTERNAL_EVENTS;
  510. start_control_c_interrupts();
  511. s48_start_alarm_interrupts();
  512. {
  513. extern void s48_fd_io_init();
  514. s48_fd_io_init();
  515. }
  516. }