Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Y
yii2bootstrap
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
yii2bootstrap
Commits
7aef563d
Commit
7aef563d
authored
Jun 24, 2013
by
resurtm
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Proposed mutex extension.
parent
4e1fc27f
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
230 additions
and
0 deletions
+230
-0
Mutex.php
extensions/mutex/Mutex.php
+76
-0
Mutex.php
extensions/mutex/db/Mutex.php
+29
-0
Mutex.php
extensions/mutex/db/mssql/Mutex.php
+28
-0
Mutex.php
extensions/mutex/db/mysql/Mutex.php
+38
-0
Mutex.php
extensions/mutex/unix/Mutex.php
+59
-0
No files found.
extensions/mutex/Mutex.php
0 → 100644
View file @
7aef563d
<?php
namespace
yii\mutex
;
use
Yii
;
use
yii\base\Component
;
abstract
class
Mutex
extends
Component
{
public
$autoRelease
=
true
;
private
$_locks
=
array
();
public
function
init
()
{
if
(
$this
->
autoRelease
)
{
register_shutdown_function
(
array
(
$this
,
'shutdownFunction'
));
}
}
/**
* NEVER CALL THIS METHOD UNDER ANY CIRCUMSTANCES
*/
public
function
shutdownFunction
()
{
foreach
(
$this
->
_locks
as
$lock
)
{
$this
->
release
(
$lock
);
}
}
public
function
acquireLock
(
$name
,
$timeout
=
0
)
{
if
(
$this
->
acquire
(
$name
,
$timeout
))
{
$this
->
_locks
[]
=
$name
;
return
true
;
}
else
{
return
false
;
}
}
public
function
releaseLock
(
$name
)
{
if
(
$this
->
release
(
$name
))
{
unset
(
$this
->
_locks
[
array_search
(
$name
,
$this
->
_locks
)]);
return
true
;
}
else
{
return
false
;
}
}
public
function
getIsLockAcquired
(
$name
)
{
if
(
in_array
(
$name
,
$this
->
_locks
))
{
return
true
;
}
else
{
return
$this
->
getIsAcquired
(
$name
);
}
}
public
function
getIsLockLocal
(
$name
)
{
return
in_array
(
$name
,
$this
->
_locks
);
}
abstract
protected
function
acquire
(
$name
,
$timeout
=
0
);
abstract
protected
function
release
(
$name
);
protected
function
getIsAcquired
(
$name
)
{
return
null
;
}
abstract
public
function
getIsDistributed
();
}
extensions/mutex/db/Mutex.php
0 → 100644
View file @
7aef563d
<?php
namespace
yii\mutex\db
;
use
Yii
;
use
yii\db\Connection
;
use
yii\base\InvalidConfigException
;
abstract
class
Mutex
extends
\yii\mutex\Mutex
{
/**
* @var string|Connection
*/
public
$db
=
'db'
;
public
function
init
()
{
parent
::
init
();
$this
->
db
=
Yii
::
$app
->
getComponent
(
$this
->
db
);
if
(
!
$this
->
db
instanceof
Connection
)
{
throw
new
InvalidConfigException
(
''
);
}
}
public
function
getIsDistributed
()
{
return
true
;
}
}
extensions/mutex/db/mssql/Mutex.php
0 → 100644
View file @
7aef563d
<?php
namespace
yii\mutex\db\mssql
;
use
Yii
;
use
yii\base\InvalidConfigException
;
class
Mutex
extends
\yii\mutex\db\Mutex
{
public
function
init
()
{
parent
::
init
();
$driverName
=
$this
->
db
->
driverName
;
if
(
$driverName
!==
'sqlsrv'
&&
$driverName
!==
'dblib'
&&
$driverName
!==
'mssql'
)
{
throw
new
InvalidConfigException
(
''
);
}
}
protected
function
acquire
(
$name
,
$timeout
=
0
)
{
// http://msdn.microsoft.com/en-us/library/ms189823.aspx
}
protected
function
release
(
$name
)
{
// http://msdn.microsoft.com/en-us/library/ms178602.aspx
}
}
extensions/mutex/db/mysql/Mutex.php
0 → 100644
View file @
7aef563d
<?php
namespace
yii\mutex\db\mysql
;
use
Yii
;
use
yii\base\InvalidConfigException
;
class
Mutex
extends
\yii\mutex\db\Mutex
{
public
function
init
()
{
parent
::
init
();
if
(
$this
->
db
->
driverName
!==
'mysql'
)
{
throw
new
InvalidConfigException
(
''
);
}
}
protected
function
acquire
(
$name
,
$timeout
=
0
)
{
return
(
boolean
)
$this
->
db
->
createCommand
(
'SELECT GET_LOCK(:name, :timeout)'
,
array
(
':name'
=>
$name
,
':timeout'
=>
$timeout
))
->
queryScalar
();
}
protected
function
release
(
$name
)
{
return
(
boolean
)
$this
->
db
->
createCommand
(
'SELECT RELEASE_LOCK(:name)'
,
array
(
':name'
=>
$name
))
->
queryScalar
();
}
protected
function
getIsAcquired
(
$name
)
{
return
(
boolean
)
$this
->
db
->
createCommand
(
'SELECT IS_FREE_LOCK(:name)'
,
array
(
':name'
=>
$name
))
->
queryScalar
();
}
}
extensions/mutex/unix/Mutex.php
0 → 100644
View file @
7aef563d
<?php
namespace
yii\mutex\unix
;
use
Yii
;
use
yii\base\InvalidConfigException
;
class
Mutex
extends
\yii\mutex\Mutex
{
private
$_files
=
array
();
public
function
init
()
{
// if (stripos(php_uname('s'), 'win') === 0) {
// throw new InvalidConfigException('');
// }
}
protected
function
acquire
(
$name
,
$timeout
=
0
)
{
$file
=
fopen
(
Yii
::
$app
->
getRuntimePath
()
.
'/mutex.'
.
md5
(
$name
)
.
'.lock'
,
'w+'
);
if
(
$file
===
false
)
{
return
false
;
}
$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
;
}
protected
function
release
(
$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
;
}
}
protected
function
getIsAcquired
(
$name
)
{
return
false
;
}
public
function
getIsDistributed
()
{
return
false
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment