Login   Register  
PHP Classes
elePHPant
Icontem

File: sharedmemory.class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Richard Keizer  >  Shared Memory  >  sharedmemory.class.php  >  Download  
File: sharedmemory.class.php
Role: Class source
Content type: text/plain
Description: Mainclass
Class: Shared Memory
Store and retrieve information in shared memory
Author: By
Last change: comment tweaks
Date: 2011-10-26 07:49
Size: 1,531 bytes
 

Contents

Class file image Download
<?php

    
// 
    // SharedMemory class 
    // 
    // Freeware 
    // you can use this freely without any warranty 
    // comments are appreciated: ra dot keizer at gmail dot com 


    
class SharedMemory {
        public 
$key;            //unique identifier for the shared memory block
        
public $shm;            //holds shared memory resource
        
public $mutex;            //holds the mutex
        
public $size;            //bytes to allocate
        
        
public function __construct($key=3354354334$size=10000) {        //default key, can be overridden, same for size
            
$this->key $key;
            
$this->size $size;
            
$this->Attach();    //create resources (shared memory + mutex)
        
}
        
        
//create resources
        
public function Attach() {
            
$this->shm shm_attach($this->key$this->size);    //allocate shared memory
            
$this->mutex sem_get($this->key1);        //create mutex with same key
        
}
        
        
//write to shared memory
        
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    
        
}
        
        
//read from shared memory
        
public function Get() {
            
sem_acquire($this->mutex);    //block until released
            
$var = @shm_get_var($this->shm$this->key);    //read var        
            
sem_release($this->mutex);    //release mutex
            
return $var;        
        }

        
// in case you want to serialize/unserialize the sharedmemory object 
        // this method restores all resources after unserializing
        
public function __wakeup() {
            
$this->Attach();
        }

    }





?>