Multiple threads can now wait on one flag simultaneously
parent
2f44e86ee1
commit
8065f8834e
|
@ -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 [ <flag> ] |
|
||||
|
@ -44,3 +45,9 @@ USING: tools.test concurrency.flags kernel threads locals ;
|
|||
] ;
|
||||
|
||||
[ t ] [ flag-test-5 ] unit-test
|
||||
|
||||
[ ] [
|
||||
{ 1 2 } <flag>
|
||||
[ [ 1000 sleep raise-flag ] curry "Flag test" spawn drop ]
|
||||
[ [ wait-for-flag drop ] curry parallel-each ] bi
|
||||
] unit-test
|
||||
|
|
|
@ -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> ( -- flag ) f <box> flag boa ;
|
||||
: <flag> ( -- flag ) f <dlist> 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 ;
|
||||
|
|
Loading…
Reference in New Issue