add variable to enable atomic refcounts for testing

This commit is contained in:
Brendan Hansknecht 2022-03-31 22:18:25 -07:00
parent 51b360b578
commit a75ecb2883

View file

@ -120,11 +120,17 @@ pub const IntWidth = enum(u8) {
I128 = 9, I128 = 9,
}; };
const USE_ATOMICS = false;
pub fn increfC(ptr_to_refcount: *isize, amount: isize) callconv(.C) void { pub fn increfC(ptr_to_refcount: *isize, amount: isize) callconv(.C) void {
var refcount = ptr_to_refcount.*; var refcount = ptr_to_refcount.*;
var masked_amount = if (refcount == REFCOUNT_MAX_ISIZE) 0 else amount; var masked_amount = if (refcount < REFCOUNT_MAX_ISIZE) amount else 0;
if (USE_ATOMICS) {
var last = @atomicRmw(isize, ptr_to_refcount, std.builtin.AtomicRmwOp.Add, masked_amount, std.builtin.AtomicOrder.Monotonic);
} else {
ptr_to_refcount.* = refcount + masked_amount; ptr_to_refcount.* = refcount + masked_amount;
} }
}
pub fn decrefC( pub fn decrefC(
bytes_or_null: ?[*]isize, bytes_or_null: ?[*]isize,
@ -169,15 +175,22 @@ inline fn decref_ptr_to_refcount(
refcount_ptr: [*]isize, refcount_ptr: [*]isize,
alignment: u32, alignment: u32,
) void { ) void {
const refcount: isize = refcount_ptr[0];
const extra_bytes = std.math.max(alignment, @sizeOf(usize)); const extra_bytes = std.math.max(alignment, @sizeOf(usize));
if (USE_ATOMICS) {
var amount : isize = if (refcount_ptr[0] < REFCOUNT_MAX_ISIZE) 1 else 0;
var last = @atomicRmw(isize, &refcount_ptr[0], std.builtin.AtomicRmwOp.Sub, amount, std.builtin.AtomicOrder.Monotonic);
if (last == REFCOUNT_ONE_ISIZE) {
dealloc(@ptrCast([*]u8, refcount_ptr) - (extra_bytes - @sizeOf(usize)), alignment);
}
} else {
const refcount: isize = refcount_ptr[0];
if (refcount == REFCOUNT_ONE_ISIZE) { if (refcount == REFCOUNT_ONE_ISIZE) {
dealloc(@ptrCast([*]u8, refcount_ptr) - (extra_bytes - @sizeOf(usize)), alignment); dealloc(@ptrCast([*]u8, refcount_ptr) - (extra_bytes - @sizeOf(usize)), alignment);
} else if (refcount < 0) { } else if (refcount < REFCOUNT_MAX_ISIZE) {
refcount_ptr[0] = refcount - 1; refcount_ptr[0] = refcount - 1;
} }
} }
}
pub fn allocateWithRefcount( pub fn allocateWithRefcount(
data_bytes: usize, data_bytes: usize,