Skip to content

Gets a semaphore and attempts to acquire to prevent code from running more than one time.

Notifications You must be signed in to change notification settings

campsoftware/semaphoreCheck-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 

Repository files navigation

semaphoreCheck-php

Summary

Gets a semaphore and attempts to acquire to prevent code from running more than one time.

  • Aborts if the semaphore cannot be retrieved.
  • Aborts if the semaphore in use.
  • The key must be an integer.

Code

// Semaphore Check
$semaphoreKey = 42 );
$semaphore = sem_get( $semaphoreKey, 1, 0666, 1 );
if ( $semaphore === false ) {
    $response[ 'Error' ] = 'SEMAPHORE NOT AVAILABLE';
    return;
} else {
    // Pass true to return false if in use. Omit to pause until not blocked.
    if ( sem_acquire( $semaphore, true ) === false ) {
        $response[ 'Error' ] = 'SEMAPHORE IN USE';
        return;
    }
}
// Semaphore Release Exclusive Control, but will auto release.
$semaphoreReleased = sem_release( $semaphore );

About

Gets a semaphore and attempts to acquire to prevent code from running more than one time.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published