Threading/IO updates
parent
422205e4f9
commit
151c6fac3c
|
@ -36,6 +36,7 @@ USE: words
|
||||||
: boot ( -- )
|
: boot ( -- )
|
||||||
#! Initialize an interpreter with the basic services.
|
#! Initialize an interpreter with the basic services.
|
||||||
init-namespaces
|
init-namespaces
|
||||||
|
init-threads
|
||||||
init-stdio
|
init-stdio
|
||||||
"HOME" os-env [ "." ] unless* "~" set
|
"HOME" os-env [ "." ] unless* "~" set
|
||||||
init-search-path ;
|
init-search-path ;
|
||||||
|
|
|
@ -26,16 +26,8 @@
|
||||||
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
IN: win32-io-internals
|
IN: win32-io-internals
|
||||||
USE: alien
|
USING: alien errors kernel kernel-internals lists math namespaces threads
|
||||||
USE: errors
|
vectors win32-api ;
|
||||||
USE: kernel
|
|
||||||
USE: kernel-internals
|
|
||||||
USE: lists
|
|
||||||
USE: math
|
|
||||||
USE: namespaces
|
|
||||||
USE: prettyprint
|
|
||||||
USE: vectors
|
|
||||||
USE: win32-api
|
|
||||||
|
|
||||||
SYMBOL: completion-port
|
SYMBOL: completion-port
|
||||||
SYMBOL: io-queue
|
SYMBOL: io-queue
|
||||||
|
@ -45,20 +37,11 @@ SYMBOL: callbacks
|
||||||
: handle-io-error ( -- )
|
: handle-io-error ( -- )
|
||||||
#! If a write or read call fails unexpectedly, throw an error.
|
#! If a write or read call fails unexpectedly, throw an error.
|
||||||
GetLastError [
|
GetLastError [
|
||||||
ERROR_IO_PENDING ERROR_HANDLE_EOF ERROR_SUCCESS
|
ERROR_IO_PENDING ERROR_HANDLE_EOF ERROR_SUCCESS WAIT_TIMEOUT
|
||||||
] contains? [
|
] contains? [
|
||||||
win32-throw-error
|
win32-throw-error
|
||||||
] unless ;
|
] unless ;
|
||||||
|
|
||||||
: win32-init-stdio ( -- )
|
|
||||||
INVALID_HANDLE_VALUE NULL NULL 1 CreateIoCompletionPort
|
|
||||||
completion-port set
|
|
||||||
|
|
||||||
<namespace> [
|
|
||||||
32 <vector> callbacks set
|
|
||||||
f free-list set
|
|
||||||
] extend io-queue set ;
|
|
||||||
|
|
||||||
: add-completion ( handle -- )
|
: add-completion ( handle -- )
|
||||||
completion-port get NULL 1 CreateIoCompletionPort drop ;
|
completion-port get NULL 1 CreateIoCompletionPort drop ;
|
||||||
|
|
||||||
|
@ -125,10 +108,43 @@ END-STRUCT
|
||||||
callbacks get vector-nth cdr
|
callbacks get vector-nth cdr
|
||||||
] bind ;
|
] bind ;
|
||||||
|
|
||||||
: win32-next-io-task ( -- quot )
|
: (wait-for-io) ( timeout -- ? overlapped len )
|
||||||
completion-port get <indirect-pointer> dup >r <indirect-pointer>
|
>r completion-port get
|
||||||
<indirect-pointer> dup >r INFINITE GetQueuedCompletionStatus
|
<indirect-pointer> [ 0 swap set-indirect-pointer-value ] keep
|
||||||
[ handle-io-error ] unless
|
<indirect-pointer>
|
||||||
r> r> indirect-pointer-value swap indirect-pointer-value <alien>
|
<indirect-pointer>
|
||||||
overlapped-ext-user-data get-io-callback call ;
|
pick over r> -rot >r >r GetQueuedCompletionStatus r> r> ;
|
||||||
|
|
||||||
|
: overlapped>callback ( overlapped -- callback )
|
||||||
|
indirect-pointer-value dup 0 = [
|
||||||
|
drop f
|
||||||
|
] [
|
||||||
|
<alien> overlapped-ext-user-data get-io-callback
|
||||||
|
] ifte ;
|
||||||
|
|
||||||
|
: wait-for-io ( timeout -- callback len )
|
||||||
|
(wait-for-io) rot [ handle-io-error ] unless
|
||||||
|
overlapped>callback swap indirect-pointer-value ;
|
||||||
|
|
||||||
|
: win32-next-io-task ( -- )
|
||||||
|
INFINITE wait-for-io swap call ;
|
||||||
|
|
||||||
|
: win32-io-thread ( -- )
|
||||||
|
10 wait-for-io swap [
|
||||||
|
[ schedule-thread call ] callcc0
|
||||||
|
] [
|
||||||
|
drop yield
|
||||||
|
] ifte*
|
||||||
|
win32-io-thread ;
|
||||||
|
|
||||||
|
: win32-init-stdio ( -- )
|
||||||
|
INVALID_HANDLE_VALUE NULL NULL 1 CreateIoCompletionPort
|
||||||
|
completion-port set
|
||||||
|
|
||||||
|
<namespace> [
|
||||||
|
32 <vector> callbacks set
|
||||||
|
f free-list set
|
||||||
|
] extend io-queue set
|
||||||
|
|
||||||
|
[ win32-io-thread ] in-thread ;
|
||||||
|
|
||||||
|
|
|
@ -166,3 +166,30 @@ M: cons hashcode ( cons -- hash ) car hashcode ;
|
||||||
#! Make a list of elements that occur in list2 but not
|
#! Make a list of elements that occur in list2 but not
|
||||||
#! list1.
|
#! list1.
|
||||||
[ over contains? not ] subset nip ;
|
[ over contains? not ] subset nip ;
|
||||||
|
|
||||||
|
TUPLE: dlist first last ;
|
||||||
|
TUPLE: dlist-node next prev data ;
|
||||||
|
|
||||||
|
C: dlist ;
|
||||||
|
C: dlist-node
|
||||||
|
[ set-dlist-node-next ] keep
|
||||||
|
[ set-dlist-node-prev ] keep
|
||||||
|
[ set-dlist-node-data ] keep ;
|
||||||
|
|
||||||
|
: dlist-push-end ( data dlist -- )
|
||||||
|
[ dlist-last f <dlist-node> ] keep
|
||||||
|
[ dlist-last [ dupd set-dlist-node-next ] when* ] keep
|
||||||
|
2dup set-dlist-last
|
||||||
|
dup dlist-first [ 2drop ] [ set-dlist-first ] ifte ;
|
||||||
|
|
||||||
|
: dlist-empty? ( dlist -- ? )
|
||||||
|
dlist-first f = ;
|
||||||
|
|
||||||
|
: (dlist-pop-front) ( dlist -- data )
|
||||||
|
[ dlist-first dlist-node-data ] keep
|
||||||
|
[ dup dlist-first dlist-node-next swap set-dlist-first ] keep
|
||||||
|
dup dlist-first [ drop ] [ f swap set-dlist-last ] ifte ;
|
||||||
|
|
||||||
|
: dlist-pop-front ( dlist -- data )
|
||||||
|
dup dlist-empty? [ drop f ] [ (dlist-pop-front) ] ifte ;
|
||||||
|
|
||||||
|
|
|
@ -9,13 +9,14 @@ USING: io-internals kernel kernel-internals lists namespaces ;
|
||||||
: run-queue ( -- queue ) 9 getenv ;
|
: run-queue ( -- queue ) 9 getenv ;
|
||||||
: set-run-queue ( queue -- ) 9 setenv ;
|
: set-run-queue ( queue -- ) 9 setenv ;
|
||||||
|
|
||||||
|
: init-threads ( -- )
|
||||||
|
<dlist> set-run-queue ;
|
||||||
|
|
||||||
: next-thread ( -- quot )
|
: next-thread ( -- quot )
|
||||||
#! Get and remove the next quotation from the run queue.
|
run-queue dlist-pop-front ;
|
||||||
run-queue dup [ uncons set-run-queue ] when ;
|
|
||||||
|
|
||||||
: schedule-thread ( quot -- )
|
: schedule-thread ( quot -- )
|
||||||
#! Add a quotation to the run queue.
|
run-queue dlist-push-end ;
|
||||||
run-queue cons set-run-queue ;
|
|
||||||
|
|
||||||
: (yield) ( -- )
|
: (yield) ( -- )
|
||||||
#! If there is a quotation in the run queue, call it,
|
#! If there is a quotation in the run queue, call it,
|
||||||
|
@ -37,3 +38,4 @@ USING: io-internals kernel kernel-internals lists namespaces ;
|
||||||
#! eventually be restored by a future call to (yield) or
|
#! eventually be restored by a future call to (yield) or
|
||||||
#! yield.
|
#! yield.
|
||||||
[ schedule-thread (yield) ] callcc0 ;
|
[ schedule-thread (yield) ] callcc0 ;
|
||||||
|
|
||||||
|
|
|
@ -363,7 +363,7 @@ M: alien handle-event ( event -- ? )
|
||||||
|
|
||||||
: console-loop ( -- )
|
: console-loop ( -- )
|
||||||
redraw-console get [ draw-console redraw-console off ] when
|
redraw-console get [ draw-console redraw-console off ] when
|
||||||
check-event [ console-loop ] when ;
|
yield check-event [ console-loop ] when ;
|
||||||
|
|
||||||
: console-quit ( -- )
|
: console-quit ( -- )
|
||||||
input-continuation get [ f swap call ] when*
|
input-continuation get [ f swap call ] when*
|
||||||
|
|
|
@ -43,6 +43,7 @@ USE: words
|
||||||
CONSTANT: ERROR_SUCCESS 0 ;
|
CONSTANT: ERROR_SUCCESS 0 ;
|
||||||
CONSTANT: ERROR_HANDLE_EOF 38 ;
|
CONSTANT: ERROR_HANDLE_EOF 38 ;
|
||||||
CONSTANT: ERROR_IO_PENDING 997 ;
|
CONSTANT: ERROR_IO_PENDING 997 ;
|
||||||
|
CONSTANT: WAIT_TIMEOUT 258 ;
|
||||||
|
|
||||||
: FORMAT_MESSAGE_ALLOCATE_BUFFER HEX: 00000100 ;
|
: FORMAT_MESSAGE_ALLOCATE_BUFFER HEX: 00000100 ;
|
||||||
: FORMAT_MESSAGE_IGNORE_INSERTS HEX: 00000200 ;
|
: FORMAT_MESSAGE_IGNORE_INSERTS HEX: 00000200 ;
|
||||||
|
|
Loading…
Reference in New Issue