With some input from Jon I've managed to reduce this back into a macro rather than a fun and a macro calling a fun. He also suggested that on eagain I sleep a small amount of time so as to allow other work to catch up a bit.
This commit is contained in:
parent
ee904b4769
commit
f153509409
1 changed files with 23 additions and 25 deletions
|
@ -21,9 +21,8 @@
|
||||||
%%
|
%%
|
||||||
%% -------------------------------------------------------------------
|
%% -------------------------------------------------------------------
|
||||||
|
|
||||||
-spec async_nif_enqueue(function(), [term()]) -> term() | {error, term()}.
|
-define(ASYNC_NIF_CALL(Fun, Args),
|
||||||
async_nif_enqueue(F, A) ->
|
F = fun(F, A, R, T) ->
|
||||||
R = erlang:make_ref(),
|
|
||||||
case erlang:apply(F, [R|A]) of
|
case erlang:apply(F, [R|A]) of
|
||||||
{ok, enqueued} ->
|
{ok, enqueued} ->
|
||||||
receive
|
receive
|
||||||
|
@ -37,12 +36,11 @@ async_nif_enqueue(F, A) ->
|
||||||
Reply
|
Reply
|
||||||
end;
|
end;
|
||||||
{error, eagain} ->
|
{error, eagain} ->
|
||||||
%% Work unit was not queued, try again.
|
SleepyTime = min(30, (T+1)*2),
|
||||||
async_nif_enqueue(F, A);
|
timer:sleep(SleepyTime),
|
||||||
%{error, enomem} ->
|
F(F, A, R, SleepyTime);
|
||||||
%{error, shutdown} ->
|
|
||||||
Other ->
|
Other ->
|
||||||
Other
|
Other
|
||||||
end.
|
end
|
||||||
|
end,
|
||||||
-define(ASYNC_NIF_CALL(Fun, Args), async_nif_enqueue(Fun, Args)).
|
F(Fun, Args, erlang:make_ref(), 1)).
|
||||||
|
|
Loading…
Reference in a new issue