Updating epoll code
parent
3bd4c0b4be
commit
a4fc4046a0
|
|
@ -1,8 +1,8 @@
|
||||||
! Copyright (C) 2008 Slava Pestov.
|
! Copyright (C) 2008 Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: alien.c-types kernel io.ports io.unix.backend
|
USING: accessors alien.c-types kernel io.ports io.unix.backend
|
||||||
bit-arrays sequences assocs unix unix.linux.epoll math
|
bit-arrays sequences assocs struct-arrays unix unix.linux.epoll
|
||||||
namespaces unix.time ;
|
math namespaces locals unix.time ;
|
||||||
IN: io.unix.epoll
|
IN: io.unix.epoll
|
||||||
|
|
||||||
TUPLE: epoll-mx < mx events ;
|
TUPLE: epoll-mx < mx events ;
|
||||||
|
|
@ -14,47 +14,48 @@ TUPLE: epoll-mx < mx events ;
|
||||||
|
|
||||||
: <epoll-mx> ( -- mx )
|
: <epoll-mx> ( -- mx )
|
||||||
epoll-mx new-mx
|
epoll-mx new-mx
|
||||||
max-events epoll_create dup io-error over set-mx-fd
|
max-events epoll_create dup io-error >>fd
|
||||||
max-events "epoll-event" <c-array> over set-epoll-mx-events ;
|
max-events "epoll-event" <struct-array> >>events ;
|
||||||
|
|
||||||
GENERIC: io-task-events ( task -- n )
|
: make-event ( fd events -- event )
|
||||||
|
|
||||||
M: input-task io-task-events drop EPOLLIN ;
|
|
||||||
|
|
||||||
M: output-task io-task-events drop EPOLLOUT ;
|
|
||||||
|
|
||||||
: make-event ( task -- event )
|
|
||||||
"epoll-event" <c-object>
|
"epoll-event" <c-object>
|
||||||
over io-task-events over set-epoll-event-events
|
[ set-epoll-event-events ] keep
|
||||||
swap io-task-fd over set-epoll-event-fd ;
|
[ set-epoll-event-fd ] keep ;
|
||||||
|
|
||||||
: do-epoll-ctl ( task mx what -- )
|
:: do-epoll-ctl ( fd mx what events -- )
|
||||||
>r mx-fd r> rot dup io-task-fd swap make-event
|
mx fd>> what fd events make-event what epoll_ctl io-error ;
|
||||||
epoll_ctl io-error ;
|
|
||||||
|
|
||||||
M: epoll-mx register-io-task ( task mx -- )
|
: do-epoll-add ( fd mx events -- )
|
||||||
[ EPOLL_CTL_ADD do-epoll-ctl ] [ call-next-method ] 2bi ;
|
EPOLL_CTL_ADD swap EPOLLONESHOT bitor do-epoll-ctl ;
|
||||||
|
|
||||||
M: epoll-mx unregister-io-task ( task mx -- )
|
: do-epoll-del ( fd mx events -- )
|
||||||
[ call-next-method ] [ EPOLL_CTL_DEL do-epoll-ctl ] 2bi ;
|
EPOLL_CTL_DEL swap do-epoll-ctl ;
|
||||||
|
|
||||||
: wait-event ( mx timeout -- n )
|
M: epoll-mx add-input-callback ( thread fd mx -- )
|
||||||
>r { mx-fd epoll-mx-events } get-slots max-events
|
[ EPOLLIN do-epoll-add ] [ call-next-method ] 2bi ;
|
||||||
r> epoll_wait dup multiplexer-error ;
|
|
||||||
|
|
||||||
: epoll-read-task ( mx fd -- )
|
M: epoll-mx add-output-callback ( thread fd mx -- )
|
||||||
over mx-reads at* [ perform-io-task ] [ 2drop ] if ;
|
[ EPOLLOUT do-epoll-add ] [ call-next-method ] 2bi ;
|
||||||
|
|
||||||
: epoll-write-task ( mx fd -- )
|
M: epoll-mx remove-input-callbacks ( fd mx -- seq )
|
||||||
over mx-writes at* [ perform-io-task ] [ 2drop ] if ;
|
2dup reads>> key? [
|
||||||
|
[ call-next-method ] [ EPOLLIN do-epoll-del ] 2bi
|
||||||
|
] [ 2drop f ] if ;
|
||||||
|
|
||||||
: handle-event ( mx kevent -- )
|
M: epoll-mx remove-output-callbacks ( fd mx -- seq )
|
||||||
epoll-event-fd 2dup epoll-read-task epoll-write-task ;
|
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 -- )
|
: handle-events ( mx n -- )
|
||||||
[
|
[ dup events>> ] dip head-slice [ handle-event ] with each ;
|
||||||
over epoll-mx-events epoll-event-nth handle-event
|
|
||||||
] with each ;
|
|
||||||
|
|
||||||
M: epoll-mx wait-for-events ( ms mx -- )
|
M: epoll-mx wait-for-events ( us mx -- )
|
||||||
dup rot wait-event handle-events ;
|
swap 60000 or dupd wait-event handle-events ;
|
||||||
|
|
|
||||||
|
|
@ -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_DEL 2 ; inline ! Remove a file decriptor from the interface.
|
||||||
: EPOLL_CTL_MOD 3 ; inline ! Change file decriptor epoll_event structure.
|
: EPOLL_CTL_MOD 3 ; inline ! Change file decriptor epoll_event structure.
|
||||||
|
|
||||||
: EPOLLIN HEX: 001 ; inline
|
: EPOLLIN HEX: 001 ; inline
|
||||||
: EPOLLPRI HEX: 002 ; inline
|
: EPOLLPRI HEX: 002 ; inline
|
||||||
: EPOLLOUT HEX: 004 ; inline
|
: EPOLLOUT HEX: 004 ; inline
|
||||||
: EPOLLRDNORM HEX: 040 ; inline
|
: EPOLLRDNORM HEX: 040 ; inline
|
||||||
: EPOLLRDBAND HEX: 080 ; inline
|
: EPOLLRDBAND HEX: 080 ; inline
|
||||||
: EPOLLWRNORM HEX: 100 ; inline
|
: EPOLLWRNORM HEX: 100 ; inline
|
||||||
: EPOLLWRBAND HEX: 200 ; inline
|
: EPOLLWRBAND HEX: 200 ; inline
|
||||||
: EPOLLMSG HEX: 400 ; inline
|
: EPOLLMSG HEX: 400 ; inline
|
||||||
: EPOLLERR HEX: 008 ; inline
|
: EPOLLERR HEX: 008 ; inline
|
||||||
: EPOLLHUP HEX: 010 ; inline
|
: EPOLLHUP HEX: 010 ; inline
|
||||||
: EPOLLET 31 2^ ; inline
|
: EPOLLONESHOT 30 2^ ; inline
|
||||||
|
: EPOLLET 31 2^ ; inline
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue