[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/include/ -> class.lock.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
   3  // +----------------------------------------------------------------------+
   4  // | Eventum - Issue Tracking System                                      |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 2003, 2004, 2005, 2006, 2007 MySQL AB                  |
   7  // |                                                                      |
   8  // | This program is free software; you can redistribute it and/or modify |
   9  // | it under the terms of the GNU General Public License as published by |
  10  // | the Free Software Foundation; either version 2 of the License, or    |
  11  // | (at your option) any later version.                                  |
  12  // |                                                                      |
  13  // | This program is distributed in the hope that it will be useful,      |
  14  // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  15  // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
  16  // | GNU General Public License for more details.                         |
  17  // |                                                                      |
  18  // | You should have received a copy of the GNU General Public License    |
  19  // | along with this program; if not, write to:                           |
  20  // |                                                                      |
  21  // | Free Software Foundation, Inc.                                       |
  22  // | 59 Temple Place - Suite 330                                          |
  23  // | Boston, MA 02111-1307, USA.                                          |
  24  // +----------------------------------------------------------------------+
  25  // | Authors: João Prado Maia <jpm@mysql.com>                             |
  26  // +----------------------------------------------------------------------+
  27  //
  28  // @(#) $Id: class.lock.php 3246 2007-02-09 09:10:12Z glen $
  29  //
  30  
  31  
  32  class Lock
  33  {
  34      /**
  35       * Creates a lock file for the given name.
  36       *
  37       * @access  public
  38       * @param   string $name The name of this lock file
  39       * @return  boolean
  40       */
  41      function acquire($name)
  42      {
  43          $pid = Lock::getProcessID($name);
  44          if (!empty($pid)) {
  45              return false;
  46          } else {
  47              // create the pid file
  48              $fp = @fopen(Lock::_getProcessFilename($name), 'w');
  49              @flock($fp, LOCK_EX);
  50              @fwrite($fp, getmypid());
  51              @flock($fp, LOCK_UN);
  52              @fclose($fp);
  53              return true;
  54          }
  55      }
  56  
  57  
  58      /**
  59       * Removes the process file to allow other instances of this 
  60       * script to run.
  61       *
  62       * @access  public
  63       * @param   string $name The name of this lock file
  64       * @return  void
  65       */
  66      function release($name)
  67      {
  68          @unlink(Lock::_getProcessFilename($name));
  69      }
  70  
  71  
  72      /**
  73       * Returns the full path to the file that keeps the process 
  74       * ID of the running script.
  75       *
  76       * @access  private
  77       * @param   string $name The name of this lock file
  78       * @return  string The full path of the process file
  79       */
  80      function _getProcessFilename($name)
  81      {
  82          return APP_LOCKS_PATH . $name . '.pid';
  83      }
  84  
  85  
  86      /**
  87       * Returns the process ID of the script, if any.
  88       *
  89       * @access  public
  90       * @param   string $name The name of this lock file
  91       * @return  integer The process ID of the script
  92       */
  93      function getProcessID($name)
  94      {
  95          static $pids;
  96  
  97          // poor man's cache system
  98          if (!empty($pids[$name])) {
  99              return $pids[$name];
 100          }
 101  
 102          $pid_file = Lock::_getProcessFilename($name);
 103          if (!file_exists($pid_file)) {
 104              return 0;
 105          } else {
 106              $pids[$name] = trim(implode('', file($pid_file)));
 107              return $pids[$name];
 108          }
 109      }
 110  }
 111  
 112  // benchmarking the included file (aka setup time)
 113  if (APP_BENCHMARK) {
 114      $GLOBALS['bench']->setMarker('Included Lock Class');
 115  }


Generated: Wed Dec 19 21:21:33 2007 Cross-referenced by PHPXref 0.7