Browse Source

ft:自旋锁添加退避策略 优化

master
SisMaker 3 months ago
parent
commit
6edcdb3891
1 changed files with 121 additions and 12 deletions
  1. +121
    -12
      src/eGLock.erl

+ 121
- 12
src/eGLock.erl View File

@ -5,8 +5,15 @@
%% :Ms %% :Ms
-define(LockTimeOut, 5000). -define(LockTimeOut, 5000).
%% :Ms
-define(ReTryTime, 1).
%% :Ms
-define(ReTryTime1, 1).
%% :Ms
-define(ReTryTime2, 2).
%% :Ms
-define(ReTryTime3, 4).
%% :Ms
-define(ReTryTimeL, 6).
%% %%
-define(eGLockSize, 2097152). -define(eGLockSize, 2097152).
%% ets key %% ets key
@ -46,20 +53,71 @@ doTryLock(KeyIx, TimeOut) ->
true -> true ->
true; true;
_ -> _ ->
loopLock(KeyIx, TimeOut)
loopLock1(KeyIx, TimeOut)
end.
loopLock1(KeyIx, TimeOut) ->
receive
after ?ReTryTime1 ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTime1),
case LTimeOut >= 0 of
true ->
case eNifLock:tryLock(KeyIx) of
true ->
true;
_ ->
loopLock2(KeyIx, LTimeOut)
end;
_ ->
lockTimeout
end
end.
loopLock2(KeyIx, TimeOut) ->
receive
after ?ReTryTime2 ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTime2),
case LTimeOut >= 0 of
true ->
case eNifLock:tryLock(KeyIx) of
true ->
true;
_ ->
loopLock3(KeyIx, LTimeOut)
end;
_ ->
lockTimeout
end
end.
loopLock3(KeyIx, TimeOut) ->
receive
after ?ReTryTime3 ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTime3),
case LTimeOut >= 0 of
true ->
case eNifLock:tryLock(KeyIx) of
true ->
true;
_ ->
loopLockL(KeyIx, LTimeOut)
end;
_ ->
lockTimeout
end
end. end.
loopLock(KeyIx, TimeOut) ->
loopLockL(KeyIx, TimeOut) ->
receive receive
after ?ReTryTime ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTime),
after ?ReTryTimeL ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTimeL),
case LTimeOut >= 0 of case LTimeOut >= 0 of
true -> true ->
case eNifLock:tryLock(KeyIx) of case eNifLock:tryLock(KeyIx) of
true -> true ->
true; true;
_ -> _ ->
loopLock(KeyIx, LTimeOut)
loopLockL(KeyIx, LTimeOut)
end; end;
_ -> _ ->
lockTimeout lockTimeout
@ -71,20 +129,71 @@ doTryLocks(KeyIxs, TimeOut) ->
true -> true ->
true; true;
_ -> _ ->
loopLocks(KeyIxs, TimeOut)
loopLocks1(KeyIxs, TimeOut)
end.
loopLocks1(KeyIxs, TimeOut) ->
receive
after ?ReTryTime1 ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTime1),
case LTimeOut >= 0 of
true ->
case eNifLock:tryLocks(KeyIxs) of
true ->
true;
_ ->
loopLocks2(KeyIxs, LTimeOut)
end;
_ ->
lockTimeout
end
end.
loopLocks2(KeyIxs, TimeOut) ->
receive
after ?ReTryTime2 ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTime2),
case LTimeOut >= 0 of
true ->
case eNifLock:tryLocks(KeyIxs) of
true ->
true;
_ ->
loopLocks3(KeyIxs, LTimeOut)
end;
_ ->
lockTimeout
end
end.
loopLocks3(KeyIxs, TimeOut) ->
receive
after ?ReTryTime3 ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTime3),
case LTimeOut >= 0 of
true ->
case eNifLock:tryLocks(KeyIxs) of
true ->
true;
_ ->
loopLocksL(KeyIxs, LTimeOut)
end;
_ ->
lockTimeout
end
end. end.
loopLocks(KeyIxs, TimeOut) ->
loopLocksL(KeyIxs, TimeOut) ->
receive receive
after ?ReTryTime ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTime),
after ?ReTryTimeL ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTimeL),
case LTimeOut >= 0 of case LTimeOut >= 0 of
true -> true ->
case eNifLock:tryLocks(KeyIxs) of case eNifLock:tryLocks(KeyIxs) of
true -> true ->
true; true;
_ -> _ ->
loopLocks(KeyIxs, LTimeOut)
loopLocksL(KeyIxs, LTimeOut)
end; end;
_ -> _ ->
lockTimeout lockTimeout

Loading…
Cancel
Save