本文整理汇总了PHP中sem_acquire函数的典型用法代码示例。如果您正苦于以下问题:PHP sem_acquire函数的具体用法?PHP sem_acquire怎么用?PHP sem_acquire使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sem_acquire函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: acquire
/**
* get a lock
* @throws \Exception
*/
public function acquire()
{
if (!sem_acquire($this->lock_id)) {
throw new \RuntimeException('Cannot acquire semaphore: ' . $this->lock_id);
}
$this->locked = true;
}
示例2: getNextValueByShareMemory
/**
* 通过本机共享内存件来生成一个auto_increment序列
*
* 序列类似MySQL的auto_increment
*
* @access private
* @param void
* @return mixed
*/
function getNextValueByShareMemory()
{
$addr = '127.0.0.1';
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$addr = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (!empty($_SERVER['SERVER_ADDR'])) {
$addr = $_SERVER['SERVER_ADDR'];
}
$skey = 'global_serial_generator_seed_' . $addr;
$ikey = crc32($skey);
$sem = $shm = null;
$retry_times = 1;
do {
$sem = sem_get($ikey, 1, 0777);
$shm = shm_attach($ikey, 128, 0777);
if (is_resource($sem) && is_resource($shm)) {
break;
}
$cmd = "ipcrm -M 0x00000000; ipcrm -S 0x00000000; ipcrm -M {$ikey} ; ipcrm -S {$ikey}";
$last_line = exec($cmd, $output, $retval);
} while ($retry_times-- > 0);
if (!sem_acquire($sem)) {
return false;
}
$next_value = false;
if (shm_has_var($shm, $ikey)) {
shm_put_var($shm, $ikey, $next_value = shm_get_var($shm, $ikey) + 1);
} else {
shm_put_var($shm, $ikey, $next_value = 1);
}
$shm && shm_detach($shm);
$sem && sem_release($sem);
return $next_value;
}
示例3: acquire
public function acquire()
{
if (!sem_acquire($this->_semaphore)) {
throw new Sera_Exception("failed to acquire semaphore");
}
$this->_acquired = true;
}
示例4: acquire
/**
* Acquire a semaphore - blocks (if necessary) until the semaphore can be acquired.
* A process attempting to acquire a semaphore which it has already acquired will
* block forever if acquiring the semaphore would cause its max_acquire value to
* be exceeded.
*
* @return bool success
* @throws io.IOException
*/
public function acquire()
{
if (FALSE === sem_acquire($this->_hdl)) {
throw new IOException('Could not acquire semaphore ' . $this->key);
}
return TRUE;
}
示例5: session_value
function session_value($name, $index)
{
global $shm_key, $shm_var, $sem_id;
switch ($index) {
case 'config':
$shm_size = 859;
break;
case 'ipdata':
$shm_size = 30050;
break;
default:
$shm_size = 0;
}
sem_acquire($sem_id['shlock']);
$shm_id = shmop_open($shm_key[$index], 'c', 0644, $shm_size);
if ($name == 'update') {
$shm_data = serialize($shm_var[$index]);
shmop_write($shm_id, str_pad($shm_data, $shm_size, "", STR_PAD_RIGHT), 0);
} else {
$shm_data = shmop_read($shm_id, 0, $shm_size);
$shm_var[$index] = @unserialize($shm_data);
}
shmop_close($shm_id);
sem_release($sem_id['shlock']);
}
示例6: generate
/**
* @return IdValue
*/
public function generate()
{
$timestamp = $this->generateTimestamp();
// Acquire semaphore
$semaphore = sem_get($this->semaphoreId);
sem_acquire($semaphore);
// Attach shared memory
$memory = shm_attach(self::SHM_KEY);
$sequence = 0;
if (!is_null($this->lastTimestamp) && $timestamp->equals($this->lastTimestamp)) {
// Get
$sequence = shm_get_var($memory, self::SHM_SEQUENCE) + 1 & $this->config->getSequenceMask();
// Increment sequence
shm_put_var($memory, self::SHM_SEQUENCE, $sequence);
if ($sequence === 0) {
usleep(1000);
$timestamp = $this->generateTimestamp();
}
} else {
// Reset sequence if timestamp is different from last one.
$sequence = 0;
shm_put_var($memory, self::SHM_SEQUENCE, $sequence);
}
// Detach shared memory
shm_detach($memory);
// Release semaphore
sem_release($semaphore);
// Update lastTimestamp
$this->lastTimestamp = $timestamp;
return new IdValue($timestamp, $this->regionId, $this->serverId, $sequence, $this->calculate($timestamp, $this->regionId, $this->serverId, $sequence));
}
示例7: acquire
/**
* Try to acquire a semaphore.
*
* NOTE: sem_acquire will wait forever if a semaphore is not released
* for any reason. Therefor as of PHP version 5.6.1 the $nowait
* flag was introduced which could is used here. Further work may
* lead to an implementation which runs an interruptable version
* of sem_acquire without using the $nowait flag, e.g. a
* TimeoutThread
*
* @param $timeout timeout for acquiring a semaphore
* @throws TimerException
* @throws SemaphoreException
* @see Timer
*/
public function acquire($timeout = 30, $tries = 3)
{
// set and start the timer
$timer = null;
if (strncmp(gettype($timeout), "integer", 7) == 0 && $timeout > 0) {
$timer = new Timer($timeout);
}
if ($timer === null) {
$this->log(__METHOD__ . ": %.", array(new TimerException("creation failed")));
throw new TimerException("creation failed");
}
$timer->start();
// now try to acquire a semaphore
$acquired = false;
$try = 1;
while (!is_null($this->semaphore->get_sem_res()) && $acquired === false && $timer !== null && $try <= $this->semaphore_max_try) {
// use sem_acquire with $nowait flag to append a timer
// NOTE: warnings should be suppressed here any error should throw an
// exception
$acquired = @sem_acquire($this->semaphore->get_sem_res(), true);
// retart the timer
if ($timer->get() == 0 && $timer->get_timed_out()) {
$this->log(__METHOD__ . ": Acquiring. Try #%. Timer timed out.", array($try));
$try++;
$timer->start();
}
}
if ($acquired === false) {
$this->log(__METHOD__ . ": %.", array(new SemaphoreException("acquisition failed", 0)));
throw new SemaphoreException("acquisition failed", 0);
}
return $acquired;
}
示例8: acquire
/**
* Acquire a semaphore - blocks (if necessary) until the semaphore can be acquired.
* A process attempting to acquire a semaphore which it has already acquired will
* block forever if acquiring the semaphore would cause its max_acquire value to
* be exceeded.
*
* @return bool success
* @throws io.IOException
*/
public function acquire()
{
if (false === sem_acquire($this->_hdl)) {
throw new \io\IOException('Could not acquire semaphore ' . $this->key);
}
return true;
}
示例9: put
public static function put($killID, $raw)
{
$file = static::getFile($killID, true);
$sem = sem_get(5632);
// kmdb is 5632 on a phone
if (!sem_acquire($sem)) {
throw new Exception('Unable to obtain kmdb semaphore');
}
// Thread safe from here until sem_release
if (!file_exists($file)) {
$kills = array();
} else {
$contents = file_get_contents($file);
$deflated = gzdecode($contents);
$kills = unserialize($deflated);
$contents = null;
}
if (!isset($kills["{$killID}"])) {
$kills["{$killID}"] = $raw;
$contents = serialize($kills);
$compressed = gzencode($contents);
file_put_contents($file, $compressed, LOCK_EX);
}
sem_release($sem);
}
示例10: registerChildPID
/**
* Registers the PID of a child-process
*
* @param int The PID
*/
public function registerChildPID($pid)
{
$sem_key = sem_get($this->crawler_uniqid);
sem_acquire($sem_key);
file_put_contents($this->working_directory . "pids", $pid . "\n", FILE_APPEND);
sem_release($sem_key);
}
示例11: get
/**
* @param $key
* @param int $size
* @return mixed
*/
public function get($key, $size = 10000)
{
$result = $this->attach($key, $size);
sem_acquire($result['mutex']);
$value = @shm_get_var($result['shm'], $key);
sem_release($result['mutex']);
return $value;
}
示例12: readShared
public function readShared()
{
sem_acquire($this->sem_id);
$data = shmop_read($this->shm_id, 0, $this->size);
$data = trim($data);
$data = unserialize($data);
return $data;
}
示例13: MMC_Lock
function MMC_Lock($name)
{
if (false && function_exists('sem_get') && function_exists('sem_acquire')) {
$semid = sem_get(crc32(MMC_Name($name)), 1, 0644 | IPC_CREAT, true);
$GLOBALS['APC_SEMAPHORE_RESOURCES'][MMC_Name($name)] = $semid;
return sem_acquire($semid);
}
}
示例14: acquire
/**
* @return bool
*/
public function acquire()
{
$result = sem_acquire($this->semaphore);
if (true === $result) {
$this->acquired = true;
}
return $result;
}
示例15: lock
public function lock()
{
if (PHP_OS !== 'Linux') {
return;
}
sem_acquire($this->getSemaphoreId());
$this->locked = true;
}