add extension to support SysV IPC
[p5sagit/p5-mst-13.2.git] / t / op / ipcsem.t
CommitLineData
9b599b2a 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
d4217c7e 6 $SIG{__DIE__} = 'cleanup';
9b599b2a 7}
8
9b599b2a 9use Config;
9b599b2a 10
11BEGIN {
12 unless($Config{'d_semget'} eq 'define' &&
13 $Config{'d_semctl'} eq 'define') {
55d729e4 14 print "1..0\n";
9b599b2a 15 exit;
16 }
9b599b2a 17}
18
19use strict;
20
0ade1984 21use IPC::SysV qw(IPC_PRIVATE IPC_CREAT IPC_STAT IPC_RMID
22 GETALL SETALL
23 S_IRWXU S_IRWXG S_IRWXO);
24
9b599b2a 25print "1..10\n";
26
0ade1984 27my $sem = semget(IPC_PRIVATE, 10, S_IRWXU | S_IRWXG | S_IRWXO | IPC_CREAT);
ce5ffdc7 28# Very first time called after machine is booted value may be 0
29die "semget: $!\n" unless defined($sem) && $sem >= 0;
9b599b2a 30
31print "ok 1\n";
32
33my $data;
0ade1984 34semctl($sem,0,IPC_STAT,$data) or print "not ";
9b599b2a 35print "ok 2\n";
36
37print "not " unless length($data);
38print "ok 3\n";
39
d4217c7e 40my $template;
41
42# Find the pack/unpack template capable of handling native C shorts.
43
44if ($Config{shortsize} == 2) {
45 $template = "s";
46} elsif ($Config{shortsize} == 4) {
47 $template = "l";
48} elsif ($Config{shortsize} == 8) {
49 foreach my $t (qw(i q)) { # Try quad last because not supported everywhere.
50 # We could trap the unsupported quad template with eval
51 # but if we get this far we should have quad support anyway.
52 if (length(pack($t, 0)) == 8) {
53 $template = $t;
54 last;
55 }
56 }
57}
58
59die "$0: cannot pack native shorts\n" unless defined $template;
60
61$template .= "*";
62
63my $nsem = 10;
64
0ade1984 65semctl($sem,0,SETALL,pack($template,(0) x $nsem)) or print "not ";
9b599b2a 66print "ok 4\n";
67
68$data = "";
0ade1984 69semctl($sem,0,GETALL,$data) or print "not ";
9b599b2a 70print "ok 5\n";
71
d4217c7e 72print "not " unless length($data) == length(pack($template,(0) x $nsem));
9b599b2a 73print "ok 6\n";
74
d4217c7e 75my @data = unpack($template,$data);
76
77my $adata = "0" x $nsem;
9b599b2a 78
d4217c7e 79print "not " unless @data == $nsem and join("",@data) eq $adata;
9b599b2a 80print "ok 7\n";
81
d4217c7e 82my $poke = 2;
83
84$data[$poke] = 1;
0ade1984 85semctl($sem,0,SETALL,pack($template,@data)) or print "not ";
9b599b2a 86print "ok 8\n";
87
88$data = "";
0ade1984 89semctl($sem,0,GETALL,$data) or print "not ";
9b599b2a 90print "ok 9\n";
91
d4217c7e 92@data = unpack($template,$data);
93
94my $bdata = "0" x $poke . "1" . "0" x ($nsem-$poke-1);
9b599b2a 95
d4217c7e 96print "not " unless join("",@data) eq $bdata;
9b599b2a 97print "ok 10\n";
98
0ade1984 99sub cleanup { semctl($sem,0,IPC_RMID,undef) if defined $sem }
9b599b2a 100
d4217c7e 101cleanup;