55e6516ef34ff9b6f6a73955f63c1efba68dd0d9
[ghc-hetmet.git] / ghc / lib / misc / cbits / selectFrom.c
1 /*
2  * (c) sof, 1999
3  *
4  * Stubs to help implement Select module.
5  */
6
7 /* we're outside the realms of POSIX here... */
8 #define NON_POSIX_SOURCE
9
10 #include "Rts.h"
11 #include "selectFrom.h"
12 #include "stgio.h"
13
14 # if defined(HAVE_SYS_TYPES_H)
15 #  include <sys/types.h>
16 # endif
17
18 # ifdef HAVE_SYS_TIME_H
19 #  include <sys/time.h>
20 # endif
21
22
23 /* Helpers for the Haskell-side unmarshalling */
24
25 int
26 sizeof_fd_set__()
27 {
28  return (sizeof(fd_set));
29 }
30
31 void
32 fd_zero__(StgByteArray a)
33 {
34   FD_ZERO((fd_set*)a);
35 }
36
37 void
38 fd_set__(StgByteArray a, StgInt fd)
39 {
40   FD_SET(fd,(fd_set*)a);
41 }
42
43 int
44 is_fd_set__(StgByteArray a, StgInt fd)
45 {
46   return FD_ISSET(fd,(fd_set*)a);
47 }
48
49 StgInt
50 selectFrom__( StgByteArray rfd
51             , StgByteArray wfd
52             , StgByteArray efd
53             , StgInt mFd
54             , StgInt tout
55             )
56 {
57  int rc, i;
58  struct timeval tv;
59
60  if (tout != (-1)) {
61    tv.tv_sec = tout / 1000000;
62    tv.tv_usec = tout % 1000000;
63  }
64
65  while ((rc = select(mFd, (fd_set*)rfd, (fd_set*)wfd, (fd_set*)efd, (tout == -1 ? NULL : &tv))) < 0) {
66       if (errno != EINTR) {
67         break;
68       }
69  }
70  return 0;
71 }
72