Use kqueue on Mac OS X instead of select

db4
Slava Pestov 2008-12-09 17:49:03 -06:00
parent 4b927f732f
commit c2504f207d
4 changed files with 26 additions and 27 deletions

View File

@ -64,10 +64,10 @@ M: mx remove-output-callbacks writes>> delete-at* drop ;
GENERIC: wait-for-events ( ms mx -- ) GENERIC: wait-for-events ( ms mx -- )
: input-available ( fd mx -- ) : input-available ( fd mx -- )
remove-input-callbacks [ resume ] each ; reads>> delete-at* drop [ resume ] each ;
: output-available ( fd mx -- ) : output-available ( fd mx -- )
remove-output-callbacks [ resume ] each ; writes>> delete-at* drop [ resume ] each ;
M: fd cancel-operation ( fd -- ) M: fd cancel-operation ( fd -- )
dup disposed>> [ drop ] [ dup disposed>> [ drop ] [

View File

@ -2,10 +2,10 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.c-types combinators io.unix.backend USING: accessors alien.c-types combinators io.unix.backend
kernel math.bitwise sequences struct-arrays unix unix.kqueue kernel math.bitwise sequences struct-arrays unix unix.kqueue
unix.time ; unix.time assocs ;
IN: io.unix.kqueue IN: io.unix.kqueue
TUPLE: kqueue-mx < mx events monitors ; TUPLE: kqueue-mx < mx events ;
: max-events ( -- n ) : max-events ( -- n )
#! We read up to 256 events at a time. This is an arbitrary #! We read up to 256 events at a time. This is an arbitrary
@ -14,7 +14,6 @@ TUPLE: kqueue-mx < mx events monitors ;
: <kqueue-mx> ( -- mx ) : <kqueue-mx> ( -- mx )
kqueue-mx new-mx kqueue-mx new-mx
H{ } clone >>monitors
kqueue dup io-error >>fd kqueue dup io-error >>fd
max-events "kevent" <struct-array> >>events ; max-events "kevent" <struct-array> >>events ;
@ -35,30 +34,25 @@ M: kqueue-mx add-input-callback ( thread fd mx -- )
M: kqueue-mx add-output-callback ( thread fd mx -- ) M: kqueue-mx add-output-callback ( thread fd mx -- )
[ call-next-method ] [ [ call-next-method ] [
[ EVFILT_WRITE EV_DELETE make-kevent ] dip [ EVFILT_WRITE { EV_ADD EV_ONESHOT } flags make-kevent ] dip
register-kevent register-kevent
] 2bi ; ] 2bi ;
: cancel-input-callbacks ( fd mx -- seq ) M: kqueue-mx remove-input-callbacks ( fd mx -- seq )
[ 2dup reads>> key? [
[ call-next-method ] [
[ EVFILT_READ EV_DELETE make-kevent ] dip [ EVFILT_READ EV_DELETE make-kevent ] dip
register-kevent register-kevent
] [ remove-input-callbacks ] 2bi ; ] 2bi
] [ 2drop f ] if ;
: cancel-output-callbacks ( fd mx -- seq ) M: kqueue-mx remove-output-callbacks ( fd mx -- seq )
2dup writes>> key? [
[ [
[ EVFILT_WRITE EV_DELETE make-kevent ] dip [ EVFILT_WRITE EV_DELETE make-kevent ] dip
register-kevent register-kevent
] [ remove-output-callbacks ] 2bi ; ] [ call-next-method ] 2bi
] [ 2drop f ] if ;
M: fd cancel-operation ( fd -- )
dup disposed>> [ drop ] [
fd>>
mx get-global
[ cancel-input-callbacks [ t swap resume-with ] each ]
[ cancel-output-callbacks [ t swap resume-with ] each ]
2bi
] if ;
: wait-kevent ( mx timespec -- n ) : wait-kevent ( mx timespec -- n )
[ [

View File

@ -1,6 +1,10 @@
! 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.
IN: io.unix.macosx IN: io.unix.macosx
USING: io.unix.bsd io.backend system ; USING: io.unix.backend io.unix.bsd io.unix.kqueue io.backend
namespaces system ;
M: macosx init-io ( -- )
<kqueue-mx> mx set-global ;
macosx set-io-backend macosx set-io-backend

View File

@ -1,6 +1,7 @@
! 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.syntax system sequences vocabs.loader words ; USING: alien.syntax system sequences vocabs.loader words
accessors ;
IN: unix.kqueue IN: unix.kqueue
<< "unix.kqueue." os name>> append require >> << "unix.kqueue." os name>> append require >>