예제 #1
0
파일: Poller.php 프로젝트: rickb838/scalr
 function __construct()
 {
     $this->logger = Logger::getLogger(__CLASS__);
     $this->db = $this->getContainer()->adodb;
     $this->lastCleanup = new Scalr_System_Ipc_Shm(array("name" => "scalr.cronjob.poller.lastCleanup"));
     $this->cleanupSem = sem_get(Scalr_System_OS::getInstance()->tok("scalr.cronjob.poller.cleanupSem"));
 }
예제 #2
0
 /**
  * Provides Mutex factories.
  *
  * @return callable[][] The mutex factories.
  */
 public function provideMutexFactories()
 {
     $cases = ["NoMutex" => [function () {
         return new NoMutex();
     }], "TransactionalMutex" => [function () {
         $pdo = new \PDO("sqlite::memory:");
         $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
         return new TransactionalMutex($pdo);
     }], "FlockMutex" => [function () {
         vfsStream::setup("test");
         return new FlockMutex(fopen(vfsStream::url("test/lock"), "w"));
     }], "SemaphoreMutex" => [function () {
         return new SemaphoreMutex(sem_get(ftok(__FILE__, "a")));
     }], "SpinlockMutex" => [function () {
         $mock = $this->getMockForAbstractClass(SpinlockMutex::class, ["test"]);
         $mock->expects($this->any())->method("acquire")->willReturn(true);
         $mock->expects($this->any())->method("release")->willReturn(true);
         return $mock;
     }], "LockMutex" => [function () {
         $mock = $this->getMockForAbstractClass(LockMutex::class);
         $mock->expects($this->any())->method("lock")->willReturn(true);
         $mock->expects($this->any())->method("unlock")->willReturn(true);
         return $mock;
     }]];
     if (getenv("MEMCACHE_HOST")) {
         $cases["MemcacheMutex"] = [function () {
             $memcache = new Memcache();
             $memcache->connect(getenv("MEMCACHE_HOST"));
             return new MemcacheMutex("test", $memcache);
         }];
         $cases["MemcachedMutex"] = [function () {
             $memcache = new Memcached();
             $memcache->addServer(getenv("MEMCACHE_HOST"), 11211);
             return new MemcachedMutex("test", $memcache);
         }];
     }
     if (getenv("REDIS_URIS")) {
         $uris = explode(",", getenv("REDIS_URIS"));
         $cases["PredisMutex"] = [function () use($uris) {
             $clients = array_map(function ($uri) {
                 return new Client($uri);
             }, $uris);
             return new PredisMutex($clients, "test");
         }];
         $cases["PHPRedisMutex"] = [function () use($uris) {
             $apis = array_map(function ($uri) {
                 $redis = new Redis();
                 $uri = parse_url($uri);
                 if (!empty($uri["port"])) {
                     $redis->connect($uri["host"], $uri["port"]);
                 } else {
                     $redis->connect($uri["host"]);
                 }
                 return $redis;
             }, $uris);
             return new PHPRedisMutex($apis, "test");
         }];
     }
     return $cases;
 }
 /**
  * Tests removing semaphore fails.
  *
  * @test
  * @expectedException bandwidthThrottle\tokenBucket\storage\StorageException
  * @expectedExceptionMessage Could not remove semaphore.
  */
 public function testfailRemovingSemaphore()
 {
     $key = ftok(__FILE__, "a");
     $storage = new IPCStorage($key);
     sem_remove(sem_get($key));
     @$storage->remove();
 }
예제 #4
0
 public function __construct($key)
 {
     if (!is_long($key)) {
         E("传入了非法的信号量key");
     }
     $this->ipc_signal = sem_get($key);
 }
예제 #5
0
 /**
  * 获得SemFd
  */
 protected static function getSemFd()
 {
     if (!self::$semFd && extension_loaded('sysvsem')) {
         self::$semFd = sem_get(self::SEM_KEY);
     }
     return self::$semFd;
 }
