2 Replies - 1650 Views - Last Post: 12 September 2010 - 07:55 PM

#1 grimpirate   User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 717
  • Joined: 03-August 06

Mutex File Lock

Posted 15 January 2008 - 10:55 AM

Description: Simply include the mutex.php file into your application and then use the class.This function locks files without using the flock function provided by PHP, as this function does not work on certain types of filesystems.
<?php
error_reporting(E_ALL);

/* Reference Material
 *
 * http://en.wikipedia.org/wiki/ACID
 *
 */

class Mutex {
	/* Private variables */
	
	var $filename;			// The file to be locked
	var $timeout; 			// The timeout value of the lock
	var $permission = 0755;	// The permission value of the locked file
	
	/* Constructor */
	
	function Mutex($filename, $timeout = 1, $permission = null){
		// Append '.lck' extension to filename for the locking mechanism
		$this->filename = $filename . '.lck';
		// Timeout should be some factor greater than the maximum script execution time
		if($timeout < 1) $this->timeout = get_cfg_var('max_execution_time');
		else $this->timeout = $timeout * get_cfg_var('max_execution_time');
		// Should some other permission value be necessary
		if(isset($permission)) $this->permission = $permission;
	}
	
	/* Methods */
	
	function acquireLock(){
		// Create the locked file, the 'x' parameter is used to detect a preexisting lock
		$fp = @fopen($this->filename, 'x');
		// If an error occurs fail lock
		if($fp === false) return false;
		// If the permission set is unsuccessful fail lock
		if(!@chmod($this->filename, $this->permission)) return false;
		// If unable to write the timeout value fail lock
		if(false === @fwrite($fp, time() + intval($this->timeout))) return false;
		// If lock is successfully closed validate lock
		return fclose($fp);
	}
	
	function releaseLock(){
		// Delete the file with the extension '.lck'
		return @unlink($this->filename);
	}
	
	function timeLock(){
		// Retrieve the contents of the lock file
		$timeout = @file_get_contents($this->filename);
		// If no contents retrieved return error
		if($timeout === false) return false;
		// Return the timeout value
		return intval($timeout);
	}
}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Mutex File Lock

#2 cheako   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 12-September 10

Re: Mutex File Lock

Posted 12 September 2010 - 12:21 PM

http://lwn.net/Articles/251004/ According to this article: "Create the locked file, the 'x' parameter is used to detect a preexisting lock." is not sufficient using NFS and I'd imagine elsewhere as well. The proposed solution is to use tempnam/link/stat(linkcount).
Was This Post Helpful? 0
  • +
  • -

#3 grimpirate   User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 717
  • Joined: 03-August 06

Re: Mutex File Lock

Posted 12 September 2010 - 07:55 PM

Agreed that if this article is valid then the 'x' flag will not work. However, tempnam, link, and stat only decrease the odds of an overwritten lock occurring, they do not eliminate the possibility entirely. Instead I will suggest people remedy the situation by using mkdir and using a directory as the locking mechanism.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1