1 ################################################################################
5 # $Date: 2010/03/07 16:01:43 +0100 $
7 ################################################################################
9 # Version 2.x, Copyright (C) 2007-2010, Marcus Holland-Moritz <mhx@cpan.org>.
10 # Version 1.x, Copyright (C) 1997, Graham Barr <gbarr@pobox.com>.
12 # This program is free software; you can redistribute it and/or
13 # modify it under the same terms as Perl itself.
15 ################################################################################
17 package IPC::Semaphore;
19 use IPC::SysV qw(GETNCNT GETZCNT GETVAL SETVAL GETPID GETALL SETALL
20 IPC_STAT IPC_SET IPC_RMID);
22 use vars qw($VERSION);
25 $VERSION = do { my @r = '$Snapshot: /IPC-SysV/2.03 $' =~ /(\d+\.\d+(?:_\d+)?)/; @r ? $r[0] : '9.99' };
26 $VERSION = eval $VERSION;
28 # Figure out if we have support for native sized types
29 my $N = do { my $foo = eval { pack "L!", 0 }; $@ ? '' : '!' };
32 package IPC::Semaphore::stat;
34 use Class::Struct qw(struct);
36 struct 'IPC::Semaphore::stat' => [
49 @_ == 4 || croak 'new ' . __PACKAGE__ . '( KEY, NSEMS, FLAGS )';
52 my $id = semget($_[0],$_[1],$_[2]);
66 (semctl($$self,0,IPC_RMID,0), undef $$self)[0];
70 @_ == 2 || croak '$sem->getncnt( SEM )';
73 my $v = semctl($$self,$sem,GETNCNT,0);
78 @_ == 2 || croak '$sem->getzcnt( SEM )';
81 my $v = semctl($$self,$sem,GETZCNT,0);
86 @_ == 2 || croak '$sem->getval( SEM )';
89 my $v = semctl($$self,$sem,GETVAL,0);
94 @_ == 2 || croak '$sem->getpid( SEM )';
97 my $v = semctl($$self,$sem,GETPID,0);
102 @_ >= 4 || croak '$sem->op( OPLIST )';
104 croak 'Bad arg count' if @_ % 3;
105 my $data = pack("s$N*",@_);
112 semctl($$self,0,IPC_STAT,$data)
114 IPC::Semaphore::stat->new->unpack($data);
125 croak 'Bad arg count' if @_ % 2;
131 while(($key,$val) = each %arg);
134 my $v = semctl($$self,0,IPC_SET,$ds->pack);
141 semctl($$self,0,GETALL,$data)
143 (unpack("s$N*",$data));
148 my $data = pack("s$N*",@_);
149 semctl($$self,0,SETALL,$data);
153 @_ == 3 || croak '$sem->setval( SEM, VAL )';
157 semctl($$self,$sem,SETVAL,$val);
166 IPC::Semaphore - SysV Semaphore IPC object class
170 use IPC::SysV qw(IPC_PRIVATE S_IRUSR S_IWUSR IPC_CREAT);
173 $sem = IPC::Semaphore->new(IPC_PRIVATE, 10, S_IRUSR | S_IWUSR | IPC_CREAT);
175 $sem->setall( (0) x 10);
179 $ncnt = $sem->getncnt;
181 $zcnt = $sem->getzcnt;
189 A class providing an object based interface to SysV IPC semaphores.
195 =item new ( KEY , NSEMS , FLAGS )
197 Create a new semaphore set associated with C<KEY>. C<NSEMS> is the number
198 of semaphores in the set. A new set is created if
204 C<KEY> is equal to C<IPC_PRIVATE>
208 C<KEY> does not already have a semaphore identifier
209 associated with it, and C<I<FLAGS> & IPC_CREAT> is true.
213 On creation of a new semaphore set C<FLAGS> is used to set the
214 permissions. Be careful not to set any flags that the Sys V
215 IPC implementation does not allow: in some systems setting
216 execute bits makes the operations fail.
220 Returns the values of the semaphore set as an array.
222 =item getncnt ( SEM )
224 Returns the number of processes waiting for the semaphore C<SEM> to
225 become greater than its current value
229 Returns the process id of the last process that performed an operation
230 on the semaphore C<SEM>.
234 Returns the current value of the semaphore C<SEM>.
236 =item getzcnt ( SEM )
238 Returns the number of processes waiting for the semaphore C<SEM> to
243 Returns the system identifier for the semaphore set.
247 C<OPLIST> is a list of operations to pass to C<semop>. C<OPLIST> is
248 a concatenation of smaller lists, each which has three values. The
249 first is the semaphore number, the second is the operation and the last
250 is a flags value. See L<semop> for more details. For example
259 Remove and destroy the semaphore set from the system.
263 =item set ( NAME => VALUE [, NAME => VALUE ...] )
265 C<set> will set the following values of the C<stat> structure associated
266 with the semaphore set.
270 mode (only the permission bits)
272 C<set> accepts either a stat object, as returned by the C<stat> method,
273 or a list of I<name>-I<value> pairs.
275 =item setall ( VALUES )
277 Sets all values in the semaphore set to those given on the C<VALUES> list.
278 C<VALUES> must contain the correct number of values.
280 =item setval ( N , VALUE )
282 Set the C<N>th value in the semaphore set to C<VALUE>
286 Returns an object of type C<IPC::Semaphore::stat> which is a sub-class of
287 C<Class::Struct>. It provides the following fields. For a description
288 of these fields see your system documentation.
303 L<IPC::SysV>, L<Class::Struct>, L<semget>, L<semctl>, L<semop>
307 Graham Barr <gbarr@pobox.com>,
308 Marcus Holland-Moritz <mhx@cpan.org>
312 Version 2.x, Copyright (C) 2007-2010, Marcus Holland-Moritz.
314 Version 1.x, Copyright (c) 1997, Graham Barr.
316 This program is free software; you can redistribute it and/or
317 modify it under the same terms as Perl itself.