\Imi\UtilAtomicManager

原子计数管理类

Summary

Methods
Properties
Constants
init()
addName()
setNames()
getNames()
getInstance()
add()
sub()
get()
set()
cmpset()
wait()
wakeup()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
$isInited
$atomics
N/A

Properties

$isInited

$isInited : boolean

是否已初始化过

Type

boolean

$atomics

$atomics : array<mixed,\Swoole\Atomic>

\Swoole\Atomic 数组

Type

array<mixed,\Swoole\Atomic>

Methods

init()

init() : void

初始化

addName()

addName(string  $name, \Imi\Util\int  $initValue) : void

增加原子计数对象名称

Parameters

string $name
\Imi\Util\int $initValue

setNames()

setNames(array<mixed,string>  $names) : void

设置原子计数对象名称

Parameters

array<mixed,string> $names

getNames()

getNames() : void

获取所有原子计数对象名称

getInstance()

getInstance(string  $name) : \Swoole\Atomic

获取原子计数类实例

Parameters

string $name

Returns

\Swoole\Atomic

add()

add(string  $name, integer  $value = 1) : integer

增加计数,返回结果数值

Parameters

string $name

原子计数对象名称

integer $value

要增加的数值,默认为1。与原值相加如果超过42亿,将会溢出,高位数值会被丢弃

Returns

integer

sub()

sub(string  $name, integer  $value = 1) : integer

减少计数,返回结果数值

Parameters

string $name

原子计数对象名称

integer $value

要减少的数值,默认为1。与原值相加如果低于0将会溢出,高位数值会被丢弃

Returns

integer

get()

get(string  $name) : integer

获取当前计数的值

Parameters

string $name

原子计数对象名称

Returns

integer

set()

set(string  $name, integer  $value) : void

将当前值设置为指定的数字。

Parameters

string $name

原子计数对象名称

integer $value

cmpset()

cmpset(string  $name, integer  $cmpValue, integer  $setValue) : boolean

如果当前数值等于$cmpValue返回true,并将当前数值设置为$setValue 如果不等于返回false

Parameters

string $name

原子计数对象名称

integer $cmpValue
integer $setValue

Returns

boolean

wait()

wait(string  $name, float  $timeout = -1) : boolean

当原子计数的值为0时程序进入等待状态。另外一个进程调用wakeup可以再次唤醒程序。底层基于Linux Futex实现,使用此特性,可以仅用4字节内存实现一个等待、通知、锁的功能。 超时返回false,错误码为EAGAIN,可使用swoole_errno函数获取 成功返回true,表示有其他进程通过wakeup成功唤醒了当前的锁 使用wait/wakeup特性时,原子计数的值只能为0或1,否则会导致无法正常使用 当然原子计数的值为1时,表示不需要进入等待状态,资源当前就是可用。wait函数会立即返回true

Parameters

string $name

原子计数对象名称

float $timeout

指定超时时间,默认为-1,表示永不超时,会持续等待直到有其他进程唤醒

Returns

boolean

wakeup()

wakeup(string  $name, integer  $n = 1) : boolean

唤醒处于wait状态的其他进程。 当前原子计数如果为0时,表示没有进程正在wait,wakeup会立即返回true 当前原子计数如果为1时,表示当前有进程正在wait,wakeup会唤醒等待的进程,并返回true 如果同时有多个进程处于wait状态,$n参数可以控制唤醒的进程数量 被唤醒的进程返回后,会将原子计数设置为0,这时可以再次调用wakeup唤醒其他正在wait的进程

Parameters

string $name

原子计数对象名称

integer $n

Returns

boolean