/** * 通过本机共享内存件来生成一个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; }
public function set($key, $value, $no_cas = false) { if (!$this->isOpen()) { $this->open(); } $this->enterCriticalSection($this->ipckey); $this->debug("SHM set: {$key} = {$value}"); $key = strtolower($key); $idx = $this->props[$key]; if (!$no_cas && shm_has_var($this->shm, $idx) && !empty($this->hashes[$key])) { $var = shm_get_var($this->shm, $idx); $check = md5($var); if ($this->hashes[$key] == $check) { $this->debug("CAS check: Key not modified: {$key}"); shm_put_var($this->shm, $idx, $value); $ok = true; } else { $this->debug("CAS check: Key modified, write blocked: {$key}"); $ok = false; } } else { $this->debug("CAS check: Check disabled for set: {$key}"); $ok = true; shm_put_var($this->shm, $idx, $value); } if ($ok) { $hash = md5($value); $this->hashes[$key] = $hash; $this->debug("CAS hash for {$key} is now {$hash}"); } $this->leaveCriticalSection(); return $ok; }
/** * Test if has datas with $uid key * @param mixed $uid * @return boolean */ public function has($uid) { if (null === $this->memory) { return false; } return shm_has_var($this->memory, $uid); }
public function __get($name) { $name = $this->intkey($name); if (shm_has_var($this->ipc, $name)) { return shm_get_var($this->ipc, $name); } return NULL; }
/** * {@inheritdoc} */ public function get() { if (shm_has_var($this->shared_memory_segment, self::$SEGMENT_VAR_ID)) { $data = shm_get_var($this->shared_memory_segment, self::$SEGMENT_VAR_ID); shm_remove_var($this->shared_memory_segment, self::$SEGMENT_VAR_ID); return $data; } }
/** * Execution commands. * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { if (is_null(Config::$sharedId) == false && @shm_has_var(Config::$sharedId, Config::STATUS)) { $output->writeln('<info>' . shm_get_var(Config::$sharedId, Config::STATUS) . '</info>'); } else { $output->writeln('<comment>' . Config::NAME . ' not running</comment>'); } }
public function has($key) { if (shm_has_var($this->shm, $this->shm_key($key))) { return true; } else { return false; } }
public function __get($k) { $key = crc32($k); if (!shm_has_var($this->sharedMemoryId, $key)) { return null; } return shm_get_var($this->sharedMemoryId, $key); }
public function set($index, $value, $overwrite = false) { if ($overwrite || !shm_has_var($this->shm, $index) || $this->data[$index] == shm_get_var($this->shm, $index)) { shm_put_var($this->shm, $index, $value); return true; } else { return false; } }
/** * Execution commands. * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { if (is_null(Config::$sharedId) == false && @shm_has_var(Config::$sharedId, Config::PID)) { $process = new Process('ps o pid --ppid ' . shm_get_var(Config::$sharedId, Config::PID)); $process->run(); } else { $output->writeln('<comment>' . Config::NAME . ' not running</comment>'); } }
public function __construct($id, $startSize = 10240) { $this->id = $id; $this->res = shm_attach($id, $startSize); if (!shm_has_var($this->res, self::DEFAULT_VAR_ID)) { $this->data = array(); $this->changed = true; } else { $this->data = shm_get_var($this->res, self::DEFAULT_VAR_ID); } }
public function remove(string $key) : bool { if (isset($this->_cache[$key])) { unset($this->_cache[$key]); } $key = crc32($key); if (shm_has_var($this->_shmid, $key)) { return shm_remove_var($this->_shmid, $key); } else { return false; } }
/** * @param int $pid * * @return int */ private function readInc($pid) { $res = shm_attach($pid); if (!shm_has_var($res, 0)) { shm_put_var($res, 0, 0); } $inc = shm_get_var($res, 0); if ($inc === 16777215) { $inc = 0; } ++$inc; shm_put_var($res, 0, $inc); return $inc; }
protected function get() { $lock = array(); if (shm_has_var($this->shm, self::ADDRESS)) { $lock = shm_get_var($this->shm, self::ADDRESS); } else { return false; } // Ensure we're not seeing our own lock if ($lock['pid'] == $this->pid) { return false; } // If it's expired... if ($lock['time'] + $this->ttl + Core_Lock_Lock::$LOCK_TTL_PADDING_SECONDS >= time()) { return $lock; } return false; }
protected static function _initClockSeq() { $shmId = shm_attach(self::$_shmKey); self::$_clockSeq = shm_get_var($shmId, self::$_clockSeqKey); if (self::$_clockSeq === false) { $semId = sem_get(self::$_semKey); sem_acquire($semId); //blocking if (shm_has_var($shmId, self::$_clockSeqKey)) { self::$_clockSeq = shm_get_var($shmId, self::$_clockSeqKey); } else { // 0x8000 variant (2 bits) // clock sequence (14 bits) self::$_clockSeq = 0x8000 | mt_rand(0, (1 << 14) - 1); shm_put_var($shmId, self::$_clockSeqKey, self::$_clockSeq); } sem_release($semId); } shm_detach($shmId); }
/** * Execution commands. * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { if (is_null(Config::$sharedId) == false && @shm_has_var(Config::$sharedId, Config::PID)) { $output->writeln('<info>' . Config::NAME . ' ' . shm_get_var(Config::$sharedId, Config::PID) . ' is running</info>'); exit; } if ($input->getOption('debug') === false) { if (function_exists('ini_set')) { ini_set('error_log', Config::$path . Config::$logsPath . '/error.log'); } fclose(STDIN); fclose(STDOUT); fclose(STDERR); $STDIN = fopen('/dev/null', 'r'); $STDOUT = fopen(Config::$path . Config::$logsPath . '/appilication.log', 'ab'); $STDERR = fopen(Config::$path . Config::$logsPath . '/error.log', 'ab'); } $phpcron = new PHPCron(Entries::getCrontab()); $phpcron->execute(); }
/** * Constructor */ function __construct($name, $timeoutms = 5000) { if (!function_exists('shm_has_var')) { throw new BaseException("No mutex support on this platform"); } $this->_lockname = $name; // Block until lock can be acquired if (Mutex::$instance == 0) { Console::debug("Creating mutex manager"); Mutex::$resource = shm_attach(Mutex::SHM_KEY); if (!shm_has_var(Mutex::$resource, Mutex::SHM_LOCKS)) { shm_put_var(Mutex::$resource, Mutex::SHM_LOCKS, array()); } } $this->enterCriticalSection(); Console::debug("Waiting for lock %s", $this->_lockname); $t = new timer(true); while (true) { $ls = shm_get_var(Mutex::$resource, Mutex::SHM_LOCKS); if (!isset($ls[$name])) { break; } usleep(100000); if ($t->getElapsed() > $timeoutms / 1000) { $this->exitCriticalSection(); throw new MutexException("Timed out waiting for lock"); } } Console::debug("Acquiring lock %s", $this->_lockname); $ls = shm_get_var(Mutex::$resource, Mutex::SHM_LOCKS); $ls[$name] = true; shm_put_var(Mutex::$resource, Mutex::SHM_LOCKS, $ls); Mutex::$instance++; $this->_lockstate = true; $this->exitCriticalSection(); }
public function remove_var($varname, $autolock = FALSE) { $varkey = $this->_gen_key($varname); if ($autolock) { sem_acquire($this->sem); } if (!shm_has_var($this->shm_id, $varkey)) { $result = TRUE; } else { $result = shm_remove_var($this->shm_id, $varkey); } if ($autolock) { sem_release($this->sem); } return $result; }
public function canRevert() { $ftok = ftok("/dev/shm/ipc_firewall", "a"); $segment = shm_attach($ftok, 1048576, 0666); // Create/attach to 10k shared memory segment if (!shm_has_var($segment, 0)) { // Hasn't been used return false; } $settings = shm_get_var($segment, 0); if (isset($settings['timestamp']) && !isset($settings['confirmed'])) { return $settings['timestamp']; } return false; }
/** * (non-PHPdoc) * @see Lexik\Bundle\MaintenanceBundle\Drivers.AbstractDriver::isExists() */ public function isExists() { if ($this->shmId) { if (!shm_has_var($this->shmId, self::VARIABLE_KEY)) { return false; } $data = shm_get_var($this->shmId, self::VARIABLE_KEY); return $data == self::VALUE_TO_STORE; } return false; }
<?php $ret = shm_attach(0xdeadbeef); if ($ret === false) { echo "failed\n"; exit(1); } $index = $ret; var_dump(shm_has_var($index, 1234)); shm_put_var($index, 1234, "test"); var_dump(shm_has_var($index, 1234)); $pid = pcntl_fork(); if ($pid == 0) { $ret = shm_attach($index); $ret = shm_get_var($index, 1234); if ($ret !== "test") { echo "oops\n"; exit(1); } shm_remove_var($index, 1234); shm_detach($index); exit(0); } // Verifying shm_remove_var worked, this is not sure test though. $ret = shm_get_var($index, 1234); for ($i = 0; $i < 1000; $i++) { if ($ret === false) { break; } usleep(1000); $ret = shm_get_var($index, 1234);
/** * 获取故障节点列表 * @return array */ public static function getBadAddressList($use_cache = true) { // 还没有初始化故障节点 if (null === self::$badAddressList || !$use_cache) { $bad_address_list = array(); if (self::$shmEnable && shm_has_var(self::getShmFd(), self::SHM_BAD_ADDRESS_KEY)) { // 获取故障节点 $bad_address_list = shm_get_var(self::getShmFd(), self::SHM_BAD_ADDRESS_KEY); if (!is_array($bad_address_list)) { // 可能是共享内寻写怀了,重新清空 self::getMutex(); shm_remove(self::getShmFd()); self::releaseMutex(); self::$badAddressShmFd = null; self::$badAddressList = array(); } else { self::$badAddressList = $bad_address_list; } } else { self::$badAddressList = $bad_address_list; } } return self::$badAddressList; }
/** * @param $key * * @return bool */ public function exists($key) { return shm_has_var(self::$shm_id, $key); }
function checkConsumer() { $flgSemKey = sem_get(9876543211); $memKey = shm_attach(123456789); $flgKey = 555555555; sem_acquire($flgSemKey); if (shm_has_var($memKey, $flgKey)) { $flag = shm_get_var($memKey, $flgKey); if ($flag == 0) { exec("(cd {$this->root}/common_files/ && exec php consumer.php > /dev/null 2>/dev/null &)"); $flag = 1; shm_put_var($memKey, $flgKey, $flag); } } else { exec("(cd {$this->root}/common_files/ && exec php consumer.php > /dev/null 2>/dev/null &)"); $flag = 1; shm_put_var($memKey, $flgKey, $flag); } sem_release($flgSemKey); }
/** * Indicates if the requested variable is available in shared memory * * @param int $id int indicating the variable * * @access protected * @return boolean */ protected function hasData($id = 2) { if (isset($this->mutexid) && $this->mutexid !== false && (isset($this->memid) && $this->memid !== false)) { if (function_exists("shm_has_var")) { return @shm_has_var($this->memid, $id); } else { $some = $this->getData($id); return isset($some); } } return false; }
/** * Returns true if this task was cancelled before it completed normally * @return boolean * @access public * @final */ public final function isCancelled() { return @shm_has_var(self::$shmId, 11511697116117115) == 'CANCELED' ? true : false; }
/** * 获取上次告警时间 */ public static function getLastAlarmTime() { // 没有加载扩展 if (!extension_loaded('sysvshm')) { return false; } // 是否有保存上次告警时间 if (!shm_has_var(self::getShmFd(), RPC_LAST_ALARM_TIME_KEY2)) { $time_now = time(); self::setLastAlarmTime($time_now); return $time_now; } return shm_get_var(self::getShmFd(), RPC_LAST_ALARM_TIME_KEY2); }
public function destroy() { if (shm_has_var($this->shm, 1)) { shm_remove_var($this->shm, 1); } shm_remove($this->shm); }
/** * Test if an entry exists in the cache. * * @param string $id cache id The cache id of the entry to check for. * @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise. */ public function contains($id) { return shm_has_var($this->shmId, $this->forgeKey($id)); }
function cache($key, $val = null, $expire = 100) { static $_shm = null; if (null === $_shm) { $_shm = @shm_attach(crc32(config('cache.solt', null, 'cache.solt')), config('cache.size', null, 10485760), 0755); register_shutdown_function(function () use($_shm) { shm_detach($_shm); }); } if (($time = time()) && ($k = crc32($key)) && $val && $expire) { @shm_put_var($_shm, $k, array($time + $expire, $val)); return $val; } return $_shm && shm_has_var($_shm, $k) && ($data = shm_get_var($_shm, $k)) && is_array($data) && $data[0] > $time ? $data[1] : null; }