Ejemplo n.º 1
0
 /**
  * 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;
 }
Ejemplo n.º 2
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;
}
Ejemplo n.º 3
0
 public function acquire()
 {
     if (!sem_acquire($this->_semaphore)) {
         throw new Sera_Exception("failed to acquire semaphore");
     }
     $this->_acquired = true;
 }
Ejemplo n.º 4
0
 /**
  * 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;
 }
Ejemplo n.º 5
0
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']);
}
Ejemplo n.º 6
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));
 }
Ejemplo n.º 7
0
 /**
  * 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;
 }
Ejemplo n.º 8
0
 /**
  * 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;
 }
Ejemplo n.º 9
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);
 }
 /**
  * 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);
 }
Ejemplo n.º 11
0
 /**
  * @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;
 }
Ejemplo n.º 12
0
 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;
 }
Ejemplo n.º 13
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);
    }
}
Ejemplo n.º 14
0
 /**
  * @return bool
  */
 public function acquire()
 {
     $result = sem_acquire($this->semaphore);
     if (true === $result) {
         $this->acquired = true;
     }
     return $result;
 }
Ejemplo n.º 15
0
 public function lock()
 {
     if (PHP_OS !== 'Linux') {
         return;
     }
     sem_acquire($this->getSemaphoreId());
     $this->locked = true;
 }
Ejemplo n.º 16
0
 /**
  * Delete shared memory var
  */
 public function del()
 {
     sem_acquire($this->__mutex);
     //block until released
     @shm_remove_var($this->__shm, $this->__key);
     sem_release($this->__mutex);
     //release mutex
 }
 /**
  * Retorna numero
  */
 public function getNumero()
 {
     if (!sem_acquire($this->_semaforo)) {
         die('Error esperando al semaforo.');
     }
     $cantidad = $this->_ci->_marea_roja_desembarco_informe_model->maximoNumeroPorLaboratorio($this->_id_laboratorio);
     return $cantidad + 1;
 }
Ejemplo n.º 18
0
 /**
  * if an error like "not enough shared memory left" occurs then set  sysvshm.init_mem to a higher value
  * Lock process without blocking.
  * This method allow protect multiple process running and fast lock validation.
  *
  */
 public function lock()
 {
     $success = sem_acquire($this->_getSemIdentifier());
     shm_put_var($this->_shmId, $this->getIndexerCodeCrc(), $this->_getMicrotimeString());
     if (false === $success) {
         Mage::throwException('FastIndexer: Cannot acquire semaphore lock!');
     }
     $this->_isLocked = true;
 }
Ejemplo n.º 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);
 }
Ejemplo n.º 20
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;
     }
 }
Ejemplo n.º 21
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;
 }
Ejemplo n.º 22
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;
}
Ejemplo n.º 23
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();
 }
Ejemplo n.º 24
0
 /** Begins a transaction. */
 public final function begin()
 {
     if ($this->count > 0) {
         ++$this->count;
     } else {
         sem_acquire($this->semId);
         $this->count = 1;
         $this->rollBackOnly = false;
         $this->doBegin();
     }
 }
 /**
  * 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");
 }
Ejemplo n.º 26
0
 public function acquire()
 {
     if (!$this->acquired) {
         $t = microtime(true);
         $this->debug("Acquiring semaphore");
         sem_acquire($this->semaphore);
         $te = microtime(true) - $t;
         $this->debug("Semaphore acquired (+%.4fs)", $te);
         $this->start = microtime(true);
         $this->acquired = true;
     }
 }
 public function get($key)
 {
     try {
         if (!isset($this->pool[$key])) {
             $this->pool[$key] = sem_get($key, 1, ONPHP_IPC_PERMS, false);
         }
         return sem_acquire($this->pool[$key]);
     } catch (BaseException $e) {
         return null;
     }
     Assert::isUnreachable();
 }
Ejemplo n.º 28
0
 /**
  * {@inheritdoc}
  * @see Scalr_System_Cronjob_MultiProcess_DefaultWorker::startForking()
  */
 function startForking($workQueue)
 {
     // Reopen DB connection after daemonizing
     $this->db = $this->getContainer()->adodb;
     // Check that time has come to cleanup dead servers
     $doCleanup = false;
     sem_acquire($this->cleanupSem);
     try {
         if (time() - (int) $this->lastCleanup->get(0) >= $this->cleanupInterval) {
             $doCleanup = true;
             $this->lastCleanup->put(0, time());
         }
     } catch (Exception $e) {
         sem_release($this->cleanupSem);
     }
     sem_release($this->cleanupSem);
     if ($doCleanup) {
         $this->logger->info("Cleanup dead servers");
         try {
             $importing_servers = $this->db->GetAll("\n                    SELECT server_id FROM servers\n                    WHERE status IN(?, ?)\n                    AND `dtadded` < NOW() - INTERVAL 1 DAY\n                ", array(SERVER_STATUS::IMPORTING, SERVER_STATUS::TEMPORARY));
             foreach ($importing_servers as $ts) {
                 $dbServer = DBServer::LoadByID($ts['server_id']);
                 if ($dbServer->status == SERVER_STATUS::TEMPORARY) {
                     try {
                         $dbServer->terminate(DBServer::TERMINATE_REASON_TEMPORARY_SERVER_ROLE_BUILDER);
                     } catch (Exception $e) {
                     }
                 } else {
                     if ($dbServer->status == SERVER_STATUS::IMPORTING) {
                         $dbServer->Remove();
                     }
                 }
             }
             $pending_launch_servers = $this->db->GetAll("SELECT server_id FROM servers WHERE status=?", array(SERVER_STATUS::PENDING_LAUNCH));
             try {
                 foreach ($pending_launch_servers as $ts) {
                     $DBServer = DBServer::LoadByID($ts['server_id']);
                     if ($DBServer->status == SERVER_STATUS::PENDING_LAUNCH) {
                         $account = Scalr_Account::init()->loadById($DBServer->clientId);
                         if ($account->status == Scalr_Account::STATUS_ACTIVE) {
                             Scalr::LaunchServer(null, $DBServer);
                         }
                     }
                 }
             } catch (Exception $e) {
                 Logger::getLogger(LOG_CATEGORY::FARM)->error(sprintf("Can't load server with ID #'%s'", $ts['server_id'], $e->getMessage()));
             }
         } catch (Exception $e) {
             $this->logger->fatal("Poller::cleanup failed: {$e->getMessage()}");
         }
     }
 }
