当前位置: 首页>>代码示例>>PHP>>正文


PHP sem_acquire函数代码示例

本文整理汇总了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;
 }
开发者ID:jzxyouok,项目名称:simple-fork-php,代码行数:11,代码来源:Semaphore.php

示例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;
}
开发者ID:fixbugs,项目名称:pubfunc-php,代码行数:43,代码来源:pub.serial.php

示例3: acquire

 public function acquire()
 {
     if (!sem_acquire($this->_semaphore)) {
         throw new Sera_Exception("failed to acquire semaphore");
     }
     $this->_acquired = true;
 }
开发者ID:99designs,项目名称:sera,代码行数:7,代码来源:Semaphore.php

示例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;
 }
开发者ID:melogamepay,项目名称:xp-framework,代码行数:16,代码来源:Semaphore.class.php

示例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']);
}
开发者ID:phateio,项目名称:php-radio-kernel,代码行数:25,代码来源:shmop.inc.php

示例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));
 }
开发者ID:ada-u,项目名称:chocoflake,代码行数:34,代码来源:IdWorkerOnSharedMemory.php

示例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;
 }
开发者ID:marcbredt,项目名称:heili,代码行数:48,代码来源:semaphorehandler.class.php

示例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;
 }
开发者ID:xp-framework,项目名称:core,代码行数:16,代码来源:Semaphore.class.php

示例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);
 }
开发者ID:Nord001,项目名称:zKillboard,代码行数:25,代码来源:Killmail.php

示例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);
 }
开发者ID:haythameyd,项目名称:socialfp,代码行数:12,代码来源:PHPCrawlerProcessHandler.class.php

示例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;
 }
开发者ID:bes89,项目名称:sharedmemory,代码行数:13,代码来源:SharedMemory.php

示例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;
 }
开发者ID:hduwzy,项目名称:multi_process,代码行数:8,代码来源:Shm.php

示例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);
    }
}
开发者ID:TinoDidriksen,项目名称:pjj-chats,代码行数:8,代码来源:apc.php

示例14: acquire

 /**
  * @return bool
  */
 public function acquire()
 {
     $result = sem_acquire($this->semaphore);
     if (true === $result) {
         $this->acquired = true;
     }
     return $result;
 }
开发者ID:alexanderc,项目名称:threadator,代码行数:11,代码来源:Mutex.php

示例15: lock

 public function lock()
 {
     if (PHP_OS !== 'Linux') {
         return;
     }
     sem_acquire($this->getSemaphoreId());
     $this->locked = true;
 }
开发者ID:jost125,项目名称:koala-parallel,代码行数:8,代码来源:BinarySemaphore.php


注:本文中的sem_acquire函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。