Skip to content

Commit 1657d9c

Browse files
authored
Merge pull request #13 from Qqwy/profiling_with_messages
Makes sure it is OK for the profiled code to send/receive messages
2 parents 25ffd42 + bbb6120 commit 1657d9c

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lib/exprof/macro.ex

Lines changed: 16 additions & 6 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-
pid = spawn_link(ExProf.Macro, :execute_profile, [fn -> unquote(code) end])
19+
ref = make_ref()
20+
pid = spawn_link(ExProf.Macro, :execute_profile, [fn -> unquote(code) end, ref])
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
@@ -35,10 +36,19 @@ defmodule ExProf.Macro do
3536
@doc """
3637
An internal method for initiating profiling.
3738
"""
38-
def execute_profile(func) do
39+
def execute_profile(func, ref) 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)