fix win32 for .76
parent
5526fc24bb
commit
dba5403e71
|
@ -25,12 +25,15 @@
|
||||||
|
|
||||||
IN: win32-io-internals
|
IN: win32-io-internals
|
||||||
USING: alien errors kernel kernel-internals lists math namespaces threads
|
USING: alien errors kernel kernel-internals lists math namespaces threads
|
||||||
vectors win32-api io generic io-internals sequences ;
|
vectors win32-api io generic io-internals sequences prettyprint ;
|
||||||
|
|
||||||
SYMBOL: completion-port
|
SYMBOL: completion-port
|
||||||
SYMBOL: io-queue
|
SYMBOL: io-queue
|
||||||
SYMBOL: free-list
|
|
||||||
SYMBOL: callbacks
|
TUPLE: io-queue free-list callbacks ;
|
||||||
|
TUPLE: io-callback overlapped quotation stream ;
|
||||||
|
|
||||||
|
GENERIC: expire
|
||||||
|
|
||||||
: expected-error? ( -- bool )
|
: expected-error? ( -- bool )
|
||||||
[
|
[
|
||||||
|
@ -73,41 +76,31 @@ BEGIN-STRUCT: indirect-pointer
|
||||||
FIELD: int value
|
FIELD: int value
|
||||||
END-STRUCT
|
END-STRUCT
|
||||||
|
|
||||||
: num-callbacks ( -- len )
|
: <overlapped> ( -- overlapped )
|
||||||
#! Returns the length of the callback vector.
|
"overlapped-ext" c-size malloc <alien> ;
|
||||||
io-queue get [ callbacks get length ] bind ;
|
|
||||||
|
|
||||||
: set-callback-quot ( quot index -- )
|
C: io-queue ( -- queue )
|
||||||
io-queue get [
|
0 <vector> over set-io-queue-callbacks ;
|
||||||
dup >r callbacks get nth car swap cons
|
|
||||||
r> callbacks get set-nth
|
|
||||||
] bind ;
|
|
||||||
|
|
||||||
: new-overlapped ( -- index )
|
C: io-callback ( -- callback )
|
||||||
#! Allocates and returns a new entry for the io queue.
|
io-queue get io-queue-callbacks [ push ] 2keep
|
||||||
#! The new index in the callback vector is returned.
|
length 1 - <overlapped> [ set-overlapped-ext-user-data ] keep
|
||||||
io-queue get [
|
swap [ set-io-callback-overlapped ] keep ;
|
||||||
"overlapped-ext" c-type [ "width" get ] bind malloc <alien>
|
|
||||||
dup num-callbacks swap
|
|
||||||
set-overlapped-ext-user-data
|
|
||||||
unit num-callbacks dup >r callbacks get set-nth r>
|
|
||||||
] bind ;
|
|
||||||
|
|
||||||
: alloc-io-task ( quot -- overlapped )
|
: alloc-io-callback ( quot stream -- overlapped )
|
||||||
io-queue get [
|
io-queue get io-queue-free-list [
|
||||||
free-list get [
|
uncons io-queue get [ set-io-queue-free-list ] keep
|
||||||
uncons free-list set
|
io-queue-callbacks nth
|
||||||
] [ new-overlapped ] ifte*
|
] [ <io-callback> ] ifte*
|
||||||
[ set-callback-quot ] keep
|
[ set-io-callback-stream ] keep
|
||||||
callbacks get nth car
|
[ set-io-callback-quotation ] keep
|
||||||
] bind ;
|
io-callback-overlapped ;
|
||||||
|
|
||||||
: get-io-callback ( index -- callback )
|
: get-io-callback ( index -- callback )
|
||||||
#! Returns and frees the io queue entry at index.
|
dup io-queue get io-queue-callbacks nth swap
|
||||||
io-queue get [
|
io-queue get [ io-queue-free-list cons ] keep set-io-queue-free-list
|
||||||
dup free-list [ cons ] change
|
[ f swap set-io-callback-stream ] keep
|
||||||
callbacks get nth cdr
|
io-callback-quotation ;
|
||||||
] bind ;
|
|
||||||
|
|
||||||
: (wait-for-io) ( timeout -- error overlapped len )
|
: (wait-for-io) ( timeout -- error overlapped len )
|
||||||
>r completion-port get
|
>r completion-port get
|
||||||
|
@ -123,6 +116,10 @@ END-STRUCT
|
||||||
<alien> overlapped-ext-user-data get-io-callback
|
<alien> overlapped-ext-user-data get-io-callback
|
||||||
] ifte ;
|
] ifte ;
|
||||||
|
|
||||||
|
: cancel-timedout ( -- )
|
||||||
|
io-queue get
|
||||||
|
io-queue-callbacks [ io-callback-stream [ expire ] when* ] each ;
|
||||||
|
|
||||||
: wait-for-io ( timeout -- callback len )
|
: wait-for-io ( timeout -- callback len )
|
||||||
(wait-for-io) overlapped>callback swap indirect-pointer-value
|
(wait-for-io) overlapped>callback swap indirect-pointer-value
|
||||||
rot [ queue-error ] unless ;
|
rot [ queue-error ] unless ;
|
||||||
|
@ -131,7 +128,7 @@ END-STRUCT
|
||||||
INFINITE wait-for-io swap call ;
|
INFINITE wait-for-io swap call ;
|
||||||
|
|
||||||
: win32-io-thread ( -- )
|
: win32-io-thread ( -- )
|
||||||
10 wait-for-io swap [
|
cancel-timedout 10 wait-for-io swap [
|
||||||
[ schedule-thread call ] callcc0 2drop
|
[ schedule-thread call ] callcc0 2drop
|
||||||
] [
|
] [
|
||||||
drop yield
|
drop yield
|
||||||
|
@ -141,11 +138,6 @@ END-STRUCT
|
||||||
: win32-init-stdio ( -- )
|
: win32-init-stdio ( -- )
|
||||||
INVALID_HANDLE_VALUE NULL NULL 1 CreateIoCompletionPort
|
INVALID_HANDLE_VALUE NULL NULL 1 CreateIoCompletionPort
|
||||||
completion-port set
|
completion-port set
|
||||||
|
<io-queue> io-queue set
|
||||||
<namespace> [
|
|
||||||
32 <vector> callbacks set
|
|
||||||
f free-list set
|
|
||||||
] extend io-queue set
|
|
||||||
|
|
||||||
[ win32-io-thread ] in-thread ;
|
[ win32-io-thread ] in-thread ;
|
||||||
|
|
||||||
|
|
|
@ -118,4 +118,7 @@ END-STRUCT
|
||||||
|
|
||||||
: CloseHandle ( handle -- ? )
|
: CloseHandle ( handle -- ? )
|
||||||
"bool" "kernel32" "CloseHandle" [ "void*" ] alien-invoke ;
|
"bool" "kernel32" "CloseHandle" [ "void*" ] alien-invoke ;
|
||||||
|
|
||||||
|
: CancelIo ( handle -- )
|
||||||
|
"bool" "kernel32" "CancelIo" [ "void*" ] alien-invoke drop ;
|
||||||
|
|
||||||
|
|
|
@ -83,21 +83,31 @@ C: win32-server ( port -- server )
|
||||||
maybe-init-winsock new-socket swap over bind-socket dup listen-socket
|
maybe-init-winsock new-socket swap over bind-socket dup listen-socket
|
||||||
dup add-completion
|
dup add-completion
|
||||||
socket set
|
socket set
|
||||||
|
dup stream set
|
||||||
] extend over set-win32-server-this ;
|
] extend over set-win32-server-this ;
|
||||||
|
|
||||||
M: win32-server stream-close ( server -- )
|
M: win32-server stream-close ( server -- )
|
||||||
win32-server-this [ socket get CloseHandle drop ] bind ;
|
win32-server-this [ socket get CloseHandle drop ] bind ;
|
||||||
|
|
||||||
|
M: win32-server set-timeout ( timeout server -- )
|
||||||
|
win32-server-this [ timeout set ] bind ;
|
||||||
|
|
||||||
|
M: win32-server expire ( -- )
|
||||||
|
win32-server-this [
|
||||||
|
timeout get [ millis cutoff get > [ socket get CancelIo ] when ] when
|
||||||
|
] bind ;
|
||||||
|
|
||||||
IN: io
|
IN: io
|
||||||
: accept ( server -- client )
|
: accept ( server -- client )
|
||||||
win32-server-this [
|
win32-server-this [
|
||||||
new-socket 64 <buffer>
|
update-timeout new-socket 64 <buffer>
|
||||||
[
|
[
|
||||||
alloc-io-task init-overlapped >r >r >r socket get r> r>
|
stream get alloc-io-callback init-overlapped
|
||||||
|
>r >r >r socket get r> r>
|
||||||
buffer-ptr <alien> 0 32 32 NULL r> AcceptEx
|
buffer-ptr <alien> 0 32 32 NULL r> AcceptEx
|
||||||
[ handle-socket-error ] unless stop
|
[ handle-socket-error ] unless stop
|
||||||
] callcc1 pending-error drop
|
] callcc1 pending-error drop
|
||||||
swap dup add-completion <win32-stream> dupd <win32-client-stream>
|
swap dup add-completion <win32-stream> <line-reader>
|
||||||
swap buffer-free
|
dupd <win32-client-stream> swap buffer-free
|
||||||
] bind ;
|
] bind ;
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,9 @@ SYMBOL: in-buffer
|
||||||
SYMBOL: out-buffer
|
SYMBOL: out-buffer
|
||||||
SYMBOL: fileptr
|
SYMBOL: fileptr
|
||||||
SYMBOL: file-size
|
SYMBOL: file-size
|
||||||
|
SYMBOL: stream
|
||||||
|
SYMBOL: timeout
|
||||||
|
SYMBOL: cutoff
|
||||||
|
|
||||||
: pending-error ( len/status -- len/status )
|
: pending-error ( len/status -- len/status )
|
||||||
dup [ win32-throw-error ] unless ;
|
dup [ win32-throw-error ] unless ;
|
||||||
|
@ -51,9 +54,12 @@ SYMBOL: file-size
|
||||||
: update-file-pointer ( whence -- )
|
: update-file-pointer ( whence -- )
|
||||||
file-size get [ fileptr [ + ] change ] [ drop ] ifte ;
|
file-size get [ fileptr [ + ] change ] [ drop ] ifte ;
|
||||||
|
|
||||||
|
: update-timeout ( -- )
|
||||||
|
timeout get [ millis + cutoff set ] when* ;
|
||||||
|
|
||||||
: flush-output ( -- )
|
: flush-output ( -- )
|
||||||
[
|
update-timeout [
|
||||||
alloc-io-task init-overlapped >r
|
stream get alloc-io-callback init-overlapped >r
|
||||||
handle get out-buffer get [ buffer@ ] keep buffer-length
|
handle get out-buffer get [ buffer@ ] keep buffer-length
|
||||||
NULL r> WriteFile [ handle-io-error ] unless stop
|
NULL r> WriteFile [ handle-io-error ] unless stop
|
||||||
] callcc1 pending-error
|
] callcc1 pending-error
|
||||||
|
@ -79,8 +85,8 @@ M: string do-write ( str -- )
|
||||||
] ifte ;
|
] ifte ;
|
||||||
|
|
||||||
: fill-input ( -- )
|
: fill-input ( -- )
|
||||||
[
|
update-timeout [
|
||||||
alloc-io-task init-overlapped >r
|
stream get alloc-io-callback init-overlapped >r
|
||||||
handle get in-buffer get [ buffer@ ] keep
|
handle get in-buffer get [ buffer@ ] keep
|
||||||
buffer-capacity file-size get [ fileptr get - min ] when*
|
buffer-capacity file-size get [ fileptr get - min ] when*
|
||||||
NULL r>
|
NULL r>
|
||||||
|
@ -113,34 +119,21 @@ M: string do-write ( str -- )
|
||||||
: peek-input ( -- str )
|
: peek-input ( -- str )
|
||||||
1 in-buffer get buffer-first-n ;
|
1 in-buffer get buffer-first-n ;
|
||||||
|
|
||||||
: do-read-line ( sbuf -- str )
|
M: win32-stream stream-format ( str style stream -- )
|
||||||
1 consume-input dup length 0 = [ drop >string-or-f ] [
|
|
||||||
dup "\r" = [
|
|
||||||
peek-input "\n" = [ 1 consume-input drop ] when
|
|
||||||
drop >string
|
|
||||||
] [
|
|
||||||
dup "\n" = [
|
|
||||||
peek-input "\r" = [ 1 consume-input drop ] when
|
|
||||||
drop >string
|
|
||||||
] [
|
|
||||||
dupd nappend do-read-line
|
|
||||||
] ifte
|
|
||||||
] ifte
|
|
||||||
] ifte ;
|
|
||||||
|
|
||||||
M: win32-stream stream-write-attr ( str style stream -- )
|
|
||||||
win32-stream-this nip [ do-write ] bind ;
|
win32-stream-this nip [ do-write ] bind ;
|
||||||
|
|
||||||
M: win32-stream stream-readln ( stream -- str )
|
|
||||||
win32-stream-this [ 80 <sbuf> do-read-line ] bind ;
|
|
||||||
|
|
||||||
M: win32-stream stream-read ( count stream -- str )
|
M: win32-stream stream-read ( count stream -- str )
|
||||||
win32-stream-this [ dup <sbuf> swap do-read-count ] bind ;
|
win32-stream-this [ dup <sbuf> swap do-read-count ] bind ;
|
||||||
|
|
||||||
|
M: win32-stream stream-read1 ( stream -- str )
|
||||||
|
win32-stream-this [
|
||||||
|
1 consume-input dup length 0 = [ drop f ] when first
|
||||||
|
] bind ;
|
||||||
|
|
||||||
M: win32-stream stream-flush ( stream -- )
|
M: win32-stream stream-flush ( stream -- )
|
||||||
win32-stream-this [ maybe-flush-output ] bind ;
|
win32-stream-this [ maybe-flush-output ] bind ;
|
||||||
|
|
||||||
M: win32-stream stream-auto-flush ( stream -- )
|
M: win32-stream stream-finish ( stream -- )
|
||||||
drop ;
|
drop ;
|
||||||
|
|
||||||
M: win32-stream stream-close ( stream -- )
|
M: win32-stream stream-close ( stream -- )
|
||||||
|
@ -154,6 +147,14 @@ M: win32-stream stream-close ( stream -- )
|
||||||
M: win32-stream win32-stream-handle ( stream -- handle )
|
M: win32-stream win32-stream-handle ( stream -- handle )
|
||||||
win32-stream-this [ handle get ] bind ;
|
win32-stream-this [ handle get ] bind ;
|
||||||
|
|
||||||
|
M: win32-stream set-timeout ( timeout stream -- )
|
||||||
|
win32-stream-this [ timeout set ] bind ;
|
||||||
|
|
||||||
|
M: win32-stream expire ( stream -- )
|
||||||
|
win32-stream-this [
|
||||||
|
timeout get [ millis cutoff get > [ handle get CancelIo ] when ] when
|
||||||
|
] bind ;
|
||||||
|
|
||||||
C: win32-stream ( handle -- stream )
|
C: win32-stream ( handle -- stream )
|
||||||
swap <namespace> [
|
swap <namespace> [
|
||||||
dup NULL GetFileSize dup -1 = not [
|
dup NULL GetFileSize dup -1 = not [
|
||||||
|
@ -163,10 +164,11 @@ C: win32-stream ( handle -- stream )
|
||||||
4096 <buffer> in-buffer set
|
4096 <buffer> in-buffer set
|
||||||
4096 <buffer> out-buffer set
|
4096 <buffer> out-buffer set
|
||||||
0 fileptr set
|
0 fileptr set
|
||||||
|
dup stream set
|
||||||
] extend over set-win32-stream-this ;
|
] extend over set-win32-stream-this ;
|
||||||
|
|
||||||
: <win32-file-reader> ( path -- stream )
|
: <win32-file-reader> ( path -- stream )
|
||||||
t f win32-open-file <win32-stream> ;
|
t f win32-open-file <win32-stream> <line-reader> ;
|
||||||
|
|
||||||
: <win32-file-writer> ( path -- stream )
|
: <win32-file-writer> ( path -- stream )
|
||||||
f t win32-open-file <win32-stream> ;
|
f t win32-open-file <win32-stream> ;
|
||||||
|
|
Loading…
Reference in New Issue