lib/Genezzo/Contrib/Clustered/GLock/GLockUR.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 94 CC 8 SCAR 27.4
CC
8
Cov
0%
ur_lock
42.0
ur_promote
6.0
ur_demote
6.0
CRAP 15.5
1 #!/usr/bin/perl
2 #
3 # copyright (c) 2005, Eric Rollins, all rights reserved, worldwide
4 #
5 #
6 #
7  
8 use strict;
9 use warnings;
10 use POSIX;
11  
12 package Genezzo::Contrib::Clustered::GLock::GLockUR;
13  
14 # Locking for Genezzo using UNIX fcntl record locking
15 use Inline (C => 'DATA',
16 DIRECTORY => '/Inline' # Apache needs a writeable directory
17 );
18  
19 require Exporter;
20  
21 Inline->init; # help for "require GLockRecord"
22  
23 our @ISA = qw(Exporter);
24 our @EXPORT = qw(ur_lock ur_unlock ur_promote);
25  
26 our $MAX_PROCS = 20; # TODO: initialize this from elsewhere
27 # shouldn't be huge, as we only get ~100locks/sec on OSX.
28  
29 sub ur_lock()
30 {
31 my ($name, $shared, $blocking) = @_;
32  
33 set_max_procs_impl($MAX_PROCS);
34  
35 # turn name into number
36 my $offset = 0;
37 my $trypid = 0;
38  
39 if($name =~ /SVR/){
40 $name =~ /(\d+)/;
41 $trypid = $1;
42 }else{
43 $offset += $MAX_PROCS+1; # 1..MAX_PROCS reserved for PID locks
44 }
45  
46 $name =~ /(\d+)/;
47 my $lockid = $1 + $offset;
48  
49 my $ret = ur_lock_impl($lockid, $shared, $blocking);
50  
51 if($ret == -1){
52 die "DEADLOCK";
53 }
54  
55 if($ret > 0){
56 if($trypid > 0){
57 if($trypid > $MAX_PROCS){
58 die "Maximum procs ($MAX_PROCS) for GLockUR exceeded";
59 }
60  
61 set_pid_impl($trypid);
62 }
63  
64 return $lockid;
65 }
66  
67 return 0;
68 }
69  
70 sub ur_unlock()
71 {
72 my ($lockid) = @_;
73  
74 return ur_unlock_impl($lockid);
75 }
76  
77 sub ur_promote()
78 {
79 my ($name, $lockid, $blocking) = @_;
80  
81 my $ret = ur_promote_impl($name, $lockid, $blocking);
82  
83 if($ret == -1){
84 die "DEADLOCK";
85 }
86  
87 return $ret;
88 }
89  
90 sub ur_demote()
91 {
92 my ($name, $lockid, $blocking) = @_;
93  
94 my $ret = ur_demote_impl($name, $lockid, $blocking);
95  
96 if($ret == -1){
97 die "DEADLOCK";
98 }
99  
100 return $ret;
101 }
102  
103 sub ur_ast_poll()
104 {
105 return 1;
106 }
107  
108 sub ur_set_notify()
109 {
110 return 1;
111 }
112  
113 BEGIN
114 {
115 print STDERR "Genezzo::Contrib::Clustered::GLock::GLockUR installed\n";
116  
117 # By default avoid dying. Real handler can be registered later.
118 my $sigset = POSIX::SigSet->new(POSIX::SIGUSR2);
119 my $old_sigset = POSIX::SigSet->new;
120 POSIX::sigprocmask(POSIX::SIG_BLOCK, $sigset, $old_sigset)
121 or die "Error blocking SIGUSR2: $!\n";
122 }
123  
124 1;
125  
126 __DATA__