Data-Pool-Shared
view release on metacpan or search on metacpan
METHODS
Allocation
my $idx = $pool->alloc; # block until available
my $idx = $pool->alloc($timeout); # with timeout (seconds)
my $idx = $pool->alloc(0); # non-blocking
my $idx = $pool->try_alloc; # non-blocking (alias)
Returns slot index on success, "undef" on failure/timeout.
$pool->free($idx); # release slot (returns true/false)
Batch Operations
my $slots = $pool->alloc_n($n); # allocate N slots (blocking)
my $slots = $pool->alloc_n($n, $timeout); # with timeout
my $slots = $pool->alloc_n($n, 0); # non-blocking
# returns arrayref of indices, or undef (all-or-nothing)
my $freed = $pool->free_n(\@indices); # batch free, returns count freed
# single used-decrement + single futex wake (faster than N individual frees)
my $slots = $pool->allocated_slots; # arrayref of all allocated indices
Data Access
my $val = $pool->get($idx); # read slot
$pool->set($idx, $val); # write slot
For I64/I32 variants:
my $ok = $pool->cas($idx, $old, $new); # atomic CAS, returns bool
my $old = $pool->cmpxchg($idx, $old, $new); # atomic CAS, returns old value
my $old = $pool->xchg($idx, $val); # atomic exchange, returns old
my $val = $pool->add($idx, $delta); # atomic add, returns new value
my $val = $pool->incr($idx); # atomic increment
my $val = $pool->decr($idx); # atomic decrement
For Str variant:
my $max = $pool->max_len; # maximum string length
Raw Pointers
my $ptr = $pool->ptr($idx); # raw C pointer to slot data (UV)
my $ptr = $pool->data_ptr; # pointer to start of data section
"ptr" returns the memory address of a slot's data as an unsigned
integer. Use with FFI::Platypus, OpenGL "_c" functions, or XS code that
needs a "void*".
"data_ptr" returns the base of the contiguous data region. Slots are
laid out as "data_ptr + idx * elem_size".
Warning: The returned pointer becomes dangling if the pool object is
destroyed. Do not use after the pool goes out of scope.
Zero-Copy Access
my $sv = $pool->slot_sv($idx); # SV backed by slot memory
Returns a read-only scalar whose PV points directly into the shared
memory slot. Reading the scalar reads the slot with no "memcpy". Useful
for large slots where avoiding copy matters.
The scalar holds a reference to the pool object, keeping it alive for as
long as the scalar (or any copy of it) is live. However, the scalar
still reflects the current contents of the slot: if the slot is free()d
and later re-allocated, reads will see the new data. To modify the slot,
use set().
Status
my $ok = $pool->is_allocated($idx);
my $cap = $pool->capacity;
my $esz = $pool->elem_size;
my $n = $pool->used; # allocated count
my $n = $pool->available; # free count
my $pid = $pool->owner($idx); # PID of allocator
Recovery
my $n = $pool->recover_stale; # free slots owned by dead PIDs
$pool->reset; # free all slots (exclusive access only)
Guards
my ($idx, $guard) = $pool->alloc_guard; # auto-free on scope exit
my ($idx, $guard) = $pool->alloc_guard($timeout);
my ($idx, $guard) = $pool->try_alloc_guard; # non-blocking
Convenience
my $idx = $pool->alloc_set($val); # alloc + set
my $idx = $pool->alloc_set($val, $timeout); # with timeout
my $idx = $pool->try_alloc_set($val); # non-blocking
$pool->each_allocated(sub { my $idx = shift; ... });
Common Methods
my $p = $pool->path; # backing file (undef if anon)
my $fd = $pool->memfd; # memfd fd (-1 if not memfd)
$pool->sync; # msync to disk
$pool->unlink; # remove backing file
my $s = $pool->stats; # diagnostic hashref
eventfd Integration
my $fd = $pool->eventfd; # create eventfd
$pool->eventfd_set($fd); # use existing fd
my $fd = $pool->fileno; # current eventfd (-1 if none)
$pool->notify; # signal eventfd
my $n = $pool->eventfd_consume; # drain counter
STATS
stats() returns a hashref with diagnostic counters. All values are
approximate under concurrency.
"capacity" â total slot count (immutable)
"elem_size" â bytes per slot (immutable)
"used" â currently allocated slot count
"available" â currently free slot count ("capacity - used")
"waiters" â processes currently blocked on "alloc"
"mmap_size" â total mmap region size in bytes
"allocs" â cumulative successful allocations
"frees" â cumulative frees (including stale recovery)
"waits" â "alloc" calls that entered the retry loop
"timeouts" â "alloc" calls that timed out
"recoveries" â slots freed by "recover_stale"
SECURITY
( run in 0.668 second using v1.01-cache-2.11-cpan-39bf76dae61 )