public function tearDown()
 {
     shm_remove($this->shmResource);
     shm_detach($this->shmResource);
     @unlink($this->tmpFileName);
     unset($this->shmAdapter, $this->kvs);
 }
 /**
  * @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));
 }
Example #3
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;
}
Example #4
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;
}
Example #5
0
 public function destroy()
 {
     $nbProcess = $this->updateNbProcess(-1);
     if ($nbProcess < 1) {
         shm_remove($this->memory);
     } else {
         shm_detach($this->memory);
     }
 }
 function __destruct()
 {
     if (null !== $this->shmHandle) {
         shm_detach($this->shmHandle);
     }
     if (null !== $this->semHandle) {
         sem_release($this->semHandle);
     }
 }
Example #7
0
 /**
  * @return bool
  */
 public function dettach()
 {
     $this->set($this->client_count_key, $this->get($this->client_count_key) - 1);
     //如果是最后一个使用的客户端,则删除共享内存
     if ($this->get($this->client_count_key) == 0) {
         return $this->remove();
     }
     return shm_detach($this->shm);
     //allocate shared memory
 }
Example #8
0
 /**
  * Destroys the shared memory segment
  */
 function __destruct()
 {
     if (shm_get_var($this->id, 1) == 1) {
         // I am the last listener so kill shared memory space
         $this->remove();
     } else {
         shm_detach($this->id);
         shm_put_var($this->id, 1, shm_get_var($this->id, 1) - 1);
     }
 }
 public function drop()
 {
     try {
         $shm = shm_attach($this->id, self::SEGMENT_SIZE, HESPER_IPC_PERMS);
     } catch (BaseException $e) {
         return false;
     }
     $result = shm_remove($shm);
     shm_detach($shm);
     return $result;
 }
Example #10
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);
 }
Example #11
0
 public function __construct($keyIdentifier = 'MultiPhreading', $MemorySize = 1000000, $perms = 0666)
 {
     $this->keyIdentifier = $keyIdentifier;
     $this->keyLocation = '/tmp/' . $keyIdentifier;
     if (!is_file($this->keyLocation)) {
         touch($this->keyLocation);
     }
     $ftOk = ftok($this->keyLocation, 'a');
     $this->sharedMemoryId = shm_attach($ftOk, $MemorySize, $perms);
     register_shutdown_function(function () {
         shm_detach($this->sharedMemoryId);
     });
 }
Example #12
0
 public function teardown()
 {
     // Check shm validity before shm_get_var, return directly if NULL or FALSE
     // This may happen when check_environment fail.
     if ($this->shm) {
         $lock = shm_get_var($this->shm, self::ADDRESS);
     } else {
         return;
     }
     // If this PID set this lock, release it
     if ($lock['pid'] == $this->pid) {
         shm_remove($this->shm);
         shm_detach($this->shm);
     }
 }
Example #13
0
 public function __destruct()
 {
     foreach (self::$attached as $segment) {
         shm_detach($segment);
     }
     // sync classes
     $segment = shm_attach(self::INDEX_SEGMENT, self::DEFAULT_SEGMENT_SIZE, HESPER_IPC_PERMS);
     try {
         $index = shm_get_var($segment, 1);
     } catch (BaseException $e) {
         $index = [];
     }
     try {
         shm_put_var($segment, 1, array_unique(array_merge($index, array_keys(self::$attached))));
     } catch (BaseException $e) {
         /*_*/
     }
     try {
         shm_detach($segment);
     } catch (BaseException $e) {
         /*_*/
     }
 }
 /**
  * Removes and detaches shared memory
  *
  * @access private
  * @return boolean
  */
 private function RemoveSharedMem()
 {
     if (isset($this->mutexid) && $this->mutexid !== false && (isset($this->memid) && $this->memid !== false)) {
         @sem_acquire($this->mutexid);
         $memid = $this->memid;
         $this->memid = false;
         @sem_release($this->mutexid);
         @sem_remove($this->mutexid);
         @shm_remove($memid);
         @shm_detach($memid);
         $this->mutexid = false;
         return true;
     }
     return false;
 }
Example #15
0
 function Delete()
 {
     if (!function_exists("shm_attach")) {
         $this->ok = false;
         return;
     }
     shm_remove($this->shmid);
     shm_detach($this->shmid);
 }
Example #16
0
 public function __destruct()
 {
     shm_detach($this->shm);
 }
Example #17
0
File: csp.php Project: straiway/fmt
 public function store($msg)
 {
     shm_put_var($this->shm, 1, $msg);
     shm_detach($this->shm);
 }
