/** * Release a semaphore * After releasing the semaphore, acquire() may be called to re-acquire it. * * @return bool success * @throws io.IOException * @see xp://io.sys.Semaphore#acquire */ public function release() { if (FALSE === sem_release($this->_hdl)) { throw new IOException('Could not release semaphore ' . $this->key); } return TRUE; }
/** * @return bool */ public function unlock() { $released = sem_release($this->lock); sem_remove($this->lock); $this->lock = null; return $released; }
/** * 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); }
/** * 通过本机共享内存件来生成一个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; }
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']); }
/** * @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)); }
/** * Release a semaphore * After releasing the semaphore, acquire() may be called to re-acquire it. * * @return bool success * @throws io.IOException * @see xp://io.sys.Semaphore#acquire */ public function release() { if (false === sem_release($this->_hdl)) { throw new \io\IOException('Could not release semaphore ' . $this->key); } return true; }
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); }
/** * Unlock process * * @return Mage_Index_Model_Process */ public function unlock() { $this->_getSemIdentifier(); shm_remove_var($this->_shmId, $this->getIndexerCodeCrc()); @sem_release($this->_getSemIdentifier()); $this->_isLocked = false; }
public function unlock() { if (PHP_OS !== 'Linux') { return; } sem_release($this->getSemaphoreId()); $this->locked = false; }
/** * 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 }
/** * @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; }
/** * release lock * @throws \RuntimeException */ public function release() { if ($this->locked) { if (!sem_release($this->lock_id)) { throw new \RuntimeException('Cannot release semaphore: ' . $this->lock_id); } $this->locked = false; } }
function __destruct() { if (null !== $this->shmHandle) { shm_detach($this->shmHandle); } if (null !== $this->semHandle) { sem_release($this->semHandle); } }
public static function unlock($sem_id) { if (substr(PHP_OS, 0, 3) == 'WIN') { // if Windows OS detected just return true; return true; } sem_release($sem_id); // release semaphore lock }
public function release() { if ($this->acquired) { $rl = microtime(true) - $this->start; $this->debug("Releasing semaphore (+%.4fs)", $rl); sem_release($this->semaphore); $this->acquired = false; } }
function release($key) { try { return sem_release($this->locks[$key]); } catch (ExecutionContextException $e) { // acquired by another process return false; } }
public function releaseLock() { if (!empty($this->semId)) { if (!sem_release($this->semId)) { echo __LINE__; return false; } } return true; }
public function release_mutex() { if ($this->use_sysv) { sem_release($this->resource); //sem_remove($this->resource); } else { flock($this->resource, LOCK_UN); fclose($this->resource); } }
function shutdown() { foreach ($this->sem as $id => $sem) { sem_release($sem); unset($this->sem[$id]); } if ($this->sem) { throw new \UnexpectedValueException(); } }
/** * @return bool */ public function release() { if (true === $this->acquired) { $result = sem_release($this->semaphore); if (true === $result) { $this->acquired = false; } return $result; } return true; }
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; }
public function writeShared($data_arr) { $null_fill = str_repeat(str_repeat(' ', 10), $this->size / 10); shmop_write($this->shm_id, $null_fill, 0); $data = serialize($data_arr); if (strlen($data) > $this->size) { return false; } shmop_write($this->shm_id, $data, 0); sem_release($this->sem_id); return true; }
/** Rolls back a transaction. */ public final function rollback() { $this->rollBackOnly == true; --$this->count; if ($this->count == 0) { try { $this->doRollback(); } finally { sem_release($this->semId); } } }
public function free($key) { if (isset($this->pool[$key])) { try { return sem_release($this->pool[$key]); } catch (BaseException $e) { // acquired by another process return false; } } return null; }
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(); }
/** * {@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()}"); } } }
/** * Releases a semaphore. * * @throws \RuntimeException When unable to release lock * @throws \LogicException When the semaphore has been removed * @throws \LogicException When the semaphore was not acquired */ public function release() { if (!is_resource($this->handle)) { throw new \LogicException('Semaphore has been removed.'); } if (!$this->locked) { throw new \LogicException('Semaphore was not acquired.'); } if (!sem_release($this->handle)) { throw new \RuntimeException('Unable to release lock.'); } $this->locked = false; }
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; }
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); } }