Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use monitor ref for all iterations #9443

Open
wants to merge 2 commits into
base: maint-26
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified erts/preloaded/ebin/prim_inet.beam
Binary file not shown.
33 changes: 16 additions & 17 deletions erts/preloaded/src/prim_inet.erl
Original file line number Diff line number Diff line change
Expand Up @@ -558,24 +558,26 @@ peeloff(S, AssocId) ->
%% NOT delegating this task to any back-end. For SCTP, this function MUST NOT
%% be called directly -- use "sendmsg" instead:
%%
send(S, Data) ->
send(S, Data, []).

send(S, Data, OptList) when is_port(S), is_list(OptList) ->
?DBG_FORMAT("prim_inet:send(~p, _, ~p)~n", [S,OptList]),
send(S, Data, OptList, monitor(port, S), make_ref()).

send(S, Data, OptList, Mref, Sref) ->
SrefBin = term_to_binary(Sref, [local]),
SrefBinSize = byte_size(SrefBin),
SrefBinSize = SrefBinSize band 16#FFFF,
try
erlang:port_command(
S, [<<SrefBinSize:16,SrefBin/binary>>, Data], OptList)
of
Mref = monitor(port, S),
MrefBin = term_to_binary(Mref, [local]),
MrefBinSize = byte_size(MrefBin),
MrefBinSize = MrefBinSize band 16#FFFF,
HdrAndData = [<<MrefBinSize:16,MrefBin/binary>>, Data],
send(S, HdrAndData, OptList, Mref).

send(S, HdrAndData, OptList, Mref) ->
try erlang:port_command(S, HdrAndData, OptList) of
false -> % Port busy when nosuspend option was passed
?DBG_FORMAT("prim_inet:send() -> {error,busy}~n", []),
{error,busy};
true ->
receive
{inet_reply,S,Sref} ->
{inet_reply,S,Mref} ->
%% This causes a wait even though nosuspend was used.
%% It only happens when the OS send operation returns
%% that it would block, which should only happen
Expand All @@ -589,15 +591,15 @@ send(S, Data, OptList, Mref, Sref) ->
"prim_inet:send(~p,,,) Waiting~n",
[S]),
receive
{inet_reply,S,ok,Sref} ->
send(S, Data, OptList, Mref, make_ref());
{inet_reply,S,ok,Mref} ->
send(S, HdrAndData, OptList, Mref);
{'DOWN',Mref,_,_,_Reason} ->
?DBG_FORMAT(
"prim_inet:send(~p,,,) 'DOWN' ~p~n",
[S,_Reason]),
{error,closed}
end;
{inet_reply,S,Status,Sref} ->
{inet_reply,S,Status,Mref} ->
demonitor(Mref, [flush]),
Status;
{'DOWN',Mref,_,_,_Reason} ->
Expand All @@ -612,9 +614,6 @@ send(S, Data, OptList, Mref, Sref) ->
{error,einval}
end.

send(S, Data) ->
send(S, Data, []).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% SENDTO(insock(), IP, Port, Data) -> ok | {error, Reason}
Expand Down
25 changes: 22 additions & 3 deletions lib/kernel/test/gen_tcp_misc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

-module(gen_tcp_misc_SUITE).

-include_lib("common_test/include/ct.hrl").
-include_lib("stdlib/include/assert.hrl").
-include("kernel_test_lib.hrl").

-export([
Expand Down Expand Up @@ -108,7 +108,8 @@
otp_18357/1,
otp_18883/1,
otp_18707/1,
send_block_unblock/1
send_block_unblock/1,
prim_inet_recv_marker/1
]).

%% Internal exports.
Expand Down Expand Up @@ -205,7 +206,7 @@ inet_backend_default_cases() ->
all_std_cases().

inet_backend_inet_cases() ->
all_std_cases().
[prim_inet_recv_marker] ++ all_std_cases().

inet_backend_socket_cases() ->
all_std_cases().
Expand Down Expand Up @@ -9433,6 +9434,24 @@ do_otp_18707(_Config) ->
?P("done"),
ok.

%% Disassemble prim_inet.beam and make that sure each function has
%% the correct number of recv markers.
prim_inet_recv_marker(_Config) ->
[PrimInet | _] = filelib:wildcard(
filename:join([code:lib_dir(erts),"**","prim_inet.beam"])),

{beam_file, prim_inet, _Exports, _Vsn, _Attr, Functions} =
beam_disasm:file(PrimInet),
RecvMarkerCnt =
[{Name,Arity,length([C || {recv_marker_use,_} = C <- Code])}
|| {function, Name, Arity, _, Code} <- Functions],
RecvMarkers =
[{Name, Arity} || {Name,Arity,Cnt} <- RecvMarkerCnt, Cnt =/= 0],

?assert(lists:member({send,4}, RecvMarkers)),
?assert(lists:member({do_sendto,4}, RecvMarkers)),

ok.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Expand Down
Loading