/basis/concurrency/semaphores/semaphores.factor
http://github.com/abeaumont/factor · Factor · 38 lines · 26 code · 10 blank · 2 comment · 2 complexity · df5cab93c21f634c2294554fbbabf9eb MD5 · raw file
- ! Copyright (C) 2008 Slava Pestov.
- ! See http://factorcode.org/license.txt for BSD license.
- USING: dlists kernel threads math concurrency.conditions
- continuations accessors summary locals fry ;
- IN: concurrency.semaphores
-
- TUPLE: semaphore count threads ;
-
- ERROR: negative-count-semaphore ;
-
- M: negative-count-semaphore summary
- drop "Cannot have semaphore with negative count" ;
-
- : <semaphore> ( n -- semaphore )
- dup 0 < [ negative-count-semaphore ] when
- <dlist> semaphore boa ;
-
- : wait-to-acquire ( semaphore timeout -- )
- [ threads>> ] dip "semaphore" wait ;
-
- : acquire-timeout ( semaphore timeout -- )
- over count>> zero?
- [ dupd wait-to-acquire ] [ drop ] if
- [ 1 - ] change-count drop ;
-
- : acquire ( semaphore -- )
- f acquire-timeout ;
-
- : release ( semaphore -- )
- [ 1 + ] change-count
- threads>> notify-1 ;
-
- :: with-semaphore-timeout ( semaphore timeout quot -- )
- semaphore timeout acquire-timeout
- quot [ semaphore release ] [ ] cleanup ; inline
-
- : with-semaphore ( semaphore quot -- )
- swap dup acquire '[ _ release ] [ ] cleanup ; inline