99999999) { self::$generatedKey = 0; } return $promiseKey; } /** * Promise constructor. * @param int $key */ public function __construct(int $key) { $this->key = $key; $this->value = null; $this->shm = shm_attach(Runtime::getSharedMemoryKey()); } /** * @return bool */ public function isResolved(): bool { $this->attempt(); return !is_null($this->value); } /** * @return bool */ public function isVoid(): bool { return $this->getValue() === self::RESPONSE_NONE; } /** * @return bool */ public function isError(): bool { return $this->getValue() === self::RESPONSE_ERROR; } /** * @return mixed|null */ public function getValue() { return $this->isResolved() ? $this->value : null; } /** * @return $this */ public function resolve(): self { /* * Actually block execution until a value is written to * the expected memory location of this Promise. */ while (!$this->isResolved()) { usleep(50); } return $this; } /** * */ public function __destruct() { /* * Clean up our mess - the variable that we stored in the * shared memory block - and detach from the block. * Note: this destructor is only called after the * garbage collector has noticed that there are no more * references to this Promise instance. */ if (Runtime::isChild()) { return; } if (shm_has_var($this->shm, $this->key)) { shm_remove_var($this->shm, $this->key); } shm_detach($this->shm); } /** * @return $this */ private function attempt(): self { if (shm_has_var($this->shm, $this->key)) { $this->value = shm_get_var($this->shm, $this->key); } return $this; } }