瀏覽代碼

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

master
SisMaker 3 月之前
父節點
當前提交
6edcdb3891
共有 1 個檔案被更改,包括 121 行新增12 行删除
  1. +121
    -12
      src/eGLock.erl

+ 121
- 12
src/eGLock.erl 查看文件

@ -5,8 +5,15 @@
%% :Ms
-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).
%% ets key
@ -46,20 +53,71 @@ doTryLock(KeyIx, TimeOut) ->
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.
loopLock(KeyIx, TimeOut) ->
loopLockL(KeyIx, TimeOut) ->
receive
after ?ReTryTime ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTime),
after ?ReTryTimeL ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTimeL),
case LTimeOut >= 0 of
true ->
case eNifLock:tryLock(KeyIx) of
true ->
true;
_ ->
loopLock(KeyIx, LTimeOut)
loopLockL(KeyIx, LTimeOut)
end;
_ ->
lockTimeout
@ -71,20 +129,71 @@ doTryLocks(KeyIxs, TimeOut) ->
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.
loopLocks(KeyIxs, TimeOut) ->
loopLocksL(KeyIxs, TimeOut) ->
receive
after ?ReTryTime ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTime),
after ?ReTryTimeL ->
LTimeOut = ?CASE(TimeOut == infinity, TimeOut, TimeOut - ?ReTryTimeL),
case LTimeOut >= 0 of
true ->
case eNifLock:tryLocks(KeyIxs) of
true ->
true;
_ ->
loopLocks(KeyIxs, LTimeOut)
loopLocksL(KeyIxs, LTimeOut)
end;
_ ->
lockTimeout

Loading…
取消
儲存