File: event.c

package info (click to toggle)
scheme48 1.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 14,980 kB
  • ctags: 14,127
  • sloc: lisp: 76,272; ansic: 71,514; sh: 3,026; makefile: 637
file content (827 lines) | stat: -rw-r--r-- 19,679 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/* Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees.
   See file COPYING. */

#include <signal.h> /* for sigaction(), pthread_sigmask() / sigprocmask() (POSIX.1) */
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/times.h>
#include <errno.h>              /* for errno, (POSIX?/ANSI) */
#include <string.h>		/* FD_ZERO sometimes needs this */
#include "sysdep.h"
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#include "c-mods.h"
#include "scheme48vm.h"
#include "event.h"

/* turning interrupts and I/O readiness into events */

static sigset_t interrupt_mask;

/*
 * They're basically the same, but the behavior of sigprocmask is
 * undefined in the presence of Pthreads.
 */
#ifdef HAVE_PTHREAD_H
#define SIGMASK pthread_sigmask
#else
/* sigprocmask can be interrupted, while pthread_sigmask cannot */
static int
our_sigmask(int how, const sigset_t *set, sigset_t *oset)
{
  int retval;
  while ((retval = sigprocmask(how, set, oset))
	 && (errno == EINTR))
    ;
  return retval;
}
#define SIGMASK our_sigmask
#endif

static void
block_keyboard_n_alarm_interrupts(void)
{
  if (SIGMASK(SIG_BLOCK, &interrupt_mask, NULL))
    {
      fprintf(stderr,
	      "Failed to block SIGINT/SIGALRM, errno = %d\n",
	      errno);
      exit(1);
    }
}

static void
allow_keyboard_n_alarm_interrupts(void)
{
  if (SIGMASK(SIG_UNBLOCK, &interrupt_mask, NULL))
    {
      fprintf(stderr,
	      "Failed to unblock SIGINT/SIGALRM, errno = %d\n",
	      errno);
      exit(1);
    }
}

/*
 * Unless a signal is being ignored, set up the handler.
 * If we return PSFALSE, something went wrong and errno is set to what.
 */

psbool
s48_setcatcher(int signum, void (*catcher)(int))
{
  struct sigaction	sa;

  if (sigaction(signum, (struct sigaction *)NULL, &sa) != 0)
    return (PSFALSE);
  if (sa.sa_handler == SIG_IGN)
    return (PSTRUE);
  sa.sa_handler = catcher;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  if (sigaction(signum, &sa, (struct sigaction *)NULL) != 0)
    return (PSFALSE);
  return (PSTRUE);
}

static long	keyboard_interrupt_count = 0;

void
s48_when_keyboard_interrupt(int ign)
{
  keyboard_interrupt_count += 1;
  NOTE_EVENT;
  return;
}

/*
   We turn off SIGPIPE interrupts by installing a handler that does nothing.
   Turning them off affects exec()'ed programs, so we don't want to do that.
   Any actual pipe problems are caught when we try to read or write to them.

   We thank Olin Shivers for this hack.
*/

static void
when_sigpipe_interrupt(int ign)
{
  return;
}

/* ticks since last timer-interrupt request */
long		s48_current_time = 0;

static long	alarm_time = -1;
static long	poll_time = -1;
static long	poll_interval = 5;

void
s48_when_alarm_interrupt(int ign)
{
  s48_current_time += 1;
  /* fprintf(stderr, "[tick]"); */

  if ((alarm_time >= 0 && alarm_time <= s48_current_time) ||
      (poll_time >= 0 && poll_time <= s48_current_time)) {
    NOTE_EVENT;
  };
  return;
}

#define USEC_PER_POLL   (1000000 / POLLS_PER_SECOND)

/* delta is in ticks, 0 cancels current alarm */

