factor/extra/io/windows/nt/backend/backend.factor

104 lines
2.9 KiB
Factor
Raw Normal View History

2007-11-07 14:01:45 -05:00
USING: alien alien.c-types arrays assocs combinators
2008-05-13 19:24:46 -04:00
continuations destructors io io.backend io.ports
io.windows libc kernel math namespaces sequences
threads classes.tuple.lib windows windows.errors
2008-02-18 06:07:40 -05:00
windows.kernel32 strings splitting io.files qualified ascii
2008-04-11 22:51:21 -04:00
combinators.lib system accessors ;
2007-11-14 17:04:29 -05:00
QUALIFIED: windows.winsock
2007-09-20 18:09:08 -04:00
IN: io.windows.nt.backend
SYMBOL: io-hash
2008-02-18 06:07:40 -05:00
TUPLE: io-callback port thread ;
2007-09-20 18:09:08 -04:00
C: <io-callback> io-callback
: (make-overlapped) ( -- overlapped-ext )
2008-05-15 01:13:08 -04:00
"OVERLAPPED" malloc-object &free ;
2007-09-20 18:09:08 -04:00
: make-overlapped ( port -- overlapped-ext )
2008-05-15 01:13:08 -04:00
>r (make-overlapped)
r> handle>> ptr>> [ over set-OVERLAPPED-offset ] when* ;
2007-09-20 18:09:08 -04:00
2007-11-14 17:04:29 -05:00
: <completion-port> ( handle existing -- handle )
2007-09-20 18:09:08 -04:00
f 1 CreateIoCompletionPort dup win32-error=0/f ;
2007-11-14 17:04:29 -05:00
SYMBOL: master-completion-port
: <master-completion-port> ( -- handle )
INVALID_HANDLE_VALUE f <completion-port> ;
2007-09-20 18:09:08 -04:00
2008-04-02 21:09:56 -04:00
M: winnt add-completion ( handle -- )
2007-11-14 17:04:29 -05:00
master-completion-port get-global <completion-port> drop ;
2007-09-20 18:09:08 -04:00
: eof? ( error -- ? )
dup ERROR_HANDLE_EOF = swap ERROR_BROKEN_PIPE = or ;
2007-09-20 18:09:08 -04:00
: overlapped-error? ( port n -- ? )
zero? [
GetLastError {
{ [ dup expected-io-error? ] [ 2drop t ] }
2008-04-11 22:51:21 -04:00
{ [ dup eof? ] [ drop t >>eof drop f ] }
2008-04-11 13:56:11 -04:00
[ (win32-error-string) throw ]
} cond
] [
drop t
] if ;
2007-09-20 18:09:08 -04:00
2008-01-28 00:59:36 -05:00
: get-overlapped-result ( overlapped port -- bytes-transferred )
2008-04-11 22:51:21 -04:00
dup handle>> handle>> rot 0 <uint>
2008-01-28 00:59:36 -05:00
[ 0 GetOverlappedResult overlapped-error? drop ] keep *uint ;
: save-callback ( overlapped port -- )
2007-09-20 18:09:08 -04:00
[
2008-01-28 00:59:36 -05:00
<io-callback> swap
dup alien? [ "bad overlapped in save-callback" throw ] unless
2008-02-18 06:07:40 -05:00
io-hash get-global set-at
] "I/O" suspend 3drop ;
2007-09-20 18:09:08 -04:00
2008-05-15 01:13:08 -04:00
: twiddle-thumbs ( overlapped port -- bytes-transferred )
[ save-callback ]
[ get-overlapped-result ]
[ nip pending-error ]
2tri ;
:: wait-for-overlapped ( ms -- overlapped ? )
master-completion-port get-global
2008-02-21 21:57:41 -05:00
r> INFINITE or ! timeout
0 <int> ! bytes
f <void*> ! key
f <void*> ! overlapped
2008-05-15 01:13:08 -04:00
[
ms INFINITE or ! timeout
GetQueuedCompletionStatus
] keep *void* swap zero? ;
2007-09-20 18:09:08 -04:00
2008-01-28 00:59:36 -05:00
: lookup-callback ( overlapped -- callback )
io-hash get-global delete-at* drop
dup io-callback? [ "no callback in io-hash" throw ] unless ;
2007-09-20 18:09:08 -04:00
2008-01-24 18:21:10 -05:00
: handle-overlapped ( timeout -- ? )
wait-for-overlapped [
2008-05-15 01:13:08 -04:00
GetLastError dup expected-io-error? [ 2drop f ] [
>r lookup-callback [ thread>> ] [ port>> ] bi r>
dup eof?
[ drop t >>eof drop ]
[ (win32-error-string) >>error drop ] if
thread>> resume t
2007-09-20 18:09:08 -04:00
] if
] [
2008-01-24 18:21:10 -05:00
lookup-callback
2008-05-15 01:13:08 -04:00
thread>> resume t
2007-09-20 18:09:08 -04:00
] if ;
2008-04-02 21:09:56 -04:00
M: winnt cancel-io
2008-04-11 22:51:21 -04:00
handle>> handle>> CancelIo drop ;
2007-09-20 18:09:08 -04:00
2008-04-02 21:09:56 -04:00
M: winnt io-multiplex ( ms -- )
2008-05-15 01:13:08 -04:00
handle-overlapped [ 0 io-multiplex ] when ;
2008-04-02 21:09:56 -04:00
M: winnt init-io ( -- )
<master-completion-port> master-completion-port set-global
H{ } clone io-hash set-global
windows.winsock:init-winsock ;