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:
Gregory Burd 2013-07-31 15:39:55 -04:00
parent ee904b4769
commit f153509409

View file

@ -21,28 +21,26 @@
%% %%
%% ------------------------------------------------------------------- %% -------------------------------------------------------------------
-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 {R, {error, shutdown}=Error} ->
{R, {error, shutdown}=Error} -> %% Work unit was queued, but not executed.
%% Work unit was queued, but not executed. Error;
Error; {R, {error, _Reason}=Error} ->
{R, {error, _Reason}=Error} -> %% Work unit returned an error.
%% Work unit returned an error. Error;
Error; {R, Reply} ->
{R, Reply} -> Reply
Reply end;
end; {error, eagain} ->
{error, eagain} -> SleepyTime = min(30, (T+1)*2),
%% Work unit was not queued, try again. timer:sleep(SleepyTime),
async_nif_enqueue(F, A); F(F, A, R, SleepyTime);
%{error, enomem} -> Other ->
%{error, shutdown} -> Other
Other -> end
Other end,
end. F(Fun, Args, erlang:make_ref(), 1)).
-define(ASYNC_NIF_CALL(Fun, Args), async_nif_enqueue(Fun, Args)).