mirror of
https://github.com/oven-sh/bun
synced 2026-02-13 20:39:05 +00:00
Add `bun.ptr.ExternalShared`, a shared pointer whose reference count is
managed externally; e.g., by extern functions. This can be used to work
with `RefCounted` C++ objects in Zig. For example:
```cpp
// C++:
struct MyType : RefCounted<MyType> { ... };
extern "C" void MyType__ref(MyType* self) { self->ref(); }
extern "C" void MyType__ref(MyType* self) { self->deref(); }
```
```zig
// Zig:
const MyType = opaque {
extern fn MyType__ref(self: *MyType) void;
extern fn MyType__deref(self: *MyType) void;
pub const Ref = bun.ptr.ExternalShared(MyType);
// This enables `ExternalShared` to work.
pub const external_shared_descriptor = struct {
pub const ref = MyType__ref;
pub const deref = MyType__deref;
};
};
// Now `MyType.Ref` behaves just like `Ref<MyType>` in C++:
var some_ref: MyType.Ref = someFunctionReturningMyTypeRef();
const ptr: *MyType = some_ref.get(); // gets the inner pointer
var some_other_ref = some_ref.clone(); // increments the ref count
some_ref.deinit(); // decrements the ref count
// decrements the ref count again; if no other refs exist, the object
// is destroyed
some_other_ref.deinit();
```
This commit also adds `RawRefCount`, a simple wrapper around an integer
reference count that can be used to implement the interface required by
`ExternalShared`. Generally, for reference-counted Zig types,
`bun.ptr.Shared` is preferred, but occasionally it is useful to have an
“intrusive” reference-counted type where the ref count is stored in the
type itself. For this purpose, `ExternalShared` + `RawRefCount` is more
flexible and less error-prone than the deprecated `bun.ptr.RefCounted`
type.
(For internal tracking: fixes STAB-1287, STAB-1288)
75 lines
2.5 KiB
Zig
75 lines
2.5 KiB
Zig
pub const ThreadSafety = enum {
|
|
single_threaded,
|
|
thread_safe,
|
|
};
|
|
|
|
pub const DecrementResult = enum {
|
|
keep_alive,
|
|
should_destroy,
|
|
};
|
|
|
|
/// A simple wrapper around an integer reference count. This type doesn't do any memory management
|
|
/// itself.
|
|
///
|
|
/// This type may be useful for implementing the interface required by `bun.ptr.ExternalShared`.
|
|
pub fn RawRefCount(comptime Int: type, comptime thread_safety: ThreadSafety) type {
|
|
return struct {
|
|
const Self = @This();
|
|
|
|
raw_value: if (thread_safety == .thread_safe) std.atomic.Value(Int) else Int,
|
|
#thread_lock: if (thread_safety == .single_threaded) bun.safety.ThreadLock else void,
|
|
|
|
/// Usually the initial count should be 1.
|
|
pub fn init(initial_count: Int) Self {
|
|
return .{
|
|
.raw_value = switch (comptime thread_safety) {
|
|
.single_threaded => initial_count,
|
|
.thread_safe => .init(initial_count),
|
|
},
|
|
.#thread_lock = switch (comptime thread_safety) {
|
|
.single_threaded => .initLockedIfNonComptime(),
|
|
.thread_safe => {},
|
|
},
|
|
};
|
|
}
|
|
|
|
pub fn increment(self: *Self) void {
|
|
switch (comptime thread_safety) {
|
|
.single_threaded => {
|
|
self.#thread_lock.lockOrAssert();
|
|
self.raw_value += 1;
|
|
},
|
|
.thread_safe => {
|
|
const old = self.raw_value.fetchAdd(1, .monotonic);
|
|
bun.assertf(
|
|
old != std.math.maxInt(Int),
|
|
"overflow of thread-safe ref count",
|
|
.{},
|
|
);
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn decrement(self: *Self) DecrementResult {
|
|
const new_count = blk: switch (comptime thread_safety) {
|
|
.single_threaded => {
|
|
self.#thread_lock.lockOrAssert();
|
|
self.raw_value -= 1;
|
|
break :blk self.raw_value;
|
|
},
|
|
.thread_safe => {
|
|
const old = self.raw_value.fetchSub(1, .acq_rel);
|
|
bun.assertf(old != 0, "underflow of thread-safe ref count", .{});
|
|
break :blk old - 1;
|
|
},
|
|
};
|
|
return if (new_count == 0) .should_destroy else .keep_alive;
|
|
}
|
|
|
|
pub const deinit = void;
|
|
};
|
|
}
|
|
|
|
const bun = @import("bun");
|
|
const std = @import("std");
|