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.
7 package IPC::Semaphore;
9 use IPC::SysV qw(GETNCNT GETZCNT GETVAL SETVAL GETPID GETALL SETALL
10 IPC_STAT IPC_SET IPC_RMID);
12 use vars qw($VERSION);
16 $VERSION = eval $VERSION;
19 package IPC::Semaphore::stat;
21 use Class::Struct qw(struct);
23 struct 'IPC::Semaphore::stat' => [
36 @_ == 4 || croak 'new ' . __PACKAGE__ . '( KEY, NSEMS, FLAGS )';
39 my $id = semget($_[0],$_[1],$_[2]);
53 (semctl($$self,0,IPC_RMID,0), undef $$self)[0];
57 @_ == 2 || croak '$sem->getncnt( SEM )';
60 my $v = semctl($$self,$sem,GETNCNT,0);
65 @_ == 2 || croak '$sem->getzcnt( SEM )';
68 my $v = semctl($$self,$sem,GETZCNT,0);
73 @_ == 2 || croak '$sem->getval( SEM )';
76 my $v = semctl($$self,$sem,GETVAL,0);
81 @_ == 2 || croak '$sem->getpid( SEM )';
84 my $v = semctl($$self,$sem,GETPID,0);
89 @_ >= 4 || croak '$sem->op( OPLIST )';
91 croak 'Bad arg count' if @_ % 3;
92 my $data = pack("s!*",@_);
99 semctl($$self,0,IPC_STAT,$data)
101 IPC::Semaphore::stat->new->unpack($data);
112 croak 'Bad arg count' if @_ % 2;
118 while(($key,$val) = each %arg);
121 my $v = semctl($$self,0,IPC_SET,$ds->pack);
128 semctl($$self,0,GETALL,$data)
130 (unpack("s!*",$data));
135 my $data = pack("s!*",@_);
136 semctl($$self,0,SETALL,$data);
140 @_ == 3 || croak '$sem->setval( SEM, VAL )';
144 semctl($$self,$sem,SETVAL,$val);
153 IPC::Semaphore - SysV Semaphore IPC object class
157 use IPC::SysV qw(IPC_PRIVATE S_IRUSR S_IWUSR IPC_CREAT);
160 $sem = new IPC::Semaphore(IPC_PRIVATE, 10, S_IRUSR | S_IWUSR | IPC_CREAT);
162 $sem->setall( (0) x 10);
166 $ncnt = $sem->getncnt;
168 $zcnt = $sem->getzcnt;
176 A class providing an object based interface to SysV IPC semaphores.
182 =item new ( KEY , NSEMS , FLAGS )
184 Create a new semaphore set associated with C<KEY>. C<NSEMS> is the number
185 of semaphores in the set. A new set is created if
191 C<KEY> is equal to C<IPC_PRIVATE>
195 C<KEY> does not already have a semaphore identifier
196 associated with it, and C<I<FLAGS> & IPC_CREAT> is true.
200 On creation of a new semaphore set C<FLAGS> is used to set the
201 permissions. Be careful not to set any flags that the Sys V
202 IPC implementation does not allow: in some systems setting
203 execute bits makes the operations fail.
207 Returns the values of the semaphore set as an array.
209 =item getncnt ( SEM )
211 Returns the number of processes waiting for the semaphore C<SEM> to
212 become greater than its current value
216 Returns the process id of the last process that performed an operation
217 on the semaphore C<SEM>.
221 Returns the current value of the semaphore C<SEM>.
223 =item getzcnt ( SEM )
225 Returns the number of processes waiting for the semaphore C<SEM> to
230 Returns the system identifier for the semaphore set.
234 C<OPLIST> is a list of operations to pass to C<semop>. C<OPLIST> is
235 a concatenation of smaller lists, each which has three values. The
236 first is the semaphore number, the second is the operation and the last
237 is a flags value. See L<semop> for more details. For example
246 Remove and destroy the semaphore set from the system.
250 =item set ( NAME => VALUE [, NAME => VALUE ...] )
252 C<set> will set the following values of the C<stat> structure associated
253 with the semaphore set.
257 mode (only the permission bits)
259 C<set> accepts either a stat object, as returned by the C<stat> method,
260 or a list of I<name>-I<value> pairs.
262 =item setall ( VALUES )
264 Sets all values in the semaphore set to those given on the C<VALUES> list.
265 C<VALUES> must contain the correct number of values.
267 =item setval ( N , VALUE )
269 Set the C<N>th value in the semaphore set to C<VALUE>
273 Returns an object of type C<IPC::Semaphore::stat> which is a sub-class of
274 C<Class::Struct>. It provides the following fields. For a description
275 of these fields see your system documentation.
290 L<IPC::SysV> L<Class::Struct> L<semget> L<semctl> L<semop>
294 Graham Barr <gbarr@pobox.com>
298 Copyright (c) 1997 Graham Barr. All rights reserved.
299 This program is free software; you can redistribute it and/or modify it
300 under the same terms as Perl itself.