semaphore->acquire(); if ($this->locked) { throw new \Error("Cannot use a semaphore with more than a single lock"); } $this->locked = true; return new Lock(function () use ($lock): void { $this->locked = false; $lock->release(); }); } }