Updating epoll code

db4
Slava Pestov 2008-12-09 20:01:23 -06:00
parent 3bd4c0b4be
commit a4fc4046a0
2 changed files with 48 additions and 46 deletions

View File

@ -1,8 +1,8 @@
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: alien.c-types kernel io.ports io.unix.backend
bit-arrays sequences assocs unix unix.linux.epoll math
namespaces unix.time ;
USING: accessors alien.c-types kernel io.ports io.unix.backend
bit-arrays sequences assocs struct-arrays unix unix.linux.epoll
math namespaces locals unix.time ;
IN: io.unix.epoll
TUPLE: epoll-mx < mx events ;
@ -14,47 +14,48 @@ TUPLE: epoll-mx < mx events ;
: <epoll-mx> ( -- mx )
epoll-mx new-mx
max-events epoll_create dup io-error over set-mx-fd
max-events "epoll-event" <c-array> over set-epoll-mx-events ;
max-events epoll_create dup io-error >>fd
max-events "epoll-event" <struct-array> >>events ;
GENERIC: io-task-events ( task -- n )
M: input-task io-task-events drop EPOLLIN ;
M: output-task io-task-events drop EPOLLOUT ;
: make-event ( task -- event )
: make-event ( fd events -- event )
"epoll-event" <c-object>
over io-task-events over set-epoll-event-events
swap io-task-fd over set-epoll-event-fd ;
[ set-epoll-event-events ] keep
[ set-epoll-event-fd ] keep ;
: do-epoll-ctl ( task mx what -- )
>r mx-fd r> rot dup io-task-fd swap make-event
epoll_ctl io-error ;
:: do-epoll-ctl ( fd mx what events -- )
mx fd>> what fd events make-event what epoll_ctl io-error ;
M: epoll-mx register-io-task ( task mx -- )
[ EPOLL_CTL_ADD do-epoll-ctl ] [ call-next-method ] 2bi ;
: do-epoll-add ( fd mx events -- )
EPOLL_CTL_ADD swap EPOLLONESHOT bitor do-epoll-ctl ;
M: epoll-mx unregister-io-task ( task mx -- )
[ call-next-method ] [ EPOLL_CTL_DEL do-epoll-ctl ] 2bi ;
: do-epoll-del ( fd mx events -- )
EPOLL_CTL_DEL swap do-epoll-ctl ;
: wait-event ( mx timeout -- n )
>r { mx-fd epoll-mx-events } get-slots max-events
r> epoll_wait dup multiplexer-error ;
M: epoll-mx add-input-callback ( thread fd mx -- )
[ EPOLLIN do-epoll-add ] [ call-next-method ] 2bi ;
: epoll-read-task ( mx fd -- )
over mx-reads at* [ perform-io-task ] [ 2drop ] if ;
M: epoll-mx add-output-callback ( thread fd mx -- )
[ EPOLLOUT do-epoll-add ] [ call-next-method ] 2bi ;
: epoll-write-task ( mx fd -- )
over mx-writes at* [ perform-io-task ] [ 2drop ] if ;
M: epoll-mx remove-input-callbacks ( fd mx -- seq )
2dup reads>> key? [
[ call-next-method ] [ EPOLLIN do-epoll-del ] 2bi
] [ 2drop f ] if ;
: handle-event ( mx kevent -- )
epoll-event-fd 2dup epoll-read-task epoll-write-task ;
M: epoll-mx remove-output-callbacks ( fd mx -- seq )
2dup writes>> key? [
[ EPOLLOUT do-epoll-del ] [ call-next-method ] 2bi
] [ 2drop f ] if ;
: wait-event ( mx us -- n )
[ [ fd>> ] [ events>> ] bi max-events ] [ 1000 /i ] bi*
epoll_wait dup multiplexer-error ;
: handle-event ( mx event -- )
epoll-event-fd [ input-available ] [ output-available ] 2bi ;
: handle-events ( mx n -- )
[
over epoll-mx-events epoll-event-nth handle-event
] with each ;
[ dup events>> ] dip head-slice [ handle-event ] with each ;
M: epoll-mx wait-for-events ( ms mx -- )
dup rot wait-event handle-events ;
M: epoll-mx wait-for-events ( us mx -- )
swap 60000 or dupd wait-event handle-events ;

View File

@ -18,14 +18,15 @@ FUNCTION: int epoll_wait ( int epfd, epoll_event* events, int maxevents, int tim
: EPOLL_CTL_DEL 2 ; inline ! Remove a file decriptor from the interface.
: EPOLL_CTL_MOD 3 ; inline ! Change file decriptor epoll_event structure.
: EPOLLIN HEX: 001 ; inline
: EPOLLPRI HEX: 002 ; inline
: EPOLLOUT HEX: 004 ; inline
: EPOLLRDNORM HEX: 040 ; inline
: EPOLLRDBAND HEX: 080 ; inline
: EPOLLWRNORM HEX: 100 ; inline
: EPOLLWRBAND HEX: 200 ; inline
: EPOLLMSG HEX: 400 ; inline
: EPOLLERR HEX: 008 ; inline
: EPOLLHUP HEX: 010 ; inline
: EPOLLET 31 2^ ; inline
: EPOLLIN HEX: 001 ; inline
: EPOLLPRI HEX: 002 ; inline
: EPOLLOUT HEX: 004 ; inline
: EPOLLRDNORM HEX: 040 ; inline
: EPOLLRDBAND HEX: 080 ; inline
: EPOLLWRNORM HEX: 100 ; inline
: EPOLLWRBAND HEX: 200 ; inline
: EPOLLMSG HEX: 400 ; inline
: EPOLLERR HEX: 008 ; inline
: EPOLLHUP HEX: 010 ; inline
: EPOLLONESHOT 30 2^ ; inline
: EPOLLET 31 2^ ; inline