From b93b1088d8d2b11e59922332ebc720dc6658ac71 Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Fri, 12 Apr 2024 00:38:46 +0800 Subject: [PATCH] =?UTF-8?q?ft:=20=E4=BC=98=E5=8C=96=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/eGLock.hrl | 11 ++---- src/eGLock.erl | 96 ---------------------------------------------- src/eGLock_sup.erl | 3 +- 3 files changed, 5 insertions(+), 105 deletions(-) delete mode 100644 src/eGLock.erl diff --git a/include/eGLock.hrl b/include/eGLock.hrl index ff66498..2694add 100644 --- a/include/eGLock.hrl +++ b/include/eGLock.hrl @@ -1,10 +1,7 @@ %% 锁key -define(EtsGLockKey, '$EtsGLockKey'). -%% 锁等待pid --define(EtsGLockPid, '$EtsGLockPid'). - -%% 锁等待pid --define(ReTryLockApply, '$ReTryLockApply'). - -%% 默认超时时间 +%% 默认超时时间单位:Ms -define(LockTimeOut, 5000). + +%% 超时重试时间单位:Ms +-define(ReTryTime, 10). \ No newline at end of file diff --git a/src/eGLock.erl b/src/eGLock.erl deleted file mode 100644 index 0bdb421..0000000 --- a/src/eGLock.erl +++ /dev/null @@ -1,96 +0,0 @@ --module(eGLock). - --include("eGLock.hrl"). - --export([ - lockApply/2 - , lockApply/3 -]). - --spec lockApply(KeyOrKeys :: tuple() |[tuple()], MFAOrFun :: {M :: atom(), F :: atom(), Args :: list()} | {Fun :: function(), Args :: list()}) -> term(). -lockApply(KeyOrKeys, MFAOrFun) -> - lockApply(KeyOrKeys, MFAOrFun, ?LockTimeOut). - --spec lockApply(KeyOrKeys :: tuple() |[tuple()], MFAOrFun :: {M :: atom(), F :: atom(), Args :: list()} | {Fun :: function(), Args :: list()}, TimeOut :: integer() | infinity) -> term(). -lockApply(KeyOrKeys, MFAOrFun, TimeOut) -> - case KeyOrKeys of - {_, _} -> - lockApply(KeyOrKeys, MFAOrFun, TimeOut, erlang:system_time(millisecond)); - _ -> - lockApplys(KeyOrKeys, MFAOrFun, TimeOut, erlang:system_time(millisecond)) - end. - --define(CASE(Cond, Then, That), case Cond of true -> Then; _ -> That end). -lockApply(Key, MFAOrFun, TimeOut, FirstTime) -> - SelfPid = self(), - PidInfo = {Key, SelfPid}, - ets:insert(?EtsGLockPid, PidInfo), - case ets:insert_new(?EtsGLockKey, Key) of - true -> - try doApply(MFAOrFun) - catch C:R:S -> - {error, {lock_apply_error, {C, R, S}}} - after - ets:delete_object(?EtsGLockPid, PidInfo), - ets:delete(?EtsGLockKey, element(1, Key)), - WaitLockList = ets:lookup(?EtsGLockPid, Key), - [WaitPid ! ?ReTryLockApply || {_Key, WaitPid} <- WaitLockList], - ok - end; - _ -> - LTimeOut = ?CASE(TimeOut == infinity, TimeOut, max(0, TimeOut - max(erlang:system_time(millisecond) - FirstTime, 0))), - receive - ?ReTryLockApply -> - lockApply(Key, MFAOrFun, TimeOut, FirstTime) - after LTimeOut -> - ets:delete_object(?EtsGLockPid, PidInfo), - {error, {lock_timeout, Key}} - end - end. - -lockApplys(Keys, MFAOrFun, TimeOut, FirstTime) -> - SelfPid = self(), - AllPidInfo = [{OneKey, SelfPid} || OneKey <- Keys], - ets:insert(?EtsGLockPid, AllPidInfo), - case ets:insert_new(?EtsGLockKey, Keys) of - true -> - try doApply(MFAOrFun) - catch C:R:S -> - {error, {lock_apply_error, {C, R, S}}} - after - [ets:delete_object(?EtsGLockPid, OnePidInfo) || OnePidInfo <- AllPidInfo], - [ets:delete(?EtsGLockKey, element(1, OneKey)) || OneKey <- Keys], - notifyKeys(Keys, #{}), - ok - end; - _ -> - LTimeOut = ?CASE(TimeOut == infinity, TimeOut, max(0, TimeOut - max(erlang:system_time(millisecond) - FirstTime, 0))), - receive - ?ReTryLockApply -> - lockApplys(Keys, MFAOrFun, TimeOut, FirstTime) - after LTimeOut -> - [ets:delete_object(?EtsGLockPid, OnePidInfo) || OnePidInfo <- AllPidInfo], - {error, {lock_timeout, Keys}} - end - end. - -doApply({M, F, A}) -> - apply(M, F, A); -doApply({Fun, Args}) -> - apply(Fun, Args). - -notifyKeys([], _AccMap) -> ok; -notifyKeys([OneKey | Keys], AccMap) -> - WaitLockList = ets:lookup(?EtsGLockPid, OneKey), - NewAccMap = notifyToPid(WaitLockList, AccMap), - notifyKeys(Keys, NewAccMap). - -notifyToPid([], AccMap) -> AccMap; -notifyToPid([{_OneKey, WaitPid} | WaitLockList], AccMap) -> - case maps:is_key(WaitPid, AccMap) of - true -> - notifyToPid(WaitLockList, AccMap); - _ -> - WaitPid ! ?ReTryLockApply, - notifyToPid(WaitLockList, AccMap#{WaitPid => true}) - end. \ No newline at end of file diff --git a/src/eGLock_sup.erl b/src/eGLock_sup.erl index abae64c..ab4abd4 100644 --- a/src/eGLock_sup.erl +++ b/src/eGLock_sup.erl @@ -13,6 +13,5 @@ start_link() -> init([]) -> SupFlags = #{strategy => one_for_all, intensity => 0, period => 1}, - ets:new(?EtsGLockKey, [named_table, set, public, {write_concurrency, true}]), - ets:new(?EtsGLockPid, [named_table, bag, public, {write_concurrency, auto}, {read_concurrency, true}]), + ets:new(?EtsGLockKey, [named_table, set, public, {write_concurrency, auto}, {read_concurrency, true}]), {ok, {SupFlags, []}}.