pass the pointer to shared memory around, instead of using a global

This commit is contained in:
Folkert 2022-12-08 23:22:06 +01:00
parent 8307a194e1
commit 95fe9cbccd
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
6 changed files with 68 additions and 55 deletions

View file

@ -44,8 +44,6 @@ pub fn expectFailedStartSharedFile() callconv(.C) [*]u8 {
const ptr = @ptrCast([*]u8, shared_ptr);
SHARED_BUFFER = ptr[0..length];
return ptr;
} else {
unreachable;
@ -81,17 +79,11 @@ pub fn readSharedBufferEnv() callconv(.C) void {
}
}
fn get_atomic_ptr() *Atomic(u32) {
const usize_ptr = @ptrCast([*]u32, @alignCast(@alignOf(usize), SHARED_BUFFER.ptr));
const atomic_ptr = @ptrCast(*Atomic(u32), &usize_ptr[5]);
return atomic_ptr;
}
pub fn expectFailedFinalize() callconv(.C) void {
pub fn notifyParent(shared_buffer: [*]u8, tag: u32) callconv(.C) void {
if (builtin.os.tag == .macos or builtin.os.tag == .linux) {
const atomic_ptr = get_atomic_ptr();
atomic_ptr.storeUnchecked(1);
const usize_ptr = @ptrCast([*]u32, @alignCast(@alignOf(usize), shared_buffer));
const atomic_ptr = @ptrCast(*Atomic(u32), &usize_ptr[5]);
atomic_ptr.storeUnchecked(tag);
// wait till the parent is done before proceeding
const Ordering = std.atomic.Ordering;
@ -101,15 +93,10 @@ pub fn expectFailedFinalize() callconv(.C) void {
}
}
pub fn sendDbg() callconv(.C) void {
if (builtin.os.tag == .macos or builtin.os.tag == .linux) {
const atomic_ptr = get_atomic_ptr();
atomic_ptr.storeUnchecked(2);
// wait till the parent is done before proceeding
const Ordering = std.atomic.Ordering;
while (atomic_ptr.load(Ordering.Acquire) != 0) {
std.atomic.spinLoopHint();
}
}
pub fn notifyParentExpect(shared_buffer: [*]u8) callconv(.C) void {
notifyParent(shared_buffer, 1);
}
pub fn notifyParentDbg(shared_buffer: [*]u8) callconv(.C) void {
notifyParent(shared_buffer, 2);
}

View file

@ -172,8 +172,8 @@ comptime {
if (builtin.target.cpu.arch != .wasm32) {
exportUtilsFn(expect.expectFailedStartSharedBuffer, "expect_failed_start_shared_buffer");
exportUtilsFn(expect.expectFailedStartSharedFile, "expect_failed_start_shared_file");
exportUtilsFn(expect.expectFailedFinalize, "expect_failed_finalize");
exportUtilsFn(expect.sendDbg, "send_dbg");
exportUtilsFn(expect.notifyParentExpect, "notify_parent_expect");
exportUtilsFn(expect.notifyParentDbg, "notify_parent_dbg");
// sets the buffer used for expect failures
@export(expect.setSharedBuffer, .{ .name = "set_shared_buffer", .linkage = .Weak });