Resync with mainline
[p5sagit/p5-mst-13.2.git] / ext / IPC / SysV / Semaphore.pm
CommitLineData
0ade1984 1# IPC::Semaphore
2#
3# Copyright (c) 1997 Graham Barr <gbarr@pobox.com>. All rights reserved.
4# This program is free software; you can redistribute it and/or
5# modify it under the same terms as Perl itself.
6
7package IPC::Semaphore;
8
9use IPC::SysV qw(GETNCNT GETZCNT GETVAL SETVAL GETPID GETALL SETALL
10 IPC_STAT IPC_SET IPC_RMID);
11use strict;
12use vars qw($VERSION);
13use Carp;
14
15$VERSION = "1.00";
16
17{
18 package IPC::Semaphore::stat;
19
20 use Class::Struct qw(struct);
21
22 struct 'IPC::Semaphore::stat' => [
23 uid => '$',
24 gid => '$',
25 cuid => '$',
26 cgid => '$',
27 mode => '$',
28 ctime => '$',
29 otime => '$',
30 nsems => '$',
31 ];
32}
33
34sub new {
35 @_ == 4 || croak 'new ' . __PACKAGE__ . '( KEY, NSEMS, FLAGS )';
36 my $class = shift;
37
38 my $id = semget($_[0],$_[1],$_[2]);
39
40 defined($id)
41 ? bless \$id, $class
42 : undef;
43}
44
45sub id {
46 my $self = shift;
47 $$self;
48}
49
50sub remove {
51 my $self = shift;
52 (semctl($$self,0,IPC_RMID,0), undef $$self)[0];
53}
54
55sub getncnt {
56 @_ == 2 || croak '$sem->getncnt( SEM )';
57 my $self = shift;
58 my $sem = shift;
59 my $v = semctl($$self,$sem,GETNCNT,0);
60 $v ? 0 + $v : undef;
61}
62
63sub getzcnt {
64 @_ == 2 || croak '$sem->getzcnt( SEM )';
65 my $self = shift;
66 my $sem = shift;
67 my $v = semctl($$self,$sem,GETZCNT,0);
68 $v ? 0 + $v : undef;
69}
70
71sub getval {
72 @_ == 2 || croak '$sem->getval( SEM )';
73 my $self = shift;
74 my $sem = shift;
75 my $v = semctl($$self,$sem,GETVAL,0);
76 $v ? 0 + $v : undef;
77}
78
79sub getpid {
80 @_ == 2 || croak '$sem->getpid( SEM )';
81 my $self = shift;
82 my $sem = shift;
83 my $v = semctl($$self,$sem,GETPID,0);
84 $v ? 0 + $v : undef;
85}
86
87sub op {
88 @_ >= 4 || croak '$sem->op( OPLIST )';
89 my $self = shift;
90 croak 'Bad arg count' if @_ % 3;
91 my $data = pack("s*",@_);
92 semop($$self,$data);
93}
94
95sub stat {
96 my $self = shift;
97 my $data = "";
98 semctl($$self,0,IPC_STAT,$data)
99 or return undef;
100 IPC::Semaphore::stat->new->unpack($data);
101}
102
103sub set {
104 my $self = shift;
105 my $ds;
106
107 if(@_ == 1) {
108 $ds = shift;
109 }
110 else {
111 croak 'Bad arg count' if @_ % 2;
112 my %arg = @_;
113 my $ds = $self->stat
114 or return undef;
115 my($key,$val);
116 $ds->$key($val)
117 while(($key,$val) = each %arg);
118 }
119
120 my $v = semctl($$self,0,IPC_SET,$ds->pack);
121 $v ? 0 + $v : undef;
122}
123
124sub getall {
125 my $self = shift;
126 my $data = "";
127 semctl($$self,0,GETALL,$data)
128 or return ();
129 (unpack("s*",$data));
130}
131
132sub setall {
133 my $self = shift;
134 my $data = pack("s*",@_);
135 semctl($$self,0,SETALL,$data);
136}
137
138sub setval {
139 @_ == 3 || croak '$sem->setval( SEM, VAL )';
140 my $self = shift;
141 my $sem = shift;
142 my $val = shift;
143 semctl($$self,$sem,SETVAL,$val);
144}
145
1461;
147
148__END__
149
150=head1 NAME
151
152IPC::Semaphore - SysV Semaphore IPC object class
153
154=head1 SYNOPSIS
155
156 use IPC::SysV qw(IPC_PRIVATE S_IRWXU IPC_CREAT);
157 use IPC::Semaphore;
158
159 $sem = new IPC::Semaphore(IPC_PRIVATE, 10, S_IRWXU | IPC_CREAT);
160
161 $sem->setall( (0) x 10);
162
163 @sem = $sem->getall;
164
165 $ncnt = $sem->getncnt;
166
167 $zcnt = $sem->getzcnt;
168
169 $ds = $sem->stat;
170
171 $sem->remove;
172
173=head1 DESCRIPTION
174
175=head1 METHODS
176
177=over 4
178
179=item new ( KEY , NSEMS , FLAGS )
180
181Create a new semaphore set associated with C<KEY>. C<NSEMS> is the number
182of semaphores in the set. A new set is created if
183
184=over 4
185
186=item *
187
188C<KEY> is equal to C<IPC_PRIVATE>
189
190=item *
191
192C<KEY> does not already have a semaphore identifier
193associated with it, and C<I<FLAGS> & IPC_CREAT> is true.
194
195=back
196
197On creation of a new semaphore set C<FLAGS> is used to set the
198permissions.
199
200=item getall
201
202Returns the values of the semaphore set as an array.
203
204=item getncnt ( SEM )
205
206Returns the number of processed waiting for the semaphore C<SEM> to
207become greater than it's current value
208
209=item getpid ( SEM )
210
211Returns the process id of the last process that performed an operation
212on the semaphore C<SEM>.
213
214=item getval ( SEM )
215
216Returns the current value of the semaphore C<SEM>.
217
218=item getzcnt ( SEM )
219
220Returns the number of processed waiting for the semaphore C<SEM> to
221become zero.
222
223=item id
224
225Returns the system identifier for the semaphore set.
226
227=item op ( OPLIST )
228
229C<OPLIST> is a list of operations to pass to C<semop>. C<OPLIST> is
230a concatenation of smaller lists, each which has three values. The
231first is the semaphore number, the second is the operation and the last
232is a flags value. See L<semop> for more details. For example
233
234 $sem->op(
235 0, -1, IPC_NOWAIT,
236 1, 1, IPC_NOWAIT
237 );
238
239=item remove
240
241Remove and destroy the semaphore set from the system.
242
243=item set ( STAT )
244
245=item set ( NAME => VALUE [, NAME => VALUE ...] )
246
247C<set> will set the following values of the C<stat> structure associated
248with the semaphore set.
249
250 uid
251 gid
252 mode (oly the permission bits)
253
254C<set> accepts either a stat object, as returned by the C<stat> method,
255or a list of I<name>-I<value> pairs.
256
257=item setall ( VALUES )
258
259Sets all values in the semaphore set to those given on the C<VALUES> list.
260C<VALUES> must contain the correct number of values.
261
262=item setval ( N , VALUE )
263
264Set the C<N>th value in the semaphore set to C<VALUE>
265
266=item stat
267
268Returns an object of type C<IPC::Semaphore::stat> which is a sub-class of
269C<Class::Struct>. It provides the following fields. For a description
270of these fields see you system documentation.
271
272 uid
273 gid
274 cuid
275 cgid
276 mode
277 ctime
278 otime
279 nsems
280
281=back
282
283=head1 SEE ALSO
284
285L<IPC::SysV> L<Class::Struct> L<semget> L<semctl> L<semop>
286
287=head1 AUTHOR
288
289Graham Barr <gbarr@pobox.com>
290
291=head1 COPYRIGHT
292
293Copyright (c) 1997 Graham Barr. All rights reserved.
294This program is free software; you can redistribute it and/or modify it
295under the same terms as Perl itself.
296
297=cut