mmeyer2k/semlock

semaphores + closures

1.0.1 2022-05-25 00:05 UTC

This package is auto-updated.

Last update: 2024-04-25 04:21:18 UTC


README

A clean way to wrap semaphores around closures to perform exclusive execution of code. Semlock also allows for naming semaphones with descriptive strings instead of ints.

Read my blog posting about this tool here:

https://mmeyer2k.github.io/posts/php-exclusive-execution-closure-semaphore

Install

composer require mmeyer2k/semlock

Use

Basic Usage

\mmeyer2k\SemLock::synchronize('some_key', function () {
    $x = get_number_from_database();

    $x++;

    sleep(5);

    save_number_to_database($x);
});

Handling return values

The semlock library will pass return values from the closure back to the calling context.

$returned = \mmeyer2k\SemLock::synchronize('some_key', function () {
    return 'something';
});

# $returned === 'something'