Skip to content

Makes sure it is OK for the profiled code to send/receive messages #13

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

Merged
merged 1 commit into from
Sep 22, 2020
Merged
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
22 changes: 16 additions & 6 deletions lib/exprof/macro.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ defmodule ExProf.Macro do
"""
defmacro profile(do: code) do
quote do
pid = spawn_link(ExProf.Macro, :execute_profile, [fn -> unquote(code) end])
ref = make_ref()
pid = spawn_link(ExProf.Macro, :execute_profile, [fn -> unquote(code) end, ref])
ExProf.start(pid)
send pid, self()
send pid, {ref, self()}

result =
receive do
result -> result
{^ref, result} -> result
end

ExProf.stop
Expand All @@ -35,10 +36,19 @@ defmodule ExProf.Macro do
@doc """
An internal method for initiating profiling.
"""
def execute_profile(func) do
def execute_profile(func, ref) do
receive do
sender ->
send sender, func.()
{^ref, sender} ->
send sender, {ref, func.()}
forward_other_messages(sender)
end
end

defp forward_other_messages(sender) do
receive do
message ->
send sender, message
forward_other_messages(sender)
end
end
end