Skip to content

Commit

Permalink
reverts threading problems due to previous shimming experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
ityonemo committed Sep 23, 2023
1 parent 3de9598 commit 44def32
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 46 deletions.
15 changes: 6 additions & 9 deletions lib/zig/templates/threaded.zig.eex
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,23 @@ fn @"<%= @type.name %>-join"(env: beam.env, argc: c_int, args: [*c] const e.ErlN
_ = argc;
// set beam.allocator due to reentry
beam.allocator = beam.raw_allocator;
_ = args;
const thread_rsrc: ThreadResource_<%= @type.name %> = undefined;
//const thread_rsrc = beam.get(ThreadResource_<%= @type.name %>, env, .{.v = args[0]}, .{}) catch {
// @panic("error getting thread resource");
//};
const thread_rsrc = beam.get(ThreadResource_<%= @type.name %>, env, .{.v = args[0]}, .{}) catch {
@panic("error getting thread resource");
};
const thread = ThreadResource_<%= @type.name %>.unpack(thread_rsrc);

const result = thread.join() catch {
// TODO: fix this
@panic("error joining thread");
};

if (comptime Thread_<%= @type.name %>.makes_error_result()) {
const result_term = switch(result) {
.ok => // |ok_result| beam.make(env, ok_result, .{}),
beam.raise_exception(env, .foo),
.ok => |ok_result| beam.make(env, ok_result, .{}),
.error_return_trace => |error_result| beam.raise_exception(env, beam.copy(env, error_result)),
};
return result_term.v;
} else {
return beam.make(env, .foo, .{}).v;
// return beam.make(env, result, .{}).v;
return beam.make(env, result, .{}).v;
}
}
39 changes: 19 additions & 20 deletions test/integration/concurrency/threaded_automatic_erroring_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule ZiglerTest.Concurrency.ThreadedAutomaticErroringTest do
use Zig, otp_app: :zigler, nifs: [threaded: [:threaded]]

~Z"""
const std = @import("std");
const beam = @import("beam");
const ThreadError = error{BadNumber};
Expand All @@ -17,23 +16,23 @@ defmodule ZiglerTest.Concurrency.ThreadedAutomaticErroringTest do
}
"""

test "threaded function can succeed" #do
# assert 48 = threaded(47)
#end

test "threaded function can error" #do
# error =
# try do
# threaded(42)
# rescue
# e in ErlangError ->
# %{payload: e.original, stacktrace: __STACKTRACE__}
# end
#
# assert %{payload: :BadNumber, stacktrace: [head | _]} = error
#
# expected_file = Path.absname(__ENV__.file)
#
# assert {__MODULE__, :threaded, [:...], [file: ^expected_file, line: 15]} = head
#end
test "threaded function can succeed" do
assert 48 = threaded(47)
end

test "threaded function can error" do

Check failure on line 23 in test/integration/concurrency/threaded_automatic_erroring_test.exs

View workflow job for this annotation

GitHub Actions / Linux OTP 25.0 / Elixir 1.13.4

test threaded function can error (ZiglerTest.Concurrency.ThreadedAutomaticErroringTest)

Check failure on line 23 in test/integration/concurrency/threaded_automatic_erroring_test.exs

View workflow job for this annotation

GitHub Actions / Linux OTP 26.0 / Elixir 1.15.2

test threaded function can error (ZiglerTest.Concurrency.ThreadedAutomaticErroringTest)

Check failure on line 23 in test/integration/concurrency/threaded_automatic_erroring_test.exs

View workflow job for this annotation

GitHub Actions / Linux OTP 26.0 / Elixir 1.14.5

test threaded function can error (ZiglerTest.Concurrency.ThreadedAutomaticErroringTest)
error =
try do
threaded(42)
rescue
e in ErlangError ->
%{payload: e.original, stacktrace: __STACKTRACE__}
end

assert %{payload: :BadNumber, stacktrace: [head | _]} = error

expected_file = Path.absname(__ENV__.file)

assert {__MODULE__, :threaded, [:...], [file: ^expected_file, line: 15]} = head
end
end
8 changes: 4 additions & 4 deletions test/integration/concurrency/threaded_automatic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ defmodule ZiglerTest.Concurrency.ThreadedAutomaticTest do
}
"""

test "threaded function" # do
# assert :ok = threaded(true, self())
# refute_receive :killed
#end
test "threaded function" do
assert :ok = threaded(true, self())
refute_receive :killed
end

test "threaded function gc's" do
this = self()
Expand Down
26 changes: 13 additions & 13 deletions test/integration/erlang/threaded_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ defmodule ZiglerTest.Erlang.ThreadedTest do

@moduletag :threaded

test "threaded function" # do
# {:ok, mod} = :compile.file(@test_file, outdir: :code.lib_dir(:zigler, :ebin))
# Code.ensure_loaded(mod)
#
# this = self()
# assert :ok = :erlang_threaded_test.threaded(true, this)
# refute_receive :killed
#
# pid = spawn(fn -> :erlang_threaded_test.threaded(false, this) end)
# :erlang.garbage_collect(pid)
# Process.exit(pid, :kill)
# assert_receive :killed, 500
#end
test "threaded function" do
{:ok, mod} = :compile.file(@test_file, outdir: :code.lib_dir(:zigler, :ebin))
Code.ensure_loaded(mod)

this = self()
assert :ok = :erlang_threaded_test.threaded(true, this)
refute_receive :killed

pid = spawn(fn -> :erlang_threaded_test.threaded(false, this) end)
:erlang.garbage_collect(pid)
Process.exit(pid, :kill)
assert_receive :killed, 500
end
end

0 comments on commit 44def32

Please sign in to comment.