예제 #6
0
 private function getSemaphoreId()
 {
     if ($this->semaphoreId === null) {
         $this->semaphoreId = sem_get($this->key);
     }
     return $this->semaphoreId;
 }
예제 #7
0
 /**
  * @param $config
  * @key string [name]
  * @key string [key]
  * @key array [items]
  */
 function __construct($config)
 {
     $this->logger = Logger::getLogger(__CLASS__);
     $this->initialConfig = $config;
     $this->shm = new Scalr_System_Ipc_Shm($config);
     $key = $this->shm->key + 8;
     $this->logger->debug(sprintf("Get semaphore (key: 0x%08x)", $key));
     $this->sem = sem_get($key, 1, 0666, true);
     if (!$this->sem) {
         throw new Scalr_System_Ipc_Exception("Cannot sem_get (key: {$key})");
     }
     if (!sem_acquire($this->sem)) {
         throw new Scalr_System_Ipc_Exception("Cannot acquire semaphore");
     }
     try {
         $meta = $this->getMeta();
         if ($meta === null) {
             $this->clear0();
         }
         sem_release($this->sem);
     } catch (Exception $e) {
         sem_release($this->sem);
         throw $e;
     }
     if ($config["items"]) {
         foreach ($config["items"] as $item) {
             $this->add($item);
         }
     }
 }
예제 #8
0
 /**
  * Wake up from a sleep
  */
 function __wakeup()
 {
     $this->id = sem_get($this->key);
     shm_attach($this->id);
     $this->refreshMemoryVarList();
     shm_put_var($this->id, 1, shm_get_var($this->id, 1) + 1);
 }
예제 #9
0
/**
 * 通过本机共享内存件来生成一个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;
}
 /**
  * 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
0
 /**
  * @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));
 }
예제 #12
0
 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);
 }
예제 #13
0
 public function __construct($path, $currentArticleFile, $debug_delete_file)
 {
     $this->path = $path;
     $this->debug_delete_file = $debug_delete_file;
     $this->currentArticleFile = $currentArticleFile;
     $this->semaphoreID = sem_get(OAIRECORDFILES);
 }
예제 #14
0
 /**
  *	Constructor.
  *
  *	@see	BasicMutex::__construct()
  */
 public function __construct($name)
 {
     $this->key = abs(crc32($name));
     $this->sem = sem_get($this->key, 1, 0666, 1);
     if ($this->sem === false) {
         throw new MutexException('Error geting semaphore');
     }
 }
예제 #15
0
 protected function initializeShm()
 {
     if (null !== $this->shmHandle) {
         return;
     }
     $this->shmHandle = shm_attach($this->getId(), 60 * 1024);
     $this->semHandle = sem_get($this->getId());
 }
예제 #16
0
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);
    }
}
예제 #17
0
 /**
  * Create shared memory block
  */
 private function attach()
 {
     $this->__key = ftok($this->__path, $this->__proj_id);
     $this->__shm = shm_attach($this->__key, $this->__size);
     //allocate shared memory
     $this->__mutex = sem_get($this->__key, 1);
     //create mutex with same key
 }
function pleac_Sharing_Variables_in_Different_Processes()
{
    // sharetest - test shared variables across forks
    $SHM_KEY = ftok(__FILE__, chr(1));
    $handle = sem_get($SHM_KEY);
    $buffer = shm_attach($handle, 1024);
    // The original recipe has an INT signal handler here. However, it
    // causes erratic behavior with PHP, and PHP seems to do the right
    // thing without it.
    for ($i = 0; $i < 10; $i++) {
        $child = pcntl_fork();
        if ($child == -1) {
            die('cannot fork');
        } elseif ($child) {
            $kids[] = $child;
            // in case we care about their pids
        } else {
            squabble();
            exit;
        }
    }
    while (true) {
        print 'Buffer is ' . shm_get_var($buffer, 1) . "\n";
        sleep(1);
    }
    die('Not reached');
    function squabble()
    {
        global $handle;
        global $buffer;
        $i = 0;
        $pid = getmypid();
        while (true) {
            if (preg_match("/^{$pid}\\b/", shm_get_var($buffer, 1))) {
                continue;
            }
            sem_acquire($handle);
            $i++;
            shm_put_var($buffer, 1, "{$pid} {$i}");
            sem_release($handle);
        }
    }
    // Buffer is 14357 1
    // Buffer is 14355 3
    // Buffer is 14355 4
    // Buffer is 14354 5
    // Buffer is 14353 6
    // Buffer is 14351 8
    // Buffer is 14351 9
    // Buffer is 14350 10
    // Buffer is 14348 11
    // Buffer is 14348 12
    // Buffer is 14357 10
    // Buffer is 14357 11
    // Buffer is 14355 13
    // ...
}
예제 #19
0
 function acquire($cacheId, $key, $block = true)
 {
     if (!$block) {
         throw new \InvalidArgumentException("Non-blocking lock not supported by Sempahore");
     }
     $id = $this->getId($cacheId, $key);
     $this->sem[$id] = $sem = sem_get($id);
     sem_acquire($sem);
 }
