Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KOJO-185 | Start of Mutexing userspace API #108

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Api/V1/Synchronization/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Kojo\Api\V1\Synchronization;

use Neighborhoods\Kojo\Semaphore;

class Service implements ServiceInterface
{
use Semaphore\Resource\Factory\AwareTrait;

// TODO: consider "namespacing" userspace mutex IDs to make sure there aren't any collisions
// with kojospace mutex IDs
public function tryAcquireMutex(string $id) : bool
{
try {
$didAcquireMutex = $this->_getSemaphoreResource($id)->testAndSetLock();
} catch (\Throwable $throwable) {
$didAcquireMutex = false;

// log
}

return $didAcquireMutex;
}

public function hasMutex(string $id) : bool
{
return $this->_getSemaphoreResource($id)->hasLock();
}

public function releaseMutex(string $id) : ServiceInterface
{
$this->_getSemaphoreResource($id)->releaseLock();

return $this;
}
}
13 changes: 13 additions & 0 deletions src/Api/V1/Synchronization/ServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Kojo\Api\V1\Synchronization;

interface ServiceInterface
{
public function tryAcquireMutex(string $id) : bool;

public function hasMutex(string $id) : bool;

public function releaseMutex(string $id) : ServiceInterface;
}