poll.scm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ;; poll
  2. ;;;; Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;;
  18. (define-module (ice-9 poll)
  19. #:use-module (srfi srfi-9)
  20. #:use-module (rnrs bytevectors)
  21. #:export (make-empty-poll-set
  22. poll-set?
  23. poll-set-nfds
  24. poll-set-find-port
  25. poll-set-port
  26. poll-set-events
  27. set-poll-set-events!
  28. poll-set-revents
  29. set-poll-set-revents!
  30. poll-set-add!
  31. poll-set-remove!
  32. poll))
  33. (eval-when (expand load eval)
  34. (load-extension (string-append "libguile-" (effective-version))
  35. "scm_init_poll"))
  36. (if (not (= %sizeof-struct-pollfd 8))
  37. (error "Unexpected struct pollfd size" %sizeof-struct-pollfd))
  38. (if (defined? 'POLLIN)
  39. (export POLLIN))
  40. (if (defined? 'POLLPRI)
  41. (export POLLPRI))
  42. (if (defined? 'POLLOUT)
  43. (export POLLOUT))
  44. (if (defined? 'POLLRDHUP)
  45. (export POLLRDHUP))
  46. (if (defined? 'POLLERR)
  47. (export POLLERR))
  48. (if (defined? 'POLLHUP)
  49. (export POLLHUP))
  50. (if (defined? 'POLLNVAL)
  51. (export POLLNVAL))
  52. (define-record-type <poll-set>
  53. (make-poll-set pollfds nfds ports)
  54. poll-set?
  55. (pollfds pset-pollfds set-pset-pollfds!)
  56. (nfds poll-set-nfds set-pset-nfds!)
  57. (ports pset-ports set-pset-ports!)
  58. )
  59. (define-syntax-rule (pollfd-offset n)
  60. (* n 8))
  61. (define* (make-empty-poll-set #:optional (pre-allocated 4))
  62. (make-poll-set (make-bytevector (pollfd-offset pre-allocated) 0)
  63. 0
  64. (make-vector pre-allocated #f)))
  65. (define (pset-size set)
  66. (vector-length (pset-ports set)))
  67. (define (ensure-pset-size! set size)
  68. (let ((prev (pset-size set)))
  69. (if (< prev size)
  70. (let lp ((new prev))
  71. (if (< new size)
  72. (lp (* new 2))
  73. (let ((old-pollfds (pset-pollfds set))
  74. (nfds (poll-set-nfds set))
  75. (old-ports (pset-ports set))
  76. (new-pollfds (make-bytevector (pollfd-offset new) 0))
  77. (new-ports (make-vector new #f)))
  78. (bytevector-copy! old-pollfds 0 new-pollfds 0
  79. (pollfd-offset nfds))
  80. (vector-move-left! old-ports 0 nfds new-ports 0)
  81. (set-pset-pollfds! set new-pollfds)
  82. (set-pset-ports! set new-ports)))))))
  83. (define (poll-set-find-port set port)
  84. (let lp ((i 0))
  85. (if (< i (poll-set-nfds set))
  86. (if (equal? (vector-ref (pset-ports set) i) port)
  87. i
  88. (lp (1+ i)))
  89. #f)))
  90. (define (poll-set-port set idx)
  91. (if (< idx (poll-set-nfds set))
  92. (vector-ref (pset-ports set) idx)
  93. (error "poll set index out of bounds" set idx)))
  94. (define (poll-set-events set idx)
  95. (if (< idx (poll-set-nfds set))
  96. (bytevector-u16-native-ref (pset-pollfds set) (+ (pollfd-offset idx) 4))
  97. (error "poll set index out of bounds" set idx)))
  98. (define (set-poll-set-events! set idx events)
  99. (if (< idx (poll-set-nfds set))
  100. (bytevector-u16-native-set! (pset-pollfds set) (+ (pollfd-offset idx) 4)
  101. events)
  102. (error "poll set index out of bounds" set idx)))
  103. (define (poll-set-revents set idx)
  104. (if (< idx (poll-set-nfds set))
  105. (bytevector-u16-native-ref (pset-pollfds set) (+ (pollfd-offset idx) 6))
  106. (error "poll set index out of bounds" set idx)))
  107. (define (set-poll-set-revents! set idx revents)
  108. (if (< idx (poll-set-nfds set))
  109. (bytevector-u16-native-set! (pset-pollfds set) (+ (pollfd-offset idx) 6)
  110. revents)
  111. (error "poll set index out of bounds" set idx)))
  112. (define (poll-set-add! set fd-or-port events)
  113. (let* ((idx (poll-set-nfds set))
  114. (off (pollfd-offset idx))
  115. (fd (if (integer? fd-or-port)
  116. fd-or-port
  117. (fileno fd-or-port))))
  118. (ensure-pset-size! set (1+ idx))
  119. (bytevector-s32-native-set! (pset-pollfds set) off fd)
  120. (bytevector-u16-native-set! (pset-pollfds set) (+ off 4) events)
  121. (bytevector-u16-native-set! (pset-pollfds set) (+ off 6) 0) ; revents
  122. (vector-set! (pset-ports set) idx fd-or-port)
  123. (set-pset-nfds! set (1+ idx))))
  124. (define (poll-set-remove! set idx)
  125. (if (not (< idx (poll-set-nfds set)))
  126. (error "poll set index out of bounds" set idx))
  127. (let ((nfds (poll-set-nfds set))
  128. (off (pollfd-offset idx))
  129. (port (vector-ref (pset-ports set) idx)))
  130. (vector-move-left! (pset-ports set) (1+ idx) nfds
  131. (pset-ports set) idx)
  132. (vector-set! (pset-ports set) (1- nfds) #f)
  133. (bytevector-copy! (pset-pollfds set) (pollfd-offset (1+ idx))
  134. (pset-pollfds set) off
  135. (- (pollfd-offset nfds) (pollfd-offset (1+ idx))))
  136. ;; zero the struct pollfd all at once
  137. (bytevector-u64-native-set! (pset-pollfds set) (pollfd-offset (1- nfds)) 0)
  138. (set-pset-nfds! set (1- nfds))
  139. port))
  140. (define* (poll poll-set #:optional (timeout -1))
  141. (primitive-poll (pset-pollfds poll-set)
  142. (poll-set-nfds poll-set)
  143. (pset-ports poll-set)
  144. timeout))