public function write($key, $value)
 {
     if ($this->shmId) {
         return shm_put_var($this->shmId, $key, $value);
     }
     return false;
 }
Exemplo n.º 2
0
function Map2GE($x, $y)
{
    if (0) {
        $SHM_KEY = ftok(__FILE__, chr(4));
        $data = shm_attach($SHM_KEY, 102400, 0666);
        $result = shm_get_var($data, 1);
        $r = $result[$x][$y];
        if (isset($r)) {
            shm_detach($data);
            return $r;
        }
    }
    // ­×¥¿
    $r = t67to97($x, $y);
    $x = $r[0];
    $y = $r[1];
    $proj = "proj -I +proj=tmerc +ellps=aust_SA +lon_0=121 +x_0=250000 +k=0.9999";
    $ret = shell_exec("echo {$x} {$y} | {$proj}");
    if (preg_match("/(\\d+)d(\\d+)'([\\d.]+)\"E\\s+(\\d+)d(\\d+)'([\\d.]+)\"N/", $ret, $matches)) {
        list($junk, $ed, $em, $es, $nd, $nm, $ns) = $matches;
        $r[0] = $ed + $em / 60 + $es / 3600;
        $r[1] = $nd + $nm / 60 + $ns / 3600;
        if (0) {
            $result[$x][$y] = $r;
            shm_put_var($data, 1, $result);
            shm_detach($data);
        }
        return $r;
    }
    return FALSE;
    // exit;
}
Exemplo n.º 3
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;
 }
Exemplo n.º 4
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));
 }
Exemplo n.º 5
0
 /**
  * (non-PHPdoc)
  * @see Lexik\Bundle\MaintenanceBundle\Drivers.AbstractDriver::createLock()
  */
 protected function createLock()
 {
     if ($this->shmId) {
         return shm_put_var($this->shmId, self::VARIABLE_KEY, self::VALUE_TO_STORE);
     }
     return false;
 }
Exemplo n.º 6
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;
}
Exemplo n.º 7
0
 /**
  * @param $key
  * @param $value
  * @param int $size
  */
 public function set($key, $value, $size = 10000)
 {
     $result = $this->attach($key, $size);
     sem_acquire($result['mutex']);
     shm_put_var($result['shm'], $key, $value);
     sem_release($result['mutex']);
 }
Exemplo n.º 8
0
 /**
  * @author Yohann Marillet
  */
 public function put($key = 1, $value = true)
 {
     $res = shm_put_var($this->segment, $key, [$value]);
     if (!$res) {
         throw new \Exception('Cannot put data in semaphore');
     }
     return $this;
 }
 /**
  * @see CachePeer::set()
  *
  * @return SysVCachePeer
  */
 function set($key, $value, $ttl = CacheTtl::HOUR)
 {
     Assert::isNumeric($ttl);
     if ($this->isAlive()) {
         shm_put_var($this->getSegmentPtr(), $this->key2int($key), array(self::FIELD_EXPIRES => time() + $ttl, self::FIELD_DATA => $value));
     }
     return $this;
 }
Exemplo n.º 10
0
 public function set()
 {
     $lock = $this->check();
     if ($lock) {
         throw new Exception('Core_Lock_Shm::set Failed. Existing Lock Detected from PID ' . $lock['pid']);
     }
     shm_put_var($this->shm, self::ADDRESS, array('pid' => $this->pid, 'time' => time()));
 }
Exemplo n.º 11
0
 /**
  * Set shared memory var
  * @param $var
  */
 public function set($var)
 {
     sem_acquire($this->__mutex);
     //block until released
     shm_put_var($this->__shm, $this->__key, $var);
     //store var
     sem_release($this->__mutex);
     //release mutex
 }
Exemplo n.º 12
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;
     }
 }
Exemplo n.º 13
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;
 }
Exemplo n.º 14
0
 public static function store($key, $value)
 {
     self::getHandle();
     if (!shm_put_var(self::$handle, self::getVarKey($key), $value)) {
         sem_remove(self::$semaphore);
         shm_remove(self::$handle);
         die('couldn\'t write to shared memory.');
     }
     self::release();
 }