long
s48_schedule_alarm_interrupt(long delta)
{
  long old;

  /*
  fprintf(stderr, "<scheduling alarm for %ld + %ld>\n", s48_current_time,
	  delta/TICKS_PER_POLL); */

  /* get remaining time */
  if (alarm_time == -1)
    old = -1;
  else
    old = (alarm_time - s48_current_time) * TICKS_PER_POLL;

  /* decrement poll_time and reset current_time */
  if (poll_time != -1)
    poll_time -= s48_current_time;
  s48_current_time = 0;

  /* set alarm_time */
  if (delta == 0) {
    NOTE_EVENT;
    alarm_time = 0; }
  else
    alarm_time = delta / TICKS_PER_POLL;

  return old;
}

/* The next two procedures return times in seconds and ticks */

long
s48_real_time(long *ticks)
{
  struct timeval tv;
  static struct timeval tv_orig;
  static int initp = PSFALSE;
  if (!initp) {
    gettimeofday(&tv_orig, NULL);
    initp = PSTRUE;
  };
  gettimeofday(&tv, NULL);
  *ticks = (tv.tv_usec - tv_orig.tv_usec)/(1000000/TICKS_PER_SECOND);
  return tv.tv_sec - tv_orig.tv_sec;
}

long
s48_run_time(long *ticks)
{
  struct tms time_buffer;
  static long clock_tick = 0;
  long cpu_time;

  if (clock_tick == 0)
    clock_tick = sysconf(_SC_CLK_TCK); /* POSIX.1, POSIX.2 */
  times(&time_buffer);		/* On Sun, getrusage() would be better */

  cpu_time = time_buffer.tms_utime + time_buffer.tms_stime;

  *ticks = (cpu_time % clock_tick) * TICKS_PER_SECOND / clock_tick;
  return cpu_time / clock_tick;
}

void
s48_start_alarm_interrupts(void)
{
  struct itimerval newv, old;

  newv.it_value.tv_sec = 0;
  newv.it_value.tv_usec = USEC_PER_POLL;
  newv.it_interval.tv_sec = 0;
  newv.it_interval.tv_usec = USEC_PER_POLL;
  if (0 != setitimer(ITIMER_REAL, &newv, &old)) {
    perror("setitimer");
    exit(-1); }
}

void
s48_stop_alarm_interrupts(void)
{
  struct itimerval newv, old;

  newv.it_value.tv_sec = 0;
  newv.it_value.tv_usec = 0;
  newv.it_interval.tv_sec = 0;
  newv.it_interval.tv_usec = 0;
  if (0 != setitimer(ITIMER_REAL, &newv, &old)) {
    perror("setitimer");
    exit(-1); }
}

/*
 * We ensure single-threadedness by sending a signal to the main
 * thread, and doing everthing critical there.  This is all probably
 * quite useless without OS threads.
 */

#ifdef HAVE_PTHREAD_H
static pthread_mutex_t external_event_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_t main_thread;
#define LOCK_EXTERNAL_EVENTS pthread_mutex_lock(&external_event_mutex)
#define UNLOCK_EXTERNAL_EVENTS pthread_mutex_unlock(&external_event_mutex)
#else
#define LOCK_EXTERNAL_EVENTS
#define UNLOCK_EXTERNAL_EVENTS
#endif

long
s48_dequeue_external_event(char* readyp)
{
  long retval;
  LOCK_EXTERNAL_EVENTS;
  retval = s48_dequeue_external_eventBUunsafe(readyp);
  UNLOCK_EXTERNAL_EVENTS;
  return retval;
}

static char
external_event_pending()
{
  char retval;
  LOCK_EXTERNAL_EVENTS;
  retval = s48_external_event_pendingPUunsafe();
  UNLOCK_EXTERNAL_EVENTS;
  return retval;
}

/* no side effect */
static char
external_event_ready()
{
  char retval;
  LOCK_EXTERNAL_EVENTS;
  retval = s48_external_event_readyPUunsafe();
  UNLOCK_EXTERNAL_EVENTS;
  return retval;
}

void
s48_note_external_event(long uid)
{
  LOCK_EXTERNAL_EVENTS;
  s48_note_external_eventBUunsafe(uid);
  UNLOCK_EXTERNAL_EVENTS;
  NOTE_EVENT;
#ifdef HAVE_PTHREAD_H
  pthread_kill(main_thread, SIG_EXTERNAL_EVENT);
#else
  /* pretty useless probably */
  raise(SIG_EXTERNAL_EVENT);
#endif
}

