Created
May 20, 2020 21:02
-
-
Save octplane/ed97cdd04054f306f60e68153ed04fc1 to your computer and use it in GitHub Desktop.
kqueue watcher in gerbil scheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env gxi | |
(import | |
:std/format | |
:std/os/fcntl | |
:std/os/fdio | |
:std/os/fd) | |
(require bsd) (import :std/os/kqueue) | |
(define observed (make-hash-table-eqv)) | |
; https://stackoverflow.com/questions/19897897/kqueue-only-works-for-folder-path | |
; https://github.com/vyzo/gerbil/blob/master/src/std/os/kqueue.ss | |
; Add support for folder | |
; Add callback support | |
(def (watch (file "server.ss")) | |
(define kq (kqueue)) | |
(define path (path-normalize file)) | |
(printf "Watching ~a\n" file) | |
(define fd (open path O_RDONLY)) | |
(hash-put! observed (fd-e fd) file) | |
(kqueue-kevent-add kq fd EVFILT_VNODE EV_CLEAR (##fxior NOTE_WRITE NOTE_DELETE)) | |
(printf "fd ~a\n" fd) | |
(defconst nevents 4) | |
(def events (make-kevents nevents)) | |
(let wait-loop () | |
(let (n (kqueue-poll kq events nevents)) | |
(when (##fxpositive? n) | |
(let (event-count n) | |
(let event-loop ((i 0)) | |
(when (fx< i event-count) | |
(printf "- ~a\n" (hash-get observed (kevent-ident events i))) | |
(event-loop (fx1+ i)))) | |
))) | |
(wait-loop) | |
) | |
) | |
(watch) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment