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.

80 lines
2.3 KiB

18 years ago
  1. #include <unistd.h>
  2. #include <sys/time.h>
  3. #include <errno.h>
  4. #include "io_internal.h"
  5. void io_wantwrite_really(int64 d, io_entry* e);
  6. int64 io_canwrite() {
  7. io_entry* e;
  8. #if defined(HAVE_SIGIO)
  9. /* We promise that the user can call io_canread() and read, and
  10. * the user uses io_tryread or calls io_eagain_read to signal if
  11. * there is no more data to read. That means if the user does not
  12. * call io_eagain_read, we need to know which fd it was so we can
  13. * keep it in the alternative queue. */
  14. if (alt_curwrite!=-1) {
  15. e=iarray_get(&io_fds,alt_curwrite);
  16. e->next_write=alt_firstwrite;
  17. alt_firstwrite=alt_curwrite;
  18. alt_curwrite=-1;
  19. }
  20. #endif
  21. if (first_writeable==-1)
  22. #if defined(HAVE_SIGIO)
  23. {
  24. if (alt_firstwrite>=0 && (e=iarray_get(&io_fds,alt_firstwrite)) && e->canwrite) {
  25. debug_printf(("io_canwrite: normal write queue is empty, swapping in alt write queue (starting with %ld)\n",alt_firstwrite));
  26. first_writeable=alt_firstwrite;
  27. alt_firstwrite=-1;
  28. } else
  29. return -1;
  30. }
  31. #else
  32. return -1;
  33. #endif
  34. for (;;) {
  35. int64 r;
  36. e=iarray_get(&io_fds,first_writeable);
  37. if (!e) break;
  38. r=first_writeable;
  39. first_writeable=e->next_write;
  40. e->next_write=-1;
  41. if (e->closed) {
  42. /* The fd was previously closed, but there were still open events on it.
  43. * To prevent race conditions, we did not actually close the fd
  44. * but only marked it as closed, so we can skip this event here */
  45. continue;
  46. }
  47. debug_printf(("io_canwrite: dequeue %lld from normal write queue (next is %ld)\n",r,first_writeable));
  48. if (e->wantwrite &&
  49. #ifdef __MINGW32__
  50. (e->canwrite || e->sendfilequeued==1)
  51. #else
  52. e->canwrite
  53. #endif
  54. ) {
  55. #if defined(HAVE_SIGIO)
  56. #if 0
  57. /* this code violates an invariant that the other code has, namely
  58. * that e->next_read is -1 once the fd is dequeued. */
  59. e->next_write=alt_firstwrite;
  60. alt_firstwrite=r;
  61. debug_printf(("io_canwrite: enqueue %ld in alt write queue (next is %ld)\n",alt_firstwrite,e->next_write));
  62. #else
  63. alt_curwrite=r;
  64. #endif
  65. if (io_waitmode!=_SIGIO)
  66. #endif
  67. e->canwrite=0;
  68. if (!e->kernelwantwrite)
  69. io_wantwrite_really(r,e);
  70. return r;
  71. }
  72. }
  73. return -1;
  74. }