moved mutex back from extensions.

This commit is contained in:
Qiang Xue
2013-11-02 14:16:38 -04:00
parent 1d092d1755
commit 13c3123ca8
7 changed files with 0 additions and 101 deletions

View File

@@ -1,32 +0,0 @@
The Yii framework is free software. It is released under the terms of
the following BSD License.
Copyright © 2008-2013 by Yii Software LLC (http://www.yiisoft.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Yii Software LLC nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

View File

@@ -1,42 +0,0 @@
Yii 2.0 Public Preview - Mutex Extension
========================================
Thank you for choosing Yii - a high-performance component-based PHP framework.
If you are looking for a production-ready PHP framework, please use
[Yii v1.1](https://github.com/yiisoft/yii).
Yii 2.0 is still under heavy development. We may make significant changes
without prior notices. **Yii 2.0 is not ready for production use yet.**
[![Build Status](https://secure.travis-ci.org/yiisoft/yii2.png)](http://travis-ci.org/yiisoft/yii2)
This is the yii2-mutex extension.
Installation
------------
The prefered way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
php composer.phar require yiisoft/yii2-mutex "*"
```
or add
```
"yiisoft/yii2-mutex": "*"
```
to the require section of your composer.json.
*Note: You might have to run `php composer.phar selfupdate`*
Usage & Documentation
---------------------
This component can be used to perform actions similar to the concept of [mutual exclusion](http://en.wikipedia.org/wiki/Mutual_exclusion).
For concrete examples and advanced usage refer to the yii guide.

View File

@@ -1,27 +0,0 @@
{
"name": "yiisoft/yii2-mutex",
"description": "Mutual exclusion extension for the Yii framework",
"keywords": ["yii", "mutex"],
"type": "yii2-extension",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/yiisoft/yii2/issues?state=open",
"forum": "http://www.yiiframework.com/forum/",
"wiki": "http://www.yiiframework.com/wiki/",
"irc": "irc://irc.freenode.net/yii",
"source": "https://github.com/yiisoft/yii2"
},
"authors": [
{
"name": "resurtm",
"email": "resurtm@gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"yiisoft/yii2": "*"
},
"autoload": {
"psr-0": { "yii\\mutex\\": "" }
}
}

View File

@@ -1,41 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mutex;
use Yii;
use yii\db\Connection;
use yii\base\InvalidConfigException;
/**
* @author resurtm <resurtm@gmail.com>
* @since 2.0
*/
abstract class DbMutex extends Mutex
{
/**
* @var Connection|string the DB connection object or the application component ID of the DB connection.
* After the Mutex object is created, if you want to change this property, you should only assign
* it with a DB connection object.
*/
public $db = 'db';
/**
* Initializes generic database table based mutex implementation.
* @throws InvalidConfigException if [[db]] is invalid.
*/
public function init()
{
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException('Mutex::db must be either a DB connection instance or the application component ID of a DB connection.');
}
}
}

View File

@@ -1,104 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mutex;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\FileHelper;
/**
* @author resurtm <resurtm@gmail.com>
* @since 2.0
*/
class FileMutex extends Mutex
{
/**
* @var string the directory to store mutex files. You may use path alias here.
* Defaults to the "mutex" subdirectory under the application runtime path.
*/
public $mutexPath = '@runtime/mutex';
/**
* @var integer the permission to be set for newly created mutex files.
* This value will be used by PHP chmod() function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
*/
public $fileMode;
/**
* @var integer the permission to be set for newly created directories.
* This value will be used by PHP chmod() function. No umask will be applied.
* Defaults to 0775, meaning the directory is read-writable by owner and group,
* but read-only for other users.
*/
public $dirMode = 0775;
/**
* @var resource[] stores all opened lock files. Keys are lock names and values are file handles.
*/
private $_files = [];
/**
* Initializes mutex component implementation dedicated for UNIX, GNU/Linux, Mac OS X, and other UNIX-like
* operating systems.
* @throws InvalidConfigException
*/
public function init()
{
if (stripos(php_uname('s'), 'win') === 0) {
throw new InvalidConfigException('FileMutex does not have MS Windows operating system support.');
}
$this->mutexPath = Yii::getAlias($this->mutexPath);
if (!is_dir($this->mutexPath)) {
FileHelper::createDirectory($this->mutexPath, $this->dirMode, true);
}
}
/**
* Acquires lock by given name.
* @param string $name of the lock to be acquired.
* @param integer $timeout to wait for lock to become released.
* @return boolean acquiring result.
*/
protected function acquireLock($name, $timeout = 0)
{
$fileName = $this->mutexPath . '/' . md5($name) . '.lock';
$file = fopen($fileName, 'w+');
if ($file === false) {
return false;
}
if ($this->fileMode !== null) {
@chmod($fileName, $this->fileMode);
}
$waitTime = 0;
while (!flock($file, LOCK_EX | LOCK_NB)) {
$waitTime++;
if ($waitTime > $timeout) {
fclose($file);
return false;
}
sleep(1);
}
$this->_files[$name] = $file;
return true;
}
/**
* Releases lock by given name.
* @param string $name of the lock to be released.
* @return boolean release result.
*/
protected function releaseLock($name)
{
if (!isset($this->_files[$name]) || !flock($this->_files[$name], LOCK_UN)) {
return false;
} else {
fclose($this->_files[$name]);
unset($this->_files[$name]);
return true;
}
}
}

View File

@@ -1,95 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mutex;
use Yii;
use yii\base\Component;
/**
* @author resurtm <resurtm@gmail.com>
* @since 2.0
*/
abstract class Mutex extends Component
{
/**
* @var boolean whether all locks acquired in this process (i.e. local locks) must be released automagically
* before finishing script execution. Defaults to true. Setting this property to true means that all locks
* acquire in this process must be released in any case (regardless any kind of errors or exceptions).
*/
public $autoRelease = true;
/**
* @var string[] names of the locks acquired in the current PHP process.
*/
private $_locks = [];
/**
* Initializes the mutex component.
*/
public function init()
{
if ($this->autoRelease) {
$locks = &$this->_locks;
register_shutdown_function(function () use (&$locks) {
foreach ($locks as $lock) {
$this->release($lock);
}
});
}
}
/**
* Acquires lock by given name.
* @param string $name of the lock to be acquired. Must be unique.
* @param integer $timeout to wait for lock to be released. Defaults to zero meaning that method will return
* false immediately in case lock was already acquired.
* @return boolean lock acquiring result.
*/
public function acquire($name, $timeout = 0)
{
if ($this->acquireLock($name, $timeout)) {
$this->_locks[] = $name;
return true;
} else {
return false;
}
}
/**
* Release acquired lock. This method will return false in case named lock was not found.
* @param string $name of the lock to be released. This lock must be already created.
* @return boolean lock release result: false in case named lock was not found..
*/
public function release($name)
{
if ($this->releaseLock($name)) {
$index = array_search($name, $this->_locks);
if ($index !== false) {
unset($this->_locks[$index]);
}
return true;
} else {
return false;
}
}
/**
* This method should be extended by concrete mutex implementations. Acquires lock by given name.
* @param string $name of the lock to be acquired.
* @param integer $timeout to wait for lock to become released.
* @return boolean acquiring result.
*/
abstract protected function acquireLock($name, $timeout = 0);
/**
* This method should be extended by concrete mutex implementations. Releases lock by given name.
* @param string $name of the lock to be released.
* @return boolean release result.
*/
abstract protected function releaseLock($name);
}

View File

@@ -1,57 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mutex;
use Yii;
use yii\base\InvalidConfigException;
/**
* @author resurtm <resurtm@gmail.com>
* @since 2.0
*/
class MysqlMutex extends Mutex
{
/**
* Initializes MySQL specific mutex component implementation.
* @throws InvalidConfigException if [[db]] is not MySQL connection.
*/
public function init()
{
parent::init();
if ($this->db->driverName !== 'mysql') {
throw new InvalidConfigException('In order to use MysqlMutex connection must be configured to use MySQL database.');
}
}
/**
* Acquires lock by given name.
* @param string $name of the lock to be acquired.
* @param integer $timeout to wait for lock to become released.
* @return boolean acquiring result.
* @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock
*/
protected function acquireLock($name, $timeout = 0)
{
return (boolean)$this->db
->createCommand('SELECT GET_LOCK(:name, :timeout)', [':name' => $name, ':timeout' => $timeout])
->queryScalar();
}
/**
* Releases lock by given name.
* @param string $name of the lock to be released.
* @return boolean release result.
* @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock
*/
protected function releaseLock($name)
{
return (boolean)$this->db
->createCommand('SELECT RELEASE_LOCK(:name)', [':name' => $name])
->queryScalar();
}
}