| 1 | package RPi::WiringPi::Meta; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp qw(croak); | ||
| 7 | use IPC::Shareable qw(:flock); | ||
| 8 | use JSON::XS; | ||
| 9 | use String::CRC32 qw(crc32); | ||
| 10 | |||
| 11 | our $VERSION = '3.1802'; | ||
| 12 | |||
| 13 | # Mirrors IPC::Shareable's signed 32-bit key_t overflow correction (_shm_key) | ||
| 14 | use constant MAX_KEY_INT_SIZE => 0x80000000; | ||
| 15 | |||
| 16 | sub meta { | ||
| 17 | my ($self) = @_; | ||
| 18 | |||
| 19 | return $self->{meta_knot} if exists $self->{meta_knot}; | ||
| 20 | |||
| 21 | # Tie a SCALAR holding our own JSON string (not a HASH). This keeps the | ||
| 22 | # entire blob in a single segment; tying a HASH (or storing a ref) would | ||
| 23 | # make IPC::Shareable fan each nested structure out into its own segment. | ||
| 24 | |||
| 25 | my $blob; | ||
| 26 | |||
| 27 | my $knot = tie $blob, 'IPC::Shareable', { | ||
| 28 | key => $self->{shm_key}, | ||
| 29 | create => 1, | ||
| 30 | destroy => 0, | ||
| 31 | } or die "Can't create shared memory segment: $!"; | ||
| 32 | |||
| 33 | $self->{meta_scalar} = \$blob; | ||
| 34 | $self->{meta_knot} = $knot; | ||
| 35 | |||
| 36 | return $knot; | ||
| 37 | } | ||
| 38 | sub meta_erase { | ||
| 39 | my ($self, $all) = @_; | ||
| 40 | |||
| 41 | $all //= 0; | ||
| 42 | |||
| 43 | $self->_meta_txn(sub { | ||
| 44 | my ($clean_store, $storage); | ||
| 45 | |||
| 46 | if ($all) { | ||
| 47 | $clean_store = {}; | ||
| 48 | } | ||
| 49 | else { | ||
| 50 | $storage = $self->meta_fetch()->{storage}; | ||
| 51 | $clean_store = defined $storage ? { storage => $storage } : {}; | ||
| 52 | } | ||
| 53 | |||
| 54 | $self->meta_store($clean_store); | ||
| 55 | }); | ||
| 56 | } | ||
| 57 | sub meta_remove { | ||
| 58 | # Removes the underlying shared memory segment (and its semaphore set) | ||
| 59 | # entirely, not just its contents. Unlike meta_erase(), which empties the | ||
| 60 | # blob but leaves the segment allocated (destroy => 0), this frees the SysV | ||
| 61 | # segment so it no longer appears in ipcs. A later meta() call transparently | ||
| 62 | # creates a fresh segment. | ||
| 63 | |||
| 64 | my ($self) = @_; | ||
| 65 | |||
| 66 | return if ! exists $self->{meta_knot}; | ||
| 67 | |||
| 68 | $self->{meta_knot}->remove; | ||
| 69 | |||
| 70 | delete $self->{meta_knot}; | ||
| 71 | delete $self->{meta_scalar}; | ||
| 72 | |||
| 73 | return 1; | ||
| 74 | } | ||
| 75 | sub meta_key_check { | ||
| 76 | # This is a class method, and must be called on the class prior to creating | ||
| 77 | # a Pi object | ||
| 78 | |||
| 79 | my ($class, $key) = @_; | ||
| 80 | |||
| 81 | if (! defined $key){ | ||
| 82 | croak "meta_key_check() requires a key sent in...\n"; | ||
| 83 | } | ||
| 84 | |||
| 85 | # Derive the integer segment key exactly as IPC::Shareable does (CRC32 of | ||
| 86 | # the string key, with signed 32-bit overflow correction), then probe for | ||
| 87 | # the segment's existence without creating it. | ||
| 88 | |||
| 89 | my $int = crc32($key); | ||
| 90 | $int -= MAX_KEY_INT_SIZE if $int >= MAX_KEY_INT_SIZE; | ||
| 91 | |||
| 92 | my $shm_check = shmget($int, 0, 0); | ||
| 93 | return defined $shm_check ? 1 : 0; | ||
| 94 | } | ||
| 95 | sub meta_key { | ||
| 96 | my ($self) = @_; | ||
| 97 | return $self->meta->seg->key; | ||
| 98 | } | ||
| 99 | sub meta_lock { | ||
| 100 | my ($self, $flags) = @_; | ||
| 101 | $flags = LOCK_EX if ! defined $flags; | ||
| 102 | $self->meta->lock($flags); | ||
| 103 | } | ||
| 104 | sub _meta_txn { | ||
| 105 | # Runs $code with the meta lock held, guaranteeing the lock is released | ||
| 106 | # even if $code dies (a die between lock and unlock would otherwise hold | ||
| 107 | # the exclusive lock indefinitely across processes). Any error is | ||
| 108 | # re-thrown after the unlock | ||
| 109 | |||
| 110 | my ($self, $code, $flags) = @_; | ||
| 111 | |||
| 112 | $self->meta_lock($flags); | ||
| 113 | |||
| 114 | my @ret = eval { $code->() }; | ||
| 115 | my $err = $@; | ||
| 116 | |||
| 117 | $self->meta_unlock; | ||
| 118 | |||
| 119 | die $err if $err; | ||
| 120 | |||
| 121 | return wantarray ? @ret : $ret[0]; | ||
| 122 | } | ||
| 123 | sub meta_unlock { | ||
| 124 | my ($self) = @_; | ||
| 125 | $self->meta->unlock; | ||
| 126 | } | ||
| 127 | sub meta_fetch { | ||
| 128 | my ($self) = @_; | ||
| 129 | |||
| 130 | $self->meta; | ||
| 131 | |||
| 132 | # decode_json() always returns a fresh, fully detached structure, so callers | ||
| 133 | # may safely mutate nested keys before calling meta_store(). A new segment | ||
| 134 | # reads back as undef. | ||
| 135 | |||
| 136 | my $json = ${ $self->{meta_scalar} }; | ||
| 137 | $json = '{}' if ! defined $json || $json eq ''; | ||
| 138 | |||
| 139 | return decode_json $json; | ||
| 140 | } | ||
| 141 | sub meta_store { | ||
| 142 | my ($self, $data) = @_; | ||
| 143 | |||
| 144 | if (! defined $data){ | ||
| 145 | croak "meta_store() requires a hash reference sent in...\n"; | ||
| 146 | } | ||
| 147 | |||
| 148 | $self->meta; | ||
| 149 | |||
| 150 | # Store a single JSON string (a non-ref scalar) so the whole blob lives in | ||
| 151 | # one segment. IPC::Shareable croaks itself if it exceeds the segment size. | ||
| 152 | |||
| 153 | ${ $self->{meta_scalar} } = encode_json $data; | ||
| 154 | } | ||
| 155 | sub meta_delete { | ||
| 156 | my ($self, $name) = @_; | ||
| 157 | |||
| 158 | if (! defined $name){ | ||
| 159 | croak "when setting a metadata slot, you must send in a name\n"; | ||
| 160 | } | ||
| 161 | |||
| 162 | $self->_meta_txn(sub { | ||
| 163 | my $shm = $self->meta_fetch; | ||
| 164 | delete $shm->{storage}{$name}; | ||
| 165 | $self->meta_store($shm); | ||
| 166 | }); | ||
| 167 | } | ||
| 168 | sub meta_get { | ||
| 169 | my ($self, $name) = @_; | ||
| 170 | |||
| 171 | if (! defined $name){ | ||
| 172 | croak "when getting a metadata slot, you must send in a name\n"; | ||
| 173 | } | ||
| 174 | |||
| 175 | return $self->_meta_txn(sub { | ||
| 176 | my $shm = $self->meta_fetch; | ||
| 177 | |||
| 178 | my $data; | ||
| 179 | $data = { %{ $shm->{storage}{$name} } } if exists $shm->{storage}{$name}; | ||
| 180 | |||
| 181 | return $data; | ||
| 182 | }); | ||
| 183 | } | ||
| 184 | sub meta_set { | ||
| 185 | my ($self, $name, $data) = @_; | ||
| 186 | |||
| 187 | if (! defined $name){ | ||
| 188 | croak "when setting a metadata slot, you must send in a name\n"; | ||
| 189 | } | ||
| 190 | |||
| 191 | if (ref $data ne 'HASH'){ | ||
| 192 | croak "when setting a metadata slot, you must supply a hash reference\n"; | ||
| 193 | } | ||
| 194 | |||
| 195 | $self->_meta_txn(sub { | ||
| 196 | my $shm = $self->meta_fetch; | ||
| 197 | $shm->{storage}{$name} = { %$data }; | ||
| 198 | $self->meta_store($shm); | ||
| 199 | }); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub _vim{1;}; | ||
| 203 | |||
| 204 | 1; | ||
| 205 | |||
| 206 | __END__ |