|
|
@ -18,13 +18,14 @@ lockApply(KeyOrKeys, MFAOrFun, TimeOut) -> |
|
|
|
case is_list(KeyOrKeys) of |
|
|
|
true -> |
|
|
|
KeyPids = [{OneKey, Pid} || OneKey <- KeyOrKeys], |
|
|
|
lockApplys(KeyPids, KeyOrKeys, MFAOrFun, TimeOut, erlang:system_time(millisecond)); |
|
|
|
[FirstKey | _] = KeyOrKeys, |
|
|
|
lockApplys(KeyPids, KeyOrKeys, FirstKey, MFAOrFun, TimeOut); |
|
|
|
_ -> |
|
|
|
lockApply({KeyOrKeys, Pid}, KeyOrKeys, MFAOrFun, TimeOut, erlang:system_time(millisecond)) |
|
|
|
lockApply({KeyOrKeys, Pid}, KeyOrKeys, MFAOrFun, TimeOut) |
|
|
|
end. |
|
|
|
|
|
|
|
-define(CASE(Cond, Then, That), case Cond of true -> Then; _ -> That end). |
|
|
|
lockApply(KeyPid, Key, MFAOrFun, TimeOut, FirstTime) -> |
|
|
|
lockApply(KeyPid, Key, MFAOrFun, TimeOut) -> |
|
|
|
case ets:insert_new(?EtsGLockKey, KeyPid) of |
|
|
|
true -> |
|
|
|
try doApply(MFAOrFun) |
|
|
@ -36,33 +37,28 @@ lockApply(KeyPid, Key, MFAOrFun, TimeOut, FirstTime) -> |
|
|
|
ok |
|
|
|
end; |
|
|
|
_ -> |
|
|
|
loopTry(KeyPid, Key, MFAOrFun, TimeOut, FirstTime) |
|
|
|
loopTry(KeyPid, Key, MFAOrFun, TimeOut) |
|
|
|
end. |
|
|
|
|
|
|
|
loopTry(KeyPid, Key, MFAOrFun, TimeOut, FirstTime) -> |
|
|
|
loopTry(KeyPid, Key, MFAOrFun, TimeOut) -> |
|
|
|
receive |
|
|
|
after ?ReTryTime -> |
|
|
|
case ets:lookup(?EtsGLockKey, Key) of |
|
|
|
[] -> |
|
|
|
lockApply(KeyPid, Key, MFAOrFun, TimeOut, FirstTime); |
|
|
|
_ -> |
|
|
|
case TimeOut of |
|
|
|
infinity -> |
|
|
|
loopTry(KeyPid, Key, MFAOrFun, TimeOut, FirstTime); |
|
|
|
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTime), |
|
|
|
case LTimeOut >= 0 of |
|
|
|
true -> |
|
|
|
case ets:lookup(?EtsGLockKey, Key) of |
|
|
|
[] -> |
|
|
|
lockApply(KeyPid, Key, MFAOrFun, LTimeOut); |
|
|
|
_ -> |
|
|
|
LTimeOut = TimeOut - abs(erlang:system_time(millisecond) - FirstTime), |
|
|
|
case LTimeOut =< 0 of |
|
|
|
true -> |
|
|
|
unlink(whereis(?eGLockMgr)), |
|
|
|
{error, {lock_timeout, Key}}; |
|
|
|
_ -> |
|
|
|
loopTry(KeyPid, Key, MFAOrFun, TimeOut, FirstTime) |
|
|
|
end |
|
|
|
end |
|
|
|
loopTry(KeyPid, Key, MFAOrFun, LTimeOut) |
|
|
|
end; |
|
|
|
_ -> |
|
|
|
unlink(whereis(?eGLockMgr)), |
|
|
|
{error, {lock_timeout, Key}} |
|
|
|
end |
|
|
|
end. |
|
|
|
|
|
|
|
lockApplys(KeyPids, Keys, MFAOrFun, TimeOut, FirstTime) -> |
|
|
|
lockApplys(KeyPids, Keys, FirstKey, MFAOrFun, TimeOut) -> |
|
|
|
case ets:insert_new(?EtsGLockKey, KeyPids) of |
|
|
|
true -> |
|
|
|
try doApply(MFAOrFun) |
|
|
@ -74,30 +70,24 @@ lockApplys(KeyPids, Keys, MFAOrFun, TimeOut, FirstTime) -> |
|
|
|
ok |
|
|
|
end; |
|
|
|
_ -> |
|
|
|
loopTrys(KeyPids, Keys, MFAOrFun, TimeOut, FirstTime) |
|
|
|
loopTrys(KeyPids, Keys, FirstKey, MFAOrFun, TimeOut) |
|
|
|
end. |
|
|
|
|
|
|
|
loopTrys(KeyPids, Keys, MFAOrFun, TimeOut, FirstTime) -> |
|
|
|
loopTrys(KeyPids, Keys, FirstKey, MFAOrFun, TimeOut) -> |
|
|
|
receive |
|
|
|
after ?ReTryTime -> |
|
|
|
[Key | _] = Keys, |
|
|
|
case ets:lookup(?EtsGLockKey, Key) of |
|
|
|
[] -> |
|
|
|
lockApplys(KeyPids, Keys, MFAOrFun, TimeOut, FirstTime); |
|
|
|
_ -> |
|
|
|
case TimeOut of |
|
|
|
infinity -> |
|
|
|
loopTrys(KeyPids, Keys, MFAOrFun, TimeOut, FirstTime); |
|
|
|
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTime), |
|
|
|
case LTimeOut >= 0 of |
|
|
|
true -> |
|
|
|
case ets:lookup(?EtsGLockKey, FirstKey) of |
|
|
|
[] -> |
|
|
|
lockApplys(KeyPids, Keys, FirstKey, MFAOrFun, LTimeOut); |
|
|
|
_ -> |
|
|
|
LTimeOut = TimeOut - abs(erlang:system_time(millisecond) - FirstTime), |
|
|
|
case LTimeOut =< 0 of |
|
|
|
true -> |
|
|
|
unlink(whereis(?eGLockMgr)), |
|
|
|
{error, {lock_timeout, Keys}}; |
|
|
|
_ -> |
|
|
|
loopTrys(KeyPids, Keys, MFAOrFun, TimeOut, FirstTime) |
|
|
|
end |
|
|
|
end |
|
|
|
loopTrys(KeyPids, Keys, FirstKey, MFAOrFun, LTimeOut) |
|
|
|
end; |
|
|
|
_ -> |
|
|
|
unlink(whereis(?eGLockMgr)), |
|
|
|
{error, {lock_timeout, Keys}} |
|
|
|
end |
|
|
|
end. |
|
|
|
|
|
|
|