log_shme() function added and DBG_SHME flag
[brisk.git] / web / briskin5 / Obj / briskin5.phh
index 718fabd..ce2bdb8 100644 (file)
@@ -528,7 +528,7 @@ class Bin5_user extends User {
                 break;
             
             if (file_exists(BIN5_PROXY_PATH) == FALSE)
-                mkdir(BIN5_PROXY_PATH);
+                mkdir(BIN5_PROXY_PATH, 0775, TRUE);
             if (($fp = @fopen(BIN5_PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
                 break;
             if (($s = fread($fp, 8)) == FALSE)
@@ -575,11 +575,9 @@ class Bin5_user extends User {
     {
         log_main("Bin5_user::load_data: tab_id [".$tab_id."] id [".$id."] sess [".($sess == FALSE ? "FALSE" : $sess)."] ");
         
-        $doexit = FALSE;
         do {
             if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$id, "B")) == -1) {
                 log_main("ftok failed");
-                $doexit = TRUE;
                 break;
             }
             
@@ -591,10 +589,11 @@ class Bin5_user extends User {
                 $shm_sz = SHM_DIMS_U_MIN;
             
             if ($shm = shm_attach($tok, $shm_sz)) {
-                $user = @shm_get_var($shm, $tok);
+                if (($user = @shm_get_var($shm, $tok)) == FALSE) {
+                    break;
+                }
                 
                 if ($sess != FALSE && $user->sess != $sess) {
-                    $doexit = TRUE;
                     break;
                 }
                 log_only("user ==  ".($user == FALSE ?   "FALSE" : "TRUE")."  user ===  ".($user === FALSE ? "FALSE" : "TRUE")."  user isset ".(isset($user) ?   "TRUE" : "FALSE"));
@@ -605,6 +604,8 @@ class Bin5_user extends User {
                     // SHSPLIT FIXME: init_data for User class ??
                     $user = User::create($id, "", "");
                     if (@shm_put_var($shm, $tok, $user) == FALSE) {
+                        log_shme("Bin5_user::save_data2");
+
                         log_only("PUT_VAR FALLITA ".strlen(serialize($user)));
                         log_only(serialize($user));
                     }
@@ -634,8 +635,7 @@ class Bin5_user extends User {
             return ($user);
         } while (0);
         
-        if ($doexit)
-            exit();
+        log_crit("Bin5_user::load_data:ret FALSE");
         
         return (FALSE);
     }
@@ -654,10 +654,13 @@ class Bin5_user extends User {
               break;
           
           // log_only("PUT_VAR DI ".strlen(serialize($user)));
-          if (shm_put_var($shm, $tok, $user) != FALSE) {
+          if (@shm_put_var($shm, $tok, $user) != FALSE) {
               shm_detach($shm);
               $user->save_step();
               log_main("User[".$id."] saved.");
+
+              log_shme("Bin5_user::save_data");
+
               return (TRUE);
           }
           if (shm_remove($shm) === FALSE) {
@@ -679,6 +682,8 @@ class Bin5_user extends User {
 
 
 class Bin5 {
+    static $delta_t = array();
+
   var $user;
   var $table;
   var $comm; // commands for many people
@@ -807,7 +812,6 @@ class Bin5 {
   // Bin5::load_data
   static function load_data($table_idx, $table_token = "") 
   {
-    $doexit = FALSE;
     $shm = FALSE;
 
     log_wr("TABLE_FTOK ".FTOK_PATH."/bin5/table".$table_idx."/table");
@@ -815,7 +819,6 @@ class Bin5 {
     do {
       if (($tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
        log_main("ftok failed");
-       $doexit = TRUE;
        break;
       }
 
@@ -839,7 +842,13 @@ class Bin5 {
       shm_detach($shm);
       
       for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
-          $bri->user[$i] = Bin5_user::load_data($table_idx, $i, FALSE);
+          if (($bri->user[$i] = Bin5_user::load_data($table_idx, $i, FALSE)) == FALSE) {
+              log_crit("Bin5_user::load_data failed");
+              break;
+          }
+      }
+      if ($i < BIN5_MAX_PLAYERS) {
+          break;
       }
       
       return ($bri); 
@@ -849,8 +858,6 @@ class Bin5 {
       shm_detach($shm);
 
     log_wr("briskin5 load_data failed");
-    if ($doexit)
-        exit();
     
     return (FALSE);
   }
@@ -880,6 +887,9 @@ class Bin5 {
               break;
           
           if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
+
+              log_shme("Bin5::save_data");
+
               $ret = TRUE;
               break;
           }
@@ -945,7 +955,7 @@ class Bin5 {
   {
     GLOBAL $sess; 
     
-    log_lock("LOCK_DATA ".FTOK_PATH."/bin5/table".$table_idx."/table");
+    log_main("lockinfo: DATA ".FTOK_PATH."/bin5/table".$table_idx."/table");
     //  echo "LOCK: ".FTOK_PATH."/main";
     //  exit;
     // WARNING monitor this step
@@ -957,20 +967,25 @@ class Bin5 {
       return (FALSE);
     }
     if (sem_acquire($res)) {   
-      log_lock("LOCK table ".$table_idx."[res: ".$res."]");
-      return ($res);
+        self::$delta_t[$table_idx] = microtime(TRUE);
+        log_lock("LOCK   table [".$table_idx."]         [".self::$delta_t[$table_idx]."]");
+
+        return (new Vect(array('res' => $res, 'tab' => $table_idx)));
     }
     else {
-      log_lock("LOCK table ".$table_idx.":FAILED");
-      return (FALSE);
+        log_lock("LOCK table ".$table_idx.":FAILED");
+        return (FALSE);
     }
   }
   
-  function unlock_data($res)
+  function unlock_data($res_vect)
   {
     GLOBAL $sess; 
-    
-    log_lock("UNLOCK table [res: ".$res."]");
+
+    $res = $res_vect->getbyid('res');
+    $tab = $res_vect->getbyid('tab');
+
+    log_lock("UNLOCK table [".$tab."]         [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
 
     return (sem_release($res));
   }
@@ -1153,7 +1168,7 @@ class Bin5 {
       $fp = FALSE;
       do {
           if (file_exists(BIN5_PROXY_PATH) == FALSE)
-              mkdir(BIN5_PROXY_PATH);
+              mkdir(BIN5_PROXY_PATH, 0775, TRUE);
           if (($fp = @fopen(BIN5_PROXY_PATH."/garbage_time.expired", 'rb')) == FALSE)
               break;
           if (($s = fread($fp, 4)) == FALSE)
@@ -1177,7 +1192,7 @@ class Bin5 {
   {
       do {
           if (file_exists(BIN5_PROXY_PATH) == FALSE)
-              mkdir(BIN5_PROXY_PATH);
+              mkdir(BIN5_PROXY_PATH, 0775, TRUE);
           if (($fp = @fopen(BIN5_PROXY_PATH."/garbage_time.expired", 'wb')) == FALSE)
               break;
           fwrite($fp, pack("L",$tm));