Exemplo n.º 15
0
 public function run()
 {
     $this->synchronized(function ($thread) {
         $thread->wait();
     }, $this);
     $counter = shm_get_var($this->shmid, 1);
     $counter++;
     shm_put_var($this->shmid, 1, $counter);
     printf("Thread #%lu says: %s\n", $this->getThreadId(), $counter);
 }
Exemplo n.º 16
0
 /**
  * Put this segment's contents
  *
  * @param   var data
  * @param   int permissions default 0666 permissions
  * @return  bool success
  * @throws  io.IOException in case an error occurs
  */
 public function put($val, $permissions = 0666)
 {
     $v = [$val];
     $h = shm_attach($this->spot, (strlen(serialize($v)) + 44) * 2, $permissions);
     $ret = shm_put_var($h, $this->name, $v);
     shm_detach($h);
     if (false === $ret) {
         throw new IOException('Could not write segment ' . $this->name);
     }
     return $ret;
 }
Exemplo n.º 17
0
 public function __destruct()
 {
     if ($this->changed) {
         if (shm_put_var($this->res, self::DEFAULT_VAR_ID, $this->data) === false) {
             $length = strlen($this->data);
             shm_remove($this->res);
             $this->res = shm_attach($this->id, ceil($length * 1.25));
             shm_put_var($this->res, self::DEFAULT_VAR_ID, $this->data);
         }
     }
     shm_detach($this->res);
 }
Exemplo n.º 18
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;
 }
Exemplo n.º 19
0
 public function put_var($varname, $value, $autolock = FALSE)
 {
     $varkey = $this->_gen_key($varname);
     if ($autolock) {
         sem_acquire($this->sem);
     }
     $result = shm_put_var($this->shm_id, $varkey, $value);
     if ($autolock) {
         sem_release($this->sem);
     }
     return $result;
     // 写入失败:空间不够或其它异常,删除共内存中所有值
     //if (!shm_remove($this->shm_id)) return FALSE;
     //return shm_put_var($this->shm_id, $varkey, $value);
 }
 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);
     }
 }
Exemplo n.º 21
0
 public function run()
 {
     if ($this->mutex) {
         $locked = Mutex::lock($this->mutex);
     }
     $counter = shm_get_var($this->shmid, 1);
     $counter++;
     shm_put_var($this->shmid, 1, $counter);
     printf("Thread #%lu lock: %s says: %s\n", $this->getThreadId(), !empty($locked) ? "Y" : "N", $counter);
     //$this->lock();
     //$this->unlock();
     if ($this->mutex) {
         Mutex::unlock($this->mutex);
     }
     return true;
 }
 public function touch($key)
 {
     try {
         $shm = shm_attach($this->id, self::SEGMENT_SIZE, HESPER_IPC_PERMS);
     } catch (BaseException $e) {
         return false;
     }
     try {
         $result = shm_put_var($shm, $key, true);
         shm_detach($shm);
     } catch (BaseException $e) {
         // not enough shared memory left, rotate it.
         shm_detach($shm);
         return $this->drop();
     }
     return $result;
 }
Exemplo n.º 23
0
 function put($key, $value)
 {
     if (!is_numeric($key)) {
         throw new Scalr_System_Exception(sprintf("key must be numeric. '%s' is given", $key));
     }
     // shm_get_var return false in case of non-existed key.
     // We need a wrapper to store boolean values
     if (is_bool($value)) {
         $wrapper = new stdClass();
         $wrapper->__shmWrapperClass = true;
         $wrapper->value = $value;
         $value = $wrapper;
     }
     if (!shm_put_var($this->shm, $key, $value)) {
         throw new Scalr_System_Exception(sprintf("Cannot put key '%s' into shared memory", $key));
     }
     return true;
 }
Exemplo n.º 24
0
 function SET($key, $value)
 {
     if (is_array($key)) {
         return null;
     }
     if (!$this->ok) {
         return null;
     }
     $array = $this->MyArray();
     if (!is_array($array)) {
         $array = array();
     }
     $array[$key] = $value;
     if ($GLOBALS["LOGON-PAGE"]) {
         error_log("[{$_SESSION["uid"]}]::@shm_put_var({$this->shmid},{$this->index},{$array}); " . basename(__FILE__) . " line " . __LINE__);
     }
     @shm_put_var($this->shmid, $this->index, $array);
     if ($GLOBALS["LOGON-PAGE"]) {
         error_log("[{$_SESSION["uid"]}]::@shm_put_var() OK " . basename(__FILE__) . " line " . __LINE__);
     }
 }
