1Semaphore(3)          User Contributed Perl Documentation         Semaphore(3)
2
3
4

NAME

6       Coro::Semaphore - counting semaphores
7

SYNOPSIS

9        use Coro;
10
11        $sig = new Coro::Semaphore [initial value];
12
13        $sig->down; # wait for signal
14
15        # ... some other "thread"
16
17        $sig->up;
18

DESCRIPTION

20       This module implements counting semaphores. You can initialize a mutex
21       with any level of parallel users, that is, you can initialize a
22       sempahore that can be "down"ed more than once until it blocks. There is
23       no owner associated with semaphores, so one thread can "down" it while
24       another can "up" it (or vice versa), "up" can be called before "down"
25       and so on: the semaphore is really just an integer counter that
26       optionally blocks when it is 0.
27
28       Counting semaphores are typically used to coordinate access to
29       resources, with the semaphore count initialized to the number of free
30       resources. Threads then increment the count when resources are added
31       and decrement the count when resources are removed.
32
33       You don't have to load "Coro::Semaphore" manually, it will be loaded
34       automatically when you "use Coro" and call the "new" constructor.
35
36       new [initial count]
37           Creates a new sempahore object with the given initial lock count.
38           The default lock count is 1, which means it is unlocked by default.
39           Zero (or negative values) are also allowed, in which case the
40           semaphore is locked by default.
41
42       $sem->count
43           Returns the current semaphore count. The semaphore can be down'ed
44           without blocking when the count is strictly higher than 0.
45
46       $sem->adjust ($diff)
47           Atomically adds the amount given to the current semaphore count. If
48           the count becomes positive, wakes up any waiters. Does not block if
49           the count becomes negative, however.
50
51       $sem->down
52           Decrement the counter, therefore "locking" the semaphore. This
53           method waits until the semaphore is available if the counter is
54           zero or less.
55
56       $sem->wait
57           Similar to "down", but does not actually decrement the counter.
58           Instead, when this function returns, a following call to "down" or
59           "try" is guaranteed to succeed without blocking, until the next
60           thread switch ("cede" etc.).
61
62           Note that using "wait" is much less efficient than using "down", so
63           try to prefer "down" whenever possible.
64
65       $sem->wait ($callback)
66           If you pass a callback argument to "wait", it will not wait, but
67           immediately return. The callback will be called as soon as the
68           semaphore becomes available (which might be instantly), and gets
69           passed the semaphore as first argument.
70
71           The callback might "down" the semaphore exactly once, might wake up
72           other threads, but is NOT allowed to block (switch to other
73           threads).
74
75       $sem->up
76           Unlock the semaphore again.
77
78       $sem->try
79           Try to "down" the semaphore. Returns true when this was possible,
80           otherwise return false and leave the semaphore unchanged.
81
82       $sem->waiters
83           In scalar context, returns the number of threads waiting for this
84           semaphore. Might accidentally cause WW3 if called in other
85           contexts, so don't use these.
86
87       $guard = $sem->guard
88           This method calls "down" and then creates a guard object. When the
89           guard object is destroyed it automatically calls "up".
90

AUTHOR/SUPPORT/CONTACT

92          Marc A. Lehmann <schmorp@schmorp.de>
93          http://software.schmorp.de/pkg/Coro.html
94
95
96
97perl v5.28.0                      2017-08-31                      Semaphore(3)
Impressum