You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.5 KiB

<?php
/*
* Created on 12.04.2008
* Created by Hecht
*
* @copyright (c) 2010 animegame.eu
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public Licence
*
*/
/*
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Eine spezielle Semaphore, die sich von gewoehnlichen Semaphoren unterscheidet
// @Returns TRUE if ok, FALSE if not, NULL on error
$GLOBALS['semaphorecount'] = 0;
/**
* Tries to grab the semaphore
* @param string $resource the name of the resource
*/
function semaphoreUP($resource){
// echo 'semaphoreUp('.$ressource.')<br>' . PHP_EOL ;
if($GLOBALS['semaphorecount'] == 0){
ignore_user_abort(true); // Verarbeitung darf NICHT abgebrochen werden!!
// echo 'DEBUG: kritischer Bereich betreten<br>';
}
// Loesche alte Ressourcen
mysql_query('DELETE FROM semaphore WHERE TIMESTAMPDIFF(MINUTE, zeit, now()) > 5 and ressource = \''.$ressource.'\'');
$sql = 'INSERT INTO semaphore(ressource) values(\''.$ressource.'\')';
mysql_query($sql);
if(mysql_affected_rows() > 0){
// echo 'DEBUG: Semaphore fuer '.$ressource.' belegt!('.mysql_affected_rows().')<br>';
$GLOBALS['semaphorecount']++;
return true;
}
// echo 'DEBUG:Semaphore fuer '.$ressource.' NICHT belegt!<br>';
if($GLOBALS['semaphorecount'] == 0){
// echo 'DEBUG: Verlassen des kritischen Bereichs<br>';
ignore_user_abort(false); // Verarbeitung darf wieder abgebrochen werden!!
}
return false;
}
// @Returns TRUE if ok, FALSE on Semaphore error, NULL on MYQL Error
function semaphoreDown($ressource){
$sql = 'DELETE FROM semaphore where ressource = \''.$ressource.'\'';
mysql_query($sql);
if(mysql_affected_rows() == 0){
// echo 'DEBUG: Semaphore '.$ressource.' war schon freigegeben<br>';
return false;
}
// echo 'DEBUG: Semaphore '.$ressource.' freigegeben<br>';
$GLOBALS['semaphorecount']--;
if($GLOBALS['semaphorecount'] == 0){
// echo 'DEBUG: kritischer Bereich verlassen<br>';
ignore_user_abort(false); // Verarbeitung darf nun wieder abgebrochen werden!!
}
return true;
}
/**
* Diese Funktion ist nur dazu da um zu pruefen ob ueberhaupt ein Lock besteht,
* ohne die Intension die Ressource selbst zu belegen!
* returns true, wenn Semaphore belegt ist, false wenn nicht
*/
function isSemaphoreUP($ressource){
return mysql_fetch_assoc(mysql_query('Select * from semaphore where WHERE TIMESTAMPDIFF(MINUTE, zeit, now()) > 5 and ressource = \''.$ressource.'\'')) != false;
}
function installSemaphoreDatabase(){
$sql1 = 'DROP TABLE `semaphore`';
$sql2 = 'CREATE TABLE `semaphore` (' .
'`ressource` varchar(20) NOT NULL,' .
'`zeit` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,' .
'PRIMARY KEY(`ressource`),' .
'INDEX `Zeitindex`(`zeit`)' .
')' .
'ENGINE = MYISAM ' .
'COMMENT = \'Die Tabelle, die hoffentlich alle Zugriffsprobleme loesen wird\'';
mysql_query($sql1);
mysql_query($sql2);
}
?>