line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
################################################################################ |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Version 2.x, Copyright (C) 2007-2013, Marcus Holland-Moritz . |
4
|
|
|
|
|
|
|
# Version 1.x, Copyright (C) 1997, Graham Barr . |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or |
7
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
################################################################################ |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
package IPC::Semaphore; |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
|
|
87
|
use IPC::SysV qw(GETNCNT GETZCNT GETVAL SETVAL GETPID GETALL SETALL |
14
|
1
|
|
|
1
|
|
632
|
IPC_STAT IPC_SET IPC_RMID); |
|
1
|
|
|
|
|
3
|
|
15
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
16
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
46
|
|
17
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
110
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$VERSION = '2.08'; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Figure out if we have support for native sized types |
22
|
|
|
|
|
|
|
my $N = do { my $foo = eval { pack "L!", 0 }; $@ ? '' : '!' }; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
{ |
25
|
|
|
|
|
|
|
package IPC::Semaphore::stat; |
26
|
|
|
|
|
|
|
|
27
|
1
|
|
|
1
|
|
557
|
use Class::Struct qw(struct); |
|
1
|
|
|
|
|
1974
|
|
|
1
|
|
|
|
|
6
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
struct 'IPC::Semaphore::stat' => [ |
30
|
|
|
|
|
|
|
uid => '$', |
31
|
|
|
|
|
|
|
gid => '$', |
32
|
|
|
|
|
|
|
cuid => '$', |
33
|
|
|
|
|
|
|
cgid => '$', |
34
|
|
|
|
|
|
|
mode => '$', |
35
|
|
|
|
|
|
|
ctime => '$', |
36
|
|
|
|
|
|
|
otime => '$', |
37
|
|
|
|
|
|
|
nsems => '$', |
38
|
|
|
|
|
|
|
]; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub new { |
42
|
1
|
50
|
|
1
|
1
|
4
|
@_ == 4 || croak 'new ' . __PACKAGE__ . '( KEY, NSEMS, FLAGS )'; |
43
|
1
|
|
|
|
|
3
|
my $class = shift; |
44
|
|
|
|
|
|
|
|
45
|
1
|
|
|
|
|
21
|
my $id = semget($_[0],$_[1],$_[2]); |
46
|
|
|
|
|
|
|
|
47
|
1
|
50
|
|
|
|
25
|
defined($id) |
48
|
|
|
|
|
|
|
? bless \$id, $class |
49
|
|
|
|
|
|
|
: undef; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub id { |
53
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
54
|
0
|
|
|
|
|
0
|
$$self; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub remove { |
58
|
1
|
|
|
1
|
1
|
447
|
my $self = shift; |
59
|
1
|
|
|
|
|
7
|
my $result = semctl($$self,0,IPC_RMID,0); |
60
|
1
|
|
|
|
|
28
|
undef $$self; |
61
|
1
|
|
|
|
|
7
|
$result; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub getncnt { |
65
|
3
|
50
|
|
3
|
1
|
553
|
@_ == 2 || croak '$sem->getncnt( SEM )'; |
66
|
3
|
|
|
|
|
6
|
my $self = shift; |
67
|
3
|
|
|
|
|
5
|
my $sem = shift; |
68
|
3
|
|
|
|
|
12
|
my $v = semctl($$self,$sem,GETNCNT,0); |
69
|
3
|
50
|
|
|
|
23
|
$v ? 0 + $v : undef; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub getzcnt { |
73
|
0
|
0
|
|
0
|
1
|
0
|
@_ == 2 || croak '$sem->getzcnt( SEM )'; |
74
|
0
|
|
|
|
|
0
|
my $self = shift; |
75
|
0
|
|
|
|
|
0
|
my $sem = shift; |
76
|
0
|
|
|
|
|
0
|
my $v = semctl($$self,$sem,GETZCNT,0); |
77
|
0
|
0
|
|
|
|
0
|
$v ? 0 + $v : undef; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub getval { |
81
|
0
|
0
|
|
0
|
1
|
0
|
@_ == 2 || croak '$sem->getval( SEM )'; |
82
|
0
|
|
|
|
|
0
|
my $self = shift; |
83
|
0
|
|
|
|
|
0
|
my $sem = shift; |
84
|
0
|
|
|
|
|
0
|
my $v = semctl($$self,$sem,GETVAL,0); |
85
|
0
|
0
|
|
|
|
0
|
$v ? 0 + $v : undef; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub getpid { |
89
|
0
|
0
|
|
0
|
1
|
0
|
@_ == 2 || croak '$sem->getpid( SEM )'; |
90
|
0
|
|
|
|
|
0
|
my $self = shift; |
91
|
0
|
|
|
|
|
0
|
my $sem = shift; |
92
|
0
|
|
|
|
|
0
|
my $v = semctl($$self,$sem,GETPID,0); |
93
|
0
|
0
|
|
|
|
0
|
$v ? 0 + $v : undef; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub op { |
97
|
1
|
50
|
|
1
|
1
|
5
|
@_ >= 4 || croak '$sem->op( OPLIST )'; |
98
|
1
|
|
|
|
|
3
|
my $self = shift; |
99
|
1
|
50
|
|
|
|
5
|
croak 'Bad arg count' if @_ % 3; |
100
|
1
|
|
|
|
|
5
|
my $data = pack("s$N*",@_); |
101
|
1
|
|
|
|
|
19
|
semop($$self,$data); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub stat { |
105
|
1
|
|
|
1
|
1
|
1153
|
my $self = shift; |
106
|
1
|
|
|
|
|
3
|
my $data = ""; |
107
|
1
|
50
|
|
|
|
10
|
semctl($$self,0,IPC_STAT,$data) |
108
|
|
|
|
|
|
|
or return undef; |
109
|
1
|
|
|
|
|
31
|
IPC::Semaphore::stat->new->unpack($data); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub set { |
113
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
114
|
0
|
|
|
|
|
0
|
my $ds; |
115
|
|
|
|
|
|
|
|
116
|
0
|
0
|
|
|
|
0
|
if(@_ == 1) { |
117
|
0
|
|
|
|
|
0
|
$ds = shift; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
else { |
120
|
0
|
0
|
|
|
|
0
|
croak 'Bad arg count' if @_ % 2; |
121
|
0
|
|
|
|
|
0
|
my %arg = @_; |
122
|
0
|
0
|
|
|
|
0
|
$ds = $self->stat |
123
|
|
|
|
|
|
|
or return undef; |
124
|
0
|
|
|
|
|
0
|
my($key,$val); |
125
|
0
|
|
|
|
|
0
|
$ds->$key($val) |
126
|
|
|
|
|
|
|
while(($key,$val) = each %arg); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
0
|
my $v = semctl($$self,0,IPC_SET,$ds->pack); |
130
|
0
|
0
|
|
|
|
0
|
$v ? 0 + $v : undef; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub getall { |
134
|
2
|
|
|
2
|
1
|
7
|
my $self = shift; |
135
|
2
|
|
|
|
|
5
|
my $data = ""; |
136
|
2
|
50
|
|
|
|
9
|
semctl($$self,0,GETALL,$data) |
137
|
|
|
|
|
|
|
or return (); |
138
|
2
|
|
|
|
|
19
|
(unpack("s$N*",$data)); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub setall { |
142
|
2
|
|
|
2
|
1
|
895
|
my $self = shift; |
143
|
2
|
|
|
|
|
16
|
my $data = pack("s$N*",@_); |
144
|
2
|
|
|
|
|
11
|
semctl($$self,0,SETALL,$data); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub setval { |
148
|
0
|
0
|
|
0
|
1
|
|
@_ == 3 || croak '$sem->setval( SEM, VAL )'; |
149
|
0
|
|
|
|
|
|
my $self = shift; |
150
|
0
|
|
|
|
|
|
my $sem = shift; |
151
|
0
|
|
|
|
|
|
my $val = shift; |
152
|
0
|
|
|
|
|
|
semctl($$self,$sem,SETVAL,$val); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
1; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
__END__ |