void
s48_when_external_event_interrupt(int ign)
{
  /* do nothing, except possibly interrupt the running select */
}


/*
 *  ; Scheme version of the get-next-event procedure
 *  ;
 *  ; 1. If there has been a keyboard interrupt, return it.
 *  ; 2. Check for ready ports if enough time has passed since the last check.
 *  ; 3. If there is a ready port, return it.
 *  ; 4. If an alarm is due, return it.
 *  ; 5. If no events are pending, clear the event flags.
 *  (define (get-next-event)
 *    (cond ((> *keyboard-interrupt-count* 0)
 *           (without-interrupts
 *             (lambda ()
 *              (set! *keyboard-interrupt-count*
 *                    (- *keyboard-interrupt-count* 1))))
 *           (values (enum event-type keyboard-interrupt) #f #f))
 *          (else
 *           (cond ((>= *current_time* *poll-time*)
 *                  (queue-ready-ports)
 *                  (set! *poll-time* (+ *time* *poll-interval*))))
 *           (cond ((not (queue-empty? ready-ports))
 *                  (values (enum event-type i/o-completion)
 *                          (dequeue! ready-ports)))
 *                 ((>= *current_time* *alarm-time*)
 *                  (set! *alarm-time* max-integer)
 *                  (values (enum event-type alarm-interrupt) #f))
 *                 (else
 *                  (without-interrupts
 *                    (lambda ()
 *                      (if (and (= *keyboard-interrupt-count* 0)
 *                               (> *alarm-time* *current_time*)
 *                               (> *poll-time* *current_time*))
 *                          (set! *pending-event?* #f))))
 *                  (values (enum event-type no-event) #f))))))
 */

static psbool	there_are_ready_ports(void);
static int	next_ready_port(void);
static int	queue_ready_ports(psbool wait, long seconds, long ticks);

int
s48_get_next_event(long *ready_fd, long *status)
{
  int io_poll_status;
  /*
    fprintf(stderr, "[poll at %d (waiting for %d)]\n", s48_current_time, alarm_time);
    */
  if (keyboard_interrupt_count > 0) {
    block_keyboard_n_alarm_interrupts();
    --keyboard_interrupt_count;
    allow_keyboard_n_alarm_interrupts();
    /* fprintf(stderr, "[keyboard interrupt]\n"); */
    return (KEYBOARD_INTERRUPT_EVENT);
  }
  if (poll_time != -1 && s48_current_time >= poll_time) {
    io_poll_status = queue_ready_ports(PSFALSE, 0, 0);
    if (io_poll_status == NO_ERRORS)
      poll_time = s48_current_time + poll_interval;
    else {
      *status = io_poll_status;
      return (ERROR_EVENT);
    }
  }
  if (there_are_ready_ports()) {
    *ready_fd = next_ready_port();
    *status = 0;   /* chars read or written */
    /* fprintf(stderr, "[i/o completion]\n"); */
    return (IO_COMPLETION_EVENT);
  }
  if (alarm_time != -1 && s48_current_time >= alarm_time) {
    alarm_time = -1;
    /* fprintf(stderr, "[alarm %ld]\n", ticks); */
    return (ALARM_EVENT);
  }
  if (s48_os_signal_pending())
    return (OS_SIGNAL_EVENT);
  if (external_event_pending())
    return (EXTERNAL_EVENT);
  block_keyboard_n_alarm_interrupts();
  if ((keyboard_interrupt_count == 0)
      &&  (alarm_time == -1 || s48_current_time < alarm_time)
      &&  (poll_time == -1 || s48_current_time < poll_time))
    s48_Spending_eventsPS = PSFALSE;
  allow_keyboard_n_alarm_interrupts();
  return (NO_EVENT);
}


/*
 * We keep two queues of ports: those that have a pending operation, and
 * those whose operation has completed.  Periodically, we call select() on
 * the pending ports and move any that are ready onto the other queue and
 * signal an event.
 */