Example #18
0
File: Shm.php Project: millken/ypf
 public function dettach()
 {
     return shm_detach($this->shm);
 }
 public function detach()
 {
     shm_detach($this->shm_id);
     return TRUE;
 }
Example #20
0
function utils_mem_idle_close()
{
    $shmid =& $GLOBALS['CHILDRENIDLE_SHMID'];
    shm_remove($shmid);
    shm_detach($shmid);
}
Example #21
0
 /**
  * Remove this segment's contents
  *
  * @return  bool success
  * @throws  io.IOException in case an error occurs
  */
 public function remove()
 {
     $h = shm_attach($this->spot);
     $ret = shm_remove_var($h, $this->name);
     shm_detach($h);
     if (false === $ret) {
         throw new IOException('Could not remove segment ' . $this->name);
     }
     return $ret;
 }
Example #22
0
 function delete()
 {
     $this->logger->debug(sprintf("Delete shm segment (key: 0x%08x)", $this->key));
     shm_remove($this->shm);
     shm_detach($this->shm);
 }
Example #23
0
    $user['chtStatus'] = 0;
    $user['lat'] = time();
    if (isset($user['chat'])) {
        foreach ($user['chat'] as $id => $chatBlock) {
            $chatMail = '';
            for ($i = count($chatBlock['msgs']) - 1; $i >= 0; $i--) {
                $chatMail .= '<br/>' . $chatBlock['msgs'][$i]['ts'] . ':' . $chatBlock['msgs'][$i]['rs'] . ':' . $chatBlock['msgs'][$i]['msg'];
            }
            $mail = mail($_SESSION['username'] . '@ferryfair.com', 'chat', $chatMail, "From: " . $id . "@ferryfair.com\nContent-type: text/plain; charset='utf-8'\nContent-Transfer-Encoding: 8bit\n");
        }
        unset($user['chat']);
    }
    unset($user['olFrns']);
    $spv = shm_put_var($shmId, $key, $user);
    $sr = sem_release($semId);
    $sd = shm_detach($shmId);
    //$shr = shm_remove($shmId);
    $userStr = serialize($user);
    $f = fopen("{$_SERVER['DOCUMENT_ROOT']}/userFiles/" . $_SESSION['username'] . "/live.shm", 'r+');
    $fw = fwrite($f, $userStr);
    $fc = fclose($f);
}
//close live tables
if (isset($_SESSION['tables'])) {
    foreach ($_SESSION['tables'] as $tid => $value) {
        $liveDBTable = getLiveTable($tid);
        foreach ($liveDBTable['dbtUpdates'] as $key => &$update) {
            if ($update['data']['swallowedBy'][$_SESSION['uid']]) {
                unset($update['data']['swallowedBy'][$_SESSION['uid']]);
            }
            if (count($sdbtu['data']['swallowedBy']) == count($liveDBTable['usersData']) - 1) {
Example #24
0
 /**
  * When the parent process exits, cleans shared memory and semaphore.
  *
  * This is called using 'register_shutdown_function' pattern.
  * @see http://php.net/register_shutdown_function
  */
 public function onShutdown()
 {
     if (posix_getpid() == $this->_nParentPid) {
         $this->_log('INFO: Parent shutdown, cleaning memory...');
         @shm_remove($this->_hShm) && @shm_detach($this->_hShm);
         @sem_remove($this->_hSem);
     }
 }
Example #25
0
 /**
  * Disconnects from shared memory segment.
  *
  * @throws \LogicException When shared memory is not attached.
  *
  * @see checkHandler
  */
 public function detach()
 {
     $this->checkHandle();
     $this->unlock(true);
     shm_detach($this->handle);
     $this->handle = null;
     $this->numberLocks = 0;
 }
 /**
  * Detach from shared memory
  */
 public function __destruct()
 {
     if ($this->shmId) {
         shm_detach($this->shmId);
     }
 }
Example #27
0
 $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 {
     echo '<status>failed</status>';
Example #28
0
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;
}
Example #29
0
    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);
}
var_dump($ret === false);
shm_remove($index);
pcntl_waitpid($pid, $status);
Example #30
0
 /**
  * Destructor
  */
 function __destruct()
 {
     if (!function_exists('shm_has_var')) {
         throw new BaseException("No mutex support on this platform");
     }
     // Release
     if ($this->_lockstate) {
         $this->release();
     }
     Mutex::$instance--;
     if (Mutex::$instance == 0) {
         Console::debug("Destroying mutex manager");
         shm_detach(Mutex::$resource);
     }
 }