예제 #20
0
 /**
  * Constructor.
  *
  * @param string name File name to use to generate the semaphore key.
  */
 public function __construct($name)
 {
     $this->count = 0;
     $this->semKey = ftok($name, 'R');
     $this->semId = sem_get($this->semKey);
     if (!is_resource($this->semId)) {
         throw new \Exception('Unable to create semaphore');
     }
 }
예제 #21
0
 /**
  * @param $key
  * @param $size
  * @throws InvalidArgumentException
  * @return array
  */
 protected function attach($key, $size)
 {
     if (!array_key_exists($key, $this->values)) {
         if (!is_long($key)) {
             throw new InvalidArgumentException(sprintf('Expected type long for "key" but "%s" given.', gettype($key)));
         }
         $this->values[$key] = array('shm' => shm_attach($key, $size), 'mutex' => sem_get($key, 1));
     }
     return $this->values[$key];
 }
예제 #22
0
 /**
  * @param mixed $identity
  * @param int $type
  * @throws \RuntimeException
  */
 public function __construct($identity, $type = self::IDX)
 {
     $this->identity = $identity;
     $this->type = $type;
     $this->generateIdentifier();
     $this->generateSystemVIPCKey();
     if (false === ($this->semaphore = @sem_get($this->systemVIPCKey))) {
         throw new \RuntimeException("Unable to get semaphore");
     }
 }
예제 #23
0
 public function get($key)
 {
     try {
         if (!isset($this->pool[$key])) {
             $this->pool[$key] = sem_get($key, 1, HESPER_IPC_PERMS, false);
         }
         return sem_acquire($this->pool[$key]);
     } catch (BaseException $e) {
         return null;
     }
 }
예제 #24
0
 public function attach()
 {
     try {
         $this->shm_id = shm_attach($this->shm_key, $this->shm_size);
         // 初始化信号量
         $this->sem = sem_get($this->sem_key, 1);
     } catch (Exception $e) {
         return FALSE;
     }
     return TRUE;
 }
