summaryrefslogblamecommitdiffstats
path: root/notFinishedCode/web/mutex1.php
blob: 488a9facd100cee361ba9774e972bf2d23bde933 (plain) (tree)




























































                                                                                     
<?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'randompasswordSQL';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');

$dbname = 'mutexDB';
mysql_select_db($dbname);
//$seconds = 10;

function Lock($seconds)
 {
  $sql = "SELECT GET_LOCK('Locked', $seconds);"; 
  $res = mysql_query($sql);
  return showValue($res);
}

function IsLocked()
 {
  $sql = "SELECT IS_FREE_LOCK('Locked');";
  $res = mysql_query($sql);
  return showValue($res);
}

function ReleaseLock()
 {
  $sql = "SELECT RELEASE_LOCK('Locked');";
  $res = mysql_query($sql);
  return showValue($res);
}

function showValue($res){
 if (!$res) {
    die('Could not query:' . mysql_error());
  }
  $num=mysql_numrows($res);
  $i=0;
  $first=mysql_result($res,$i);
  return $first;
}

$locking = isLocked();
if ($locking == '1')
{
 $lockMe = Lock(50);
 echo $lockMe . "<br>";
 sleep(10);
 $release = ReleaseLock();
 echo $release . "<br>";
}
elseif ($locking == '0')
{
 echo 'Somebody is using the web site already!' . "<br>";
}
else
{
 echo 'We have some serious error here';
}
?>