#define FD_QUIESCENT 0			/* idle */
#define FD_READY     1			/* I/O ready to be performed */
#define FD_PENDING   2			/* waiting */

typedef struct fd_struct {
 int	fd,			/* file descriptor */
	status;			/* one of the FD_* constants */
 psbool	is_input;		/* iff input */
 struct fd_struct	*next;	/* next on same queue */
} fd_struct;


/*
 * A queue of fd_structs is empty iff the first field is NULL.  In
 * that case, lastp points to first.
 */
typedef struct fdque {
  long          count;
  fd_struct	*first,
		**lastp;
} fdque;


static long		fd_setsize;
static fd_struct	**fds;
static fdque	ready = {
			 0,
			 NULL,
			 &ready.first
			},
		pending = {
			   0,
			   NULL,
			   &pending.first
			  };


static void		findrm(fd_struct *entry, fdque *que);
static fd_struct	*rmque(fd_struct **link, fdque *que);
static void		addque(fd_struct *entry, fdque *que);
static fd_struct	*add_fd(int fd, psbool is_input);

/*
 * Find a fd_struct in a queue, and remove it.
 */
static void
findrm(fd_struct *entry, fdque *que)
{
  fd_struct	**fp,
    *f;

  for (fp = &que->first; (f = *fp) != entry; fp = &f->next)
    if (f == NULL) {
      fprintf(stderr, "ERROR: findrm fd %d, status %d not on queue.\n",
	      entry->fd, entry->status);
      return;
    }
  rmque(fp, que);
}


/*
 * Given a pointer to the link of a fd_struct, and a pointer to
 * the queue it is on, remove the entry from the queue.
 * The entry removed is returned.
 */
static fd_struct	*
rmque(fd_struct **link, fdque *que)
{
  fd_struct	*res;

  res = *link;
  *link = res->next;
  if (res->next == NULL)
    que->lastp = link;
  que->count--;
  return (res);
}


/*
 * Add a fd_struct to a queue.
 */
static void
addque(fd_struct *entry, fdque *que)
{
  *que->lastp = entry;
  entry->next = NULL;
  que->lastp = &entry->next;
  que->count++;
}


static psbool
there_are_ready_ports(void)
{
  return (ready.first != NULL);
}


static int
next_ready_port(void)
{
  fd_struct	*p;

  p = rmque(&ready.first, &ready);
  p->status = FD_QUIESCENT;
  return (p->fd);
}


/*
 * Put fd on to the queue of ports with pending operations.
 * Return PSTRUE if successful, and PSFALSE otherwise.
 */
psbool
s48_add_pending_fd(int fd, psbool is_input)
{
  fd_struct	*data;

  if (! (0 <= fd && fd < fd_setsize)) {
    fd_setsize *= 2;
    fds = (fd_struct **) realloc (fds, sizeof (fd_struct *) * fd_setsize);
    if (fds == NULL) 
      fprintf(stderr, "ERROR: realloc of fds to %d elements failed, errno = %d\n",
	      errno,
	      fd_setsize);
    return (PSFALSE);
  }
  data = fds[fd];
  if (data == NULL) {
    data = add_fd(fd, is_input);
    if (data == NULL)
      return (PSFALSE); }		/* no more memory */

  data->is_input = is_input;

  if (data->status == FD_PENDING)
    return (PSTRUE);			/* fd is already pending */

  if (data->status == FD_READY)
    findrm(data, &ready);
  data->status = FD_PENDING;
  addque(data, &pending);
  if (poll_time == -1)
    poll_time = s48_current_time + poll_interval;
  return PSTRUE;
}


/*
 * Add a new fd_struct for fd.
 */
static fd_struct	*
add_fd(int fd, psbool is_input)
{
  struct fd_struct	*new_fd;

  new_fd = (struct fd_struct *)malloc(sizeof(struct fd_struct));
  if (new_fd != NULL) {
    new_fd->fd = fd;
    new_fd->status = FD_QUIESCENT;
    new_fd->is_input = is_input;
    new_fd->next = NULL;
    fds[fd] = new_fd;
  }
  return (new_fd);
}


