php / pecl-system-sync

Synchronization objects
http://pecl.php.net/package/sync
MIT License
16 stars 11 forks source link

api abstraction problem #7

Closed determin1st closed 1 year ago

determin1st commented 1 year ago

there is no method to check the state of the lock, either SyncMutex or SyncSemaphore - is it locked or not? no way to check, so the abstraction with SyncEvent is the simplest way to go, but semaphore allows multiple instances which makes checking unreliable

for example, set method

$sem->lock
!$event->wait(0) && $event->fire

and clear method

$sem->unlock($n)
$n === $lastPossible && $event->reset

will break when

$sem->unlock($n) # INSTANCE 1
$sem->lock # INSTANCE 2
!$event->wait(0) && $event->fire # INTANCE 2
$n === $lastPossible && $event->reset # INSTANCE 1

lock is set but event is cleared, so the state is incorrect. couldn't it be simplified with get() method?

$n = $sem->get();# SyncSemaphore $x = $mutex->get();# SyncMutex

determin1st commented 1 year ago

ooke, i think i found a solution to SyncSemaphore with $initialval greater than 1, - have to wrap such a lock into object with another guard lock and shared memory which will keep lock counter.. so its solveable.

anyway, may hung as a feature request for getter methods