예제 #25
0
function lock_que_on($callback)
{
    $mutex = sem_get(ftok(__FILE__, 'g'), 1);
    sem_acquire($mutex);
    try {
        $res = call_user_func($callback);
    } catch (Exception $e) {
    }
    sem_release($mutex);
    return $res;
}
 /**
  * Updates the status of the crawler
  *
  * @param PHPCrawlerDocumentInfo $PageInfo          The PHPCrawlerDocumentInfo-object of the last received document
  *                                                  or NULL if no document was received.
  * @param int                    $abort_reason      One of the PHPCrawlerAbortReasons::ABORTREASON-constants if the crawling-process
  *                                                  should get aborted, otherwise NULL
  * @param string                 $first_content_url The first URL some content was found in (or NULL if no content was found so far).
  */
 public function updateCrawlerStatus($PageInfo, $abort_reason = null, $first_content_url = null, $last_request_time = null)
 {
     PHPCrawlerBenchmark::start("updating_crawler_status");
     // Set semaphore/lock if
     if ($this->lock_status_updates == true) {
         $sem_key = sem_get($this->crawler_uniqid);
         sem_acquire($sem_key);
     }
     // Get current Status
     $crawler_status = $this->getCrawlerStatus();
     // Update status
     if ($PageInfo != null) {
         // Increase number of followed links
         $crawler_status->links_followed++;
         // Increase documents_received-counter
         if ($PageInfo->received == true) {
             $crawler_status->documents_received++;
         }
         // Increase bytes-counter
         $crawler_status->bytes_received += $PageInfo->bytes_received + $PageInfo->header_bytes_received;
         // Benchmarks
         if ($PageInfo->error_occured == false) {
             // server connect time
             $crawler_status->sum_server_connect_time += $PageInfo->server_connect_time;
             $crawler_status->sum_server_connects++;
             // server response time
             $crawler_status->sum_server_response_time += $PageInfo->server_response_time;
             $crawler_status->sum_server_responses++;
             // data transfer time
             $crawler_status->sum_data_transfer_time += $PageInfo->data_transfer_time;
             // unbuffered bytes read
             $crawler_status->unbuffered_bytes_read += $PageInfo->unbuffered_bytes_read;
         }
     }
     // Set abortreason
     if ($abort_reason !== null) {
         $crawler_status->abort_reason = $abort_reason;
     }
     // Set first_content_url
     if ($first_content_url !== null) {
         $crawler_status->first_content_url = $first_content_url;
     }
     // Set last request-time
     if ($last_request_time !== null) {
         $crawler_status->last_request_time = $last_request_time;
     }
     // Write crawler-status back
     $this->setCrawlerStatus($crawler_status);
     // Remove semaphore/lock
     if ($this->lock_status_updates == true) {
         sem_release($sem_key);
     }
     PHPCrawlerBenchmark::stop("updating_crawler_status");
 }
예제 #27
0
 public function __construct()
 {
     $shmkey = ftok(__FILE__, 't');
     $this->shmId = shmop_open($shmkey, "c", 0644, $this->memSize);
     $this->maxQSize = $this->memSize / $this->blockSize;
     // 申請一个信号量
     $this->semId = sem_get($shmkey, 1);
     sem_acquire($this->semId);
     // 申请进入临界区
     $this->init();
 }
예제 #28
0
 private static function get_exclusive_by_semaphore()
 {
     if (JxBotExclusion::$sem = sem_get(ftok(__FILE__, "j"), 1)) {
         if (!sem_acquire(JxBotExclusion::$sem, true)) {
             return false;
         }
         return true;
     }
     throw new Exception("Couldn't acquire System V semaphore.");
     return false;
 }
예제 #29
0
 /**
  * Constructor.
  *
  * @param int $key      The key
  * @param int $acquires The number of processes that can acquire the semaphore simultaneously
  *
  * @throws \InvalidArgumentException When $acquires less than 1
  * @throws \RuntimeException         When unable to get semaphore
  */
 public function __construct($key, $acquires = 1)
 {
     $acquires = (int) $acquires;
     if ($acquires < 1) {
         throw new \InvalidArgumentException(sprintf('Number of acquires (%d) cannot be less than 1.', $acquires));
     }
     $this->handle = sem_get((int) $key, (int) $acquires);
     if (!is_resource($this->handle)) {
         throw new \RuntimeException(sprintf('Unable to get semaphore by key "%s".', $key));
     }
     $this->locked = false;
 }
예제 #30
0
 /**
  * On any *nix use the commands ipcs and ipcrm to work with the memory
  * @return resource
  */
 protected function _getSemIdentifier()
 {
     if (null !== $this->_semId) {
         return $this->_semId;
     }
     $this->_semId = sem_get($this->getIndexerCodeCrc(), 8, 0666);
     if (false === $this->_semId) {
         Mage::throwException('FastIndexer: Cannot create semaphore id lock for ' . $this->getIndexerCode());
     }
     $this->_shmId = shm_attach($this->getIndexerCodeCrc(), 128);
     return $this->_semId;
 }