예제 #1
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;
}
예제 #2
0
 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;
 }
예제 #3
0
 /**
  * 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);
 }
예제 #4
0
 public function __get($name)
 {
     $name = $this->intkey($name);
     if (shm_has_var($this->ipc, $name)) {
         return shm_get_var($this->ipc, $name);
     }
     return NULL;
 }
예제 #5
0
 /**
  * {@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;
     }
 }
예제 #6
0
 /**
  * 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>');
     }
 }
예제 #7
0
파일: Shm.php 프로젝트: millken/ypf
 public function has($key)
 {
     if (shm_has_var($this->shm, $this->shm_key($key))) {
         return true;
     } else {
         return false;
     }
 }
예제 #8
0
 public function __get($k)
 {
     $key = crc32($k);
     if (!shm_has_var($this->sharedMemoryId, $key)) {
         return null;
     }
     return shm_get_var($this->sharedMemoryId, $key);
 }
예제 #9
0
 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;
     }
 }
예제 #10
0
 /**
  * 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>');
     }
 }
예제 #11
0
파일: Shm.php 프로젝트: wapmorgan/kvstorage
 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);
     }
 }
예제 #12
0
파일: shm.php 프로젝트: boyxp/bluefin-base
 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;
     }
 }
예제 #13
0
 /**
  * @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;
 }
예제 #14
0
 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;
 }
예제 #15
0
파일: Uuid.php 프로젝트: duoshuo/uuid
 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);
 }
예제 #16
0
 /**
  * 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();
 }
예제 #17
0
파일: mutex.php 프로젝트: noccy80/lepton-ng
 /**
  * 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();
 }
예제 #18
0
 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;
 }
예제 #19
0
 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;
 }
예제 #20
0
 /**
  * (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;
 }
예제 #21
0
<?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);
예제 #22
0
 /**
  * 获取故障节点列表
  * @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;
 }
예제 #23
0
 /**
  * @param $key
  *
  * @return bool
  */
 public function exists($key)
 {
     return shm_has_var(self::$shm_id, $key);
 }
예제 #24
0
 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;
 }
예제 #26
0
 /**
  * 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;
 }
예제 #27
0
 /**
  * 获取上次告警时间
  */
 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);
 }
예제 #28
0
파일: csp.php 프로젝트: straiway/fmt
 public function destroy()
 {
     if (shm_has_var($this->shm, 1)) {
         shm_remove_var($this->shm, 1);
     }
     shm_remove($this->shm);
 }
예제 #29
0
 /**
  * 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));
 }
예제 #30
0
파일: utils.php 프로젝트: bephp/utils
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;
}