src/Semaphore.php
public function create($semKey = Semaphore::SEM_FTOK_KEY, $maxAcquire = 1, $perms=0666) {
		if (is_resource($this->semaphore)) {
			throw new SemaphoreException('Semaphore has already been created.');
		}

		if (!is_int($maxAcquire)) {
			$maxAcquire = 1;
		}
		$perms=(int)$perms;
		if ($perms < 0 || $perms > 0777) {
			$perms = 0666;
		}

		// randomly generate semaphore, without collision
		if ($semKey == Semaphore::SEM_RAND_KEY) {
			$retries = 5;
			mt_srand((int)(microtime(true)*10000));
		} else {
			$retries = 1;
		}
		// try to generate a semaphore
		while (!is_resource($this->semaphore) && $retries > 0) {
			$retries--;
			// generate a semKey
			if (!is_int($semKey)) {
				if ($semKey == Semaphore::SEM_RAND_KEY) {
					$this->semKey = mt_rand(Semaphore::SEM_MIN_INT, Semaphore::SEM_MAX_INT);
				} else {
					$this->semKey = ftok(__FILE__, 's');
				}
			} else {
				$this->semKey = $semKey;
			}
			// check the range
			if($this->semKey < Semaphore::SEM_MIN_INT || $this->semKey > Semaphore::SEM_MAX_INT) {
				$this->semKey = ftok(__FILE__, 's');
			}
			$this->semaphore = sem_get($this->semKey, $maxAcquire, $perms, 0);
		}
		if (!is_resource($this->semaphore)) {
			$this->semaphore = NULL;
			$this->semKey = NULL;
			throw new SemaphoreException('Cannot create the semaphore.');
		}

		return $this;
	}
public function create($semKey = Semaphore::SEM_FTOK_KEY, $maxAcquire = 1, $perms=0666) {
		if (is_resource($this->semaphore)) {
			throw new SemaphoreException('Semaphore has already been created.');
		}

		if (!is_int($maxAcquire)) {
			$maxAcquire = 1;
		}
		$perms=(int)$perms;
		if ($perms < 0 || $perms > 0777) {
			$perms = 0666;
		}

		// randomly generate semaphore, without collision
		if ($semKey == Semaphore::SEM_RAND_KEY) {
			$retries = 5;
			mt_srand((int)(microtime(true)*10000));
		} else {
			$retries = 1;
		}
		// try to generate a semaphore
		while (!is_resource($this->semaphore) && $retries > 0) {
			$retries--;
			// generate a semKey
			if (!is_int($semKey)) {
				if ($semKey == Semaphore::SEM_RAND_KEY) {
					$this->semKey = mt_rand(Semaphore::SEM_MIN_INT, Semaphore::SEM_MAX_INT);
				} else {
					$this->semKey = ftok(__FILE__, 's');
				}
			} else {
				$this->semKey = $semKey;
			}
			// check the range
			if($this->semKey < Semaphore::SEM_MIN_INT || $this->semKey > Semaphore::SEM_MAX_INT) {
				$this->semKey = ftok(__FILE__, 's');
			}
			$this->semaphore = sem_get($this->semKey, $maxAcquire, $perms, 0);
		}
		if (!is_resource($this->semaphore)) {
			$this->semaphore = NULL;
			$this->semKey = NULL;
			throw new SemaphoreException('Cannot create the semaphore.');
		}

		return $this;
	}
Create a semaphore
@param string $semKey the key of the semaphore - use a specific number or Semaphore::SEM_RAND_KEY or Semaphore::SEM_FTOK_KEY
@param int $maxAcquire the maximum number of processes, that can acquire the semaphore
@param int $perms the unix permissions for (user,group,others) - valid range from 0 to 0777
@throws SemaphoreException
@return \QXS\WorkerPool\Semaphore the current object