Ejemplo n.º 29
0
 function execute()
 {
     set_error_handler('fragtable_error_handler');
     define('FRAGTABLE_ROOT', dirname(__FILE__));
     $HTTP_HOST = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'NULL';
     $HTTP_USER_AGENT = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'NULL';
     if (!isset($_SERVER['REQUEST_URI'])) {
         trigger_error("'REQUEST_URI' not found", E_USER_WARNING);
     }
     $salt = $HTTP_USER_AGENT . $_SERVER['REQUEST_URI'];
     $hashi = hexdec(substr(hash('crc32b', $salt), 0, 2));
     $subject = "[{$HTTP_HOST}] DDoS WARNING!!";
     $accucnt = 0;
     $logfile = FRAGTABLE_ROOT . '/secure.log';
     $shm_key['data'] = ftok(__FILE__, 't');
     $shm_key['lock'] = ftok(__FILE__, 'u');
     $sem_id = sem_get($shm_key['lock']);
     $shm_size = 9626;
     sem_acquire($sem_id);
     $shm_id = shmop_open($shm_key['data'], 'c', 0644, $shm_size);
     $shm_data = shmop_read($shm_id, 0, $shm_size);
     $fragtable = @unserialize($shm_data);
     if (!$fragtable) {
         $fragtable = array();
     }
     $nowtime = time();
     if (isset($fragtable[$hashi]) && $nowtime < $fragtable[$hashi][0] + self::$intervsl) {
         $accucnt = $fragtable[$hashi][1] < 99 ? $fragtable[$hashi][1] + 1 : $fragtable[$hashi][1];
         $acctime = $fragtable[$hashi][0];
     } else {
         $accucnt = 1;
         $acctime = $nowtime;
     }
     $fragtable[$hashi] = array($acctime, $accucnt);
     $shm_data = serialize($fragtable);
     shmop_write($shm_id, str_pad($shm_data, $shm_size, "", STR_PAD_RIGHT), 0);
     shmop_close($shm_id);
     sem_release($sem_id);
     $fragtable = $shm_data = NULL;
     if ($accucnt > self::$threshold) {
         if (!file_exists($logfile) || filesize($logfile) < 10 * 1024 * 1024) {
             $message = sprintf("%s | %d | %d | %s | %s | %s\n", gmdate('Y-m-d H:i:s', $nowtime + self::$timezone * 3600), $acctime, $hashi, str_pad($_SERVER["REMOTE_ADDR"], 15, ' ', STR_PAD_RIGHT), "{$_SERVER['REQUEST_METHOD']} {$_SERVER['REQUEST_URI']} {$_SERVER['SERVER_PROTOCOL']}", $HTTP_USER_AGENT);
             if (!file_exists($logfile) || $nowtime > filemtime($logfile) + 3600) {
                 @mail(self::$mailto, $subject, $message);
             }
             file_put_contents($logfile, $message, FILE_APPEND | LOCK_EX);
         }
         header('HTTP/1.1 503 Service Temporarily Unavailable');
         die('<h1>Service Temporarily Unavailable</h1>');
     }
     restore_error_handler();
 }
Ejemplo n.º 30
0
function obtainLock($sem, $i)
{
    echo "process {$i} is getting the sem \n";
    $res = sem_acquire($sem, true);
    sleep(1);
    if (!$res) {
        echo "process {$i} unable to get sem. \n";
    } else {
        echo "process {$i} successfully got the sem \n";
        sem_release($sem);
    }
    exit;
}