d18213c4cb581b1ea7721c8deab3d3a4f7c77749
[bpt/guile.git] / libguile / iselect.c
1 /* Copyright (C) 1997, 1998, 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 \f
42 #include <stdio.h>
43 #include <limits.h>
44 #include <string.h>
45
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49
50 #include "libguile/_scm.h"
51 #include "libguile/async.h"
52
53 #include "libguile/iselect.h"
54
55 #ifdef GUILE_ISELECT
56
57 #include "libguile/coop-threads.h"
58
59 #ifdef MISSING_BZERO_DECL
60 extern void bzero (void *, size_t);
61 #endif
62
63 \f
64
65 /* COOP queue macros */
66 #define QEMPTYP(q) (q.t.next == &q.t)
67 #define QFIRST(q) (q.t.next)
68
69 /* These macros count the number of bits in a word. */
70 #define SCM_BITS_PER_LONG (8 * sizeof (unsigned long))
71 /* Use LONG_MAX instead of ULONG_MAX here since not all systems define
72 ULONG_MAX */
73 #if LONG_MAX >> 16 == 0
74 #define SCM_NLONGBITS(p) (bc[((unsigned char *)(p))[0]]\
75 + bc[((unsigned char *)(p))[1]])
76 #elif LONG_MAX >> 32 == 0 || LONG_MAX == 2147483647L /* bug in Sun CC 4.2 */
77 #define SCM_NLONGBITS(p) (bc[((unsigned char *)(p))[0]]\
78 + bc[((unsigned char *)(p))[1]]\
79 + bc[((unsigned char *)(p))[2]]\
80 + bc[((unsigned char *)(p))[3]])
81 #elif LONG_MAX >> 64 == 0
82 #define SCM_NLONGBITS(p) (bc[((unsigned char *)(p))[0]]\
83 + bc[((unsigned char *)(p))[1]]\
84 + bc[((unsigned char *)(p))[2]]\
85 + bc[((unsigned char *)(p))[3]]\
86 + bc[((unsigned char *)(p))[4]]\
87 + bc[((unsigned char *)(p))[5]]\
88 + bc[((unsigned char *)(p))[6]]\
89 + bc[((unsigned char *)(p))[7]])
90 #else
91 #error Could not determine suitable definition for SCM_NLONGBITS
92 #endif
93
94 #ifdef HAVE_BZERO
95 #define FD_ZERO_N(pos, n) bzero ((pos), (n))
96 #else
97 #define FD_ZERO_N(pos, n) memset ((void *) (pos), 0, (n))
98 #endif
99
100 typedef unsigned long *ulongptr;
101
102 static char bc[256]; /* Bit counting array. bc[x] is the number of
103 bits in x. */
104
105 int scm_I_am_dead;
106
107 /* This flag indicates that several threads are waiting on the same
108 file descriptor. When this is the case, the common fd sets are
109 updated in a more inefficient way. */
110 int collisionp;
111
112 /* These are the common fd sets. When new select calls are made,
113 those sets are merged into these. */
114 int gnfds;
115 SELECT_TYPE greadfds;
116 SELECT_TYPE gwritefds;
117 SELECT_TYPE gexceptfds;
118
119 /* These are the result sets. They are used when we call OS select.
120 We couldn't use the common fd sets above, since that would destroy
121 them. */
122 SELECT_TYPE rreadfds;
123 SELECT_TYPE rwritefds;
124 SELECT_TYPE rexceptfds;
125
126 /* Constant timeval struct representing a zero timeout which we use
127 when polling. */
128 static struct timeval timeout0;
129
130 /* As select, but doesn't destroy the file descriptor sets passed as
131 arguments. The results are stored into the result sets. */
132 static int
133 safe_select (int nfds,
134 SELECT_TYPE *readfds,
135 SELECT_TYPE *writefds,
136 SELECT_TYPE *exceptfds,
137 struct timeval *timeout)
138 {
139 int n = (nfds + 7) / 8;
140 /* Copy file descriptor sets to result area */
141 if (readfds == NULL)
142 FD_ZERO (&rreadfds);
143 else
144 {
145 memcpy (&rreadfds, readfds, n);
146 FD_ZERO_N ((char *) &rreadfds + n, SELECT_SET_SIZE / 8 - n);
147 }
148 if (writefds == NULL)
149 FD_ZERO (&rwritefds);
150 else
151 {
152 memcpy (&rwritefds, writefds, n);
153 FD_ZERO_N ((char *) &rwritefds + n, SELECT_SET_SIZE / 8 - n);
154 }
155 if (exceptfds == NULL)
156 FD_ZERO (&rexceptfds);
157 else
158 {
159 memcpy (&rexceptfds, exceptfds, n);
160 FD_ZERO_N ((char *) &rexceptfds + n, SELECT_SET_SIZE / 8 - n);
161 }
162 return select (nfds, &rreadfds, &rwritefds, &rexceptfds, timeout);
163 }
164
165 /* Merge new file descriptor sets into the common sets. */
166 static void
167 add_fd_sets (coop_t *t)
168 {
169 int n = (t->nfds + SCM_BITS_PER_LONG - 1) / SCM_BITS_PER_LONG;
170 int i;
171
172 /* Detect if the fd sets of the thread have any bits in common with
173 the rest of the waiting threads. If that is so, set the
174 collision flag. This causes a more time consuming handling of
175 the common fd sets---they need to recalculated every time a
176 thread wakes up. */
177 if (!collisionp)
178 for (i = 0; i < n; ++i)
179 if ((t->readfds != NULL
180 && (((ulongptr) t->readfds)[i] & ((ulongptr) &greadfds)[i]) != 0)
181 || (t->writefds != NULL
182 && ((((ulongptr) t->writefds)[i] & ((ulongptr) &gwritefds)[i])
183 != 0))
184 || (t->exceptfds != NULL
185 && ((((ulongptr) t->exceptfds)[i] & ((ulongptr) &gexceptfds)[i])
186 != 0)))
187 {
188 collisionp = 1;
189 break;
190 }
191
192 /* We recalculate nfds below. The cost for this can be paid back
193 with a great bonus since many programs are lazy with the nfds
194 arg. Many even pass 1024 when using one of the lowest fd:s!
195
196 We approach from above, checking for non-zero bits. As soon as
197 we have determined the value of nfds, we jump down to code below
198 which concludes the updating of the common sets. */
199 t->nfds = 0;
200 i = n;
201 while (i > 0)
202 {
203 --i;
204 if (t->readfds != NULL && ((ulongptr) t->readfds)[i] != 0)
205 {
206 ((ulongptr) &greadfds)[i] |= ((ulongptr) t->readfds)[i];
207 n = (i + 1) * SCM_BITS_PER_LONG;
208 t->nfds = n;
209 if (n > gnfds)
210 gnfds = n;
211 goto cont_read;
212 }
213 if (t->writefds != NULL && ((ulongptr) t->writefds)[i] != 0)
214 {
215 ((ulongptr) &gwritefds)[i] |= ((ulongptr) t->writefds)[i];
216 n = (i + 1) * SCM_BITS_PER_LONG;
217 t->nfds = n;
218 if (n > gnfds)
219 gnfds = n;
220 goto cont_write;
221 }
222 if (t->exceptfds != NULL && ((ulongptr) t->exceptfds)[i] != 0)
223 {
224 ((ulongptr) &gexceptfds)[i] |= ((ulongptr) t->exceptfds)[i];
225 n = (i + 1) * SCM_BITS_PER_LONG;
226 t->nfds = n;
227 if (n > gnfds)
228 gnfds = n;
229 goto cont_except;
230 }
231 }
232 return;
233
234 /* nfds is now determined. Just finish updating the common sets. */
235 while (i > 0)
236 {
237 --i;
238 if (t->readfds != NULL && ((ulongptr) t->readfds)[i] != 0)
239 ((ulongptr) &greadfds)[i] |= ((ulongptr) t->readfds)[i];
240 cont_read:
241 if (t->writefds != NULL && ((ulongptr) t->writefds)[i] != 0)
242 ((ulongptr) &gwritefds)[i] |= ((ulongptr) t->writefds)[i];
243 cont_write:
244 if (t->exceptfds != NULL && ((ulongptr) t->exceptfds)[i] != 0)
245 ((ulongptr) &gexceptfds)[i] |= ((ulongptr) t->exceptfds)[i];
246 cont_except:
247 ;
248 }
249 }
250
251 /* Update the fd sets pointed to by the thread so that they reflect
252 the status of the file descriptors which the thread was interested
253 in. Also clear those bits in the common sets. This function is
254 only called when there are no bit collisions. */
255 static void
256 finalize_fd_sets (coop_t *t)
257 {
258 int i = (t->nfds + SCM_BITS_PER_LONG - 1) / SCM_BITS_PER_LONG;
259 int n_ones = 0;
260 register unsigned long s;
261
262 if (t->nfds == gnfds)
263 {
264 /* This thread is the one responsible for the current high value
265 of gnfds. First do our other jobs while at the same time
266 trying to decrease gnfds. */
267 while (i > 0)
268 {
269 --i;
270 if (t->readfds != NULL && (s = ((ulongptr) t->readfds)[i]) != 0)
271 {
272 ((ulongptr) t->readfds)[i] &= ((ulongptr) &rreadfds)[i];
273 ((ulongptr) &greadfds)[i] &= ~s;
274 n_ones += SCM_NLONGBITS (&((ulongptr) t->readfds)[i]);
275 }
276 if (((ulongptr) &greadfds)[i] != 0)
277 {
278 gnfds = (i + 1) * SCM_BITS_PER_LONG;
279 goto cont_read;
280 }
281 if (t->writefds != NULL && (s = ((ulongptr) t->writefds)[i]) != 0)
282 {
283 ((ulongptr) t->writefds)[i] &= ((ulongptr) &rwritefds)[i];
284 ((ulongptr) &gwritefds)[i] &= ~s;
285 n_ones += SCM_NLONGBITS (&((ulongptr) t->writefds)[i]);
286 }
287 if (((ulongptr) &gwritefds)[i] != 0)
288 {
289 gnfds = (i + 1) * SCM_BITS_PER_LONG;
290 goto cont_write;
291 }
292 if (t->exceptfds != NULL && (s = ((ulongptr) t->exceptfds)[i]) != 0)
293 {
294 ((ulongptr) t->exceptfds)[i] &= ((ulongptr) &rexceptfds)[i];
295 ((ulongptr) &gexceptfds)[i] &= ~s;
296 n_ones += SCM_NLONGBITS (&((ulongptr) t->exceptfds)[i]);
297 }
298 if (((ulongptr) &gexceptfds)[i] != 0)
299 {
300 gnfds = (i + 1) * SCM_BITS_PER_LONG;
301 goto cont_except;
302 }
303 }
304 gnfds = 0;
305 t->retval = n_ones;
306 return;
307 }
308
309 /* Either this thread wasn't responsible for gnfds or gnfds has been
310 determined. */
311 while (i > 0)
312 {
313 --i;
314 if (t->readfds != NULL && (s = ((ulongptr) t->readfds)[i]) != 0)
315 {
316 ((ulongptr) t->readfds)[i] &= ((ulongptr) &rreadfds)[i];
317 ((ulongptr) &greadfds)[i] &= ~s;
318 n_ones += SCM_NLONGBITS (&((ulongptr) t->readfds)[i]);
319 }
320 cont_read:
321 if (t->writefds != NULL && (s = ((ulongptr) t->writefds)[i]) != 0)
322 {
323 ((ulongptr) t->writefds)[i] &= ((ulongptr) &rwritefds)[i];
324 ((ulongptr) &gwritefds)[i] &= ~s;
325 n_ones += SCM_NLONGBITS (&((ulongptr) t->writefds)[i]);
326 }
327 cont_write:
328 if (t->exceptfds != NULL && (s = ((ulongptr) t->exceptfds)[i]) != 0)
329 {
330 ((ulongptr) t->exceptfds)[i] &= ((ulongptr) &rexceptfds)[i];
331 ((ulongptr) &gexceptfds)[i] &= ~s;
332 n_ones += SCM_NLONGBITS (&((ulongptr) t->exceptfds)[i]);
333 }
334 cont_except:
335 ;
336 }
337 t->retval = n_ones;
338 }
339
340 /* Just like finalize_fd_sets except that we don't have to update the
341 global fd sets. Those will be recalulated elsewhere. */
342 static void
343 finalize_fd_sets_lazily (coop_t *t)
344 {
345 int i = (t->nfds + SCM_BITS_PER_LONG - 1) / SCM_BITS_PER_LONG;
346 int n_ones = 0;
347 while (i > 0)
348 {
349 --i;
350 if (t->readfds != NULL && ((ulongptr) t->readfds)[i] != 0)
351 {
352 ((ulongptr) t->readfds)[i] &= ((ulongptr) &rreadfds)[i];
353 n_ones += SCM_NLONGBITS (&((ulongptr) t->readfds)[i]);
354 }
355 if (t->writefds != NULL && ((ulongptr) t->writefds)[i] != 0)
356 {
357 ((ulongptr) t->writefds)[i] &= ((ulongptr) &rwritefds)[i];
358 n_ones += SCM_NLONGBITS (&((ulongptr) t->writefds)[i]);
359 }
360 if (t->exceptfds != NULL && ((ulongptr) t->exceptfds)[i] != 0)
361 {
362 ((ulongptr) t->exceptfds)[i] &= ((ulongptr) &rexceptfds)[i];
363 n_ones += SCM_NLONGBITS (&((ulongptr) t->exceptfds)[i]);
364 }
365 }
366 t->retval = n_ones;
367 }
368
369 /* Return first fd with a non-zero bit in any of the result sets. */
370 static int
371 first_interesting_fd (void)
372 {
373 int i = 0;
374 SELECT_TYPE *s;
375 while (1)
376 {
377 if (((ulongptr) &rreadfds)[i] != 0)
378 {
379 s = &rreadfds;
380 break;
381 }
382 if (((ulongptr) &rwritefds)[i] != 0)
383 {
384 s = &rwritefds;
385 break;
386 }
387 if (((ulongptr) &rexceptfds)[i] != 0)
388 {
389 s = &rexceptfds;
390 break;
391 }
392 ++i;
393 }
394 i *= SCM_BITS_PER_LONG;
395 while (i < gnfds)
396 {
397 if (FD_ISSET (i, s))
398 return i;
399 ++i;
400 }
401 fprintf (stderr, "first_interesting_fd: internal error\n");
402 exit (1);
403 }
404
405 /* Revive all threads with an error status. */
406 void
407 scm_error_revive_threads (void)
408 {
409 coop_t *t;
410
411 while ((t = coop_qget (&coop_global_sleepq)) != NULL)
412 {
413 t->_errno = errno;
414 t->retval = -1;
415 if (t != coop_global_curr)
416 coop_qput (&coop_global_runq, t);
417 }
418 collisionp = 0;
419 gnfds = 0;
420 FD_ZERO (&greadfds);
421 FD_ZERO (&gwritefds);
422 FD_ZERO (&gexceptfds);
423 }
424
425 /* Given the result of a call to safe_select and the current time,
426 try to wake up some threads and return the first one. Return NULL
427 if we couldn't find any. */
428 static coop_t *
429 find_thread (int n, struct timeval *now, int sleepingp)
430 {
431 coop_t *t;
432 int fd;
433
434 if (n < 0)
435 /* An error or a signal has occured. Wake all threads. Since we
436 don't care to calculate if there is a sinner we report the
437 error to all of them. */
438 {
439 scm_error_revive_threads ();
440 if (!scm_I_am_dead)
441 return coop_global_curr;
442 }
443 else if (n == 0)
444 {
445 while (!QEMPTYP (coop_global_sleepq)
446 && (t = QFIRST (coop_global_sleepq))->timeoutp
447 && (t->wakeup_time.tv_sec < now->tv_sec
448 || (t->wakeup_time.tv_sec == now->tv_sec
449 && t->wakeup_time.tv_usec <= now->tv_usec)))
450 {
451 coop_qget (&coop_global_sleepq);
452 if (collisionp)
453 finalize_fd_sets_lazily (t);
454 else
455 finalize_fd_sets (t);
456 coop_qput (&coop_global_runq, t);
457 }
458 if (collisionp)
459 {
460 while ((t = coop_qget (&coop_global_sleepq)) != NULL)
461 coop_qput (&coop_tmp_queue, t);
462 goto rebuild_global_fd_sets;
463 }
464 }
465 else if (n > 0)
466 {
467 /* Find the first interesting file descriptor */
468 fd = first_interesting_fd ();
469 /* Check the sleeping queue for this file descriptor.
470 Other file descriptors will be handled next time
471 coop_next_runnable_thread is called. */
472 /* This code is inefficient. We'll improve it later. */
473 while ((t = coop_qget (&coop_global_sleepq)) != NULL)
474 {
475 if ((t->readfds && FD_ISSET (fd, t->readfds))
476 || (t->writefds && FD_ISSET (fd, t->writefds))
477 || (t->exceptfds && FD_ISSET (fd, t->exceptfds))
478 || (t->timeoutp
479 && (t->wakeup_time.tv_sec < now->tv_sec
480 || (t->wakeup_time.tv_sec == now->tv_sec
481 && t->wakeup_time.tv_usec <= now->tv_usec))))
482 {
483 if (collisionp)
484 finalize_fd_sets_lazily (t);
485 else
486 finalize_fd_sets (t);
487 coop_qput (&coop_global_runq, t);
488 }
489 else
490 coop_qput(&coop_tmp_queue, t);
491 }
492 if (collisionp)
493 {
494 rebuild_global_fd_sets:
495 collisionp = 0;
496 gnfds = 0;
497 FD_ZERO (&greadfds);
498 FD_ZERO (&gwritefds);
499 FD_ZERO (&gexceptfds);
500 while ((t = coop_qget (&coop_tmp_queue)) != NULL)
501 {
502 add_fd_sets (t);
503 coop_qput (&coop_global_sleepq, t);
504 }
505 }
506 else
507 {
508 while ((t = coop_qget (&coop_tmp_queue)) != NULL)
509 coop_qput (&coop_global_sleepq, t);
510 }
511 }
512
513 return coop_qget (&coop_global_runq);
514 }
515
516 /* Return next runnable thread on the run queue.
517 * First update the queue with possible I/O or timeouts.
518 * If no thread is found, return NULL.
519 */
520 coop_t *
521 coop_next_runnable_thread ()
522 {
523 coop_t *t;
524 struct timeval now;
525 int n;
526
527 /* Just return next thread on the runq if the sleepq is empty. */
528 if (QEMPTYP (coop_global_sleepq))
529 {
530 if (QEMPTYP (coop_global_runq))
531 return coop_global_curr;
532 else
533 return coop_qget (&coop_global_runq);
534 }
535
536 if (gnfds > 0)
537 n = safe_select (gnfds, &greadfds, &gwritefds, &gexceptfds, &timeout0);
538 else
539 n = 0;
540 if (QFIRST (coop_global_sleepq)->timeoutp)
541 {
542 gettimeofday (&now, NULL);
543 t = find_thread (n, &now, 0);
544 }
545 else
546 t = find_thread (n, 0, 0);
547 return t == NULL ? coop_global_curr : t;
548 }
549
550 coop_t *
551 coop_wait_for_runnable_thread_now (struct timeval *now)
552 {
553 int n;
554 coop_t *t;
555
556 if (gnfds > 0)
557 n = safe_select (gnfds, &greadfds, &gwritefds, &gexceptfds, &timeout0);
558 else
559 n = 0;
560 /* Is there any other runnable thread? */
561 t = find_thread (n, now, 1);
562 while (t == NULL)
563 {
564 /* No. Let the process go to sleep. */
565 if ((t = QFIRST (coop_global_sleepq))->timeoutp)
566 {
567 now->tv_sec = t->wakeup_time.tv_sec - now->tv_sec;
568 if (now->tv_usec > t->wakeup_time.tv_usec)
569 {
570 --now->tv_sec;
571 now->tv_usec = 1000000 + t->wakeup_time.tv_usec - now->tv_usec;
572 }
573 else
574 now->tv_usec = t->wakeup_time.tv_usec - now->tv_usec;
575 n = safe_select (gnfds, &greadfds, &gwritefds, &gexceptfds, now);
576 }
577 else
578 n = safe_select (gnfds, &greadfds, &gwritefds, &gexceptfds, NULL);
579 gettimeofday (now, NULL);
580 t = find_thread (n, now, 1);
581 }
582
583 return t;
584 }
585
586 coop_t *
587 coop_wait_for_runnable_thread ()
588 {
589 struct timeval now;
590
591 if (QEMPTYP (coop_global_sleepq))
592 {
593 if (QEMPTYP (coop_global_runq))
594 return coop_global_curr;
595 else
596 return coop_qget (&coop_global_runq);
597 }
598
599 if (QFIRST (coop_global_sleepq)->timeoutp)
600 gettimeofday (&now, NULL);
601
602 return coop_wait_for_runnable_thread_now (&now);
603 }
604
605 /* Initialize bit counting array */
606 static void init_bc (int bit, int i, int n)
607 {
608 if (bit == 0)
609 bc[i] = n;
610 else
611 {
612 init_bc (bit >> 1, i, n);
613 init_bc (bit >> 1, i | bit, n + 1);
614 }
615 }
616
617 void
618 scm_init_iselect ()
619 {
620 #if 0 /* This is just symbolic */
621 collisionp = 0;
622 gnfds = 0;
623 FD_ZERO (&greadfds);
624 FD_ZERO (&gwritefds);
625 FD_ZERO (&gexceptfds);
626 timeout0.tv_sec = 0;
627 timeout0.tv_usec = 0;
628 #endif
629 init_bc (0x80, 0, 0);
630 #ifndef SCM_MAGIC_SNARFER
631 #include "libguile/iselect.x"
632 #endif
633 }
634
635 #endif /* GUILE_ISELECT */
636
637 int
638 scm_internal_select (int nfds,
639 SELECT_TYPE *readfds,
640 SELECT_TYPE *writefds,
641 SELECT_TYPE *exceptfds,
642 struct timeval *timeout)
643 {
644 #ifndef GUILE_ISELECT
645 int res = select (nfds, readfds, writefds, exceptfds, timeout);
646 SCM_ASYNC_TICK;
647 return res;
648 #else /* GUILE_ISELECT */
649 struct timeval now;
650 coop_t *t, *curr = coop_global_curr;
651
652 /* If the timeout is 0, we're polling and can handle it quickly. */
653 if (timeout != NULL
654 && timeout->tv_sec == 0
655 && timeout->tv_usec == 0)
656 return select (nfds, readfds, writefds, exceptfds, timeout);
657
658 SCM_DEFER_INTS;
659
660 /* Add our file descriptor flags to the common set. */
661 curr->nfds = nfds;
662 curr->readfds = readfds;
663 curr->writefds = writefds;
664 curr->exceptfds = exceptfds;
665 add_fd_sets (curr);
666
667 /* Place ourselves on the sleep queue and get a new thread to run. */
668 if (timeout == NULL)
669 {
670 curr->timeoutp = 0;
671 coop_qput (&coop_global_sleepq, curr);
672 t = coop_wait_for_runnable_thread ();
673 }
674 else
675 {
676 gettimeofday (&now, NULL);
677 curr->timeoutp = 1;
678 curr->wakeup_time.tv_sec = now.tv_sec + timeout->tv_sec;
679 curr->wakeup_time.tv_usec = now.tv_usec + timeout->tv_usec;
680 if (curr->wakeup_time.tv_usec >= 1000000)
681 {
682 ++curr->wakeup_time.tv_sec;
683 curr->wakeup_time.tv_usec -= 1000000;
684 }
685 /* Insert the current thread at the right place in the sleep queue */
686 coop_timeout_qinsert (&coop_global_sleepq, curr);
687 t = coop_wait_for_runnable_thread_now (&now);
688 }
689
690 /* If the new thread is the same as the sleeping thread, do nothing */
691 if (t != coop_global_curr)
692 {
693 /* Do a context switch. */
694 coop_global_curr = t;
695 QT_BLOCK (coop_sleephelp, curr, NULL, t->sp);
696 }
697
698 if (coop_global_curr->retval == -1)
699 errno = coop_global_curr->_errno;
700 SCM_ALLOW_INTS;
701 SCM_ASYNC_TICK;
702 return coop_global_curr->retval;
703 #endif /* GUILE_ISELECT */
704 }
705
706 /*
707 Local Variables:
708 c-file-style: "gnu"
709 End:
710 */