Skip to content

💎 A semaphore wrapper for closures

License

Notifications You must be signed in to change notification settings

mmeyer2k/semlock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

💎semlock

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'