/*
 * Remove fd from any queues it is on.  Returns true if the FD was on a queue
 * and false if it wasn't.
 */
psbool
s48_remove_fd(int fd)
{
  struct fd_struct	*data;

  if (! (0 <= fd && fd < fd_setsize)) {
    fprintf(stderr, "ERROR: s48_remove_fd fd %d not in [0, %d)\n",
	    fd,
	    fd_setsize);
    return PSFALSE;
  }
  data = fds[fd];
  if (data == NULL)
    return PSFALSE;
  if (data->status == FD_PENDING) {
    findrm(data, &pending);
    if (pending.first == NULL)
      poll_time = -1;
  } else if (data->status == FD_READY)
    findrm(data, &ready);
  free((void *)data);
  fds[fd] = NULL;
  return PSTRUE;
}


int
s48_wait_for_event(long max_wait, psbool is_minutes)
{
  int	status;
  long	seconds,
	ticks;

  /* fprintf(stderr, "[waiting]\n"); */

  s48_stop_alarm_interrupts();
  ticks = 0;
  if (max_wait == -1)
    seconds = -1;
  else if (is_minutes)
    seconds = max_wait * 60;
  else {
    seconds = max_wait / TICKS_PER_SECOND;
    ticks = max_wait % TICKS_PER_SECOND;
  }
  if (keyboard_interrupt_count > 0)
    status = NO_ERRORS;
  else {
    status = queue_ready_ports(PSTRUE, seconds, ticks);
    if (there_are_ready_ports()
	|| external_event_ready())
      NOTE_EVENT;
  }
  s48_start_alarm_interrupts();
  return (status);
}


#ifdef HAVE_POLL
static struct pollfd    *pollfds;
static long             pollfds_size;

/*
 * Call poll() on the pending ports and move any ready ones to the ready
 * queue.  If wait is true, seconds is either -1 (wait forever) or the
 * maximum number of seconds to wait (with ticks any additional ticks).
 * The returned value is a status code.
 */
static int
queue_ready_ports(psbool wait, long seconds, long ticks)
{
  int           npollfds;
  int           timeout;
  fd_struct	*fdp,
    		**fdpp;
  int		left;

  if ((! wait)
      &&  (pending.first == NULL))
    return (NO_ERRORS);

  if (pending.count > pollfds_size) {
    pollfds_size *= 2;
    pollfds = (struct pollfd *) realloc (pollfds, 
					 sizeof (struct pollfd) * pollfds_size);
    if (pollfds == NULL) {
      fprintf(stderr,
	      "Failed to realloc array of file descriptors to poll, errno = %d\n",
	      errno);
      exit(1);
    }
  }

  for (fdp = pending.first, npollfds = 0; fdp != NULL; fdp = fdp->next, npollfds++) {
    pollfds[npollfds].fd = fdp->fd;
    pollfds[npollfds].events = fdp->is_input? POLLIN : POLLOUT;
  }

  if (wait)
    if (seconds == -1)
      timeout = -1;
    else
      timeout = (int) seconds;
  else
    timeout = 0;

  while(1) {
    left = poll(pollfds, pending.count, timeout);
    if (left > 0) {
      fdpp = &pending.first;
      for (fdp = *fdpp, npollfds = 0;
	   (left > 0) && (fdp != NULL); 
	   fdp = *fdpp, npollfds++) {
	if (pollfds[npollfds].revents & (fdp->is_input? POLLIN : POLLOUT) 
	    | POLLHUP | POLLERR) {
	  rmque(fdpp, &pending);
	  fdp->status = FD_READY;
	  addque(fdp, &ready);
	}
	else
	  fdpp = &fdp->next;
      }
      if (pending.first == NULL)
	poll_time = -1;
      return NO_ERRORS;
    }
    else if (left == 0)
      return NO_ERRORS;
    else if (errno == EINTR) {
      if (external_event_ready())
	return NO_ERRORS;
      timeout = 0;		/* turn off blocking and try again */
    }	      
    else
      return errno;
  }
}
#else /* not HAVE_POLL */
/*
 * Call select() on the pending ports and move any ready ones to the ready
 * queue.  If wait is true, seconds is either -1 (wait forever) or the
 * maximum number of seconds to wait (with ticks any additional ticks).
 * The returned value is a status code.
 */
