Skip to content

Commit 4b3eddf

Browse files
committed
Makes sure it is OK for the profiled code to send/receive messages
by: 1. Making sure at the end the message with the actual result of the whole block is obtained by using a reference. 2. Forwarding all messages the spawned process might have obtained in the meantime to the original process.
1 parent 25ffd42 commit 4b3eddf

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/exprof/macro.ex

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ defmodule ExProf.Macro do
1616
"""
1717
defmacro profile(do: code) do
1818
quote do
19+
ref = make_ref()
1920
pid = spawn_link(ExProf.Macro, :execute_profile, [fn -> unquote(code) end])
2021
ExProf.start(pid)
21-
send pid, self()
22+
send pid, {ref, self()}
2223

2324
result =
2425
receive do
25-
result -> result
26+
{^ref, result} -> result
2627
end
2728

2829
ExProf.stop
@@ -37,8 +38,17 @@ defmodule ExProf.Macro do
3738
"""
3839
def execute_profile(func) do
3940
receive do
40-
sender ->
41-
send sender, func.()
41+
{ref, sender} ->
42+
send sender, {ref, func.()}
43+
forward_other_messages(sender)
44+
end
45+
end
46+
47+
defp forward_other_messages(sender) do
48+
receive do
49+
message ->
50+
send sender, message
51+
forward_other_messages(sender)
4252
end
4353
end
4454
end

0 commit comments

Comments
 (0)