24 lines
644 B
Factor
Executable File
24 lines
644 B
Factor
Executable File
! Copyright (C) 2008 Slava Pestov.
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
USING: dlists kernel threads math ;
|
|
IN: concurrency.semaphores
|
|
|
|
TUPLE: semaphore count threads ;
|
|
|
|
: <semaphore> ( -- semaphore )
|
|
0 <dlist> semaphore construct-boa ;
|
|
|
|
: wait-to-acquire ( semaphore -- )
|
|
[ semaphore-threads push-front ] suspend drop ;
|
|
|
|
: acquire ( semaphore -- )
|
|
dup semaphore-count zero? [
|
|
wait-to-acquire
|
|
] [
|
|
dup semaphore-count 1- swap set-semaphore-count
|
|
] if ;
|
|
|
|
: release ( semaphore -- )
|
|
dup semaphore-count 1+ over set-semaphore-count
|
|
semaphore-threads notify-1 ;
|