Skip to content

Instantly share code, notes, and snippets.

@ahwayakchih
Created June 23, 2011 18:03
Show Gist options
  • Save ahwayakchih/1043145 to your computer and use it in GitHub Desktop.
Save ahwayakchih/1043145 to your computer and use it in GitHub Desktop.
Test new implementation of Mutex class for Symphony CMS
<?php
/*
* Just upload it to root directory of Symphony CMS installation and access it through web browser, e.g.,
* http://www.example.com/mutex.php
*
* It should output text like:
*
* Try: http://www.example.com/mutex.php?mode=block
* Blocking: Acquired
*
* Try: http://www.example.com/mutex.php?mode=quick
* Quick: Other thread beat us to it
*
* Try: http://www.example.com/mutex.php?mode=quick
* Quick: Other thread beat us to it
*
* Try: http://www.example.com/mutex.php?mode=quick
* Quick: Other thread beat us to it
*
* Try: http://www.example.com/mutex.php?mode=quick
* Quick: Other thread beat us to it
*
* Try: http://www.example.com/mutex.php?mode=quick
* Quick: Other thread beat us to it
*
* Try: http://www.example.com/mutex.php?mode=quick
* Quick: Acquired and released
*
* Try: http://www.example.com/mutex.php?mode=quick
* Quick: Acquired and released
*
* Try: http://www.example.com/mutex.php?mode=quick
* Quick: Acquired and released
*
* Finished
*/
define('DOCROOT', rtrim(dirname(__FILE__), '\\/'));
define('DOMAIN', rtrim(rtrim($_SERVER['HTTP_HOST'], '\\/') . dirname($_SERVER['PHP_SELF']), '\\/'));
define('SCHEMA', (empty($_SERVER['https']) || $_SERVER['https'] == 'off' ? 'http://' : 'https://'));
ini_set("display_errors","2");
ERROR_REPORTING(E_ALL);
include('symphony/lib/toolkit/class.gateway.php');
include('symphony/lib/toolkit/class.mutex.php');
header('Content-Type: text/plain');
$mode = (empty($_GET['mode']) ? '' : $_GET['mode']);
switch ($mode) {
case 'block':
$locked = Mutex::acquire('test');
ignore_user_abort(true);
set_time_limit(0);
header("Connection: close");
$message = ($locked ? "Blocking: Acquired\n" : "Blocking: failed\n");
header("Content-Length: ".strlen($message));
echo $message;
echo str_repeat("\r\n", 10); // just to be sure
flush();
sleep(5);
if ($locked) {
$locked = Mutex::release('test');
}
break;
case 'quick':
$locked = Mutex::acquire('test');
if ($locked) {
$locked = Mutex::release('test');
echo 'Quick: Acquired and '.($locked ? "released\n" : "left locked\n");
}
else {
echo "Quick: Other thread beat us to it\n";
}
break;
default:
$result = '';
$ch = new Gateway;
$ch->setopt('TIMEOUT', 3);
echo "Try: " . SCHEMA . DOMAIN . $_SERVER['PHP_SELF'] . "?mode=block\n";
$ch->init(SCHEMA . DOMAIN . $_SERVER['PHP_SELF'] . '?mode=block');
echo $ch->exec() . "\n";
for ($i = 7; $i >= 0; $i--) {
echo "Try: " . SCHEMA . DOMAIN . $_SERVER['PHP_SELF'] . "?mode=quick\n";
$ch->init(SCHEMA . DOMAIN . $_SERVER['PHP_SELF'] . '?mode=quick');
echo $ch->exec() . "\n";
if ($i > 0) sleep(1);
}
echo "Finished\n";
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment