ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Semaphore.pm
Revision: 1.127
Committed: Thu Dec 13 01:50:29 2012 UTC (11 years, 5 months ago) by root
Branch: MAIN
Changes since 1.126: +6 -4 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Coro::Semaphore - counting semaphores
4
5 =head1 SYNOPSIS
6
7 use Coro;
8
9 $sig = new Coro::Semaphore [initial value];
10
11 $sig->down; # wait for signal
12
13 # ... some other "thread"
14
15 $sig->up;
16
17 =head1 DESCRIPTION
18
19 This module implements counting semaphores. You can initialize a mutex
20 with any level of parallel users, that is, you can intialize a sempahore
21 that can be C<down>ed more than once until it blocks. There is no owner
22 associated with semaphores, so one thread can C<down> it while another can
23 C<up> it (or vice versa), C<up> can be called before C<down> and so on:
24 the semaphore is really just an integer counter that optionally blocks
25 when it is 0.
26
27 Counting semaphores are typically used to coordinate access to
28 resources, with the semaphore count initialized to the number of free
29 resources. Threads then increment the count when resources are added
30 and decrement the count when resources are removed.
31
32 You don't have to load C<Coro::Semaphore> manually, it will be loaded
33 automatically when you C<use Coro> and call the C<new> constructor.
34
35 =over 4
36
37 =cut
38
39 package Coro::Semaphore;
40
41 use common::sense;
42
43 use Coro ();
44
45 our $VERSION = 6.23;
46
47 =item new [inital count]
48
49 Creates a new sempahore object with the given initial lock count. The
50 default lock count is 1, which means it is unlocked by default. Zero (or
51 negative values) are also allowed, in which case the semaphore is locked
52 by default.
53
54 =item $sem->count
55
56 Returns the current semaphore count.
57
58 =item $sem->adjust ($diff)
59
60 Atomically adds the amount given to the current semaphore count. If the
61 count becomes positive, wakes up any waiters. Does not block if the count
62 becomes negative, however.
63
64 =item $sem->down
65
66 Decrement the counter, therefore "locking" the semaphore. This method
67 waits until the semaphore is available if the counter is zero.
68
69 =item $sem->wait
70
71 Similar to C<down>, but does not actually decrement the counter. Instead,
72 when this function returns, a following call to C<down> or C<try> is
73 guaranteed to succeed without blocking, until the next thread switch
74 (C<cede> etc.).
75
76 Note that using C<wait> is much less efficient than using C<down>, so try
77 to prefer C<down> whenever possible.
78
79 =item $sem->wait ($callback)
80
81 If you pass a callback argument to C<wait>, it will not wait, but
82 immediately return. The callback will be called as soon as the semaphore
83 becomes available (which might be instantly), and gets passed the
84 semaphore as first argument.
85
86 The callback might C<down> the semaphore exactly once, might wake up other
87 threads, but is I<NOT> allowed to block (switch to other threads).
88
89 =cut
90
91 #=item $status = $sem->timed_down ($timeout)
92 #
93 #Like C<down>, but returns false if semaphore couldn't be acquired within
94 #$timeout seconds, otherwise true.
95
96 #sub timed_down {
97 # require Coro::Timer;
98 # my $timeout = Coro::Timer::timeout ($_[1]);
99 #
100 # while ($_[0][0] <= 0) {
101 # push @{$_[0][1]}, $Coro::current;
102 # &Coro::schedule;
103 # if ($timeout) {
104 # # ugly as hell. slow, too, btw!
105 # for (0..$#{$_[0][1]}) {
106 # if ($_[0][1][$_] == $Coro::current) {
107 # splice @{$_[0][1]}, $_, 1;
108 # return;
109 # }
110 # }
111 # die;
112 # }
113 # }
114 #
115 # --$_[0][0];
116 # return 1;
117 #}
118
119 =item $sem->up
120
121 Unlock the semaphore again.
122
123 =item $sem->try
124
125 Try to C<down> the semaphore. Returns true when this was possible,
126 otherwise return false and leave the semaphore unchanged.
127
128 =item $sem->waiters
129
130 In scalar context, returns the number of threads waiting for this
131 semaphore.
132
133 =item $guard = $sem->guard
134
135 This method calls C<down> and then creates a guard object. When the guard
136 object is destroyed it automatically calls C<up>.
137
138 =cut
139
140 sub guard {
141 &down;
142 bless [$_[0]], Coro::Semaphore::guard::
143 }
144
145 #=item $guard = $sem->timed_guard ($timeout)
146 #
147 #Like C<guard>, but returns undef if semaphore couldn't be acquired within
148 #$timeout seconds, otherwise the guard object.
149
150 #sub timed_guard {
151 # &timed_down
152 # ? bless \\$_[0], Coro::Semaphore::guard::
153 # : ();
154 #}
155
156 sub Coro::Semaphore::guard::DESTROY {
157 &up($_[0][0]);
158 }
159
160 =back
161
162 =head1 AUTHOR
163
164 Marc Lehmann <schmorp@schmorp.de>
165 http://home.schmorp.de/
166
167 =cut
168
169 1
170