diff --git a/extra/concurrency/flags/flags-tests.factor b/extra/concurrency/flags/flags-tests.factor index f23ea95167..d13c474554 100755 --- a/extra/concurrency/flags/flags-tests.factor +++ b/extra/concurrency/flags/flags-tests.factor @@ -1,5 +1,6 @@ IN: concurrency.flags.tests -USING: tools.test concurrency.flags kernel threads locals ; +USING: tools.test concurrency.flags concurrency.combinators +kernel threads locals ; :: flag-test-1 ( -- ) [let | f [ ] | @@ -44,3 +45,9 @@ USING: tools.test concurrency.flags kernel threads locals ; ] ; [ t ] [ flag-test-5 ] unit-test + +[ ] [ + { 1 2 } + [ [ 1000 sleep raise-flag ] curry "Flag test" spawn drop ] + [ [ wait-for-flag drop ] curry parallel-each ] bi +] unit-test diff --git a/extra/concurrency/flags/flags.factor b/extra/concurrency/flags/flags.factor index b3c76a7a01..ec260961d0 100755 --- a/extra/concurrency/flags/flags.factor +++ b/extra/concurrency/flags/flags.factor @@ -1,22 +1,20 @@ ! Copyright (C) 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: boxes kernel threads ; +USING: dlists kernel threads concurrency.conditions accessors ; IN: concurrency.flags -TUPLE: flag value? thread ; +TUPLE: flag value threads ; -: ( -- flag ) f flag boa ; +: ( -- flag ) f flag boa ; : raise-flag ( flag -- ) - dup flag-value? [ - t over set-flag-value? - dup flag-thread [ resume ] if-box? - ] unless drop ; + dup value>> [ drop ] [ t >>value threads>> notify-all ] if ; + +: wait-for-flag-timeout ( flag timeout -- ) + over value>> [ 2drop ] [ >r threads>> r> "flag" wait ] if ; : wait-for-flag ( flag -- ) - dup flag-value? [ drop ] [ - [ flag-thread >box ] curry "flag" suspend drop - ] if ; + f wait-for-flag-timeout ; : lower-flag ( flag -- ) - dup wait-for-flag f swap set-flag-value? ; + [ wait-for-flag ] [ f >>value drop ] bi ;