summaryrefslogtreecommitdiffstats
path: root/notFinishedCode/web/mutex1.php
diff options
context:
space:
mode:
Diffstat (limited to 'notFinishedCode/web/mutex1.php')
-rw-r--r--notFinishedCode/web/mutex1.php61
1 files changed, 61 insertions, 0 deletions
diff --git a/notFinishedCode/web/mutex1.php b/notFinishedCode/web/mutex1.php
new file mode 100644
index 0000000..488a9fa
--- /dev/null
+++ b/notFinishedCode/web/mutex1.php
@@ -0,0 +1,61 @@
+<?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';
+}
+?>