Exemplo n.º 25
0
 /**
  * Write a KEY/VALUE into shm
  */
 public function put($varkey, $varval)
 {
     // Write a value into shm
     if ($this->mode == 'sysv') {
         return shm_put_var($this->id, $varkey, $varval);
     } else {
         if ($this->mode == 'shmop') {
             // shmop is much more low-level than sysv, you need to operate every byte yourself!
             $curr = shmop_read($this->id, 0, shmop_size($this->id));
             $curr = substr($curr, 0, strpos($curr, ""));
             $curr = $curr ? unserialize($curr) : array();
             $curr[$varkey] = $varval;
             $curr = serialize($curr) . "";
             return shmop_write($this->id, $curr, 0);
         } else {
             $curr = @file_get_contents($this->id);
             $curr = substr($curr, 0, strpos($curr, ""));
             $curr = $curr ? unserialize($curr) : array();
             $curr[$varkey] = $varval;
             $curr = serialize($curr) . "";
             return file_put_contents($this->id, $curr, LOCK_EX) or error('Unable to write file:' . $this->id, __FILE__, __LINE__);
         }
     }
 }
Exemplo n.º 26
0
 /**
  * 更新主进程收集的状态信息到共享内存
  * @return bool
  */
 protected static function updateStatusToShm()
 {
     if (!self::$shmId) {
         return true;
     }
     return shm_put_var(self::$shmId, self::STATUS_VAR_ID, array_merge(self::$serviceStatusInfo, array('pid_map' => self::$workerPidMap)));
 }
Exemplo n.º 27
0
 $sa = sem_acquire($toSemId);
 $toUser = shm_get_var($toShmId, $toKey);
 if (!$toUser) {
     $f = fopen($ffn, 'r+');
     $userStr = fread($f, filesize($ffn));
     $toUser = unserialize($userStr);
     $fo = true;
 }
 unset($toUser['friends'][$_SESSION['username']]['req']);
 $toUser['friends'][$_SESSION['username']]['nickName'] = $_SESSION['nickName'];
 $toUser['friends'][$_SESSION['username']]['key'] = $_SESSION['key'];
 unset($toUser['olFrns'][$_SESSION['username']]['req']);
 $toUser['olFrns'][$_SESSION['username']]['nickName'] = $_SESSION['nickName'];
 $toUser['olFrns'][$_SESSION['username']]['key'] = $_SESSION['key'];
 if (!$fo) {
     $spv = shm_put_var($toShmId, $toKey, $toUser);
     $sd = shm_detach($toShmId);
     $sr = sem_release($toSemId);
 } else {
     $userStr = serialize($toUser);
     $fw = fwrite($f, $userStr);
     $fc = fclose($f);
 }
 if ($spv or $fw) {
     $fn = '../userFiles/' . $_SESSION['username'] . '/live.shm';
     $f = fopen($fn, 'r+');
     $userStr = serialize($user);
     $fw = fwrite($f, $userStr);
     $fc = fclose($f);
     echo '<status>success</status><frnStatus>' . $toUser['chtStatus'] . '</frnStatus>';
 } else {
Exemplo n.º 28
0
 /**
  * set value of variable in shared mem
  *
  * @param string $name  name of the variable
  * @param string $value value of the variable
  *
  * @return bool true on success, false on fail
  * @access public
  */
 function set($name, $value)
 {
     return shm_put_var($this->_h, $this->_s2i($name), $value);
 }
Exemplo n.º 29
0
 protected function set($key, $value)
 {
     return shm_put_var($this->id, $key, $value);
 }
 /**
  * Writes the transmitted variable to shared memory
  * Subclasses may never use an id < 2!
  *
  * @param mixed $data   data which should be saved into shared memory
  * @param int   $id     int indicating the variable (bigger than 2!)
  *
  * @access protected
  * @return boolean
  */
 protected function setData($data, $id = 2)
 {
     if (isset($this->mutexid) && $this->mutexid !== false && (isset($this->memid) && $this->memid !== false)) {
         return @shm_put_var($this->memid, $id, $data);
     }
     return false;
 }