
Next: Ports
Up: Programming Constructs
Previous: Utilities
A semaphore is a value that is used to synchronize MzScheme
threads. Each semaphore has an internal counter; when this counter is
zero, the semaphore can block a thread's execution (through
semaphore-wait) until another thread increments the counter
(using semaphore-post).
- (make-semaphore i)
creates and returns a new semaphore with the counter initially set to
i. The i argument must be a fixnum integer, but it is
optional; i defaults to 0.
- (semaphore? v) returns #t
if v is a semaphore created by make-semaphore or #f
otherwise.
- (semaphore-post sema)
increments the semaphore's internal counter and returns
#<void>.
- (semaphore-wait sema)
blocks until the internal counter for semaphore sema is
non-zero. When the counter is non-zero, it is decremented and
semaphore-wait returns #<void>.
- (semaphore-try-wait? sema)
is like semaphore-wait,
but semaphore-try-wait? never blocks execution. If sema's
internal counter is zero, semaphore-try-wait? returns #f
immediately without decrementing the counter. If sema's
counter is positive, it is decremented and #t is returned.
- (semaphore-callback sema f)
can only be used within applications that
have a top-level event loop instead of a simple read-eval-print loop. (MrEd is such
an application.) The semaphore-callback
procedure installs the thunk f to be invoked when
sema's counter is positive, decrementing the counter as
f is invoked. The f procedure is invoked in the thread
that handles events -- usually the main thread -- in a
single-threaded manner. The procedure is invoked only once and then
the callback is removed. The return value of semaphore-callback
is #<void>.
- (input-port-post-semaphore port semaphore)
installs a
poll on port that increments sema's counter when a
character is ready to be read from port. The semaphore is only
incremented once and then the poll is removed. The port is polled
by periodically evaluating
(char-ready? port)
. Since it is possible to read characters from port
without incurring a post to sema, the
input-port-post-semaphore procedure is most useful when
port is read only after a hit on sema. A call to
input-port-post-semaphore returns immediately with
#<void>.

Next: Ports
Up: Programming Constructs
Previous: Utilities
PLT