Skip to content

Instantly share code, notes, and snippets.

@milesrout
Last active May 17, 2017 03:27
Show Gist options
  • Save milesrout/d4bd40ef3ab3910d0c879d14b14c8cf7 to your computer and use it in GitHub Desktop.
Save milesrout/d4bd40ef3ab3910d0c879d14b14c8cf7 to your computer and use it in GitHub Desktop.
(define (do-read sock n)
(linux/do-syscall 'read (sock n 'linux/onoblock)))
(define (read sock n)
(let ((result (do-read sock n)))
(if (equal? result 'linux/ewouldblock)
(call-with-current-continuation
(lambda (cc)
(wait sock cc))
result)))
;; Efficiently doing this requires keeping a queue of ready
;; tasks and a queue of not-ready tasks. You loop over the
;; ready tasks and run them until they wait, then push them
;; to the unready tasks. Then you loop over the unready tasks
;; and check if they're ready, if they are you push them to
;; a new ready queue and otherwise you push them to a new
;; unready queue.
(define (wait obj cont)
nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment