Merge git://factorcode.org/git/factor
commit
23a429deeb
|
@ -1,4 +1,4 @@
|
|||
! Copyright (C) 2006, 2007 Slava Pestov.
|
||||
! Copyright (C) 2006, 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: arrays generator generator.registers generator.fixup
|
||||
hashtables kernel math namespaces sequences words
|
||||
|
|
|
@ -36,7 +36,7 @@ IN: bootstrap.image
|
|||
|
||||
: data-base 1024 ; inline
|
||||
|
||||
: userenv-size 40 ; inline
|
||||
: userenv-size 64 ; inline
|
||||
|
||||
: header-size 10 ; inline
|
||||
|
||||
|
|
|
@ -31,6 +31,7 @@ vocabs.loader system ;
|
|||
"libc" require
|
||||
|
||||
"io.streams.c" require
|
||||
"io.thread" require
|
||||
"vocabs.loader" require
|
||||
|
||||
"syntax" require
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
USING: help.markup help.syntax kernel ;
|
||||
IN: boxes
|
||||
|
||||
HELP: box
|
||||
{ $class-description "A data type holding a single value in the " { $link box-value } " slot. The " { $link box-full? } " slot indicates if the value is set." } ;
|
||||
|
||||
HELP: <box>
|
||||
{ $values { "box" box } }
|
||||
{ $description "Creates a new empty box." } ;
|
||||
|
||||
HELP: >box
|
||||
{ $values { "value" object } { "box" box } }
|
||||
{ $description "Stores a value into a box." }
|
||||
{ $errors "Throws an error if the box is full." } ;
|
||||
|
||||
HELP: box>
|
||||
{ $values { "box" box } { "value" "the value of the box" } }
|
||||
{ $description "Removes a value from a box." }
|
||||
{ $errors "Throws an error if the box is empty." } ;
|
||||
|
||||
HELP: ?box
|
||||
{ $values { "box" box } { "value" "the value of the box or " { $link f } } { "?" "a boolean" } }
|
||||
{ $description "If the box is full, removes the value from the box and pushes " { $link t } ". If the box is empty pushes " { $snippet "f f" } "." } ;
|
||||
|
||||
ARTICLE: "boxes" "Boxes"
|
||||
"A " { $emphasis "box" } " is a container which can either be empty or hold a single value."
|
||||
{ $subsection box }
|
||||
"Creating an empty box:"
|
||||
{ $subsection <box> }
|
||||
"Testing if a box is full:"
|
||||
{ $subsection box-full? }
|
||||
"Storing a value and removing a value from a box:"
|
||||
{ $subsection >box }
|
||||
{ $subsection box> }
|
||||
"Safely removing a value:"
|
||||
{ $subsection ?box } ;
|
||||
|
||||
ABOUT: "boxes"
|
|
@ -0,0 +1,24 @@
|
|||
IN: temporary
|
||||
USING: boxes namespaces tools.test ;
|
||||
|
||||
[ ] [ <box> "b" set ] unit-test
|
||||
|
||||
[ ] [ 3 "b" get >box ] unit-test
|
||||
|
||||
[ t ] [ "b" get box-full? ] unit-test
|
||||
|
||||
[ 4 "b" >box ] must-fail
|
||||
|
||||
[ 3 ] [ "b" get box> ] unit-test
|
||||
|
||||
[ f ] [ "b" get box-full? ] unit-test
|
||||
|
||||
[ "b" get box> ] must-fail
|
||||
|
||||
[ f f ] [ "b" get ?box ] unit-test
|
||||
|
||||
[ ] [ 12 "b" get >box ] unit-test
|
||||
|
||||
[ 12 t ] [ "b" get ?box ] unit-test
|
||||
|
||||
[ f ] [ "b" get box-full? ] unit-test
|
|
@ -0,0 +1,21 @@
|
|||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel ;
|
||||
IN: boxes
|
||||
|
||||
TUPLE: box value full? ;
|
||||
|
||||
: <box> ( -- box ) box construct-empty ;
|
||||
|
||||
: >box ( value box -- )
|
||||
dup box-full? [ "Box already has a value" throw ] when
|
||||
t over set-box-full?
|
||||
set-box-value ;
|
||||
|
||||
: box> ( box -- value )
|
||||
dup box-full? [ "Box empty" throw ] unless
|
||||
dup box-value f pick set-box-value
|
||||
f rot set-box-full? ;
|
||||
|
||||
: ?box ( box -- value/f ? )
|
||||
dup box-full? [ box> t ] [ drop f f ] if ;
|
|
@ -113,8 +113,13 @@ GENERIC: compute-restarts ( error -- seq )
|
|||
|
||||
PRIVATE>
|
||||
|
||||
SYMBOL: thread-error-hook
|
||||
|
||||
: rethrow ( error -- * )
|
||||
catchstack* empty? [ die ] when
|
||||
catchstack* empty? [
|
||||
thread-error-hook get-global
|
||||
[ 1 (throw) ] [ die ] if*
|
||||
] when
|
||||
dup save-error c> continue-with ;
|
||||
|
||||
: recover ( try recovery -- )
|
||||
|
|
|
@ -19,8 +19,8 @@ HOOK: normalize-pathname io-backend ( str -- newstr )
|
|||
|
||||
M: object normalize-pathname ;
|
||||
|
||||
[ init-io embedded? [ init-stdio ] unless ]
|
||||
"io.backend" add-init-hook
|
||||
|
||||
: set-io-backend ( backend -- )
|
||||
io-backend set-global init-io init-stdio ;
|
||||
|
||||
[ init-io embedded? [ init-stdio ] unless ]
|
||||
"io.backend" add-init-hook
|
||||
|
|
|
@ -55,11 +55,11 @@ USING: tools.test io.files io threads kernel continuations ;
|
|||
|
||||
[ f ] [ "test-blah" resource-path exists? ] unit-test
|
||||
|
||||
[ ] [ "test-quux.txt" resource-path [ [ yield "Hi" write ] in-thread ] with-file-writer ] unit-test
|
||||
[ ] [ "test-quux.txt" resource-path [ [ yield "Hi" write ] "Test" spawn drop ] with-file-writer ] unit-test
|
||||
|
||||
[ ] [ "test-quux.txt" resource-path delete-file ] unit-test
|
||||
|
||||
[ ] [ "test-quux.txt" resource-path [ [ yield "Hi" write ] in-thread ] with-file-writer ] unit-test
|
||||
[ ] [ "test-quux.txt" resource-path [ [ yield "Hi" write ] "Test" spawn drop ] with-file-writer ] unit-test
|
||||
|
||||
[ ] [ "test-quux.txt" "quux-test.txt" [ resource-path ] 2apply rename-file ] unit-test
|
||||
[ t ] [ "quux-test.txt" resource-path exists? ] unit-test
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
IN: io.thread
|
||||
USING: threads io.backend namespaces init ;
|
||||
|
||||
: io-thread ( -- )
|
||||
sleep-time io-multiplex yield ;
|
||||
|
||||
: start-io-thread ( -- )
|
||||
[ io-thread t ]
|
||||
"I/O wait" spawn-server
|
||||
\ io-thread set-global ;
|
||||
|
||||
[ start-io-thread ] "io.thread" add-init-hook
|
|
@ -1,4 +1,4 @@
|
|||
! Copyright (C) 2003, 2007 Slava Pestov.
|
||||
! Copyright (C) 2003, 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: arrays hashtables io kernel math memory namespaces
|
||||
parser sequences strings io.styles io.streams.lines
|
||||
|
|
|
@ -179,8 +179,5 @@ HELP: %
|
|||
{ $description "Appends a sequence to the end of the sequence being constructed by " { $link make } "." } ;
|
||||
|
||||
HELP: init-namespaces
|
||||
{ $description "Resets the name stack to its initial state, holding a single copy of the global namespace. This word is called during startup and is rarely useful, except in certain situations such as the example below." }
|
||||
{ $examples
|
||||
"You can use this word to spawn a new thread which does not inherit the parent thread's dynamic variable bindings:"
|
||||
{ $code "[ init-namestack do-some-work ] in-thread" }
|
||||
} ;
|
||||
{ $description "Resets the name stack to its initial state, holding a single copy of the global namespace." }
|
||||
$low-level-note ;
|
||||
|
|
|
@ -409,6 +409,7 @@ SYMBOL: interactive-vocabs
|
|||
"tools.memory"
|
||||
"tools.profiler"
|
||||
"tools.test"
|
||||
"tools.threads"
|
||||
"tools.time"
|
||||
"vocabs"
|
||||
"vocabs.loader"
|
||||
|
|
|
@ -1,47 +1,104 @@
|
|||
USING: help.markup help.syntax kernel kernel.private io
|
||||
threads.private continuations dlists ;
|
||||
threads.private continuations dlists init quotations strings
|
||||
assocs heaps boxes ;
|
||||
IN: threads
|
||||
|
||||
ARTICLE: "threads" "Threads"
|
||||
"A limited form of multiprocessing is supported in the form of cooperative threads, which are implemented on top of continuations. A thread will yield while waiting for I/O operations to complete, or when a yield has been explicitly requested."
|
||||
$nl
|
||||
"Words for working with threads are in the " { $vocab-link "threads" } " vocabulary."
|
||||
{ $subsection in-thread }
|
||||
ARTICLE: "threads-start/stop" "Starting and stopping threads"
|
||||
"Spawning new threads:"
|
||||
{ $subsection spawn }
|
||||
{ $subsection spawn-server }
|
||||
"Creating and spawning a thread can be factored out into two separate steps:"
|
||||
{ $subsection <thread> }
|
||||
{ $subsection (spawn) }
|
||||
"Threads stop either when the quotation given to " { $link spawn } " returns, or when the following word is called:"
|
||||
{ $subsection stop }
|
||||
"If the image is saved and started again, all runnable threads are stopped. Vocabularies wishing to have a background thread always running should use " { $link add-init-hook } "." ;
|
||||
|
||||
ARTICLE: "threads-yield" "Yielding and suspending threads"
|
||||
"Yielding to other threads:"
|
||||
{ $subsection yield }
|
||||
{ $subsection sleep }
|
||||
"Threads stop either when the quotation given to " { $link in-thread } " returns, or when the following word is called:"
|
||||
{ $subsection stop }
|
||||
"Continuations can be added to the run queue directly:"
|
||||
{ $subsection schedule-thread }
|
||||
{ $subsection schedule-thread-with }
|
||||
"Threads can be suspended and woken up at some point in the future when a condition is satisfied:"
|
||||
{ $subsection suspend }
|
||||
{ $subsection resume }
|
||||
{ $subsection resume-with } ;
|
||||
|
||||
ARTICLE: "thread-state" "Thread-local state"
|
||||
"Threads form a class of objects:"
|
||||
{ $subsection thread }
|
||||
"The current thread:"
|
||||
{ $subsection self }
|
||||
"Thread-local variables:"
|
||||
{ $subsection tnamespace }
|
||||
{ $subsection tget }
|
||||
{ $subsection tset }
|
||||
{ $subsection tchange }
|
||||
"Global hashtable of all threads, keyed by " { $link thread-id } ":"
|
||||
{ $subsection threads }
|
||||
"Threads have an identity independent of continuations. If a continuation is refied in one thread and then resumed in another thread, the code running in that continuation will observe a change in the value output by " { $link self } "." ;
|
||||
|
||||
ARTICLE: "thread-impl" "Thread implementation"
|
||||
"Thread implementation:"
|
||||
{ $subsection run-queue }
|
||||
{ $subsection sleep-queue } ;
|
||||
|
||||
ARTICLE: "threads" "Lightweight co-operative threads"
|
||||
"Factor supports lightweight co-operative threads implemented on top of continuations. A thread will yield while waiting for input/output operations to complete, or when a yield has been explicitly requested."
|
||||
$nl
|
||||
"Factor threads are very lightweight. Each thread can take as little as 900 bytes of memory. This library has been tested running hundreds of thousands of simple threads."
|
||||
$nl
|
||||
"Words for working with threads are in the " { $vocab-link "threads" } " vocabulary."
|
||||
{ $subsection "threads-start/stop" }
|
||||
{ $subsection "threads-yield" }
|
||||
{ $subsection "thread-state" }
|
||||
{ $subsection "thread-impl" } ;
|
||||
|
||||
ABOUT: "threads"
|
||||
|
||||
HELP: thread
|
||||
{ $class-description "A thread. The slots are as follows:"
|
||||
{ $list
|
||||
{ { $link thread-id } " - a unique identifier assigned to each thread." }
|
||||
{ { $link thread-name } " - the name passed to " { $link spawn } "." }
|
||||
{ { $link thread-quot } " - the initial quotation passed to " { $link spawn } "." }
|
||||
{ { $link thread-continuation } " - a " { $link box } "; if the thread is ready to run, the box holds the continuation, otherwise it is empty." }
|
||||
{ { $link thread-registered? } " - a boolean indicating whether the thread is eligible to run or not. Spawning a thread with " { $link (spawn) } " sets this flag and " { $link stop } " clears it." }
|
||||
}
|
||||
} ;
|
||||
|
||||
HELP: self
|
||||
{ $values { "thread" thread } }
|
||||
{ $description "Pushes the currently-running thread." } ;
|
||||
|
||||
HELP: <thread>
|
||||
{ $values { "quot" quotation } { "name" string } { "error-handler" quotation } }
|
||||
{ $description "Low-level thread constructor. The thread runs the quotation when spawned; the name is simply used to identify the thread for debugging purposes. The error handler is called if the thread's quotation throws an unhandled error; it should either print the error or notify another thread." }
|
||||
{ $notes "In most cases, user code should call " { $link spawn } " instead, however for control over the error handler quotation, threads can be created with " { $link <thread> } " then passed to " { $link (spawn) } "." } ;
|
||||
|
||||
HELP: run-queue
|
||||
{ $values { "queue" dlist } }
|
||||
{ $description "Outputs the runnable thread queue. By convention, continuations are queued with " { $link push-front }
|
||||
{ $var-description "Global variable holding the queue of runnable threads. Calls to " { $link yield } " switch to the thread which has been in the queue for the longest period of time."
|
||||
$nl
|
||||
"By convention, threads are queued with " { $link push-front }
|
||||
" and dequeued with " { $link pop-back } "." } ;
|
||||
|
||||
HELP: schedule-thread
|
||||
{ $values { "continuation" "a continuation reified by " { $link callcc0 } } }
|
||||
{ $description "Adds a runnable thread to the end of the run queue." } ;
|
||||
HELP: resume
|
||||
{ $values { "thread" thread } }
|
||||
{ $description "Adds a thread to the end of the run queue. The thread must have previously been suspended by a call to " { $link suspend } "." } ;
|
||||
|
||||
HELP: schedule-thread-with
|
||||
{ $values { "obj" "an object" } { "continuation" "a continuation reified by " { $link callcc1 } } }
|
||||
{ $description "Adds a runnable thread to the end of the run queue. When the thread runs the object is passed to the continuation using " { $link continue-with } "." } ;
|
||||
HELP: resume-with
|
||||
{ $values { "obj" object } { "thread" thread } }
|
||||
{ $description "Adds a thread to the end of the run queue together with an object to pass to the thread. The thread must have previously been suspended by a call to " { $link suspend } "; the object is returned from the " { $link suspend } " call." } ;
|
||||
|
||||
HELP: sleep-queue
|
||||
{ $var-description "Sleeping thread queue. This is not actually a queue, but an array of pairs of the shape " { $snippet "{ time continuation }" } "." } ;
|
||||
{ $var-description "A " { $link min-heap } " storing the queue of sleeping threads." } ;
|
||||
|
||||
HELP: sleep-time
|
||||
{ $values { "ms" "a non-negative integer" } }
|
||||
{ $description "Outputs the time until the next sleeping thread is scheduled to wake up, or a default sleep time if there are no sleeping threads." } ;
|
||||
{ $values { "ms" "a non-negative integer or " { $link f } } }
|
||||
{ $description "Outputs the time until the next sleeping thread is scheduled to wake up, which could be zero if there are threads in the run queue, or threads which need to wake up right now. If there are no runnable or sleeping threads, outputs " { $link f } "." } ;
|
||||
|
||||
HELP: stop
|
||||
{ $description "Stops the current thread." } ;
|
||||
{ $description "Stops the current thread. The thread may be started again from another thread using " { $link (spawn) } "." } ;
|
||||
|
||||
HELP: yield
|
||||
{ $description "Adds the current thread to the end of the run queue, and switches to the next runnable thread." } ;
|
||||
|
@ -50,20 +107,42 @@ HELP: sleep
|
|||
{ $values { "ms" "a non-negative integer" } }
|
||||
{ $description "Suspends the current thread for " { $snippet "ms" } " milliseconds. It will not get woken up before this time period elapses, but since the multitasker is co-operative, the precise wakeup time is dependent on when other threads yield." } ;
|
||||
|
||||
HELP: in-thread
|
||||
{ $values { "quot" "a quotation" } }
|
||||
{ $description "Spawns a new thread. The new thread begins running immediately."
|
||||
HELP: suspend
|
||||
{ $values { "quot" "a quotation with stack effect " { $snippet "( thread -- )" } } { "obj" object } }
|
||||
{ $description "Suspends the current thread and passes it to the quotation. After the quotation returns, control yields to the next runnable thread and the current thread does not execute again until it is resumed, and so the quotation must arrange for another thread to later resume the suspended thread with a call to " { $link resume } " or " { $link resume-with } "." } ;
|
||||
|
||||
HELP: spawn
|
||||
{ $values { "quot" quotation } { "name" string } }
|
||||
{ $description "Spawns a new thread. The thread begins executing the given quotation; the name is for debugging purposes. The new thread begins running immediately and the current thread is added to the end of the run queue."
|
||||
$nl
|
||||
"The new thread inherits the current data stack and name stack. The call stack initially contains the new quotation only, so when the quotation returns the thread stops. The catch stack contains a default handler which logs errors to the " { $link stdio } " stream." }
|
||||
"The new thread begins with an empty data stack, an empty catch stack, and a name stack containing the global namespace only. This means that the only way to pass data to the new thread is to explicitly construct a quotation containing the data, for example using " { $link curry } " or " { $link compose } "." }
|
||||
{ $examples
|
||||
{ $code "1 2 [ + . ] in-thread" }
|
||||
{ $code "1 2 [ + . ] 2curry \"Addition thread\" spawn" }
|
||||
} ;
|
||||
|
||||
HELP: idle-thread
|
||||
{ $description "Runs the idle thread, which services I/O requests and relinquishes control to the operating system until the next Factor thread has to wake up again."
|
||||
$nl
|
||||
"If the run queue is empty, the idle thread will sleep until the next sleeping thread is scheduled to wake up, otherwise it yields immediately after checking for any completed I/O requests." }
|
||||
{ $notes "This word should never be called directly. The idle thread is always running." } ;
|
||||
HELP: spawn-server
|
||||
{ $values { "quot" "a quotation with stack effect " { $snippet "( -- ? )" } } { "name" string } }
|
||||
{ $description "Convenience wrapper around " { $link spawn } " which repeatedly calls the quotation in a new thread until it outputs " { $link f } "." }
|
||||
{ $examples
|
||||
"A thread that runs forever:"
|
||||
{ $code "[ do-foo-bar t ] \"Foo bar server\" spawn-server" }
|
||||
} ;
|
||||
|
||||
HELP: init-threads
|
||||
{ $description "Called during startup to initialize the threading system. This word should never be called directly." } ;
|
||||
|
||||
HELP: tnamespace
|
||||
{ $values { "assoc" assoc } }
|
||||
{ $description "Outputs the current thread's set of thread-local variables." } ;
|
||||
|
||||
HELP: tget
|
||||
{ $values { "key" object } { "value" object } }
|
||||
{ $description "Outputs the value of a thread-local variable." } ;
|
||||
|
||||
HELP: tset
|
||||
{ $values { "value" object } { "key" object } }
|
||||
{ $description "Sets the value of a thread-local variable." } ;
|
||||
|
||||
HELP: tchange
|
||||
{ $values { "key" object } { "quot" "a quotation with stack effect " { $snippet "( value -- newvalue )" } } }
|
||||
{ $description "Applies the quotation to the current value of a thread-local variable, storing the result back to the same variable." } ;
|
||||
|
|
|
@ -2,11 +2,15 @@ USING: namespaces io tools.test threads kernel ;
|
|||
IN: temporary
|
||||
|
||||
3 "x" set
|
||||
[ yield 2 "x" set ] in-thread
|
||||
namespace [ [ yield 2 "x" set ] bind ] curry "Test" spawn drop
|
||||
[ 2 ] [ yield "x" get ] unit-test
|
||||
[ ] [ [ flush ] in-thread flush ] unit-test
|
||||
[ ] [ [ "Errors, errors" throw ] in-thread ] unit-test
|
||||
[ ] [ [ flush ] "flush test" spawn drop flush ] unit-test
|
||||
[ ] [ [ "Errors, errors" throw ] "error test" spawn drop ] unit-test
|
||||
yield
|
||||
|
||||
[ ] [ 0.3 sleep ] unit-test
|
||||
[ "hey" sleep ] must-fail
|
||||
|
||||
[ 3 ] [
|
||||
[ 3 swap resume-with ] "Test suspend" suspend
|
||||
] unit-test
|
||||
|
|
|
@ -1,71 +1,189 @@
|
|||
! Copyright (C) 2004, 2007 Slava Pestov.
|
||||
! Copyright (C) 2004, 2008 Slava Pestov.
|
||||
! Copyright (C) 2005 Mackenzie Straight.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
IN: threads
|
||||
USING: arrays init hashtables heaps io.backend kernel
|
||||
kernel.private math namespaces sequences vectors io system
|
||||
continuations debugger dlists ;
|
||||
USING: arrays hashtables heaps kernel kernel.private math
|
||||
namespaces sequences vectors continuations continuations.private
|
||||
dlists assocs system combinators debugger prettyprint io init
|
||||
boxes ;
|
||||
|
||||
SYMBOL: initial-thread
|
||||
|
||||
TUPLE: thread
|
||||
name quot error-handler
|
||||
id registered?
|
||||
continuation state
|
||||
mailbox variables ;
|
||||
|
||||
: self ( -- thread ) 40 getenv ; inline
|
||||
|
||||
! Thread-local storage
|
||||
: tnamespace ( -- assoc )
|
||||
self dup thread-variables
|
||||
[ ] [ H{ } clone dup rot set-thread-variables ] ?if ;
|
||||
|
||||
: tget ( key -- value )
|
||||
self thread-variables at ;
|
||||
|
||||
: tset ( value key -- )
|
||||
tnamespace set-at ;
|
||||
|
||||
: tchange ( key quot -- )
|
||||
tnamespace change-at ; inline
|
||||
|
||||
: threads 41 getenv ;
|
||||
|
||||
threads global [ H{ } assoc-like ] change-at
|
||||
|
||||
: thread ( id -- thread ) threads at ;
|
||||
|
||||
<PRIVATE
|
||||
|
||||
SYMBOL: sleep-queue
|
||||
: check-unregistered
|
||||
dup thread-registered?
|
||||
[ "Registering a thread twice" throw ] when ;
|
||||
|
||||
: sleep-time ( -- ms )
|
||||
sleep-queue get-global dup heap-empty?
|
||||
[ drop 1000 ] [ heap-peek nip millis [-] ] if ;
|
||||
: check-registered
|
||||
dup thread-registered?
|
||||
[ "Unregistering a thread twice" throw ] unless ;
|
||||
|
||||
: run-queue ( -- queue ) \ run-queue get-global ;
|
||||
: register-thread ( thread -- )
|
||||
check-unregistered
|
||||
t over set-thread-registered?
|
||||
dup thread-id threads set-at ;
|
||||
|
||||
: schedule-sleep ( continuation ms -- )
|
||||
sleep-queue get-global heap-push ;
|
||||
: unregister-thread ( thread -- )
|
||||
check-registered
|
||||
f over set-thread-registered?
|
||||
thread-id threads delete-at ;
|
||||
|
||||
: wake-up ( -- continuation )
|
||||
sleep-queue get-global heap-pop drop ;
|
||||
: set-self ( thread -- ) 40 setenv ; inline
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: schedule-thread ( continuation -- )
|
||||
run-queue push-front ;
|
||||
: <thread> ( quot name error-handler -- thread )
|
||||
\ thread counter <box> {
|
||||
set-thread-quot
|
||||
set-thread-name
|
||||
set-thread-error-handler
|
||||
set-thread-id
|
||||
set-thread-continuation
|
||||
} \ thread construct ;
|
||||
|
||||
: schedule-thread-with ( obj continuation -- )
|
||||
2array schedule-thread ;
|
||||
: run-queue 42 getenv ;
|
||||
|
||||
: stop ( -- )
|
||||
: sleep-queue 43 getenv ;
|
||||
|
||||
: resume ( thread -- )
|
||||
check-registered run-queue push-front ;
|
||||
|
||||
: resume-with ( obj thread -- )
|
||||
check-registered 2array run-queue push-front ;
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: schedule-sleep ( thread ms -- )
|
||||
>r check-registered r> sleep-queue heap-push ;
|
||||
|
||||
: wake-up? ( heap -- ? )
|
||||
dup heap-empty?
|
||||
[ drop f ] [ heap-peek nip millis <= ] if ;
|
||||
|
||||
: wake-up ( -- )
|
||||
sleep-queue
|
||||
[ dup wake-up? ] [ dup heap-pop drop resume ] [ ] while
|
||||
drop ;
|
||||
|
||||
: next ( -- )
|
||||
walker-hook [
|
||||
continue
|
||||
] [
|
||||
run-queue pop-back dup array?
|
||||
[ first2 continue-with ] [ continue ] if
|
||||
wake-up
|
||||
run-queue pop-back
|
||||
dup array? [ first2 ] [ f swap ] if dup set-self
|
||||
f over set-thread-state
|
||||
thread-continuation box>
|
||||
continue-with
|
||||
] if* ;
|
||||
|
||||
: yield ( -- ) [ schedule-thread stop ] callcc0 ;
|
||||
PRIVATE>
|
||||
|
||||
: sleep-time ( -- ms )
|
||||
{
|
||||
{ [ run-queue dlist-empty? not ] [ 0 ] }
|
||||
{ [ sleep-queue heap-empty? ] [ f ] }
|
||||
{ [ t ] [ sleep-queue heap-peek nip millis [-] ] }
|
||||
} cond ;
|
||||
|
||||
: stop ( -- )
|
||||
self unregister-thread next ;
|
||||
|
||||
: suspend ( quot state -- obj )
|
||||
[
|
||||
self thread-continuation >box
|
||||
self set-thread-state
|
||||
self swap call next
|
||||
] callcc1 2nip ; inline
|
||||
|
||||
: yield ( -- ) [ resume ] "yield" suspend drop ;
|
||||
|
||||
: sleep ( ms -- )
|
||||
>fixnum millis + [ schedule-sleep stop ] curry callcc0 ;
|
||||
>fixnum millis +
|
||||
[ schedule-sleep ] curry
|
||||
"sleep" suspend drop ;
|
||||
|
||||
: in-thread ( quot -- )
|
||||
: (spawn) ( thread -- )
|
||||
[
|
||||
>r schedule-thread r> [
|
||||
resume [
|
||||
dup set-self
|
||||
dup register-thread
|
||||
init-namespaces
|
||||
V{ } set-catchstack
|
||||
{ } set-retainstack
|
||||
[ [ print-error ] recover stop ] call-clear
|
||||
>r { } set-datastack r>
|
||||
thread-quot [ call stop ] call-clear
|
||||
] 1 (throw)
|
||||
] curry callcc0 ;
|
||||
] "spawn" suspend 2drop ;
|
||||
|
||||
: spawn ( quot name -- thread )
|
||||
[
|
||||
global [
|
||||
"Error in thread " write
|
||||
dup thread-id pprint
|
||||
" (" write
|
||||
dup thread-name pprint ")" print
|
||||
"spawned to call " write
|
||||
thread-quot short.
|
||||
nl
|
||||
print-error flush
|
||||
] bind
|
||||
] <thread>
|
||||
[ (spawn) ] keep ;
|
||||
|
||||
: spawn-server ( quot name -- thread )
|
||||
>r [ [ ] [ ] while ] curry r> spawn ;
|
||||
|
||||
: in-thread ( quot -- )
|
||||
>r datastack namestack r>
|
||||
[ >r set-namestack set-datastack r> call ] 3curry
|
||||
"Thread" spawn drop ;
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: (idle-thread) ( slow? -- )
|
||||
sleep-time dup zero?
|
||||
[ wake-up schedule-thread 2drop ]
|
||||
[ 0 ? io-multiplex ] if ;
|
||||
|
||||
: idle-thread ( -- )
|
||||
run-queue dlist-empty? (idle-thread) yield idle-thread ;
|
||||
|
||||
: init-threads ( -- )
|
||||
<dlist> \ run-queue set-global
|
||||
<min-heap> sleep-queue set-global
|
||||
[ idle-thread ] in-thread ;
|
||||
H{ } clone 41 setenv
|
||||
<dlist> 42 setenv
|
||||
<min-heap> 43 setenv
|
||||
initial-thread global
|
||||
[ drop f "Initial" [ die ] <thread> ] cache
|
||||
<box> over set-thread-continuation
|
||||
f over set-thread-registered?
|
||||
dup register-thread
|
||||
set-self ;
|
||||
|
||||
[ self dup thread-error-handler call stop ]
|
||||
thread-error-hook set-global
|
||||
|
||||
PRIVATE>
|
||||
|
||||
[ init-threads ] "threads" add-init-hook
|
||||
PRIVATE>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
! Copyright (C) 2007 Doug Coleman.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: arrays calendar combinators concurrency generic
|
||||
init kernel math namespaces sequences threads ;
|
||||
USING: arrays calendar combinators concurrency.messaging
|
||||
threads generic init kernel math namespaces sequences ;
|
||||
IN: alarms
|
||||
|
||||
TUPLE: alarm time quot ;
|
||||
|
@ -36,7 +36,7 @@ SYMBOL: alarm-looper
|
|||
[ alarm-time <=> 0 <= ] with subset ;
|
||||
|
||||
: call-alarm ( alarm -- )
|
||||
alarm-quot spawn drop ;
|
||||
alarm-quot "Alarm invocation" spawn drop ;
|
||||
|
||||
: do-alarms ( -- )
|
||||
expired-alarms [ call-alarm ] each
|
||||
|
@ -49,7 +49,7 @@ SYMBOL: alarm-looper
|
|||
: start-alarm-receiver ( -- )
|
||||
[
|
||||
alarm-receive-loop
|
||||
] spawn alarm-receiver set-global ;
|
||||
] "Alarm receiver" spawn alarm-receiver set-global ;
|
||||
|
||||
: alarm-loop ( -- )
|
||||
alarms get-global empty? [
|
||||
|
@ -59,7 +59,7 @@ SYMBOL: alarm-looper
|
|||
: start-alarm-looper ( -- )
|
||||
[
|
||||
alarm-loop
|
||||
] spawn alarm-looper set-global ;
|
||||
] "Alarm looper" spawn alarm-looper set-global ;
|
||||
|
||||
: send-alarm ( str alarm -- )
|
||||
over set-delegate
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
USING: concurrency kernel tools.time math sequences ;
|
||||
USING: threads concurrency.messaging kernel
|
||||
tools.time math sequences ;
|
||||
IN: benchmark.ring
|
||||
|
||||
SYMBOL: done
|
||||
|
@ -7,7 +8,7 @@ SYMBOL: done
|
|||
receive 2dup swap send done eq? [ tunnel ] unless ;
|
||||
|
||||
: create-ring ( processes -- target )
|
||||
self swap [ [ tunnel ] spawn nip ] times ;
|
||||
self swap [ [ tunnel ] "Tunnel" spawn nip ] times ;
|
||||
|
||||
: send-messages ( messages target -- )
|
||||
dupd [ send ] curry each [ receive drop ] times ;
|
||||
|
@ -22,4 +23,3 @@ SYMBOL: done
|
|||
1000 1000 ring-bench ;
|
||||
|
||||
MAIN: main-ring-bench
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
USING: io.sockets io.server io kernel math threads debugger
|
||||
concurrency tools.time prettyprint ;
|
||||
USING: io.sockets io.server io kernel math threads
|
||||
debugger tools.time prettyprint concurrency.combinators ;
|
||||
IN: benchmark.sockets
|
||||
|
||||
: simple-server ( -- )
|
||||
|
|
|
@ -5,10 +5,11 @@ USING: vocabs.loader sequences ;
|
|||
"tools.annotations"
|
||||
"tools.crossref"
|
||||
"tools.deploy"
|
||||
"tools.disassembler"
|
||||
"tools.memory"
|
||||
"tools.profiler"
|
||||
"tools.test"
|
||||
"tools.time"
|
||||
"tools.disassembler"
|
||||
"tools.threads"
|
||||
"editors"
|
||||
} [ require ] each
|
||||
|
|
|
@ -17,7 +17,7 @@ IN: bunny.model
|
|||
} cond (parse-model)
|
||||
] when* ;
|
||||
|
||||
: parse-model ( stream -- vs is )
|
||||
: parse-model ( -- vs is )
|
||||
100000 <vector> 100000 <vector> (parse-model) ;
|
||||
|
||||
: n ( vs triple -- n )
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
! Copyright (C) 2008 Slava Pestov
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: calendar namespaces models threads init ;
|
||||
USING: calendar namespaces models threads kernel init ;
|
||||
IN: calendar.model
|
||||
|
||||
SYMBOL: time
|
||||
|
@ -9,7 +9,8 @@ SYMBOL: time
|
|||
now time get set-model
|
||||
1000 sleep (time-thread) ;
|
||||
|
||||
: time-thread ( -- ) [ (time-thread) ] in-thread ;
|
||||
: time-thread ( -- )
|
||||
[ (time-thread) ] "Time model update" spawn drop ;
|
||||
|
||||
f <model> time set-global
|
||||
[ time-thread ] "calendar.model" add-init-hook
|
||||
|
|
|
@ -19,7 +19,7 @@ GENERIC: from ( channel -- value )
|
|||
[ channel-senders push stop ] curry callcc0 ;
|
||||
|
||||
: (to) ( value receivers -- )
|
||||
delete-random schedule-thread-with yield ;
|
||||
delete-random resume-with yield ;
|
||||
|
||||
: notify ( continuation channel -- channel )
|
||||
[ channel-receivers push ] keep ;
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
! See http://factorcode.org/license.txt for BSD license.
|
||||
!
|
||||
! Examples of using channels
|
||||
USING: kernel concurrency channels math namespaces locals
|
||||
sequences ;
|
||||
USING: kernel threads channels math namespaces
|
||||
locals sequences ;
|
||||
IN: channels.examples
|
||||
|
||||
: (counter) ( channel n -- )
|
||||
|
@ -13,7 +13,7 @@ IN: channels.examples
|
|||
2 (counter) ;
|
||||
|
||||
: counter-test ( -- n1 n2 n3 )
|
||||
<channel> [ counter ] spawn drop
|
||||
<channel> dup [ counter ] curry "Counter" spawn drop
|
||||
[ from ] keep [ from ] keep from ;
|
||||
|
||||
: filter ( send prime recv -- )
|
||||
|
@ -28,17 +28,17 @@ IN: channels.examples
|
|||
[let | p [ c from ]
|
||||
newc [ <channel> ] |
|
||||
p prime to
|
||||
[ newc p c filter ] spawn drop
|
||||
[ newc p c filter ] "Filter" spawn drop
|
||||
prime newc (sieve)
|
||||
] ;
|
||||
|
||||
: sieve ( prime -- )
|
||||
#! Send prime numbers to 'prime' channel
|
||||
<channel> [ counter ] spawn drop
|
||||
<channel> dup [ counter ] curry "Counter" spawn drop
|
||||
(sieve) ;
|
||||
|
||||
: sieve-test ( -- seq )
|
||||
<channel> [ sieve ] spawn drop
|
||||
<channel> dup [ sieve ] curry "Sieve" spawn drop
|
||||
V{ } clone swap
|
||||
[ from swap push ] 2keep
|
||||
[ from swap push ] 2keep
|
||||
|
|
|
@ -3,7 +3,8 @@
|
|||
!
|
||||
! Remote Channels
|
||||
USING: kernel init namespaces assocs arrays random
|
||||
sequences channels match concurrency concurrency.distributed ;
|
||||
sequences channels match concurrency.messaging
|
||||
concurrency.distributed threads ;
|
||||
IN: channels.remote
|
||||
|
||||
<PRIVATE
|
||||
|
@ -23,25 +24,27 @@ PRIVATE>
|
|||
|
||||
<PRIVATE
|
||||
|
||||
MATCH-VARS: ?id ?value ;
|
||||
MATCH-VARS: ?from ?tag ?id ?value ;
|
||||
|
||||
SYMBOL: no-channel
|
||||
|
||||
: channel-process ( -- )
|
||||
receive
|
||||
{
|
||||
{ { ?from ?tag { to ?id ?value } }
|
||||
[ ?value ?id get-channel [ to f ] [ no-channel ] if* ?tag swap 2array ?from send ] }
|
||||
{ { ?from ?tag { from ?id } }
|
||||
[ ?id get-channel [ from ] [ no-channel ] if* ?tag swap 2array ?from send ] }
|
||||
} match-cond
|
||||
channel-process ;
|
||||
receive [
|
||||
{
|
||||
{ { to ?id ?value }
|
||||
[ ?value ?id get-channel dup [ to f ] [ 2drop no-channel ] if ] }
|
||||
{ { from ?id }
|
||||
[ ?id get-channel [ from ] [ no-channel ] if* ] }
|
||||
} match-cond
|
||||
] keep reply-synchronous ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: start-channel-node ( -- )
|
||||
"remote-channels" get-process [
|
||||
[ channel-process ] spawn "remote-channels" swap register-process
|
||||
"remote-channels" get-process [
|
||||
"remote-channels"
|
||||
[ channel-process t ] "Remote channels" spawn-server
|
||||
register-process
|
||||
] unless ;
|
||||
|
||||
TUPLE: remote-channel node id ;
|
||||
|
@ -49,12 +52,12 @@ TUPLE: remote-channel node id ;
|
|||
C: <remote-channel> remote-channel
|
||||
|
||||
M: remote-channel to ( value remote-channel -- )
|
||||
dup >r [ \ to , remote-channel-id , , ] { } make r>
|
||||
[ [ \ to , remote-channel-id , , ] { } make ] keep
|
||||
remote-channel-node "remote-channels" <remote-process>
|
||||
send-synchronous no-channel = [ no-channel throw ] when ;
|
||||
|
||||
M: remote-channel from ( remote-channel -- value )
|
||||
dup >r [ \ from , remote-channel-id , ] { } make r>
|
||||
[ [ \ from , remote-channel-id , ] { } make ] keep
|
||||
remote-channel-node "remote-channels" <remote-process>
|
||||
send-synchronous dup no-channel = [ no-channel throw ] when* ;
|
||||
|
||||
|
|
|
@ -2,12 +2,15 @@
|
|||
! See http://factorcode.org/license.txt for BSD license.
|
||||
!
|
||||
! Wrap a sniffer in a channel
|
||||
USING: kernel channels channels.sniffer.backend concurrency io
|
||||
io.sniffer.backend io.sniffer.bsd io.unix.backend ;
|
||||
USING: kernel channels channels.sniffer.backend
|
||||
threads io io.sniffer.backend io.sniffer.bsd
|
||||
io.unix.backend ;
|
||||
IN: channels.sniffer.bsd
|
||||
|
||||
M: unix-io sniff-channel ( -- channel )
|
||||
"/dev/bpf0" "en1" <sniffer-spec> <sniffer> <channel> [
|
||||
(sniff-channel)
|
||||
] spawn drop nip ;
|
||||
[
|
||||
(sniff-channel)
|
||||
] 3curry spawn drop
|
||||
] keep ;
|
||||
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
! See http://factorcode.org/license.txt for BSD license.
|
||||
!
|
||||
! Wrap a sniffer in a channel
|
||||
USING: kernel channels concurrency io io.backend
|
||||
io.sniffer io.sniffer.backend system vocabs.loader ;
|
||||
USING: kernel channels io io.backend io.sniffer
|
||||
io.sniffer.backend system vocabs.loader ;
|
||||
|
||||
: (sniff-channel) ( stream channel -- )
|
||||
4096 pick stream-read-partial over to (sniff-channel) ;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
! Copyright (C) 2006, 2007 Slava Pestov
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: alien io kernel namespaces core-foundation cocoa.messages
|
||||
cocoa cocoa.classes cocoa.runtime sequences threads debugger
|
||||
init inspector kernel.private ;
|
||||
cocoa cocoa.classes cocoa.runtime sequences threads
|
||||
debugger init inspector kernel.private ;
|
||||
IN: cocoa.application
|
||||
|
||||
: <NSString> ( str -- alien ) <CFString> -> autorelease ;
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
USING: help.markup help.syntax sequences ;
|
||||
IN: concurrency.combinators
|
||||
|
||||
HELP: parallel-map
|
||||
{ $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- newelt )" } } { "newseq" sequence } }
|
||||
{ $description "Spawns a new thread for applying " { $snippet "quot" } " to every element of " { $snippet "seq" } ", collecting the results at the end." }
|
||||
{ $errors "Throws an error if one of the iterations throws an error." } ;
|
||||
|
||||
HELP: parallel-each
|
||||
{ $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- )" } } }
|
||||
{ $description "Spawns a new thread for applying " { $snippet "quot" } " to every element of " { $snippet "seq" } ", blocking until all quotations complete." }
|
||||
{ $errors "Throws an error if one of the iterations throws an error." } ;
|
||||
|
||||
HELP: parallel-subset
|
||||
{ $values { "seq" sequence } { "quot" "a quotation with stack effect " { $snippet "( elt -- ? )" } } { "newseq" sequence } }
|
||||
{ $description "Spawns a new thread for applying " { $snippet "quot" } " to every element of " { $snippet "seq" } ", collecting the elements for which the quotation yielded a true value." }
|
||||
{ $errors "Throws an error if one of the iterations throws an error." } ;
|
||||
|
||||
ARTICLE: "concurrency.combinators" "Concurrent combinators"
|
||||
"The " { $vocab-link "concurrency.combinators" } " vocabulary provides concurrent variants of " { $link each } ", " { $link map } " and " { $link subset } ":"
|
||||
{ $subsection parallel-each }
|
||||
{ $subsection parallel-map }
|
||||
{ $subsection parallel-subset } ;
|
||||
|
||||
ABOUT: "concurrency.combinators"
|
|
@ -0,0 +1,24 @@
|
|||
IN: temporary
|
||||
USING: concurrency.combinators tools.test random kernel math
|
||||
concurrency.messaging threads sequences ;
|
||||
|
||||
[ [ drop ] parallel-each ] must-infer
|
||||
[ [ ] parallel-map ] must-infer
|
||||
[ [ ] parallel-subset ] must-infer
|
||||
|
||||
[ { 1 4 9 } ] [ { 1 2 3 } [ sq ] parallel-map ] unit-test
|
||||
|
||||
[ { 1 4 9 } ] [ { 1 2 3 } [ 1000 random sleep sq ] parallel-map ] unit-test
|
||||
|
||||
[ { 1 2 3 } [ dup 2 mod 0 = [ "Even" throw ] when ] parallel-map ]
|
||||
[ linked-error "Even" = ] must-fail-with
|
||||
|
||||
[ V{ 0 3 6 9 } ]
|
||||
[ 10 [ 3 mod zero? ] parallel-subset ] unit-test
|
||||
|
||||
[ 10 ]
|
||||
[
|
||||
V{ } clone
|
||||
10 over [ push ] curry parallel-each
|
||||
length
|
||||
] unit-test
|
|
@ -0,0 +1,17 @@
|
|||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: concurrency.futures concurrency.count-downs sequences
|
||||
kernel ;
|
||||
IN: concurrency.combinators
|
||||
|
||||
: parallel-map ( seq quot -- newseq )
|
||||
[ curry future ] curry map dup [ ?future ] change-each ;
|
||||
inline
|
||||
|
||||
: parallel-each ( seq quot -- )
|
||||
over length <count-down>
|
||||
[ [ >r curry r> spawn-stage ] 2curry each ] keep await ;
|
||||
inline
|
||||
|
||||
: parallel-subset ( seq quot -- newseq )
|
||||
over >r pusher >r each r> r> like ; inline
|
|
@ -0,0 +1 @@
|
|||
Parallel sequence operations
|
|
@ -1,171 +0,0 @@
|
|||
! Copyright (C) 2006 Chris Double.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: help.syntax help.markup concurrency.private match ;
|
||||
IN: concurrency
|
||||
|
||||
HELP: make-mailbox
|
||||
{ $values { "mailbox" "a mailbox object" }
|
||||
}
|
||||
{ $description "A mailbox is an object that can be used for safe thread communication. Items can be put in the mailbox and retrieved in a FIFO order. If the mailbox is empty when a get operation is performed then the thread will block until another thread places something in the mailbox. If multiple threads are waiting on the same mailbox, only one of the waiting threads will be unblocked to process the get operation." }
|
||||
{ $see-also mailbox-empty? mailbox-put mailbox-get mailbox-get-all while-mailbox-empty mailbox-get? } ;
|
||||
|
||||
HELP: mailbox-empty?
|
||||
{ $values { "mailbox" "a mailbox object" }
|
||||
{ "bool" "a boolean value" }
|
||||
}
|
||||
{ $description "Return true if the mailbox is empty." }
|
||||
{ $see-also make-mailbox mailbox-put mailbox-get mailbox-get-all while-mailbox-empty mailbox-get? } ;
|
||||
|
||||
HELP: mailbox-put
|
||||
{ $values { "obj" "an object" }
|
||||
{ "mailbox" "a mailbox object" }
|
||||
}
|
||||
{ $description "Put the object into the mailbox. Any threads that have a blocking get on the mailbox are resumed. Only one of those threads will successfully get the object, the rest will immediately block waiting for the next item in the mailbox." }
|
||||
{ $see-also make-mailbox mailbox-empty? mailbox-get mailbox-get-all while-mailbox-empty mailbox-get? } ;
|
||||
|
||||
HELP: (mailbox-block-unless-pred)
|
||||
{ $values { "pred" "a quotation with stack effect " { $snippet "( X -- bool )" } }
|
||||
{ "mailbox" "a mailbox object" }
|
||||
{ "timeout" "a timeout in milliseconds" }
|
||||
}
|
||||
{ $description "Block the thread if there are no items in the mailbox that return true when the predicate is called with the item on the stack. The predicate must have stack effect " { $snippet "( X -- bool )" } "." }
|
||||
{ $see-also make-mailbox mailbox-empty? mailbox-put mailbox-get mailbox-get-all while-mailbox-empty mailbox-get? } ;
|
||||
|
||||
HELP: (mailbox-block-if-empty)
|
||||
{ $values { "mailbox" "a mailbox object" }
|
||||
{ "mailbox2" "same object as 'mailbox'" }
|
||||
{ "timeout" "a timeout in milliseconds" }
|
||||
}
|
||||
{ $description "Block the thread if the mailbox is empty." }
|
||||
{ $see-also make-mailbox mailbox-empty? mailbox-put mailbox-get mailbox-get-all while-mailbox-empty mailbox-get? } ;
|
||||
|
||||
HELP: mailbox-get
|
||||
{ $values { "mailbox" "a mailbox object" }
|
||||
{ "obj" "an object" }
|
||||
}
|
||||
{ $description "Get the first item put into the mailbox. If it is empty the thread blocks until an item is put into it. The thread then resumes, leaving the item on the stack." }
|
||||
{ $see-also make-mailbox mailbox-empty? mailbox-put while-mailbox-empty mailbox-get-all mailbox-get? } ;
|
||||
|
||||
HELP: mailbox-get-all
|
||||
{ $values { "mailbox" "a mailbox object" }
|
||||
{ "array" "an array" }
|
||||
}
|
||||
{ $description "Blocks the thread if the mailbox is empty, otherwise removes all objects in the mailbox and returns an array containing the objects." }
|
||||
{ $see-also make-mailbox mailbox-empty? mailbox-put while-mailbox-empty mailbox-get-all mailbox-get? } ;
|
||||
|
||||
HELP: while-mailbox-empty
|
||||
{ $values { "mailbox" "a mailbox object" }
|
||||
{ "quot" "a quotation with stack effect " { $snippet "( -- )" } }
|
||||
}
|
||||
{ $description "Repeatedly call the quotation while there are no items in the mailbox. Quotation should have stack effect " { $snippet "( -- )" } "." }
|
||||
{ $see-also make-mailbox mailbox-empty? mailbox-put mailbox-get mailbox-get-all mailbox-get? } ;
|
||||
|
||||
HELP: mailbox-get?
|
||||
{ $values { "pred" "a quotation with stack effect " { $snippet "( X -- bool )" } }
|
||||
{ "mailbox" "a mailbox object" }
|
||||
{ "obj" "an object" }
|
||||
}
|
||||
{ $description "Get the first item in the mailbox which satisfies the predicate. 'pred' will be called repeatedly for each item in the mailbox. When 'pred' returns true that item will be returned. If nothing in the mailbox satisfies the predicate then the thread will block until something does. 'pred' must have stack effect " { $snippet "( X -- bool }" } "." }
|
||||
{ $see-also make-mailbox mailbox-empty? mailbox-put mailbox-get mailbox-get-all while-mailbox-empty } ;
|
||||
|
||||
HELP: <process>
|
||||
{ $values { "links" "an array of processes" }
|
||||
{ "pid" "the process id" }
|
||||
{ "mailbox" "a mailbox object" }
|
||||
}
|
||||
{ $description "Constructs a process object. A process is a lightweight thread with a mailbox that can be used to communicate with other processes. Each process has a unique process id." }
|
||||
{ $see-also spawn send receive } ;
|
||||
|
||||
HELP: self
|
||||
{ $values { "process" "a process object" }
|
||||
}
|
||||
{ $description "Returns the currently running process object." }
|
||||
{ $see-also <process> send receive receive-if } ;
|
||||
|
||||
HELP: send
|
||||
{ $values { "message" "an object" }
|
||||
{ "process" "a process object" }
|
||||
}
|
||||
{ $description "Send the message to the process by placing it in the processes mailbox. This is an asynchronous operation and will return immediately. The receving process will act on the message the next time it retrieves that item from its mailbox (usually using the " { $link receive } " word. The message can be any Factor object. For destinations that are instances of remote-process the message must be a serializable Factor type." }
|
||||
{ $see-also <process> receive receive-if } ;
|
||||
|
||||
HELP: receive
|
||||
{ $values { "message" "an object" }
|
||||
}
|
||||
{ $description "Return a message from the current processes mailbox. If the box is empty, suspend the process until another process places an item in the mailbox (usually via the " { $link send } " word." }
|
||||
{ $see-also send receive-if } ;
|
||||
|
||||
HELP: receive-if
|
||||
{ $values { "pred" "a predicate with stack effect " { $snippet "( X -- bool )" } }
|
||||
{ "message" "an object" }
|
||||
}
|
||||
{ $description "Return the first message from the current processes mailbox that satisfies the predicate. To satisfy the predicate, 'pred' is called with the item on the stack and the predicate should leave a boolean indicating whether it was satisfied or not. The predicate must have stack effect " { $snippet "( X -- bool )" } ". If nothing in the mailbox satisfies the predicate then the process will block until something does." }
|
||||
{ $see-also send receive } ;
|
||||
|
||||
HELP: spawn
|
||||
{ $values { "quot" "a predicate with stack effect " { $snippet "( -- )" } }
|
||||
{ "process" "a process object" }
|
||||
}
|
||||
{ $description "Start a process which runs the given quotation." }
|
||||
{ $see-also send receive receive-if self spawn-link } ;
|
||||
|
||||
HELP: spawn-link
|
||||
{ $values { "quot" "a predicate with stack effect " { $snippet "( -- )" } }
|
||||
{ "process" "a process object" }
|
||||
}
|
||||
{ $description "Start a process which runs the given quotation. If that quotation throws an error which is not caught then the error will get propagated to the process that spawned it. This can be used to set up 'supervisor' processes that restart child processes that crash due to uncaught errors.\n" }
|
||||
{ $see-also spawn } ;
|
||||
|
||||
ARTICLE: { "concurrency" "loading" } "Loading"
|
||||
"The Factor module system can be used to load the Concurrency library:"
|
||||
{ $code "USING: concurrency ;" } ;
|
||||
|
||||
ARTICLE: { "concurrency" "processes" } "Processes"
|
||||
"A process is basically a thread with a message queue. Other processes can place items on this queue by sending the process a message. A process can check its queue for messages, blocking if none are pending, and process them as they are queued.\n\nFactor processes are very lightweight. Each process can take as little as 900 bytes of memory. This library has been tested running hundreds of thousands of simple processes.\n\nThe messages that are sent from process to process are any Factor value. Factor tuples are ideal for this sort of thing as you can send a tuple to a process and the predicate dispatch mechanism can be used to perform actions depending on what the type of the tuple is.\n\nProcesses are usually created using " { $link spawn } ". This word takes a quotation on the stack and starts a process that will execute that quotation asynchronously. When the quotation completes the process will die. 'spawn' leaves on the stack the process object that was started. This object can be used to send messages to the process using " { $link send } ".\n\n'send' will return immediately after placing the message in the target processes message queue.\n\nA process can get a message from its queue using " { $link receive } ". This will get the most recent message and leave it on the stack. If there are no messages in the queue the process will 'block' until a message is available. When a process is blocked it takes no CPU time at all."
|
||||
{ $code "[ receive print ] spawn\n\"Hello Process!\" swap send" }
|
||||
"This example spawns a process that first blocks, waiting to receive a message. When a message is received, the 'receive' call returns leaving it on the stack. It then prints the message and exits. 'spawn' left the process on the stack so it's available to send the 'Hello Process!' message to it. Immediately after the 'send' you should see 'Hello Process!' printed on the console.\n\nIt is also possible to selectively retrieve messages from the message queue. " { $link receive-if } " takes a predicate quotation on the stack and returns the first message in the queue that satisfies the predicate. If no items satisfy the predicate then the process is blocked until a message is received that does."
|
||||
{ $code ": odd? ( n -- ? ) 2 mod 1 = ;\n1 self send 2 self send 3 self send\n\nreceive .\n => 1\n\n[ odd? ] receive-if .\n => 3\n\nreceive .\n => 2" } ;
|
||||
|
||||
ARTICLE: { "concurrency" "self" } "Self"
|
||||
"A process can get access to its own process object using " { $link self } " so it can pass it to other processes. This allows the other processes to send messages back. A simple example of using this gets the current process' 'self' and spawns a process which sends a message to it. We then receive the message from the original process:"
|
||||
{ $code "self [ \"Hello!\" swap send ] spawn 2drop receive .\n => \"Hello!\"" } ;
|
||||
|
||||
ARTICLE: { "concurrency" "servers" } "Servers"
|
||||
"A common idiom is to create 'server' processes that act on messages that are sent to it. These follow a basic pattern of blocking until a message is received, processing that message then looping back to blocking for a message.\n\nThe following example shows a very simple server that expects an array as its message. The first item of the array should be the senders process object. If the second item is 'ping' then the server sends 'pong' back to the caller. If the second item is anything else then the server exits:"
|
||||
{ $code ": pong-server ( -- )\n receive {\n { { ?from \"ping\" } [ \"pong\" ?from send pong-server ] }\n { { ?from _ } [ \"server shutdown\" ?from send ] }\n } match-cond ;\n\n[ pong-server ] spawn" }
|
||||
"Handling the deconstructing of messages and dispatching based on the message can be a bit of a chore. Especially in servers that take a number of different messages. The approach taken above is to use the 'match' library which allows easy deconstructing of messages using " { $link match-cond } "." ;
|
||||
|
||||
ARTICLE: { "concurrency" "synchronous-sends" } "Synchronous Sends"
|
||||
{ $link send } " sends a message asynchronously, and the sending process continues immediately. The 'pong server' example shown previously all sent messages to the server and waited for a reply back from the server. This pattern of synchronous sending is made easier with " { $link send-synchronous } ".\n\nThis word will send a message to the given process and immediately block until a reply is received for this particular message send. It leaves the reply on the stack. Note that it doesn't wait for just any reply, it waits for a reply specifically to this send.\n\nTo do this it wraps the requested message inside a tagged message format using " { $link tag-message } ":"
|
||||
{ $code "\"My Message\" tag-message .\n => { ...from... ...tag... \"My Message\" }" }
|
||||
"The message is wrapped in array where the first item is the sending process object, the second is a unique tag, and the third is the original message. Server processes can use the 'from' to reply to the process that originally sent the message. The tag is used in the receiving server to include the value in the reply. After the send-synchronous call the current process will block waiting for a reply that has the exact same tag. In this way you can be sure that the reply you got was for the specific message sent. Here is the pong-server recoded to use 'send-synchronous':"
|
||||
{ $code ": pong-server ( -- )\n receive {\n { { ?from ?tag \"ping\" } [ ?tag \"pong\" 2array ?from send pong-server ] }\n { { ?from _ } [ ?tag \"server shutdown\" 2array ?from send ] }\n } match-cond ;\n\n[ pong-server ] spawn \"ping\" swap send-synchronous .\n => \"pong\"" }
|
||||
"Notice that the code to send the reply back to the original caller wraps the reply in an array where the first item is the tag originally sent. 'send-synchronous' only returns if it receives a reply containing that specific tag." ;
|
||||
|
||||
ARTICLE: { "concurrency" "exceptions" } "Exceptions"
|
||||
"A process can handle exceptions using the standard Factor exception handling mechanism. If an exception is uncaught the process will terminate. For example:"
|
||||
{ $code "[ 1 0 / \"This will not print\" print ] spawn" }
|
||||
"Processes can be linked so that a parent process can receive the exception that caused the child process to terminate. In this way 'supervisor' processes can be created that are notified when child processes terminate and possibly restart them.\n\nThe easiest way to form this link is using " { $link spawn-link } ". This will create a unidirectional link, such that if an uncaught exception causes the child to terminate, the parent process can catch it:"
|
||||
{ $code "[\n [ 1 0 / \"This will not print\" print ] spawn-link drop\n receive\n] [ \"Exception caught.\" print ] recover" }
|
||||
"Exceptions are only raised in the parent when the parent does a " { $link receive } " or " { $link receive-if } ". This is because the exception is sent from the child to the parent as a message." ;
|
||||
|
||||
ARTICLE: { "concurrency" "futures" } "Futures"
|
||||
"A future is a placeholder for the result of a computation that is being calculated in a process. When the process has completed the computation the future can be queried to find out the result. If the computation has not completed when the future is queried them the process will block until the result is completed. A future is created using " { $link future } ".\n\nThe quotation will be run in a spawned process, and a future object is immediately returned. This future object can be resolved using " { $link ?future } ".\n\nFutures are useful for starting calculations that take a long time to run but aren't needed until later in the process. When the process needs the value it can use '?future' to get the result or block until the result is available. For example:"
|
||||
{ $code "[ 30 fib ] future\n...do stuff...\n?future" } ;
|
||||
|
||||
ARTICLE: { "concurrency" "promises" } "Promises"
|
||||
"A promise is similar to a future but it is not produced by calculating something in the background. It represents a promise to provide a value sometime later. A process can request the value of a promise and will block if the promise is not fulfilled. Later, another process can fulfill the promise, providing a value. All threads waiting on the promise will then resume with that value on the stack. Use " { $link <promise> } " to create a promise, " { $link fulfill } " to set it to a value, and " { $link ?promise } " to retrieve the value, or block until the promise is fulfilled:"
|
||||
{ $code "<promise>\n[ ?promise \"Promise fulfilled: \" write print ] spawn drop\n[ ?promise \"Promise fulfilled: \" write print ] spawn drop\n[ ?promise \"Promise fulfilled: \" write print ] spawn drop\n\"hello\" swap fulfill\n => Promise fulfilled: hello\n Promise fulfilled: hello\n Promise fulfilled: hello" } ;
|
||||
|
||||
ARTICLE: { "concurrency" "concurrency" } "Concurrency"
|
||||
"The concurrency library is based upon the style of concurrency used in systems like Erlang and Termite. It is built on top of the standard Factor lightweight thread system.\nA concurrency oriented program is one in which multiple processes run simultaneously in a single Factor image or across multiple running Factor instances. The processes can communicate with each other by asynchronous message sends. Although processes can share data via Factor's mutable data structures it is not recommended as the use of shared state concurrency is often a cause of problems."
|
||||
{ $subsection { "concurrency" "loading" } }
|
||||
{ $subsection { "concurrency" "processes" } }
|
||||
{ $subsection { "concurrency" "self" } }
|
||||
{ $subsection { "concurrency" "servers" } }
|
||||
{ $subsection { "concurrency" "synchronous-sends" } }
|
||||
{ $subsection { "concurrency" "exceptions" } }
|
||||
{ $subsection { "concurrency" "futures" } }
|
||||
{ $subsection { "concurrency" "promises" } } ;
|
||||
|
||||
ABOUT: { "concurrency" "concurrency" }
|
|
@ -1,141 +0,0 @@
|
|||
! Copyright (C) 2005 Chris Double. All Rights Reserved.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
!
|
||||
USING: kernel concurrency threads vectors arrays sequences
|
||||
namespaces tools.test continuations dlists strings math words
|
||||
match quotations concurrency.private ;
|
||||
IN: temporary
|
||||
|
||||
[ ] [ self process-mailbox mailbox-data dlist-delete-all ] unit-test
|
||||
|
||||
[ V{ 1 2 3 } ] [
|
||||
0 <vector>
|
||||
make-mailbox
|
||||
2dup [ mailbox-get swap push ] 2curry in-thread
|
||||
2dup [ mailbox-get swap push ] 2curry in-thread
|
||||
2dup [ mailbox-get swap push ] 2curry in-thread
|
||||
1 over mailbox-put
|
||||
2 over mailbox-put
|
||||
3 swap mailbox-put
|
||||
] unit-test
|
||||
|
||||
[ V{ 1 2 3 } ] [
|
||||
0 <vector>
|
||||
make-mailbox
|
||||
2dup [ [ integer? ] swap mailbox-get? swap push ] 2curry in-thread
|
||||
2dup [ [ integer? ] swap mailbox-get? swap push ] 2curry in-thread
|
||||
2dup [ [ integer? ] swap mailbox-get? swap push ] 2curry in-thread
|
||||
1 over mailbox-put
|
||||
2 over mailbox-put
|
||||
3 swap mailbox-put
|
||||
] unit-test
|
||||
|
||||
[ V{ 1 "junk" 3 "junk2" } [ 456 ] ] [
|
||||
0 <vector>
|
||||
make-mailbox
|
||||
2dup [ [ integer? ] swap mailbox-get? swap push ] 2curry in-thread
|
||||
2dup [ [ integer? ] swap mailbox-get? swap push ] 2curry in-thread
|
||||
2dup [ [ string? ] swap mailbox-get? swap push ] 2curry in-thread
|
||||
2dup [ [ string? ] swap mailbox-get? swap push ] 2curry in-thread
|
||||
1 over mailbox-put
|
||||
"junk" over mailbox-put
|
||||
[ 456 ] over mailbox-put
|
||||
3 over mailbox-put
|
||||
"junk2" over mailbox-put
|
||||
mailbox-get
|
||||
] unit-test
|
||||
|
||||
[ "test" ] [
|
||||
[ self ] "test" with-process
|
||||
] unit-test
|
||||
|
||||
|
||||
[ "received" ] [
|
||||
[
|
||||
receive {
|
||||
{ { ?from ?tag _ } [ ?tag "received" 2array ?from send ] }
|
||||
} match-cond
|
||||
] spawn
|
||||
"sent" swap send-synchronous
|
||||
] unit-test
|
||||
|
||||
[ 1 3 2 ] [
|
||||
1 self send
|
||||
2 self send
|
||||
3 self send
|
||||
receive
|
||||
[ 2 mod 0 = not ] receive-if
|
||||
receive
|
||||
] unit-test
|
||||
|
||||
|
||||
[
|
||||
[
|
||||
"crash" throw
|
||||
] spawn-link drop
|
||||
receive
|
||||
] [ "crash" = ] must-fail-with
|
||||
|
||||
[ 50 ] [
|
||||
[ 50 ] future ?future
|
||||
] unit-test
|
||||
|
||||
[ V{ 50 50 50 } ] [
|
||||
0 <vector>
|
||||
<promise>
|
||||
2dup [ ?promise swap push ] 2curry spawn drop
|
||||
2dup [ ?promise swap push ] 2curry spawn drop
|
||||
2dup [ ?promise swap push ] 2curry spawn drop
|
||||
50 swap fulfill
|
||||
] unit-test
|
||||
|
||||
MATCH-VARS: ?value ;
|
||||
SYMBOL: increment
|
||||
SYMBOL: decrement
|
||||
SYMBOL: value
|
||||
|
||||
: counter ( value -- )
|
||||
receive {
|
||||
{ { increment ?value } [ ?value + counter ] }
|
||||
{ { decrement ?value } [ ?value - counter ] }
|
||||
{ { value ?from } [ dup ?from send counter ] }
|
||||
} match-cond ;
|
||||
|
||||
[ -5 ] [
|
||||
[ 0 counter ] spawn
|
||||
{ increment 10 } over send
|
||||
{ decrement 15 } over send
|
||||
[ value , self , ] { } make swap send
|
||||
receive
|
||||
] unit-test
|
||||
|
||||
! The following unit test blocks forever if the
|
||||
! exception does not propogate. Uncomment when
|
||||
! this is fixed (via a timeout).
|
||||
[
|
||||
[ "this should propogate" throw ] future ?future
|
||||
] must-fail
|
||||
|
||||
[ ] [
|
||||
[ "this should not propogate" throw ] future drop
|
||||
] unit-test
|
||||
|
||||
[ f ] [
|
||||
[ 1 drop ] spawn 100 sleep process-pid get-process
|
||||
] unit-test
|
||||
|
||||
[ f ] [
|
||||
[ "testing unregistering on error" throw ] spawn
|
||||
100 sleep process-pid get-process
|
||||
] unit-test
|
||||
|
||||
! Race condition with futures
|
||||
[ 3 3 ] [
|
||||
[ 3 ] future
|
||||
dup ?future swap ?future
|
||||
] unit-test
|
||||
|
||||
! Another race
|
||||
[ 3 ] [
|
||||
[ 3 yield ] future ?future
|
||||
] unit-test
|
|
@ -1,384 +0,0 @@
|
|||
! Copyright (C) 2005 Chris Double. All Rights Reserved.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
!
|
||||
! Concurrency library for Factor based on Erlang/Termite style
|
||||
! concurrency.
|
||||
USING: vectors dlists threads sequences continuations
|
||||
namespaces random math quotations words kernel match
|
||||
arrays io assocs init shuffle system ;
|
||||
IN: concurrency
|
||||
|
||||
TUPLE: mailbox threads data ;
|
||||
|
||||
TUPLE: thread timeout continuation continued? ;
|
||||
|
||||
: <thread> ( timeout continuation -- obj )
|
||||
>r dup [ millis + ] when r>
|
||||
{
|
||||
set-thread-timeout
|
||||
set-thread-continuation
|
||||
} thread construct ;
|
||||
|
||||
: make-mailbox ( -- mailbox )
|
||||
V{ } clone <dlist> mailbox construct-boa ;
|
||||
|
||||
: mailbox-empty? ( mailbox -- bool )
|
||||
mailbox-data dlist-empty? ;
|
||||
|
||||
: mailbox-put ( obj mailbox -- )
|
||||
[ mailbox-data push-back ] keep
|
||||
[ mailbox-threads ] keep
|
||||
V{ } clone swap set-mailbox-threads
|
||||
[ thread-continuation schedule-thread ] each yield ;
|
||||
|
||||
<PRIVATE
|
||||
: (mailbox-block-unless-pred) ( pred mailbox timeout -- )
|
||||
2over mailbox-data dlist-contains? [
|
||||
3drop
|
||||
] [
|
||||
[ <thread> swap mailbox-threads push stop ] callcc0
|
||||
(mailbox-block-unless-pred)
|
||||
] if ; inline
|
||||
|
||||
: (mailbox-block-if-empty) ( mailbox timeout -- mailbox2 )
|
||||
over mailbox-empty? [
|
||||
[ <thread> swap mailbox-threads push stop ] callcc0
|
||||
(mailbox-block-if-empty)
|
||||
] [
|
||||
drop
|
||||
] if ;
|
||||
PRIVATE>
|
||||
: mailbox-get* ( mailbox timeout -- obj )
|
||||
(mailbox-block-if-empty)
|
||||
mailbox-data pop-front ;
|
||||
|
||||
: mailbox-get ( mailbox -- obj )
|
||||
f mailbox-get* ;
|
||||
|
||||
: mailbox-get-all* ( mailbox timeout -- array )
|
||||
(mailbox-block-if-empty)
|
||||
[ dup mailbox-empty? ]
|
||||
[ dup mailbox-data pop-front ]
|
||||
[ ] unfold nip ;
|
||||
|
||||
: mailbox-get-all ( mailbox -- array )
|
||||
f mailbox-get-all* ;
|
||||
|
||||
: while-mailbox-empty ( mailbox quot -- )
|
||||
over mailbox-empty? [
|
||||
dup >r swap slip r> while-mailbox-empty
|
||||
] [
|
||||
2drop
|
||||
] if ; inline
|
||||
|
||||
: mailbox-get?* ( pred mailbox timeout -- obj )
|
||||
2over >r >r (mailbox-block-unless-pred) r> r>
|
||||
mailbox-data delete-node-if ; inline
|
||||
|
||||
: mailbox-get? ( pred mailbox -- obj )
|
||||
f mailbox-get?* ;
|
||||
|
||||
TUPLE: process links pid mailbox ;
|
||||
|
||||
C: <process> process
|
||||
|
||||
GENERIC: send ( message process -- )
|
||||
|
||||
<PRIVATE
|
||||
: make-process ( -- process )
|
||||
#! Return a process set to run on the local node. A process is
|
||||
#! similar to a thread but can send and receive messages to and
|
||||
#! from other processes. It may also be linked to other processes so
|
||||
#! that it receives a message if that process terminates.
|
||||
[ ] random-256 make-mailbox <process> ;
|
||||
|
||||
: make-linked-process ( process -- process )
|
||||
#! Return a process set to run on the local node. That process is
|
||||
#! linked to the process on the stack. It will receive a message if
|
||||
#! that process terminates.
|
||||
1quotation random-256 make-mailbox <process> ;
|
||||
PRIVATE>
|
||||
|
||||
: self ( -- process )
|
||||
\ self get ;
|
||||
|
||||
<PRIVATE
|
||||
: init-main-process ( -- )
|
||||
#! Setup the main process.
|
||||
make-process \ self set-global ;
|
||||
|
||||
: with-process ( quot process -- )
|
||||
#! Calls the quotation with 'self' set
|
||||
#! to the given process.
|
||||
\ self rot with-variable ; inline
|
||||
|
||||
PRIVATE>
|
||||
|
||||
DEFER: register-process
|
||||
DEFER: unregister-process
|
||||
|
||||
<PRIVATE
|
||||
: ((spawn)) ( quot -- )
|
||||
self dup process-pid swap register-process
|
||||
[ self process-pid unregister-process ] [ ] cleanup ; inline
|
||||
|
||||
: (spawn) ( quot -- process )
|
||||
[ in-thread ] make-process [ with-process ] keep ; inline
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: spawn ( quot -- process )
|
||||
[ ((spawn)) ] curry (spawn) ; inline
|
||||
|
||||
TUPLE: linked-exception error ;
|
||||
|
||||
C: <linked-exception> linked-exception
|
||||
|
||||
: while-no-messages ( quot -- )
|
||||
#! Run the quotation in a loop while no messages are in
|
||||
#! the processes mailbox. The quot should have stack effect
|
||||
#! ( -- ).
|
||||
>r self process-mailbox r> while-mailbox-empty ; inline
|
||||
|
||||
M: process send ( message process -- )
|
||||
process-mailbox mailbox-put ;
|
||||
|
||||
: receive ( -- message )
|
||||
self process-mailbox mailbox-get dup linked-exception? [
|
||||
linked-exception-error rethrow
|
||||
] when ;
|
||||
|
||||
: receive-if ( pred -- message )
|
||||
self process-mailbox mailbox-get? dup linked-exception? [
|
||||
linked-exception-error rethrow
|
||||
] when ; inline
|
||||
|
||||
: rethrow-linked ( error -- )
|
||||
#! Rethrow the error to the linked process
|
||||
self process-links [
|
||||
over <linked-exception> swap send
|
||||
] each drop ;
|
||||
|
||||
<PRIVATE
|
||||
: (spawn-link) ( quot -- process )
|
||||
[ in-thread ] self make-linked-process
|
||||
[ with-process ] keep ; inline
|
||||
PRIVATE>
|
||||
|
||||
: spawn-link ( quot -- process )
|
||||
[ [ rethrow-linked ] recover ] curry
|
||||
[ ((spawn)) ] curry (spawn-link) ; inline
|
||||
|
||||
<PRIVATE
|
||||
: (recv) ( msg form -- )
|
||||
#! Process a form with the following format:
|
||||
#! [ pred match-quot ]
|
||||
#! 'pred' is a word that has stack effect ( msg -- bool ). It is
|
||||
#! executed with the message on the stack. It should return a
|
||||
#! boolean if it is a message this form should process.
|
||||
#! 'match-quot' is a quotation with stack effect ( msg -- ). It
|
||||
#! will be called with the message on the top of the stack if
|
||||
#! the 'pred' word returned true.
|
||||
[ first execute ] 2keep rot [ second call ] [ 2drop ] if ;
|
||||
PRIVATE>
|
||||
|
||||
: recv ( forms -- )
|
||||
#! Get a message from the processes mailbox. Compare it against the
|
||||
#! forms to run a quotation if it matches the given message. 'forms'
|
||||
#! is a list of quotations in the following format:
|
||||
#! [ pred match-quot ]
|
||||
#! 'pred' is a word that has stack effect ( msg -- bool ). It is
|
||||
#! executed with the message on the stack. It should return a
|
||||
#! boolean if it is a message this form should process.
|
||||
#! 'match-quot' is a quotation with stack effect ( msg -- ). It
|
||||
#! will be called with the message on the top of the stack if
|
||||
#! the 'pred' word returned true.
|
||||
#! Each form in the list will be matched against the message,
|
||||
#! even if a prior match succeeded. This means multiple quotations
|
||||
#! may be run against the message.
|
||||
receive swap [ dupd (recv) ] each drop ;
|
||||
|
||||
MATCH-VARS: ?from ?tag ;
|
||||
|
||||
<PRIVATE
|
||||
: tag-message ( message -- tagged-message )
|
||||
#! Given a message, wrap it with the sending process and a unique tag.
|
||||
>r self random-256 r> 3array ;
|
||||
PRIVATE>
|
||||
|
||||
: send-synchronous ( message process -- reply )
|
||||
#! Sends a message to the process synchronously. The
|
||||
#! message will be wrapped to include the process of the sender
|
||||
#! and a unique tag. After being sent the sending process will
|
||||
#! block for a reply tagged with the same unique tag.
|
||||
>r tag-message dup r> send second _ 2array [ match ] curry
|
||||
receive-if second ;
|
||||
|
||||
<PRIVATE
|
||||
: forever ( quot -- )
|
||||
#! Loops forever executing the quotation.
|
||||
dup slip forever ;
|
||||
|
||||
SYMBOL: quit-cc
|
||||
|
||||
: (spawn-server) ( quot -- )
|
||||
#! Receive a message, and run 'quot' on it. If 'quot'
|
||||
#! returns true, start again, otherwise exit loop.
|
||||
#! The quotation should have stack effect ( message -- bool ).
|
||||
"Waiting for message in server: " write
|
||||
self process-pid print
|
||||
receive over call [ (spawn-server) ] when ;
|
||||
PRIVATE>
|
||||
|
||||
: spawn-server ( quot -- process )
|
||||
#! Spawn a server that receives messages, calling the
|
||||
#! quotation on the message. If the quotation returns false
|
||||
#! the spawned process exits. If it returns true, the process
|
||||
#! starts from the beginning again. The quotation should have
|
||||
#! stack effect ( message -- bool ).
|
||||
[
|
||||
(spawn-server)
|
||||
"Exiting process: " write self process-pid print
|
||||
] curry spawn ; inline
|
||||
|
||||
: spawn-linked-server ( quot -- process )
|
||||
#! Similar to 'spawn-server' but the parent process will be linked
|
||||
#! to the child.
|
||||
[
|
||||
(spawn-server)
|
||||
"Exiting process: " write self process-pid print
|
||||
] curry spawn-link ; inline
|
||||
|
||||
: server-cc ( -- cc|process )
|
||||
#! Captures the current continuation and returns the value.
|
||||
#! If that CC is called with a process on the stack it will
|
||||
#! set 'self' for the current process to it. Otherwise it will
|
||||
#! return the value. This allows capturing a continuation in a server,
|
||||
#! and jumping back into it from a spawn and keeping the 'self'
|
||||
#! variable correct. It's a workaround until I can find out how to
|
||||
#! stop 'self' from being clobbered back to its old value.
|
||||
[ ] callcc1 dup process? [ \ self set-global f ] when ;
|
||||
|
||||
: call-server-cc ( server-cc -- )
|
||||
#! Calls the server continuation passing the current 'self'
|
||||
#! so the server continuation gets its new self updated.
|
||||
self swap call ;
|
||||
|
||||
TUPLE: future value processes ;
|
||||
|
||||
: notify-future ( value future -- )
|
||||
tuck set-future-value
|
||||
dup future-processes [ schedule-thread ] each
|
||||
f swap set-future-processes ;
|
||||
|
||||
: future ( quot -- future )
|
||||
#! Spawn a process to call the quotation and immediately return.
|
||||
f V{ } clone \ future construct-boa [
|
||||
[
|
||||
>r [ t 2array ] compose [ f 2array ] recover r>
|
||||
notify-future
|
||||
] 2curry spawn drop
|
||||
] keep ;
|
||||
|
||||
: ?future ( future -- result )
|
||||
#! Block the process until the future has completed and then
|
||||
#! place the result on the stack. Return the result
|
||||
#! immediately if the future has completed.
|
||||
dup future-value [
|
||||
first2 [ rethrow ] unless
|
||||
] [
|
||||
dup [ future-processes push stop ] curry callcc0 ?future
|
||||
] ?if ;
|
||||
|
||||
: parallel-map ( seq quot -- newseq )
|
||||
#! Spawn a process to apply quot to each element of seq,
|
||||
#! joining the results into a sequence at the end.
|
||||
[ curry future ] curry map [ ?future ] map ;
|
||||
|
||||
: parallel-each ( seq quot -- )
|
||||
#! Spawn a process to apply quot to each element of seq,
|
||||
#! and waits for all processes to complete.
|
||||
[ f ] compose parallel-map drop ;
|
||||
|
||||
TUPLE: promise fulfilled? value processes ;
|
||||
|
||||
: <promise> ( -- <promise> )
|
||||
f f V{ } clone promise construct-boa ;
|
||||
|
||||
: fulfill ( value promise -- )
|
||||
#! Set the future of the promise to the given value. Threads
|
||||
#! blocking on the promise will then be released.
|
||||
dup promise-fulfilled? [
|
||||
2drop
|
||||
] [
|
||||
[ set-promise-value ] keep
|
||||
[ t swap set-promise-fulfilled? ] keep
|
||||
[ promise-processes ] keep
|
||||
V{ } clone swap set-promise-processes
|
||||
[ thread-continuation schedule-thread ] each yield
|
||||
] if ;
|
||||
|
||||
<PRIVATE
|
||||
: (maybe-block-promise) ( promise timeout -- promise )
|
||||
#! Block the process if the promise is unfulfilled. This is different from
|
||||
#! (mailbox-block-if-empty) in that when a promise is fulfilled, all threads
|
||||
#! need to be resumed, rather than just one.
|
||||
over promise-fulfilled? [
|
||||
drop
|
||||
] [
|
||||
[ <thread> swap promise-processes push stop ] callcc0
|
||||
drop
|
||||
] if ;
|
||||
PRIVATE>
|
||||
|
||||
: ?promise* ( promise timeout -- result )
|
||||
(maybe-block-promise) promise-value ;
|
||||
|
||||
: ?promise ( promise -- result )
|
||||
f ?promise* ;
|
||||
|
||||
! ******************************
|
||||
! Experimental code below
|
||||
! ******************************
|
||||
<PRIVATE
|
||||
: (lazy) ( v -- )
|
||||
receive {
|
||||
{ { ?from ?tag _ }
|
||||
[ ?tag over 2array ?from send (lazy) ] }
|
||||
} match-cond ;
|
||||
PRIVATE>
|
||||
|
||||
: lazy ( quot -- lazy )
|
||||
#! Spawn a process that immediately blocks and return it.
|
||||
#! When '?lazy' is called on the returned process, call the quotation
|
||||
#! and return the result. The quotation must have stack effect ( -- X ).
|
||||
[
|
||||
receive {
|
||||
{ { ?from ?tag _ }
|
||||
[ call ?tag over 2array ?from send (lazy) ] }
|
||||
} match-cond
|
||||
] spawn nip ;
|
||||
|
||||
: ?lazy ( lazy -- result )
|
||||
#! Given a process spawned using 'lazy', evaluate it and return the result.
|
||||
f swap send-synchronous ;
|
||||
|
||||
<PRIVATE
|
||||
: remote-processes ( -- hash )
|
||||
\ remote-processes get-global ;
|
||||
PRIVATE>
|
||||
|
||||
: register-process ( name process -- )
|
||||
swap remote-processes set-at ;
|
||||
|
||||
: unregister-process ( name -- )
|
||||
remote-processes delete-at ;
|
||||
|
||||
: get-process ( name -- process )
|
||||
remote-processes at ;
|
||||
|
||||
[
|
||||
H{ } clone \ remote-processes set-global
|
||||
init-main-process
|
||||
self [ process-pid ] keep register-process
|
||||
] "process-registry" add-init-hook
|
|
@ -0,0 +1,13 @@
|
|||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: dlists threads kernel arrays sequences ;
|
||||
IN: concurrency.conditions
|
||||
|
||||
: notify-1 ( dlist -- )
|
||||
dup dlist-empty? [ drop ] [ pop-back second resume ] if ;
|
||||
|
||||
: notify-all ( dlist -- )
|
||||
[ second resume ] dlist-slurp yield ;
|
||||
|
||||
: wait ( queue timeout status -- )
|
||||
>r [ 2array swap push-front ] r> suspend 3drop ; inline
|
|
@ -0,0 +1 @@
|
|||
Low-level wait/notify support
|
|
@ -0,0 +1,24 @@
|
|||
USING: help.markup help.syntax sequences ;
|
||||
IN: concurrency.count-downs
|
||||
|
||||
HELP: <count-down>
|
||||
{ $values { "n" "a non-negative integer" } { "count-down" count-down } }
|
||||
{ $description "Creates a new count-down latch." }
|
||||
{ $errors "Throws an error if the count is lower than zero." } ;
|
||||
|
||||
HELP: count-down
|
||||
{ $values { "count-down" count-down } }
|
||||
{ $description "Decrements a count-down latch. If it reaches zero, all threads blocking on " { $link await } " are notified." }
|
||||
{ $errors "Throws an error if an attempt is made to decrement the count lower than zero." } ;
|
||||
|
||||
HELP: await
|
||||
{ $values { "count-down" count-down } }
|
||||
{ $description "Waits until the count-down value reaches zero." } ;
|
||||
|
||||
ARTICLE: "concurrency.count-downs" "Count-down latches"
|
||||
"The " { $vocab-link "concurrency.count-downs" } " vocabulary implements the " { $emphasis "count-down latch" } " data type, whichis a wrapper for a non-negative integer value which tends towards zero. A thread can either decrement the value, or wait for it to become zero."
|
||||
{ $subsection <count-down> }
|
||||
{ $subsection count-down }
|
||||
{ $subsection await } ;
|
||||
|
||||
ABOUT: "concurrency.count-downs"
|
|
@ -0,0 +1,16 @@
|
|||
USING: concurrency.count-downs threads kernel tools.test ;
|
||||
IN: temporary`
|
||||
|
||||
[ ] [ 0 <count-down> await ] unit-test
|
||||
|
||||
[ 1 <count-down> dup count-down count-down ] must-fail
|
||||
|
||||
[ ] [
|
||||
1 <count-down>
|
||||
3 <count-down>
|
||||
2dup [ await count-down ] 2curry "Master" spawn drop
|
||||
dup [ count-down ] curry "Slave" spawn drop
|
||||
dup [ count-down ] curry "Slave" spawn drop
|
||||
dup [ count-down ] curry "Slave" spawn drop
|
||||
drop await
|
||||
] unit-test
|
|
@ -0,0 +1,39 @@
|
|||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: dlists kernel math concurrency.promises
|
||||
concurrency.messaging ;
|
||||
IN: concurrency.count-downs
|
||||
|
||||
! http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/CountDownLatch.html
|
||||
|
||||
TUPLE: count-down n promise ;
|
||||
|
||||
: count-down-check ( count-down -- )
|
||||
dup count-down-n zero? [
|
||||
t swap count-down-promise fulfill
|
||||
] [ drop ] if ;
|
||||
|
||||
: <count-down> ( n -- count-down )
|
||||
dup 0 < [ "Invalid count for count down" throw ] when
|
||||
<promise> \ count-down construct-boa
|
||||
dup count-down-check ;
|
||||
|
||||
: count-down ( count-down -- )
|
||||
dup count-down-n dup zero? [
|
||||
"Count down already done" throw
|
||||
] [
|
||||
1- over set-count-down-n
|
||||
count-down-check
|
||||
] if ;
|
||||
|
||||
: await-timeout ( count-down timeout -- )
|
||||
>r count-down-promise r> ?promise-timeout drop ;
|
||||
|
||||
: await ( count-down -- )
|
||||
f await-timeout ;
|
||||
|
||||
: spawn-stage ( quot count-down -- )
|
||||
[ [ count-down ] curry compose ] keep
|
||||
"Count down stage"
|
||||
swap count-down-promise
|
||||
promise-mailbox spawn-linked-to drop ;
|
|
@ -0,0 +1 @@
|
|||
Count-down latches
|
|
@ -1,25 +1,20 @@
|
|||
USING: help.markup help.syntax concurrency ;
|
||||
USING: help.markup help.syntax concurrency.messaging threads ;
|
||||
IN: concurrency.distributed
|
||||
|
||||
HELP: <remote-process>
|
||||
{ $values { "node" "a node object" }
|
||||
{ "pid" "a process id" }
|
||||
{ "remote-process" "the constructed remote-process object" }
|
||||
HELP: local-node
|
||||
{ $values { "addrspec" "an address specifier" }
|
||||
}
|
||||
{ $description "Constructs a proxy to a process running on another node. It can be used to send messages to the process it is acting as a proxy for." }
|
||||
{ $see-also <node> <process> spawn send } ;
|
||||
{ $description "Return the node the current thread is running on." } ;
|
||||
|
||||
HELP: start-node
|
||||
{ $values { "port" "a port number between 0 and 65535" } }
|
||||
{ $description "Starts a node server for receiving messages from remote Factor instances." } ;
|
||||
|
||||
HELP: <node>
|
||||
{ $values { "hostname" "the hostname of the node as a string" }
|
||||
{ "port" "the integer port number of the node" }
|
||||
{ "node" "the constructed node object" }
|
||||
}
|
||||
{ $description "Processes run on nodes. Each node has a hostname and a port." }
|
||||
{ $see-also localnode } ;
|
||||
ARTICLE: "concurrency.distributed" "Distributed message passing"
|
||||
"The " { $vocab-link "concurrency.distributed" } " implements transparent distributed message passing."
|
||||
{ $subsection start-node }
|
||||
"Instances of " { $link thread } " can be sent to remote processes, at which point they are converted to objects holding the thread ID and the current node's host name:"
|
||||
{ $subsection remote-process }
|
||||
"The " { $vocab-link "serialize" } " vocabulary is used to convert Factor objects to byte arrays for transfer over a socket." ;
|
||||
|
||||
HELP: localnode
|
||||
{ $values { "node" "a node object" }
|
||||
}
|
||||
{ $description "Return the node the process is currently running on." }
|
||||
{ $see-also <node> } ;
|
||||
ABOUT: "concurrency.distributed"
|
||||
|
|
|
@ -1,43 +1,36 @@
|
|||
! Copyright (C) 2005 Chris Double. All Rights Reserved.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: serialize sequences concurrency io io.server qualified
|
||||
threads arrays namespaces kernel ;
|
||||
USING: serialize sequences concurrency.messaging
|
||||
threads io io.server qualified arrays
|
||||
namespaces kernel ;
|
||||
QUALIFIED: io.sockets
|
||||
IN: concurrency.distributed
|
||||
|
||||
TUPLE: node hostname port ;
|
||||
|
||||
C: <node> node
|
||||
SYMBOL: local-node ( -- addrspec )
|
||||
|
||||
: handle-node-client ( -- )
|
||||
deserialize first2 get-process send ;
|
||||
|
||||
: node-server ( port -- )
|
||||
internet-server
|
||||
"concurrency.distributed"
|
||||
[ handle-node-client ] with-server ;
|
||||
: (start-node) ( addrspecs addrspec -- )
|
||||
[
|
||||
local-node set-global
|
||||
"concurrency.distributed"
|
||||
[ handle-node-client ] with-server
|
||||
] 2curry f spawn drop ;
|
||||
|
||||
: send-to-node ( msg pid host port -- )
|
||||
io.sockets:<inet> io.sockets:<client> [
|
||||
2array serialize
|
||||
] with-stream ;
|
||||
: start-node ( port -- )
|
||||
dup internet-server io.sockets:host-name
|
||||
rot io.sockets:<inet> (start-node) ;
|
||||
|
||||
: localnode ( -- node )
|
||||
\ localnode get ;
|
||||
|
||||
: start-node ( hostname port -- )
|
||||
[ node-server ] in-thread
|
||||
<node> \ localnode set-global ;
|
||||
|
||||
TUPLE: remote-process node pid ;
|
||||
TUPLE: remote-process id node ;
|
||||
|
||||
C: <remote-process> remote-process
|
||||
|
||||
M: remote-process send ( message process -- )
|
||||
#! Send the message via the inter-node protocol
|
||||
{ remote-process-pid remote-process-node } get-slots
|
||||
{ node-hostname node-port } get-slots
|
||||
send-to-node ;
|
||||
M: remote-process send ( message thread -- )
|
||||
{ remote-process-id remote-process-node } get-slots
|
||||
io.sockets:<client> [ 2array serialize ] with-stream ;
|
||||
|
||||
M: process (serialize) ( obj -- )
|
||||
localnode swap process-pid <remote-process> (serialize) ;
|
||||
M: thread (serialize) ( obj -- )
|
||||
thread-id local-node get-global
|
||||
<remote-process>
|
||||
(serialize) ;
|
||||
|
|
0
extra/network-clipboard/authors.txt → extra/concurrency/exchangers/authors.txt
Executable file → Normal file
0
extra/network-clipboard/authors.txt → extra/concurrency/exchangers/authors.txt
Executable file → Normal file
|
@ -0,0 +1,22 @@
|
|||
USING: help.markup help.syntax sequences kernel ;
|
||||
IN: concurrency.exchangers
|
||||
|
||||
HELP: exchanger
|
||||
{ $class-description "The class of object exchange points." } ;
|
||||
|
||||
HELP: <exchanger>
|
||||
{ $values { "exchanger" exchanger } }
|
||||
{ $description "Creates a new object exchange point." } ;
|
||||
|
||||
HELP: exchange
|
||||
{ $values { "obj" object } { "exchanger" exchanger } { "newobj" object } }
|
||||
{ $description "Waits for another thread to call " { $link exchange } " on the same exchanger. The thread's call to " { $link exchange } " returns with " { $snippet "obj" } " on the stack, and the object passed to " { $link exchange } " by the other thread is left on the current's thread stack as " { $snippet "newobj" } "." } ;
|
||||
|
||||
ARTICLE: "concurrency.exchangers" "Object exchange points"
|
||||
"The " { $vocab-link "concurrency.exchangers" } " vocabulary implements " { $emphasis "object exchange points" } ", which are rendezvous points where two threads can exchange objects."
|
||||
{ $subsection exchanger }
|
||||
{ $subsection <exchanger> }
|
||||
{ $subsection exchange }
|
||||
"One use-case is two threads, where one thread reads data into a buffer and another thread processes the data. The reader thread can begin by reading the data, then passing the buffer through an exchanger, then recursing. The processing thread can begin by creating an empty buffer, and exchanging it through the exchanger. It then processes the result and recurses." ;
|
||||
|
||||
ABOUT: "concurrency.exchangers"
|
|
@ -0,0 +1,30 @@
|
|||
IN: temporary
|
||||
USING: sequences tools.test concurrency.exchangers
|
||||
concurrency.count-downs concurrency.promises locals kernel
|
||||
threads ;
|
||||
|
||||
:: exchanger-test | |
|
||||
[let |
|
||||
ex [ <exchanger> ]
|
||||
c [ 2 <count-down> ]
|
||||
v1! [ f ]
|
||||
v2! [ f ]
|
||||
pr [ <promise> ] |
|
||||
|
||||
[
|
||||
c await
|
||||
v1 ", " v2 3append pr fulfill
|
||||
] "Awaiter" spawn drop
|
||||
|
||||
[
|
||||
"Goodbye world" ex exchange v1! c count-down
|
||||
] "Exchanger 1" spawn drop
|
||||
|
||||
[
|
||||
"Hello world" ex exchange v2! c count-down
|
||||
] "Exchanger 2" spawn drop
|
||||
|
||||
pr ?promise
|
||||
] ;
|
||||
|
||||
[ "Hello world, Goodbye world" ] [ exchanger-test ] unit-test
|
|
@ -0,0 +1,21 @@
|
|||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel threads boxes ;
|
||||
IN: concurrency.exchangers
|
||||
|
||||
! Motivated by
|
||||
! http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Exchanger.html
|
||||
|
||||
TUPLE: exchanger thread object ;
|
||||
|
||||
: <exchanger> ( -- exchanger )
|
||||
<box> <box> exchanger construct-boa ;
|
||||
|
||||
: exchange ( obj exchanger -- newobj )
|
||||
dup exchanger-thread box-full? [
|
||||
dup exchanger-object box>
|
||||
>r exchanger-thread box> resume-with r>
|
||||
] [
|
||||
[ exchanger-object >box ] keep
|
||||
[ exchanger-thread >box ] curry "Exchange wait" suspend
|
||||
] if ;
|
|
@ -0,0 +1 @@
|
|||
Object exchange points
|
|
@ -1 +1,2 @@
|
|||
Chris Double
|
||||
Slava Pestov
|
|
@ -0,0 +1,29 @@
|
|||
! Copyright (C) 2005, 2008 Chris Double, Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: concurrency.promises concurrency.messaging kernel arrays
|
||||
continuations help.markup help.syntax quotations ;
|
||||
IN: concurrency.futures
|
||||
|
||||
HELP: future
|
||||
{ $values { "quot" "a quotation with stack effect " { $snippet "( -- value )" } } { "future" future } }
|
||||
{ $description "Creates a deferred computation."
|
||||
$nl
|
||||
"The quotation begins with an empty data stack, an empty catch stack, and a name stack containing the global namespace only. This means that the only way to pass data to the quotation is to partially apply the data, for example using " { $link curry } " or " { $link compose } "." } ;
|
||||
|
||||
HELP: ?future-timeout
|
||||
{ $values { "future" future } { "timeout" "a timeout in milliseconds or " { $link f } } { "value" object } }
|
||||
{ $description "Waits for a deferred computation to complete, blocking indefinitely if " { $snippet "timeout" } " is " { $link f } ", otherwise waiting up to " { $snippet "timeout" } " milliseconds." }
|
||||
{ $errors "Throws an error if the timeout expires before the computation completes. Also throws an error if the future quotation threw an error." } ;
|
||||
|
||||
HELP: ?future
|
||||
{ $values { "future" future } { "value" object } }
|
||||
{ $description "Waits for a deferred computation to complete, blocking indefinitely." }
|
||||
{ $errors "Throws an error if future quotation threw an error." } ;
|
||||
|
||||
ARTICLE: "concurrency.futures" "Futures"
|
||||
"The " { $vocab-link "concurrency.futures" } " vocabulary implements " { $emphasis "futures" } ", which are deferred computations performed in a background thread. A thread may create a future, then proceed to perform other tasks, then later wait for the future to complete."
|
||||
{ $subsection future }
|
||||
{ $subsection ?future }
|
||||
{ $subsection ?future-timeout } ;
|
||||
|
||||
ABOUT: "concurrency.futures"
|
|
@ -0,0 +1,25 @@
|
|||
IN: temporary
|
||||
USING: concurrency.futures kernel tools.test threads ;
|
||||
|
||||
[ 50 ] [
|
||||
[ 50 ] future ?future
|
||||
] unit-test
|
||||
|
||||
[
|
||||
[ "this should propogate" throw ] future ?future
|
||||
] must-fail
|
||||
|
||||
[ ] [
|
||||
[ "this should not propogate" throw ] future drop
|
||||
] unit-test
|
||||
|
||||
! Race condition with futures
|
||||
[ 3 3 ] [
|
||||
[ 3 ] future
|
||||
dup ?future swap ?future
|
||||
] unit-test
|
||||
|
||||
! Another race
|
||||
[ 3 ] [
|
||||
[ 3 yield ] future ?future
|
||||
] unit-test
|
|
@ -0,0 +1,17 @@
|
|||
! Copyright (C) 2005, 2008 Chris Double, Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: concurrency.promises concurrency.messaging kernel arrays
|
||||
continuations ;
|
||||
IN: concurrency.futures
|
||||
|
||||
: future ( quot -- future )
|
||||
<promise> [
|
||||
[ [ >r call r> fulfill ] 2curry "Future" ] keep
|
||||
promise-mailbox spawn-linked-to drop
|
||||
] keep ; inline
|
||||
|
||||
: ?future-timeout ( future timeout -- value )
|
||||
?promise-timeout ;
|
||||
|
||||
: ?future ( future -- value )
|
||||
?promise ;
|
|
@ -0,0 +1 @@
|
|||
Deferred computations
|
|
@ -0,0 +1 @@
|
|||
Slava Pestov
|
|
@ -0,0 +1,60 @@
|
|||
USING: help.markup help.syntax sequences kernel quotations ;
|
||||
IN: concurrency.locks
|
||||
|
||||
HELP: lock
|
||||
{ $class-description "The class of mutual exclusion locks." } ;
|
||||
|
||||
HELP: <lock>
|
||||
{ $values { "lock" lock } }
|
||||
{ $description "Creates a non-reentrant lock." } ;
|
||||
|
||||
HELP: <reentrant-lock>
|
||||
{ $values { "lock" lock } }
|
||||
{ $description "Creates a reentrant lock." } ;
|
||||
|
||||
HELP: with-lock
|
||||
{ $values { "lock" lock } { "timeout" "a timeout in milliseconds or " { $link f } } { "quot" quotation } }
|
||||
{ $description "Calls the quotation, ensuring that only one thread executes with the lock held at a time. If another thread is holding the lock, blocks until the thread releases the lock." }
|
||||
{ $errors "Throws an error if the lock could not be acquired before the timeout expires. A timeout value of " { $link f } " means the thread is willing to wait indefinitely." } ;
|
||||
|
||||
ARTICLE: "concurrency.locks.mutex" "Mutual-exclusion locks"
|
||||
"A mutual-exclusion lock ensures that only one thread executes with the lock held at a time. They are used to protect critical sections so that certain operations appear to be atomic to other threads."
|
||||
$nl
|
||||
"There are two varieties of locks: non-reentrant and reentrant. The latter may be acquired recursively by the same thread. Attempting to do so with the former will deadlock."
|
||||
{ $subsection lock }
|
||||
{ $subsection <lock> }
|
||||
{ $subsection <reentrant-lock> }
|
||||
{ $subsection with-lock } ;
|
||||
|
||||
HELP: rw-lock
|
||||
{ $class-description "The class of reader/writer locks." } ;
|
||||
|
||||
HELP: with-read-lock
|
||||
{ $values { "lock" lock } { "timeout" "a timeout in milliseconds or " { $link f } } { "quot" quotation } }
|
||||
{ $description "Calls the quotation, ensuring that no other thread is holding a write lock at the same time. If another thread is holding a write lock, blocks until the thread releases the lock." }
|
||||
{ $errors "Throws an error if the lock could not be acquired before the timeout expires. A timeout value of " { $link f } " means the thread is willing to wait indefinitely." } ;
|
||||
|
||||
HELP: with-write-lock
|
||||
{ $values { "lock" lock } { "timeout" "a timeout in milliseconds or " { $link f } } { "quot" quotation } }
|
||||
{ $description "Calls the quotation, ensuring that no other thread is holding a read or write lock at the same time. If another thread is holding a read or write lock, blocks until the thread releases the lock." }
|
||||
{ $errors "Throws an error if the lock could not be acquired before the timeout expires. A timeout value of " { $link f } " means the thread is willing to wait indefinitely." } ;
|
||||
|
||||
ARTICLE: "concurrency.locks.rw" "Read-write locks"
|
||||
"A read-write lock encapsulates a common pattern in the implementation of concurrent data structures, where one wishes to ensure that a thread is able to see a consistent view of the structure for a period of time, during which no other thread modifies the structure."
|
||||
$nl
|
||||
"While this can be achieved with a simple " { $link "concurrency.locks.mutex" } ", performance will suffer, since in fact multiple threads can view the structure at the same time; serialization must only be enforced for writes."
|
||||
$nl
|
||||
"Read/write locks allow any number of threads to hold the read lock simulateneously, however attempting to acquire a write lock blocks until all other threads release read locks and write locks."
|
||||
$nl
|
||||
"Read/write locks are reentrant. A thread holding a read lock may acquire a write lock recursively, and a thread holding a write lock may acquire a write lock or a read lock recursively, however a thread holding a read lock may not acquire a write lock recursively since that could break invariants assumed by the code executing with the read lock held."
|
||||
{ $subsection rw-lock }
|
||||
{ $subsection <rw-lock> }
|
||||
{ $subsection with-read-lock }
|
||||
{ $subsection with-write-lock } ;
|
||||
|
||||
ARTICLE: "concurrency.locks" "Locks"
|
||||
"A " { $emphasis "lock" } " is an object protecting a critical region of code, enforcing a particular mutual-exclusion policy. The " { $vocab-link "concurrency.locks" } " vocabulary implements two types of locks:"
|
||||
{ $subsection "concurrency.locks.mutex" }
|
||||
{ $subsection "concurrency.locks.rw" } ;
|
||||
|
||||
ABOUT: "concurrency.locks"
|
|
@ -0,0 +1,159 @@
|
|||
IN: temporary
|
||||
USING: tools.test concurrency.locks concurrency.count-downs
|
||||
locals kernel threads sequences ;
|
||||
|
||||
:: lock-test-0 | |
|
||||
[let | v [ V{ } clone ]
|
||||
c [ 2 <count-down> ] |
|
||||
|
||||
[
|
||||
yield
|
||||
1 v push
|
||||
yield
|
||||
2 v push
|
||||
c count-down
|
||||
] "Lock test 1" spawn drop
|
||||
|
||||
[
|
||||
yield
|
||||
3 v push
|
||||
yield
|
||||
4 v push
|
||||
c count-down
|
||||
] "Lock test 2" spawn drop
|
||||
|
||||
c await
|
||||
v
|
||||
] ;
|
||||
|
||||
:: lock-test-1 | |
|
||||
[let | v [ V{ } clone ]
|
||||
l [ <lock> ]
|
||||
c [ 2 <count-down> ] |
|
||||
|
||||
[
|
||||
l f [
|
||||
yield
|
||||
1 v push
|
||||
yield
|
||||
2 v push
|
||||
] with-lock
|
||||
c count-down
|
||||
] "Lock test 1" spawn drop
|
||||
|
||||
[
|
||||
l f [
|
||||
yield
|
||||
3 v push
|
||||
yield
|
||||
4 v push
|
||||
] with-lock
|
||||
c count-down
|
||||
] "Lock test 2" spawn drop
|
||||
|
||||
c await
|
||||
v
|
||||
] ;
|
||||
|
||||
[ V{ 1 3 2 4 } ] [ lock-test-0 ] unit-test
|
||||
[ V{ 1 2 3 4 } ] [ lock-test-1 ] unit-test
|
||||
|
||||
[ 3 ] [
|
||||
<reentrant-lock> dup f [
|
||||
f [
|
||||
3
|
||||
] with-lock
|
||||
] with-lock
|
||||
] unit-test
|
||||
|
||||
[ ] [ <rw-lock> drop ] unit-test
|
||||
|
||||
[ ] [ <rw-lock> f [ ] with-read-lock ] unit-test
|
||||
|
||||
[ ] [ <rw-lock> dup f [ f [ ] with-read-lock ] with-read-lock ] unit-test
|
||||
|
||||
[ ] [ <rw-lock> f [ ] with-write-lock ] unit-test
|
||||
|
||||
[ ] [ <rw-lock> dup f [ f [ ] with-write-lock ] with-write-lock ] unit-test
|
||||
|
||||
[ ] [ <rw-lock> dup f [ f [ ] with-read-lock ] with-write-lock ] unit-test
|
||||
|
||||
:: rw-lock-test-1 | |
|
||||
[let | l [ <rw-lock> ]
|
||||
c [ 1 <count-down> ]
|
||||
c' [ 1 <count-down> ]
|
||||
c'' [ 4 <count-down> ]
|
||||
v [ V{ } clone ] |
|
||||
|
||||
[
|
||||
l f [
|
||||
1 v push
|
||||
c count-down
|
||||
yield
|
||||
3 v push
|
||||
] with-read-lock
|
||||
c'' count-down
|
||||
] "R/W lock test 1" spawn drop
|
||||
|
||||
[
|
||||
c await
|
||||
l f [
|
||||
4 v push
|
||||
1000 sleep
|
||||
5 v push
|
||||
] with-write-lock
|
||||
c'' count-down
|
||||
] "R/W lock test 2" spawn drop
|
||||
|
||||
[
|
||||
c await
|
||||
l f [
|
||||
2 v push
|
||||
c' count-down
|
||||
] with-read-lock
|
||||
c'' count-down
|
||||
] "R/W lock test 4" spawn drop
|
||||
|
||||
[
|
||||
c' await
|
||||
l f [
|
||||
6 v push
|
||||
] with-write-lock
|
||||
c'' count-down
|
||||
] "R/W lock test 5" spawn drop
|
||||
|
||||
c'' await
|
||||
v
|
||||
] ;
|
||||
|
||||
[ V{ 1 2 3 4 5 6 } ] [ rw-lock-test-1 ] unit-test
|
||||
|
||||
:: rw-lock-test-2 | |
|
||||
[let | l [ <rw-lock> ]
|
||||
c [ 1 <count-down> ]
|
||||
c' [ 2 <count-down> ]
|
||||
v [ V{ } clone ] |
|
||||
|
||||
[
|
||||
l f [
|
||||
1 v push
|
||||
c count-down
|
||||
1000 sleep
|
||||
2 v push
|
||||
] with-write-lock
|
||||
c' count-down
|
||||
] "R/W lock test 1" spawn drop
|
||||
|
||||
[
|
||||
c await
|
||||
l f [
|
||||
3 v push
|
||||
] with-read-lock
|
||||
c' count-down
|
||||
] "R/W lock test 2" spawn drop
|
||||
|
||||
c' await
|
||||
v
|
||||
] ;
|
||||
|
||||
[ V{ 1 2 3 } ] [ rw-lock-test-2 ] unit-test
|
|
@ -0,0 +1,90 @@
|
|||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: dlists kernel threads continuations math
|
||||
concurrency.conditions ;
|
||||
IN: concurrency.locks
|
||||
|
||||
! Simple critical sections
|
||||
TUPLE: lock threads owner reentrant? ;
|
||||
|
||||
: <lock> ( -- lock )
|
||||
<dlist> f f lock construct-boa ;
|
||||
|
||||
: <reentrant-lock> ( -- lock )
|
||||
<dlist> f t lock construct-boa ;
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: acquire-lock ( lock timeout -- )
|
||||
over lock-owner
|
||||
[ 2dup >r lock-threads r> "lock" wait ] when drop
|
||||
self swap set-lock-owner ;
|
||||
|
||||
: release-lock ( lock -- )
|
||||
f over set-lock-owner
|
||||
lock-threads notify-1 ;
|
||||
|
||||
: do-lock ( lock timeout quot acquire release -- )
|
||||
>r swap compose pick >r 2curry r> r> curry [ ] cleanup ;
|
||||
inline
|
||||
|
||||
: (with-lock) ( lock timeout quot -- )
|
||||
[ acquire-lock ] [ release-lock ] do-lock ; inline
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: with-lock ( lock timeout quot -- )
|
||||
pick lock-reentrant? [
|
||||
pick lock-owner self eq? [
|
||||
2nip call
|
||||
] [
|
||||
(with-lock)
|
||||
] if
|
||||
] [
|
||||
(with-lock)
|
||||
] if ; inline
|
||||
|
||||
! Many-reader/single-writer locks
|
||||
TUPLE: rw-lock readers writers reader# writer ;
|
||||
|
||||
: <rw-lock> ( -- lock )
|
||||
<dlist> <dlist> 0 f rw-lock construct-boa ;
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: acquire-read-lock ( lock timeout -- )
|
||||
over rw-lock-writer
|
||||
[ 2dup >r rw-lock-readers r> "read lock" wait ] when drop
|
||||
dup rw-lock-reader# 1+ swap set-rw-lock-reader# ;
|
||||
|
||||
: notify-writer ( lock -- )
|
||||
rw-lock-writers notify-1 ;
|
||||
|
||||
: release-read-lock ( lock -- )
|
||||
dup rw-lock-reader# 1- dup pick set-rw-lock-reader#
|
||||
zero? [ notify-writer ] [ drop ] if ;
|
||||
|
||||
: acquire-write-lock ( lock timeout -- )
|
||||
over rw-lock-writer pick rw-lock-reader# 0 > or
|
||||
[ 2dup >r rw-lock-writers r> "write lock" wait ] when drop
|
||||
self swap set-rw-lock-writer ;
|
||||
|
||||
: release-write-lock ( lock -- )
|
||||
f over set-rw-lock-writer
|
||||
dup rw-lock-readers dlist-empty?
|
||||
[ notify-writer ] [ rw-lock-readers notify-all ] if ;
|
||||
|
||||
: do-reentrant-rw-lock ( lock timeout quot quot' -- )
|
||||
>r pick rw-lock-writer self eq? [ 2nip call ] r> if ; inline
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: with-read-lock ( lock timeout quot -- )
|
||||
[
|
||||
[ acquire-read-lock ] [ release-read-lock ] do-lock
|
||||
] do-reentrant-rw-lock ; inline
|
||||
|
||||
: with-write-lock ( lock timeout quot -- )
|
||||
[
|
||||
[ acquire-write-lock ] [ release-write-lock ] do-lock
|
||||
] do-reentrant-rw-lock ; inline
|
|
@ -0,0 +1 @@
|
|||
Traditional locks and many reader/single writer locks
|
|
@ -0,0 +1,2 @@
|
|||
Chris Double
|
||||
Slava Pestov
|
|
@ -0,0 +1,155 @@
|
|||
! Copyright (C) 2006 Chris Double.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: help.syntax help.markup concurrency.messaging.private
|
||||
threads kernel arrays quotations ;
|
||||
IN: concurrency.messaging
|
||||
|
||||
HELP: <mailbox>
|
||||
{ $values { "mailbox" mailbox }
|
||||
}
|
||||
{ $description "A mailbox is an object that can be used for safe thread communication. Items can be put in the mailbox and retrieved in a FIFO order. If the mailbox is empty when a get operation is performed then the thread will block until another thread places something in the mailbox. If multiple threads are waiting on the same mailbox, only one of the waiting threads will be unblocked to thread the get operation." }
|
||||
{ $see-also mailbox-empty? mailbox-put mailbox-get mailbox-get-all while-mailbox-empty mailbox-get? } ;
|
||||
|
||||
HELP: mailbox-empty?
|
||||
{ $values { "mailbox" mailbox }
|
||||
{ "bool" "a boolean" }
|
||||
}
|
||||
{ $description "Return true if the mailbox is empty." }
|
||||
{ $see-also <mailbox> mailbox-put mailbox-get mailbox-get-all while-mailbox-empty mailbox-get? } ;
|
||||
|
||||
HELP: mailbox-put
|
||||
{ $values { "obj" object }
|
||||
{ "mailbox" mailbox }
|
||||
}
|
||||
{ $description "Put the object into the mailbox. Any threads that have a blocking get on the mailbox are resumed. Only one of those threads will successfully get the object, the rest will immediately block waiting for the next item in the mailbox." }
|
||||
{ $see-also <mailbox> mailbox-empty? mailbox-get mailbox-get-all while-mailbox-empty mailbox-get? } ;
|
||||
|
||||
HELP: block-unless-pred
|
||||
{ $values { "pred" "a quotation with stack effect " { $snippet "( X -- bool )" } }
|
||||
{ "mailbox" mailbox }
|
||||
{ "timeout" "a timeout in milliseconds, or " { $link f } }
|
||||
}
|
||||
{ $description "Block the thread if there are no items in the mailbox that return true when the predicate is called with the item on the stack." }
|
||||
{ $see-also <mailbox> mailbox-empty? mailbox-put mailbox-get mailbox-get-all while-mailbox-empty mailbox-get? } ;
|
||||
|
||||
HELP: block-if-empty
|
||||
{ $values { "mailbox" mailbox }
|
||||
{ "timeout" "a timeout in milliseconds, or " { $link f } }
|
||||
}
|
||||
{ $description "Block the thread if the mailbox is empty." }
|
||||
{ $see-also <mailbox> mailbox-empty? mailbox-put mailbox-get mailbox-get-all while-mailbox-empty mailbox-get? } ;
|
||||
|
||||
HELP: mailbox-get
|
||||
{ $values { "mailbox" mailbox }
|
||||
{ "obj" object }
|
||||
}
|
||||
{ $description "Get the first item put into the mailbox. If it is empty the thread blocks until an item is put into it. The thread then resumes, leaving the item on the stack." }
|
||||
{ $see-also <mailbox> mailbox-empty? mailbox-put while-mailbox-empty mailbox-get-all mailbox-get? } ;
|
||||
|
||||
HELP: mailbox-get-all
|
||||
{ $values { "mailbox" mailbox }
|
||||
{ "array" array }
|
||||
}
|
||||
{ $description "Blocks the thread if the mailbox is empty, otherwise removes all objects in the mailbox and returns an array containing the objects." }
|
||||
{ $see-also <mailbox> mailbox-empty? mailbox-put while-mailbox-empty mailbox-get-all mailbox-get? } ;
|
||||
|
||||
HELP: while-mailbox-empty
|
||||
{ $values { "mailbox" mailbox }
|
||||
{ "quot" "a quotation with stack effect " { $snippet "( -- )" } }
|
||||
}
|
||||
{ $description "Repeatedly call the quotation while there are no items in the mailbox." }
|
||||
{ $see-also <mailbox> mailbox-empty? mailbox-put mailbox-get mailbox-get-all mailbox-get? } ;
|
||||
|
||||
HELP: mailbox-get?
|
||||
{ $values { "pred" "a quotation with stack effect " { $snippet "( X -- bool )" } }
|
||||
{ "mailbox" mailbox }
|
||||
{ "obj" object }
|
||||
}
|
||||
{ $description "Get the first item in the mailbox which satisfies the predicate. 'pred' will be called repeatedly for each item in the mailbox. When 'pred' returns true that item will be returned. If nothing in the mailbox satisfies the predicate then the thread will block until something does." }
|
||||
{ $see-also <mailbox> mailbox-empty? mailbox-put mailbox-get mailbox-get-all while-mailbox-empty } ;
|
||||
|
||||
HELP: send
|
||||
{ $values { "message" object }
|
||||
{ "thread" "a thread object" }
|
||||
}
|
||||
{ $description "Send the message to the thread by placing it in the threades mailbox. This is an asynchronous operation and will return immediately. The receving thread will act on the message the next time it retrieves that item from its mailbox (usually using the " { $link receive } " word. The message can be any Factor object. For destinations that are instances of remote-thread the message must be a serializable Factor type." }
|
||||
{ $see-also receive receive-if } ;
|
||||
|
||||
HELP: receive
|
||||
{ $values { "message" object }
|
||||
}
|
||||
{ $description "Return a message from the current threades mailbox. If the box is empty, suspend the thread until another thread places an item in the mailbox (usually via the " { $link send } " word." }
|
||||
{ $see-also send receive-if } ;
|
||||
|
||||
HELP: receive-if
|
||||
{ $values { "pred" "a predicate with stack effect " { $snippet "( obj -- ? )" } }
|
||||
{ "message" object }
|
||||
}
|
||||
{ $description "Return the first message from the current threades mailbox that satisfies the predicate. To satisfy the predicate, " { $snippet "pred" } " is called with the item on the stack and the predicate should leave a boolean indicating whether it was satisfied or not. If nothing in the mailbox satisfies the predicate then the thread will block until something does." }
|
||||
{ $see-also send receive } ;
|
||||
|
||||
HELP: spawn-linked
|
||||
{ $values { "quot" quotation }
|
||||
{ "thread" "a thread object" }
|
||||
}
|
||||
{ $description "Start a thread which runs the given quotation. If that quotation throws an error which is not caught then the error will get propagated to the thread that spawned it. This can be used to set up 'supervisor' threades that restart child threades that crash due to uncaught errors.\n" }
|
||||
{ $see-also spawn } ;
|
||||
|
||||
ARTICLE: { "concurrency" "mailboxes" } "Mailboxes"
|
||||
"Each thread has an associated message queue. Other threads can place items on this queue by sending the thread a message. A thread can check its queue for messages, blocking if none are pending, and thread them as they are queued."
|
||||
$nl
|
||||
"The messages that are sent from thread to thread are any Factor value. Factor tuples are ideal for this sort of thing as you can send a tuple to a thread and the generic word dispatch mechanism can be used to perform actions depending on what the type of the tuple is."
|
||||
$nl
|
||||
"The " { $link spawn } " word pushes the newly-created thread on the calling thread's stack; this thread object can then be sent messages:"
|
||||
{ $subsection send }
|
||||
"A thread can get a message from its queue:"
|
||||
{ $subsection receive }
|
||||
{ $subsection receive }
|
||||
{ $subsection receive-if }
|
||||
"Mailboxes can be created and used directly:"
|
||||
{ $subsection mailbox }
|
||||
{ $subsection <mailbox> }
|
||||
{ $subsection mailbox-get }
|
||||
{ $subsection mailbox-put }
|
||||
{ $subsection mailbox-empty? } ;
|
||||
|
||||
ARTICLE: { "concurrency" "synchronous-sends" } "Synchronous sends"
|
||||
"The " { $link send } " word sends a message asynchronously, and the sending thread continues immediately. It is also possible to send a message to a thread and block until a response is received:"
|
||||
{ $subsection send-synchronous }
|
||||
"To reply to a synchronous message:"
|
||||
{ $subsection reply-synchronous }
|
||||
"An example:"
|
||||
{ $example
|
||||
"USING: concurrency.messaging kernel threads ;"
|
||||
": pong-server ( -- )"
|
||||
" receive >r \"pong\" r> reply-synchronous ;"
|
||||
"[ pong-server t ] spawn-server"
|
||||
"\"ping\" swap send-synchronous ."
|
||||
"\"pong\""
|
||||
} ;
|
||||
|
||||
ARTICLE: { "concurrency" "exceptions" } "Linked exceptions"
|
||||
"A thread can handle exceptions using the standard Factor exception handling mechanism. If an exception is uncaught the thread will terminate. For example:"
|
||||
{ $code "[ 1 0 / \"This will not print\" print ] spawn" }
|
||||
"Processes can be linked so that a parent thread can receive the exception that caused the child thread to terminate. In this way 'supervisor' threades can be created that are notified when child threades terminate and possibly restart them."
|
||||
{ $subsection spawn-linked }
|
||||
"A more flexible version of the above deposits the error in an arbitary mailbox:"
|
||||
{ $subsection spawn-linked-to }
|
||||
"This will create a unidirectional link, such that if an uncaught exception causes the child to terminate, the parent thread can catch it:"
|
||||
{ $code "["
|
||||
" [ 1 0 / \"This will not print\" print ] spawn-linked drop"
|
||||
" receive"
|
||||
"] [ \"Exception caught.\" print ] recover" }
|
||||
"Exceptions are only raised in the parent when the parent does a " { $link receive } " or " { $link receive-if } ". This is because the exception is sent from the child to the parent as a message." ;
|
||||
|
||||
ARTICLE: "concurrency.messaging" "Message-passing concurrency"
|
||||
"The " { $vocab-link "concurrency.messaging" } " vocabulary is based upon the style of concurrency used in systems like Erlang and Termite. It is built on top of the standard Factor lightweight thread system."
|
||||
$nl
|
||||
"A concurrency oriented program is one in which multiple threades run simultaneously in a single Factor image or across multiple running Factor instances. The threades can communicate with each other by asynchronous message sends."
|
||||
$nl
|
||||
"Although threades can share data via Factor's mutable data structures it is not recommended to mix shared state with message passing as it can lead to confusing code."
|
||||
{ $subsection { "concurrency" "mailboxes" } }
|
||||
{ $subsection { "concurrency" "synchronous-sends" } }
|
||||
{ $subsection { "concurrency" "exceptions" } } ;
|
||||
|
||||
ABOUT: "concurrency.messaging"
|
|
@ -0,0 +1,93 @@
|
|||
! Copyright (C) 2005 Chris Double. All Rights Reserved.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
!
|
||||
USING: kernel threads vectors arrays sequences
|
||||
namespaces tools.test continuations dlists strings math words
|
||||
match quotations concurrency.messaging ;
|
||||
IN: temporary
|
||||
|
||||
[ ] [ mailbox mailbox-data dlist-delete-all ] unit-test
|
||||
|
||||
[ V{ 1 2 3 } ] [
|
||||
0 <vector>
|
||||
<mailbox>
|
||||
[ mailbox-get swap push ] in-thread
|
||||
[ mailbox-get swap push ] in-thread
|
||||
[ mailbox-get swap push ] in-thread
|
||||
1 over mailbox-put
|
||||
2 over mailbox-put
|
||||
3 swap mailbox-put
|
||||
] unit-test
|
||||
|
||||
[ V{ 1 2 3 } ] [
|
||||
0 <vector>
|
||||
<mailbox>
|
||||
[ [ integer? ] swap mailbox-get? swap push ] in-thread
|
||||
[ [ integer? ] swap mailbox-get? swap push ] in-thread
|
||||
[ [ integer? ] swap mailbox-get? swap push ] in-thread
|
||||
1 over mailbox-put
|
||||
2 over mailbox-put
|
||||
3 swap mailbox-put
|
||||
] unit-test
|
||||
|
||||
[ V{ 1 "junk" 3 "junk2" } [ 456 ] ] [
|
||||
0 <vector>
|
||||
<mailbox>
|
||||
[ [ integer? ] swap mailbox-get? swap push ] in-thread
|
||||
[ [ integer? ] swap mailbox-get? swap push ] in-thread
|
||||
[ [ string? ] swap mailbox-get? swap push ] in-thread
|
||||
[ [ string? ] swap mailbox-get? swap push ] in-thread
|
||||
1 over mailbox-put
|
||||
"junk" over mailbox-put
|
||||
[ 456 ] over mailbox-put
|
||||
3 over mailbox-put
|
||||
"junk2" over mailbox-put
|
||||
mailbox-get
|
||||
] unit-test
|
||||
|
||||
|
||||
[ "received" ] [
|
||||
[
|
||||
receive "received" swap reply-synchronous
|
||||
] "Synchronous test" spawn
|
||||
"sent" swap send-synchronous
|
||||
] unit-test
|
||||
|
||||
[ 1 3 2 ] [
|
||||
1 self send
|
||||
2 self send
|
||||
3 self send
|
||||
receive
|
||||
[ 2 mod 0 = not ] receive-if
|
||||
receive
|
||||
] unit-test
|
||||
|
||||
[
|
||||
[
|
||||
"crash" throw
|
||||
] "Linked test" spawn-linked drop
|
||||
receive
|
||||
] [ linked-error "crash" = ] must-fail-with
|
||||
|
||||
MATCH-VARS: ?from ?to ?value ;
|
||||
SYMBOL: increment
|
||||
SYMBOL: decrement
|
||||
SYMBOL: value
|
||||
SYMBOL: exit
|
||||
|
||||
: counter ( value -- value ? )
|
||||
receive {
|
||||
{ { increment ?value } [ ?value + t ] }
|
||||
{ { decrement ?value } [ ?value - t ] }
|
||||
{ { value ?from } [ dup ?from send t ] }
|
||||
{ exit [ f ] }
|
||||
} match-cond ;
|
||||
|
||||
[ -5 ] [
|
||||
[ 0 [ counter ] [ ] [ ] while ] "Counter" spawn "counter" set
|
||||
{ increment 10 } "counter" get send
|
||||
{ decrement 15 } "counter" get send
|
||||
[ value , self , ] { } make "counter" get send
|
||||
receive
|
||||
exit "counter" get send
|
||||
] unit-test
|
|
@ -0,0 +1,147 @@
|
|||
! Copyright (C) 2005, 2008 Chris Double, Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
!
|
||||
! Concurrency library for Factor based on Erlang/Termite style
|
||||
! concurrency.
|
||||
IN: concurrency.messaging
|
||||
USING: dlists threads sequences continuations
|
||||
namespaces random math quotations words kernel arrays assocs
|
||||
init system concurrency.conditions ;
|
||||
|
||||
TUPLE: mailbox threads data ;
|
||||
|
||||
: <mailbox> ( -- mailbox )
|
||||
<dlist> <dlist> \ mailbox construct-boa ;
|
||||
|
||||
: mailbox-empty? ( mailbox -- bool )
|
||||
mailbox-data dlist-empty? ;
|
||||
|
||||
: mailbox-put ( obj mailbox -- )
|
||||
[ mailbox-data push-front ] keep
|
||||
mailbox-threads notify-all ;
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: block-unless-pred ( pred mailbox timeout -- )
|
||||
2over mailbox-data dlist-contains? [
|
||||
3drop
|
||||
] [
|
||||
2dup >r mailbox-threads r> "mailbox" wait
|
||||
block-unless-pred
|
||||
] if ; inline
|
||||
|
||||
: block-if-empty ( mailbox timeout -- mailbox )
|
||||
over mailbox-empty? [
|
||||
2dup >r mailbox-threads r> "mailbox" wait
|
||||
block-if-empty
|
||||
] [
|
||||
drop
|
||||
] if ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: mailbox-peek ( mailbox -- obj )
|
||||
mailbox-data peek-back ;
|
||||
|
||||
: mailbox-get-timeout ( mailbox timeout -- obj )
|
||||
block-if-empty mailbox-data pop-back ;
|
||||
|
||||
: mailbox-get ( mailbox -- obj )
|
||||
f mailbox-get-timeout ;
|
||||
|
||||
: mailbox-get-all-timeout ( mailbox timeout -- array )
|
||||
block-if-empty
|
||||
[ dup mailbox-empty? ]
|
||||
[ dup mailbox-data pop-back ]
|
||||
[ ] unfold nip ;
|
||||
|
||||
: mailbox-get-all ( mailbox -- array )
|
||||
f mailbox-get-all-timeout ;
|
||||
|
||||
: while-mailbox-empty ( mailbox quot -- )
|
||||
over mailbox-empty? [
|
||||
dup >r swap slip r> while-mailbox-empty
|
||||
] [
|
||||
2drop
|
||||
] if ; inline
|
||||
|
||||
: mailbox-timeout-get? ( pred mailbox timeout -- obj )
|
||||
[ block-unless-pred ] 3keep drop
|
||||
mailbox-data delete-node-if ; inline
|
||||
|
||||
: mailbox-get? ( pred mailbox -- obj )
|
||||
f mailbox-timeout-get? ; inline
|
||||
|
||||
TUPLE: linked error thread ;
|
||||
|
||||
C: <linked> linked
|
||||
|
||||
GENERIC: send ( message process -- )
|
||||
|
||||
: mailbox-of ( thread -- mailbox )
|
||||
dup thread-mailbox [ ] [
|
||||
<mailbox> dup rot set-thread-mailbox
|
||||
] ?if ;
|
||||
|
||||
M: thread send ( message thread -- )
|
||||
mailbox-of mailbox-put ;
|
||||
|
||||
: ?linked dup linked? [ rethrow ] when ;
|
||||
|
||||
: mailbox self mailbox-of ;
|
||||
|
||||
: receive ( -- message )
|
||||
mailbox mailbox-get ?linked ;
|
||||
|
||||
: receive-if ( pred -- message )
|
||||
mailbox mailbox-get? ?linked ; inline
|
||||
|
||||
: rethrow-linked ( error process supervisor -- )
|
||||
>r <linked> r> send ;
|
||||
|
||||
: spawn-linked-to ( quot name mailbox -- thread )
|
||||
[ >r <linked> r> mailbox-put ] curry <thread>
|
||||
[ (spawn) ] keep ;
|
||||
|
||||
: spawn-linked ( quot name -- thread )
|
||||
mailbox spawn-linked-to ;
|
||||
|
||||
TUPLE: synchronous data sender tag ;
|
||||
|
||||
: <synchronous> ( data -- sync )
|
||||
self random-256 synchronous construct-boa ;
|
||||
|
||||
TUPLE: reply data tag ;
|
||||
|
||||
: <reply> ( data synchronous -- reply )
|
||||
synchronous-tag \ reply construct-boa ;
|
||||
|
||||
: send-synchronous ( message thread -- reply )
|
||||
>r <synchronous> dup r> send [
|
||||
over reply? [
|
||||
>r reply-tag r> synchronous-tag =
|
||||
] [
|
||||
2drop f
|
||||
] if
|
||||
] curry receive-if reply-data ;
|
||||
|
||||
: reply-synchronous ( message synchronous -- )
|
||||
[ <reply> ] keep synchronous-sender send ;
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: remote-processes ( -- hash )
|
||||
\ remote-processes get-global ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: register-process ( name process -- )
|
||||
swap remote-processes set-at ;
|
||||
|
||||
: unregister-process ( name -- )
|
||||
remote-processes delete-at ;
|
||||
|
||||
: get-process ( name -- process )
|
||||
dup remote-processes at [ ] [ thread ] ?if ;
|
||||
|
||||
\ remote-processes global [ H{ } assoc-like ] change-at
|
|
@ -0,0 +1 @@
|
|||
Erlang/Termite-style message-passing concurrency
|
|
@ -0,0 +1,2 @@
|
|||
Chris Double
|
||||
Slava Pestov
|
|
@ -0,0 +1,36 @@
|
|||
! Copyright (C) 2005, 2008 Chris Double, Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: concurrency.messaging kernel arrays
|
||||
continuations help.markup help.syntax quotations ;
|
||||
IN: concurrency.promises
|
||||
|
||||
HELP: promise
|
||||
{ $class-description "The class of write-once promises." } ;
|
||||
|
||||
HELP: promise-fulfilled?
|
||||
{ $values { "promise" promise } { "?" "a boolean" } }
|
||||
{ $description "Tests if " { $link fulfill } " has previously been called on the promise, in which case " { $link ?promise } " will return immediately without blocking." } ;
|
||||
|
||||
HELP: ?promise-timeout
|
||||
{ $values { "promise" promise } { "timeout" "a timeout in milliseconds or " { $link f } } { "value" object } }
|
||||
{ $description "Waits for another thread to fulfill a promise, returning immediately if the promise has already been fulfilled. A timeout of " { $link f } " indicates that the thread may block indefinitely, otherwise it will wait up to " { $snippet "timeout" } " milliseconds." }
|
||||
{ $errors "Throws an error if the timeout expires before the promise has been fulfilled." } ;
|
||||
|
||||
HELP: ?promise
|
||||
{ $values { "promise" promise } { "value" object } }
|
||||
{ $description "Waits for another thread to fulfill a promise, returning immediately if the promise has already been fulfilled." } ;
|
||||
|
||||
HELP: fulfill
|
||||
{ $values { "value" object } { "promise" promise } }
|
||||
{ $description "Fulfills a promise by writing a value to it. Any threads waiting for the value are notified." }
|
||||
{ $errors "Throws an error if the promise has already been fulfilled." } ;
|
||||
|
||||
ARTICLE: "concurrency.promises" "Promises"
|
||||
"The " { $vocab-link "concurrency.promises" } " vocabulary implements " { $emphasis "promises" } ", which are thread-safe write-once variables. Once a promise is created, threads may block waiting for it to be " { $emphasis "fulfilled" } "; at some point in the future, another thread may provide a value at which point all waiting threads are notified."
|
||||
{ $subsection promise }
|
||||
{ $subsection <promise> }
|
||||
{ $subsection fulfill }
|
||||
{ $subsection ?promise }
|
||||
{ $subsection ?promise-timeout } ;
|
||||
|
||||
ABOUT: "concurrency.promises"
|
|
@ -0,0 +1,12 @@
|
|||
IN: temporary
|
||||
USING: vectors concurrency.promises kernel threads sequences
|
||||
tools.test ;
|
||||
|
||||
[ V{ 50 50 50 } ] [
|
||||
0 <vector>
|
||||
<promise>
|
||||
[ ?promise swap push ] in-thread
|
||||
[ ?promise swap push ] in-thread
|
||||
[ ?promise swap push ] in-thread
|
||||
50 swap fulfill
|
||||
] unit-test
|
|
@ -0,0 +1,27 @@
|
|||
! Copyright (C) 2005, 2008 Chris Double, Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: concurrency.messaging concurrency.messaging.private
|
||||
kernel ;
|
||||
IN: concurrency.promises
|
||||
|
||||
TUPLE: promise mailbox ;
|
||||
|
||||
: <promise> ( -- promise )
|
||||
<mailbox> promise construct-boa ;
|
||||
|
||||
: promise-fulfilled? ( promise -- ? )
|
||||
promise-mailbox mailbox-empty? not ;
|
||||
|
||||
: fulfill ( value promise -- )
|
||||
dup promise-fulfilled? [
|
||||
"Promise already fulfilled" throw
|
||||
] [
|
||||
promise-mailbox mailbox-put
|
||||
] if ;
|
||||
|
||||
: ?promise-timeout ( promise timeout -- result )
|
||||
>r promise-mailbox r> block-if-empty
|
||||
mailbox-peek ?linked ;
|
||||
|
||||
: ?promise ( promise -- result )
|
||||
f ?promise-timeout ;
|
|
@ -0,0 +1 @@
|
|||
Thread-safe write-once variables
|
|
@ -0,0 +1 @@
|
|||
Slava Pestov
|
|
@ -0,0 +1,45 @@
|
|||
IN: concurrency.semaphores
|
||||
USING: help.markup help.syntax kernel quotations ;
|
||||
|
||||
HELP: semaphore
|
||||
{ $class-description "The class of counting semaphores." } ;
|
||||
|
||||
HELP: <semaphore>
|
||||
{ $values { "n" "a non-negative integer" } { "semaphore" semaphore } }
|
||||
{ $description "Creates a counting semaphore with the specified initial count." } ;
|
||||
|
||||
HELP: acquire
|
||||
{ $values { "semaphore" semaphore } { "timeout" "a timeout in milliseconds or " { $link f } } { "value" object } }
|
||||
{ $description "If the semaphore has a non-zero count, decrements it and returns immediately. Otherwise, if the timeout is " { $link f } ", waits indefinitely for the semaphore to be released. If the timeout is not " { $link f } ", waits up to that number of milliseconds for the semaphore to be released." } ;
|
||||
|
||||
HELP: release
|
||||
{ $values { "semaphore" semaphore } }
|
||||
{ $description "Increments a semaphore's count. If the count was previously zero, any threads waiting on the semaphore are woken up." } ;
|
||||
|
||||
HELP: with-semaphore
|
||||
{ $values { "semaphore" semaphore } { "quot" quotation } }
|
||||
{ $description "Calls the quotation with the semaphore held." } ;
|
||||
|
||||
ARTICLE: "concurrency.semaphores" "Counting semaphores"
|
||||
"Counting semaphores are used to ensure that no more than a fixed number of threads are executing in a critical section at a time; as such, they generalize " { $link "concurrency.locks.mutex" } ", since locks can be thought of as semaphores with an initial count of 1."
|
||||
$nl
|
||||
"A use-case would be a batch processing server which runs a large number of jobs which perform calculations but then need to fire off expensive external processes or perform heavy network I/O. While for most of the time, the threads can all run in parallel, it might be desired that the expensive operation is not run by more than 10 threads at once, to avoid thrashing swap space or saturating the network. This can be accomplished with a counting semaphore:"
|
||||
{ $code
|
||||
"SYMBOL: expensive-section"
|
||||
"10 <semaphore> expensive-section set-global"
|
||||
"requests ["
|
||||
" ..."
|
||||
" expensive-section [ do-expensive-stuff ] with-semaphore"
|
||||
" ..."
|
||||
"] parallel-map"
|
||||
}
|
||||
"Creating semaphores:"
|
||||
{ $subsection semaphore }
|
||||
{ $subsection <semaphore> }
|
||||
"Unlike locks, where acquisition and release are always paired by a combinator, semaphores expose these operations directly and there is no requirement that they be performed in the same thread:"
|
||||
{ $subsection acquire }
|
||||
{ $subsection release }
|
||||
"A combinator which pairs acquisition and release:"
|
||||
{ $subsection with-semaphore } ;
|
||||
|
||||
ABOUT: "concurrency.semaphores"
|
|
@ -0,0 +1,29 @@
|
|||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: dlists kernel threads math concurrency.conditions
|
||||
continuations ;
|
||||
IN: concurrency.semaphores
|
||||
|
||||
TUPLE: semaphore count threads ;
|
||||
|
||||
: <semaphore> ( n -- semaphore )
|
||||
dup 0 < [ "Cannot have semaphore with negative count" throw ] when
|
||||
<dlist> semaphore construct-boa ;
|
||||
|
||||
: wait-to-acquire ( semaphore timeout -- )
|
||||
>r semaphore-threads r> "semaphore" wait ;
|
||||
|
||||
: acquire ( semaphore timeout -- )
|
||||
dup semaphore-count zero? [
|
||||
wait-to-acquire
|
||||
] [
|
||||
drop
|
||||
dup semaphore-count 1- swap set-semaphore-count
|
||||
] if ;
|
||||
|
||||
: release ( semaphore -- )
|
||||
dup semaphore-count 1+ over set-semaphore-count
|
||||
semaphore-threads notify-1 ;
|
||||
|
||||
: with-semaphore ( semaphore quot -- )
|
||||
over acquire [ release ] curry [ ] cleanup ; inline
|
|
@ -0,0 +1 @@
|
|||
Counting semaphores
|
|
@ -1 +0,0 @@
|
|||
Erlang-style concurrency
|
|
@ -1 +0,0 @@
|
|||
extensions
|
|
@ -1,5 +1,5 @@
|
|||
USING: kernel math math-contrib sequences namespaces errors
|
||||
hashtables words arrays parser compiler syntax io threads ;
|
||||
hashtables words arrays parser compiler syntax io ;
|
||||
IN: crypto
|
||||
: make-bits ( quot numbits -- n | quot: -- 0/1 )
|
||||
0 -rot [ drop dup call rot 1 shift bitor swap ] each drop ;
|
||||
|
|
|
@ -78,8 +78,35 @@ ARTICLE: "dataflow" "Data and control flow"
|
|||
{ $subsection "conditionals" }
|
||||
{ $subsection "basic-combinators" }
|
||||
{ $subsection "combinators" }
|
||||
{ $subsection "continuations" }
|
||||
{ $subsection "threads" } ;
|
||||
{ $subsection "continuations" } ;
|
||||
|
||||
USING: concurrency.combinators
|
||||
concurrency.messaging
|
||||
concurrency.promises
|
||||
concurrency.futures
|
||||
concurrency.locks
|
||||
concurrency.semaphores
|
||||
concurrency.count-downs
|
||||
concurrency.exchangers ;
|
||||
|
||||
ARTICLE: "concurrency" "Concurrency"
|
||||
"Factor supports a variety of concurrency abstractions, however they are mostly used to multiplex input/output operations since the thread scheduling is co-operative and only one CPU is used at a time."
|
||||
$nl
|
||||
"Factor's concurrency support was insipired by Erlang, Termite, Scheme48 and Java's " { $snippet "java.util.concurrent" } " library."
|
||||
$nl
|
||||
"The basic building blocks:"
|
||||
{ $subsection "threads" }
|
||||
"High-level abstractions:"
|
||||
{ $subsection "concurrency.combinators" }
|
||||
{ $subsection "concurrency.promises" }
|
||||
{ $subsection "concurrency.futures" }
|
||||
{ $subsection "concurrency.messaging" }
|
||||
"Shared-state abstractions:"
|
||||
{ $subsection "concurrency.locks" }
|
||||
{ $subsection "concurrency.semaphores" }
|
||||
{ $subsection "concurrency.count-downs" }
|
||||
{ $subsection "concurrency.exchangers" }
|
||||
"Other concurrency abstractions include " { $vocab-link "concurrency.distributed" } " and " { $vocab-link "channels" } "." ;
|
||||
|
||||
ARTICLE: "objects" "Objects"
|
||||
"An " { $emphasis "object" } " is any datum which may be identified. All values are objects in Factor. Each object carries type information, and types are checked at runtime; Factor is dynamically typed."
|
||||
|
@ -134,6 +161,7 @@ ARTICLE: "collections" "Collections"
|
|||
{ $subsection "hashtables" }
|
||||
{ $subsection "alists" }
|
||||
{ $heading "Other collections" }
|
||||
{ $subsection "boxes" }
|
||||
{ $subsection "dlists" }
|
||||
{ $subsection "heaps" }
|
||||
{ $subsection "graphs" }
|
||||
|
@ -216,6 +244,7 @@ ARTICLE: "handbook" "Factor documentation"
|
|||
{ $subsection "numbers" }
|
||||
{ $subsection "collections" }
|
||||
{ $subsection "io" }
|
||||
{ $subsection "concurrency" }
|
||||
{ $subsection "os" }
|
||||
{ $subsection "alien" }
|
||||
{ $heading "Environment reference" }
|
||||
|
|
|
@ -83,7 +83,10 @@ HOOK: run-process* io-backend ( desc -- handle )
|
|||
: wait-for-process ( process -- status )
|
||||
[
|
||||
dup process-handle
|
||||
[ dup [ processes get at push stop ] curry callcc0 ] when
|
||||
[
|
||||
dup [ processes get at push ] curry
|
||||
"process" suspend drop
|
||||
] when
|
||||
dup process-killed?
|
||||
[ "Process was killed" throw ] [ process-status ] if
|
||||
] with-timeout ;
|
||||
|
@ -134,5 +137,5 @@ TUPLE: process-stream process ;
|
|||
|
||||
: notify-exit ( status process -- )
|
||||
[ set-process-status ] keep
|
||||
[ processes get delete-at* drop [ schedule-thread ] each ] keep
|
||||
[ processes get delete-at* drop [ resume ] each ] keep
|
||||
f swap set-process-handle ;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: io.backend kernel continuations namespaces sequences
|
||||
assocs hashtables sorting arrays threads ;
|
||||
assocs hashtables sorting arrays threads boxes ;
|
||||
IN: io.monitors
|
||||
|
||||
<PRIVATE
|
||||
|
@ -35,24 +35,21 @@ M: monitor dispose
|
|||
TUPLE: simple-monitor handle callback ;
|
||||
|
||||
: <simple-monitor> ( handle -- simple-monitor )
|
||||
f (monitor) {
|
||||
f (monitor) <box> {
|
||||
set-simple-monitor-handle
|
||||
set-delegate
|
||||
set-simple-monitor-callback
|
||||
} simple-monitor construct ;
|
||||
|
||||
: construct-simple-monitor ( handle class -- simple-monitor )
|
||||
>r <simple-monitor> r> construct-delegate ; inline
|
||||
|
||||
: notify-callback ( simple-monitor -- )
|
||||
dup simple-monitor-callback
|
||||
f rot set-simple-monitor-callback
|
||||
[ schedule-thread ] when* ;
|
||||
simple-monitor-callback ?box [ resume ] [ drop ] if ;
|
||||
|
||||
M: simple-monitor fill-queue ( monitor -- )
|
||||
dup simple-monitor-callback [
|
||||
"Cannot wait for changes on the same file from multiple threads" throw
|
||||
] when
|
||||
[ swap set-simple-monitor-callback stop ] callcc0
|
||||
[ swap simple-monitor-callback >box ]
|
||||
"monitor" suspend drop
|
||||
check-monitor ;
|
||||
|
||||
M: simple-monitor dispose ( monitor -- )
|
||||
|
|
|
@ -2,11 +2,18 @@
|
|||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: io io.sockets io.files logging continuations kernel
|
||||
math math.parser namespaces parser sequences strings
|
||||
prettyprint debugger quotations calendar qualified ;
|
||||
QUALIFIED: concurrency
|
||||
|
||||
prettyprint debugger quotations calendar
|
||||
threads concurrency.combinators assocs ;
|
||||
IN: io.server
|
||||
|
||||
SYMBOL: servers
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: spawn-vars ( quot vars name -- )
|
||||
>r [ dup get ] H{ } map>assoc [ swap bind ] 2curry r>
|
||||
spawn drop ;
|
||||
|
||||
LOG: accepted-connection NOTICE
|
||||
|
||||
: with-client ( client quot -- )
|
||||
|
@ -15,23 +22,21 @@ LOG: accepted-connection NOTICE
|
|||
with-stream*
|
||||
] curry with-disposal ; inline
|
||||
|
||||
\ with-client NOTICE add-error-logging
|
||||
\ with-client DEBUG add-error-logging
|
||||
|
||||
: accept-loop ( server quot -- )
|
||||
[
|
||||
>r accept r> [ with-client ] 2curry
|
||||
concurrency:spawn drop
|
||||
{ log-service servers } "Client" spawn-vars
|
||||
] 2keep accept-loop ; inline
|
||||
|
||||
: server-loop ( server quot -- )
|
||||
: server-loop ( addrspec quot -- )
|
||||
>r <server> dup servers get push r>
|
||||
[ accept-loop ] curry with-disposal ; inline
|
||||
|
||||
SYMBOL: servers
|
||||
\ server-loop NOTICE add-error-logging
|
||||
|
||||
: spawn-server ( addrspec quot -- )
|
||||
>r <server> dup servers get push r> server-loop ; inline
|
||||
|
||||
\ spawn-server NOTICE add-error-logging
|
||||
PRIVATE>
|
||||
|
||||
: local-server ( port -- seq )
|
||||
"localhost" swap t resolve-host ;
|
||||
|
@ -40,17 +45,18 @@ SYMBOL: servers
|
|||
f swap t resolve-host ;
|
||||
|
||||
: with-server ( seq service quot -- )
|
||||
[
|
||||
V{ } clone servers set
|
||||
[ spawn-server ] curry concurrency:parallel-each
|
||||
] curry with-logging ; inline
|
||||
V{ } clone [
|
||||
servers [
|
||||
[ server-loop ] curry with-logging
|
||||
] with-variable
|
||||
] 3curry parallel-each ; inline
|
||||
|
||||
: stop-server ( -- )
|
||||
servers get [ dispose ] each ;
|
||||
|
||||
: received-datagram ( addrspec -- ) drop ;
|
||||
<PRIVATE
|
||||
|
||||
\ received-datagram NOTICE add-input-logging
|
||||
LOG: received-datagram NOTICE
|
||||
|
||||
: datagram-loop ( quot datagram -- )
|
||||
[
|
||||
|
@ -63,7 +69,9 @@ SYMBOL: servers
|
|||
|
||||
\ spawn-datagrams NOTICE add-input-logging
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: with-datagrams ( seq service quot -- )
|
||||
[
|
||||
[ swap spawn-datagrams ] curry concurrency:parallel-each
|
||||
[ swap spawn-datagrams ] curry parallel-each
|
||||
] curry with-logging ; inline
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
! Copyright (C) 2008 Slava Pestov, Doug Coleman
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel math system dlists namespaces assocs init threads
|
||||
io.streams.duplex ;
|
||||
USING: kernel math system dlists namespaces assocs init
|
||||
threads io.streams.duplex ;
|
||||
IN: io.timeouts
|
||||
|
||||
TUPLE: lapse entry timeout cutoff ;
|
||||
|
@ -73,4 +73,7 @@ M: object timed-out drop ;
|
|||
: expiry-thread ( -- )
|
||||
expire-timeouts 5000 sleep expiry-thread ;
|
||||
|
||||
[ [ expiry-thread ] in-thread ] "io.timeouts" add-init-hook
|
||||
: start-expiry-thread ( -- )
|
||||
[ expiry-thread ] "I/O expiry" spawn drop ;
|
||||
|
||||
[ start-expiry-thread ] "io.timeouts" add-init-hook
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
! Copyright (C) 2004, 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: alien generic assocs kernel kernel.private math
|
||||
io.nonblocking sequences strings structs sbufs threads unix
|
||||
vectors io.buffers io.backend io.streams.duplex math.parser
|
||||
continuations system libc qualified namespaces io.timeouts ;
|
||||
io.nonblocking sequences strings structs sbufs
|
||||
threads unix vectors io.buffers io.backend
|
||||
io.streams.duplex math.parser continuations system libc
|
||||
qualified namespaces io.timeouts ;
|
||||
QUALIFIED: io
|
||||
IN: io.unix.backend
|
||||
|
||||
|
@ -58,10 +59,10 @@ M: mx register-io-task ( task mx -- )
|
|||
2dup check-io-task fd/container set-at ;
|
||||
|
||||
: add-io-task ( task -- )
|
||||
mx get-global register-io-task stop ;
|
||||
mx get-global register-io-task ;
|
||||
|
||||
: with-port-continuation ( port quot -- port )
|
||||
[ callcc0 ] curry with-timeout ; inline
|
||||
[ "I/O" suspend drop ] curry with-timeout ; inline
|
||||
|
||||
M: mx unregister-io-task ( task mx -- )
|
||||
fd/container delete-at drop ;
|
||||
|
@ -99,7 +100,7 @@ M: integer close-handle ( fd -- )
|
|||
|
||||
: pop-callbacks ( mx task -- )
|
||||
dup rot unregister-io-task
|
||||
io-task-callbacks [ schedule-thread ] each ;
|
||||
io-task-callbacks [ resume ] each ;
|
||||
|
||||
: handle-io-task ( mx task -- )
|
||||
dup do-io-task [ pop-callbacks ] [ 2drop ] if ;
|
||||
|
@ -168,7 +169,7 @@ M: write-task do-io-task
|
|||
|
||||
: add-write-io-task ( port continuation -- )
|
||||
over port-handle mx get-global mx-writes at*
|
||||
[ io-task-callbacks push stop ]
|
||||
[ io-task-callbacks push ]
|
||||
[ drop <write-task> add-io-task ] if ;
|
||||
|
||||
: (wait-to-write) ( port -- )
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
! See http://factorcode.org/license.txt for BSD license.
|
||||
IN: io.unix.bsd
|
||||
USING: io.backend io.unix.backend io.unix.kqueue io.unix.select
|
||||
io.launcher io.unix.launcher namespaces kernel assocs threads
|
||||
continuations ;
|
||||
io.launcher io.unix.launcher namespaces kernel assocs
|
||||
threads continuations ;
|
||||
|
||||
! On Mac OS X, we use select() for the top-level
|
||||
! multiplexer, and we hang a kqueue off of it for process exit
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: alien.c-types kernel io.nonblocking io.unix.backend
|
||||
sequences assocs unix unix.kqueue unix.process math namespaces
|
||||
combinators threads vectors io.launcher io.unix.launcher ;
|
||||
combinators threads vectors io.launcher
|
||||
io.unix.launcher ;
|
||||
IN: io.unix.kqueue
|
||||
|
||||
TUPLE: kqueue-mx events ;
|
||||
|
|
|
@ -120,8 +120,6 @@ M: unix-io process-stream*
|
|||
] if
|
||||
] if ;
|
||||
|
||||
: wait-loop ( -- )
|
||||
wait-for-processes [ 250 sleep ] when wait-loop ;
|
||||
|
||||
: start-wait-thread ( -- )
|
||||
[ wait-loop ] in-thread ;
|
||||
[ wait-for-processes [ 250 sleep ] when t ]
|
||||
"Process reaper" spawn-server drop ;
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
USING: kernel io.backend io.monitors io.monitors.private
|
||||
io.files io.buffers io.nonblocking io.timeouts io.unix.backend
|
||||
io.unix.select io.unix.launcher unix.linux.inotify assocs
|
||||
namespaces threads continuations init math alien.c-types alien
|
||||
vocabs.loader ;
|
||||
namespaces threads continuations init math
|
||||
alien.c-types alien vocabs.loader ;
|
||||
IN: io.unix.linux
|
||||
|
||||
TUPLE: linux-io ;
|
||||
|
|
|
@ -4,11 +4,11 @@
|
|||
! We need to fiddle with the exact search order here, since
|
||||
! unix::accept shadows streams::accept.
|
||||
IN: io.unix.sockets
|
||||
USING: alien alien.c-types generic io
|
||||
kernel math namespaces io.nonblocking parser threads unix
|
||||
sequences byte-arrays io.sockets io.binary io.unix.backend
|
||||
io.streams.duplex io.sockets.impl math.parser continuations
|
||||
libc combinators ;
|
||||
USING: alien alien.c-types generic io kernel math namespaces
|
||||
io.nonblocking parser threads unix sequences
|
||||
byte-arrays io.sockets io.binary io.unix.backend
|
||||
io.streams.duplex io.sockets.impl math.parser continuations libc
|
||||
combinators ;
|
||||
|
||||
: pending-init-error ( port -- )
|
||||
#! We close it here to avoid a resource leak; callers of
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
USING: io.files io.sockets io kernel threads namespaces
|
||||
tools.test continuations strings byte-arrays sequences
|
||||
prettyprint system ;
|
||||
USING: io.files io.sockets io kernel threads
|
||||
namespaces tools.test continuations strings byte-arrays
|
||||
sequences prettyprint system ;
|
||||
IN: temporary
|
||||
|
||||
! Unix domain stream sockets
|
||||
|
@ -18,7 +18,7 @@ IN: temporary
|
|||
] with-stream
|
||||
|
||||
"unix-domain-socket-test" resource-path delete-file
|
||||
] in-thread
|
||||
] "Test" spawn drop
|
||||
|
||||
yield
|
||||
|
||||
|
@ -69,7 +69,7 @@ yield
|
|||
|
||||
"unix-domain-datagram-test" resource-path delete-file
|
||||
] with-scope
|
||||
] in-thread
|
||||
] "Test" spawn drop
|
||||
|
||||
yield
|
||||
|
||||
|
|
|
@ -4,7 +4,8 @@ USING: alien alien.c-types arrays continuations destructors io
|
|||
io.windows io.windows.nt.pipes libc io.nonblocking
|
||||
io.streams.duplex windows.types math windows.kernel32 windows
|
||||
namespaces io.launcher kernel sequences windows.errors assocs
|
||||
splitting system threads init strings combinators io.backend ;
|
||||
splitting system threads init strings combinators
|
||||
io.backend ;
|
||||
IN: io.windows.launcher
|
||||
|
||||
TUPLE: CreateProcess-args
|
||||
|
@ -146,10 +147,9 @@ M: windows-io kill-process* ( handle -- )
|
|||
: wait-loop ( -- )
|
||||
processes get dup assoc-empty?
|
||||
[ drop t ] [ wait-for-processes ] if
|
||||
[ 250 sleep ] when
|
||||
wait-loop ;
|
||||
[ 250 sleep ] when ;
|
||||
|
||||
: start-wait-thread ( -- )
|
||||
[ wait-loop ] in-thread ;
|
||||
[ wait-loop t ] "Process wait" spawn-server drop ;
|
||||
|
||||
[ start-wait-thread ] "io.windows.launcher" add-init-hook
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
USING: alien alien.c-types arrays assocs combinators
|
||||
continuations destructors io io.backend io.nonblocking
|
||||
io.windows libc kernel math namespaces sequences
|
||||
threads tuples.lib windows windows.errors windows.kernel32
|
||||
strings splitting io.files qualified ascii combinators.lib ;
|
||||
threads tuples.lib windows windows.errors
|
||||
windows.kernel32 strings splitting io.files qualified ascii
|
||||
combinators.lib ;
|
||||
QUALIFIED: windows.winsock
|
||||
IN: io.windows.nt.backend
|
||||
|
||||
SYMBOL: io-hash
|
||||
|
||||
TUPLE: io-callback port continuation ;
|
||||
TUPLE: io-callback port thread ;
|
||||
|
||||
C: <io-callback> io-callback
|
||||
|
||||
|
@ -52,8 +53,8 @@ M: windows-nt-io add-completion ( handle -- )
|
|||
[
|
||||
<io-callback> swap
|
||||
dup alien? [ "bad overlapped in save-callback" throw ] unless
|
||||
io-hash get-global set-at stop
|
||||
] callcc0 2drop ;
|
||||
io-hash get-global set-at
|
||||
] "I/O" suspend 3drop ;
|
||||
|
||||
: wait-for-overlapped ( ms -- overlapped ? )
|
||||
>r master-completion-port get-global r> ! port ms
|
||||
|
@ -77,11 +78,11 @@ M: windows-nt-io add-completion ( handle -- )
|
|||
] [
|
||||
(win32-error-string) swap lookup-callback
|
||||
[ io-callback-port set-port-error ] keep
|
||||
] if io-callback-continuation schedule-thread f
|
||||
] if io-callback-thread resume f
|
||||
] if
|
||||
] [
|
||||
lookup-callback
|
||||
io-callback-continuation schedule-thread f
|
||||
io-callback-thread resume f
|
||||
] if ;
|
||||
|
||||
: drain-overlapped ( timeout -- )
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
USING: continuations destructors io.buffers io.files io.backend
|
||||
io.timeouts io.nonblocking io.windows io.windows.nt.backend
|
||||
kernel libc math threads windows windows.kernel32 alien.c-types
|
||||
alien.arrays sequences combinators combinators.lib sequences.lib
|
||||
ascii splitting alien strings assocs ;
|
||||
kernel libc math threads windows windows.kernel32
|
||||
alien.c-types alien.arrays sequences combinators combinators.lib
|
||||
sequences.lib ascii splitting alien strings assocs ;
|
||||
IN: io.windows.nt.files
|
||||
|
||||
M: windows-nt-io cwd
|
||||
|
|
|
@ -12,3 +12,5 @@ USE: io.windows.mmap
|
|||
USE: io.backend
|
||||
|
||||
T{ windows-nt-io } set-io-backend
|
||||
|
||||
"vocabs.monitor" require
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
! Copyright (C) 2003, 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: logging.server sequences namespaces concurrency
|
||||
USING: logging.server sequences namespaces concurrency.messaging
|
||||
words kernel arrays shuffle tools.annotations
|
||||
prettyprint.config prettyprint debugger io.streams.string
|
||||
splitting continuations effects arrays.lib parser strings
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: namespaces kernel io calendar sequences io.files
|
||||
io.sockets continuations prettyprint assocs math.parser
|
||||
words debugger math combinators concurrency arrays init
|
||||
math.ranges strings ;
|
||||
words debugger math combinators concurrency.messaging
|
||||
threads arrays init math.ranges strings ;
|
||||
IN: logging.server
|
||||
|
||||
: log-root ( -- string )
|
||||
|
@ -85,17 +85,16 @@ SYMBOL: log-files
|
|||
log-root directory [ drop rotate-log ] assoc-each ;
|
||||
|
||||
: log-server-loop ( -- )
|
||||
[
|
||||
receive unclip {
|
||||
{ "log-message" [ (log-message) ] }
|
||||
{ "rotate-logs" [ drop (rotate-logs) ] }
|
||||
{ "close-logs" [ drop (close-logs) ] }
|
||||
} case
|
||||
] [ error. (close-logs) ] recover
|
||||
log-server-loop ;
|
||||
receive unclip {
|
||||
{ "log-message" [ (log-message) ] }
|
||||
{ "rotate-logs" [ drop (rotate-logs) ] }
|
||||
{ "close-logs" [ drop (close-logs) ] }
|
||||
} case log-server-loop ;
|
||||
|
||||
: log-server ( -- )
|
||||
[ log-server-loop ] spawn "log-server" set-global ;
|
||||
[ [ log-server-loop ] [ error. (close-logs) ] recover t ]
|
||||
"Log server" spawn-server
|
||||
"log-server" set-global ;
|
||||
|
||||
[
|
||||
H{ } clone log-files set-global
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
USING: alien alien.c-types arrays assocs byte-arrays inference
|
||||
inference.transforms io io.binary io.streams.string kernel
|
||||
math math.parser namespaces parser prettyprint
|
||||
quotations sequences strings threads vectors
|
||||
quotations sequences strings vectors
|
||||
words macros math.functions ;
|
||||
IN: pack
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ SYMBOL: data-mode
|
|||
data-mode off
|
||||
"220 OK\r\n" write flush t
|
||||
] }
|
||||
{ [ data-mode get ] [ global [ print ] bind t ] }
|
||||
{ [ data-mode get ] [ dup global [ print ] bind t ] }
|
||||
{ [ t ] [
|
||||
"500 ERROR\r\n" write flush t
|
||||
] }
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
!
|
||||
USING: cpu.8080 cpu.8080.emulator openal math alien.c-types
|
||||
sequences kernel shuffle arrays io.files combinators ui.gestures
|
||||
ui.gadgets ui.render opengl.gl system threads concurrency match
|
||||
ui.gadgets ui.render opengl.gl system threads match
|
||||
ui byte-arrays combinators.lib ;
|
||||
IN: space-invaders
|
||||
|
||||
|
@ -353,9 +353,10 @@ M: space-invaders update-video ( value addr cpu -- )
|
|||
] if ;
|
||||
|
||||
M: invaders-gadget graft* ( gadget -- )
|
||||
dup invaders-gadget-cpu init-sounds
|
||||
[ f swap set-invaders-gadget-quit? ] keep
|
||||
[ millis swap invaders-process ] spawn 2drop ;
|
||||
dup invaders-gadget-cpu init-sounds
|
||||
f over set-invaders-gadget-quit?
|
||||
[ millis swap invaders-process ] curry
|
||||
"Space invaders" spawn drop ;
|
||||
|
||||
M: invaders-gadget ungraft* ( gadget -- )
|
||||
t swap set-invaders-gadget-quit? ;
|
||||
|
|
|
@ -107,6 +107,7 @@ MEMO: all-vocabs-seq ( -- seq )
|
|||
{ [ "ui.windows" ?head ] [ t ] }
|
||||
{ [ "ui.cocoa" ?head ] [ t ] }
|
||||
{ [ "cocoa" ?head ] [ t ] }
|
||||
{ [ "core-foundation" ?head ] [ t ] }
|
||||
{ [ "vocabs.loader.test" ?head ] [ t ] }
|
||||
{ [ "editors." ?head ] [ t ] }
|
||||
{ [ ".windows" ?tail ] [ t ] }
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue