« lib/Genezzo/Contrib/Clustered/GLock/GLock.pm
↑ Summary
lib/Genezzo/Contrib/Clustered/GLock/GLockUR.pm »
| 1 | #!/usr/bin/perl | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # copyright (c) 2005, Eric Rollins, all rights reserved, worldwide | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | # | ||||||||||||||||||||
| 6 | # | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | # GTXLock.pm | ||||||||||||||||||||
| 9 | # retains hash of all locks held by transaction | ||||||||||||||||||||
| 10 | # hashes lock names | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | package Genezzo::Contrib::Clustered::GLock::GTXLock; | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 9 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 9 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 9 | ▶ | use Genezzo::Contrib::Clustered::GLock::GLock; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 9 | ▶ | use Genezzo::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 21 | 0 | ▶ | @_ >= 1 or croak 'usage: GTXLock->new()'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | 0 | my $proto = shift; | |||||||||||||||||||
| 23 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 24 | 0 | my $self = {}; | |||||||||||||||||||
| 25 | 0 | $self->{locks} = {}; | |||||||||||||||||||
| 26 | 0 | bless $self, $class; | |||||||||||||||||||
| 27 | 0 | whisper "Genezzo::Contrib::Clustered::GLock::GTXLock::new()"; | |||||||||||||||||||
| 28 | 0 | return $self; | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 9 | our $LOCK_HASH_SIZE = 10000; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # options: lock: lockname (currently block number) | ||||||||||||||||||||
| 34 | # shared: 1 for shared lock | ||||||||||||||||||||
| 35 | # 0 for exclusive (default) | ||||||||||||||||||||
| 36 | 0 | ▶ | sub lock { | ||||||||||||||||||
|
Subroutine
lock: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||
| 37 | 0 | my $self = shift; | |||||||||||||||||||
| 38 | 0 | my $tmp = { @_,}; | |||||||||||||||||||
| 39 | 0 | my $lock = $tmp->{lock}; | |||||||||||||||||||
| 40 | 0 | my $shared = $tmp->{shared}; | |||||||||||||||||||
| 41 | 0 | ▶ | $shared = 0 if !defined($shared); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 0 | whisper "Genezzo::Contrib::Clustered::GLock::GTXLock::lock(lock => $lock, shared => $shared)"; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 0 | my $hashKey = $lock % $LOCK_HASH_SIZE; | |||||||||||||||||||
| 46 | 0 | my $curLock = $self->{locks}->{$hashKey}; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | ▶ | if(defined($curLock)){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | ▶ | if($shared == 1){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | 0 | return 1; # current mode is at least SHARED | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 0 | my $curShared = $curLock->isShared(); | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | ▶ | if($curShared == 0){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 0 | return 1; # already EX | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # $shared == 0 and $curShared == 1 => need to promote | ||||||||||||||||||||
| 60 | 0 | whisper "Genezzo::Contrib::Clustered::GLock::GTXLock::lock promote $lock (hashKey = $hashKey)"; | |||||||||||||||||||
| 61 | 0 | ▶ | $curLock->promote() or croak "GTXLock::lock($lock) failed promote"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | }else{ | ||||||||||||||||||||
| 63 | 0 | my $lockName = "GBL$hashKey"; | |||||||||||||||||||
| 64 | 0 | whisper "Genezzo::Contrib::Clustered::GLock::GTXLock::lock new $lock (hashKey = $hashKey)"; | |||||||||||||||||||
| 65 | 0 | $curLock = new Genezzo::Contrib::Clustered::GLock::GLock(lock => $lockName, block => 1); | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | ▶ | defined($curLock) or croak "GTXLock::lock($lock) failed new"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | $curLock->lock(shared => $shared) or croak "GTXLock::lock($lock) failed lock"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | $self->{locks}->{$hashKey} = $curLock; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | return 1; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | ▶ | sub unlockAll{ | ||||||||||||||||||
|
Subroutine
unlockAll: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 77 | 0 | my $self = shift; | |||||||||||||||||||
| 78 | 0 | my $locks = $self->{locks}; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | whisper "Genezzo::Contrib::Clustered::GTXLock::unlockAll()"; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | my $key; | |||||||||||||||||||
| 83 | my $value; | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | ▶ | while(($key,$value) = each(%$locks)){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 0 | ▶ | $value->unlock() or croak "GTXLock::unlock() failed unlock($key)"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | $self->{locks} = {}; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 0 | return 1; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | ▶ | sub demoteAll{ | ||||||||||||||||||
|
Subroutine
demoteAll: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 95 | 0 | my $self = shift; | |||||||||||||||||||
| 96 | 0 | my $locks = $self->{locks}; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 0 | whisper "Genezzo::Contrib::Clustered::GTXLock::demoteAll()"; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | my $key; | |||||||||||||||||||
| 101 | my $value; | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | ▶ | while(($key,$value) = each(%$locks)){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 0 | ▶ | if(!$value->isShared()){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | 0 | ▶ | $value->demote() or croak "GTXLock::demoteAll() failed demote($key)"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 0 | return 1; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 9 | 1; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | __DATA__ | ||||||||||||||||||||