static int
queue_ready_ports(psbool wait, long seconds, long ticks)
{
  fd_set	reads,
    		writes,
    		alls;
  int		limfd;
  fd_struct	*fdp,
    		**fdpp;
  int		left;
  struct timeval	tv,
    			*tvp;

  if ((! wait)
      &&  (pending.first == NULL))
    return (NO_ERRORS);
  FD_ZERO(&reads);
  FD_ZERO(&writes);
  FD_ZERO(&alls);
  limfd = 0;
  for (fdp = pending.first; fdp != NULL; fdp = fdp->next) {
    FD_SET(fdp->fd, fdp->is_input ? &reads : &writes);
    FD_SET(fdp->fd, &alls);
    if (limfd <= fdp->fd)
      limfd = fdp->fd + 1;
  }
  tvp = &tv;
  if (wait)
    if (seconds == -1)
      tvp = NULL;
    else {
      tv.tv_sec = seconds;
      tv.tv_usec = ticks * (1000000 / TICKS_PER_SECOND);
    }
  else
    timerclear(&tv);
  while(1) {
    left = select(limfd, &reads, &writes, &alls, tvp);
    if (left > 0) {
      fdpp = &pending.first;
      while (left > 0 && (fdp = *fdpp) != NULL)
	if ((FD_ISSET(fdp->fd, &alls))
	    ||  (FD_ISSET(fdp->fd, fdp->is_input ? &reads : &writes))) {
	  --left;
	  rmque(fdpp, &pending);
	  fdp->status = FD_READY;
	  addque(fdp, &ready);
	} else
	  fdpp = &fdp->next;
      if (pending.first == NULL)
	poll_time = -1;
      return NO_ERRORS;
    }
    else if (left == 0)
      return NO_ERRORS;
    else if (errno == EINTR) {
      if (external_event_ready())
	return NO_ERRORS;
      tvp = &tv;		/* turn off blocking and try again */
      timerclear(tvp);
    }	      
    else
      return errno;
  }
}
#endif /* not HAVE_POLL */

void
s48_sysdep_init(void)
{
#ifdef HAVE_PTHREAD_H
  main_thread = pthread_self();
#endif

#ifdef HAVE_POLL
  pollfds_size = FD_SETSIZE;
  pollfds = (struct pollfd *) calloc (sizeof (struct pollfd), pollfds_size);

  if (pollfds == NULL) {
    fprintf(stderr,
	    "Failed to alloc array of file descriptors to poll with %d elements, errno = %d\n",
	    pollfds_size,
	    errno);
    exit(1);
  }
#endif /* HAVE_POLL */

  fd_setsize = FD_SETSIZE;
  fds = (fd_struct **) calloc (sizeof (fd_struct *), fd_setsize);

  if (fds == NULL) {
    fprintf(stderr,
	    "Failed to alloc fds with %d elements, errno = %d\n",
	    fd_setsize,
	    errno);
    exit(1);
  }

  if (!s48_setcatcher(SIGINT, s48_when_keyboard_interrupt)
      || !s48_setcatcher(SIGALRM, s48_when_alarm_interrupt)
      || !s48_setcatcher(SIGPIPE, when_sigpipe_interrupt)
      || !s48_setcatcher(SIG_EXTERNAL_EVENT, s48_when_external_event_interrupt)) {
    fprintf(stderr,
	    "Failed to install signal handlers, errno = %d\n",
	    errno);
    exit(1);
  }

  sigemptyset(&interrupt_mask);
  sigaddset(&interrupt_mask, SIGINT);
  sigaddset(&interrupt_mask, SIGALRM);

  s48_start_alarm_interrupts();
}