Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.4 KiB

12 years ago
18 years ago
12 years ago
12 years ago
12 years ago
18 years ago
18 years ago
12 years ago
12 years ago
12 years ago
18 years ago
  1. #ifndef my_extern
  2. #define my_extern extern
  3. #endif
  4. #include "io.h"
  5. #include "array.h"
  6. #include "iarray.h"
  7. #ifdef __MINGW32__
  8. #include "socket.h"
  9. my_extern HANDLE io_comport;
  10. #else
  11. #include "haveepoll.h"
  12. #include "havekqueue.h"
  13. #include "havedevpoll.h"
  14. #include "havesigio.h"
  15. #ifdef HAVE_SIGIO
  16. #define _GNU_SOURCE
  17. #include <signal.h>
  18. #endif
  19. #endif
  20. /* We simulate a level-triggered API on top of an event signalling
  21. * mechanism that can be level-triggered (epoll/kqueue/poll) or
  22. * edge-triggered (SIGIO).
  23. * Difficulty: we want to avoid unnecessary syscalls, so we keep state
  24. * internally. If the user says he does not want to read/write anymore,
  25. * we don't tell the kernel straight away. The rationale is that the
  26. * typical protocol consists of interleaved reading and writing, so
  27. * after each read you'd call io_dontwantread, io_wantwrite, io_wait,
  28. * io_dontwantwrite, io_wantread, and in the regular case there is no
  29. * incoming data between io_dontwantread and io_wantread, so we might as
  30. * well optimistically not do those syscalls and then handle the
  31. * complexity if there is more incoming data. */
  32. /* Basically, we tell the kernel that we want to read if !canread,
  33. * and we tell the kernel that we want to write if !canwrite. */
  34. typedef struct {
  35. tai6464 timeout;
  36. int fd;
  37. unsigned int wantread:1; /* does the app want to read/write? */
  38. unsigned int wantwrite:1;
  39. unsigned int canread:1; /* do we know we can read/write? */
  40. unsigned int canwrite:1;
  41. unsigned int nonblock:1; /* is this socket non-blocking? */
  42. unsigned int inuse:1; /* internal consistency checking */
  43. unsigned int kernelwantread:1; /* did we tell the kernel we want to read/write? */
  44. unsigned int kernelwantwrite:1;
  45. unsigned int epolladded:1;
  46. #ifdef __MINGW32__
  47. unsigned int readqueued:2;
  48. unsigned int writequeued:2;
  49. unsigned int acceptqueued:2;
  50. unsigned int connectqueued:2;
  51. unsigned int sendfilequeued:2;
  52. unsigned int listened:1;
  53. #endif
  54. long next_read;
  55. long next_write;
  56. void* cookie;
  57. void* mmapped;
  58. long maplen;
  59. uint64 mapofs;
  60. #ifdef __MINGW32__
  61. OVERLAPPED or,ow,os; /* overlapped for read+accept, write+connect, sendfile */
  62. HANDLE /* fd, */ mh;
  63. char inbuf[8192];
  64. int bytes_read,bytes_written;
  65. DWORD errorcode;
  66. SOCKET next_accept;
  67. #endif
  68. } io_entry;
  69. extern int io_multithreaded;
  70. extern int io_sockets[2];
  71. my_extern iarray io_fds;
  72. my_extern uint64 io_wanted_fds;
  73. my_extern array io_pollfds;
  74. my_extern long first_readable;
  75. my_extern long first_writeable;
  76. my_extern enum __io_waitmode {
  77. UNDECIDED,
  78. POLL
  79. #ifdef HAVE_KQUEUE
  80. ,KQUEUE
  81. #endif
  82. #ifdef HAVE_EPOLL
  83. ,EPOLL
  84. #endif
  85. #ifdef HAVE_SIGIO
  86. ,_SIGIO
  87. #endif
  88. #ifdef HAVE_DEVPOLL
  89. ,DEVPOLL
  90. #endif
  91. #ifdef __MINGW32__
  92. ,COMPLETIONPORT
  93. #endif
  94. } io_waitmode;
  95. #if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL) || defined(HAVE_DEVPOLL)
  96. my_extern int io_master;
  97. #endif
  98. #if defined(HAVE_SIGIO)
  99. my_extern int io_signum;
  100. my_extern sigset_t io_ss;
  101. my_extern long alt_firstread;
  102. my_extern long alt_firstwrite;
  103. #endif
  104. int64 io_waituntil2(int64 milliseconds);
  105. void io_sigpipe(void);
  106. /* return next descriptor from io_wait that can be read from */
  107. int64 io_canread_unlocked();
  108. /* return next descriptor from io_wait that can be written to */
  109. int64 io_canwrite_unlocked();
  110. /* return next descriptor with expired timeout */
  111. int64 io_timeouted_unlocked();
  112. struct eventpacket {
  113. int fd;
  114. enum { CANREAD, CANWRITE, TIMEOUT } what;
  115. };
  116. #define debug_printf(x)