Browse Source

ft: 优化

master
SisMaker 1 year ago
parent
commit
a8b3c39347
1 changed files with 31 additions and 41 deletions
  1. +31
    -41
      src/eGLock.erl

+ 31
- 41
src/eGLock.erl View File

@ -18,13 +18,14 @@ lockApply(KeyOrKeys, MFAOrFun, TimeOut) ->
case is_list(KeyOrKeys) of case is_list(KeyOrKeys) of
true -> true ->
KeyPids = [{OneKey, Pid} || OneKey <- KeyOrKeys], 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. end.
-define(CASE(Cond, Then, That), case Cond of true -> Then; _ -> That 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 case ets:insert_new(?EtsGLockKey, KeyPid) of
true -> true ->
try doApply(MFAOrFun) try doApply(MFAOrFun)
@ -36,33 +37,28 @@ lockApply(KeyPid, Key, MFAOrFun, TimeOut, FirstTime) ->
ok ok
end; end;
_ -> _ ->
loopTry(KeyPid, Key, MFAOrFun, TimeOut, FirstTime)
loopTry(KeyPid, Key, MFAOrFun, TimeOut)
end. end.
loopTry(KeyPid, Key, MFAOrFun, TimeOut, FirstTime) ->
loopTry(KeyPid, Key, MFAOrFun, TimeOut) ->
receive receive
after ?ReTryTime -> 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
end. end.
lockApplys(KeyPids, Keys, MFAOrFun, TimeOut, FirstTime) ->
lockApplys(KeyPids, Keys, FirstKey, MFAOrFun, TimeOut) ->
case ets:insert_new(?EtsGLockKey, KeyPids) of case ets:insert_new(?EtsGLockKey, KeyPids) of
true -> true ->
try doApply(MFAOrFun) try doApply(MFAOrFun)
@ -74,30 +70,24 @@ lockApplys(KeyPids, Keys, MFAOrFun, TimeOut, FirstTime) ->
ok ok
end; end;
_ -> _ ->
loopTrys(KeyPids, Keys, MFAOrFun, TimeOut, FirstTime)
loopTrys(KeyPids, Keys, FirstKey, MFAOrFun, TimeOut)
end. end.
loopTrys(KeyPids, Keys, MFAOrFun, TimeOut, FirstTime) ->
loopTrys(KeyPids, Keys, FirstKey, MFAOrFun, TimeOut) ->
receive receive
after ?ReTryTime -> 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
end. end.

Loading…
Cancel
Save