Skip to content

Commit

Permalink
Merge pull request #10 from Aandreba/fix-example
Browse files Browse the repository at this point in the history
Fix example
  • Loading branch information
Aandreba authored May 10, 2024
2 parents c896790 + 1301259 commit da7e21b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
1 change: 0 additions & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
patreon: paltryorphan96
github: Aandreba
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,28 @@ test "example" {
std.debug.print("Heap align: {}\n\n", .{Arc.innerAlign()});
var value = try Arc.init(std.testing.allocator, .{});
errdefer value.releaseWithFn(Data.deinit);
errdefer value.releaseWithFn(Data.deinit, .{});
var handles: [THREADS]Thread = undefined;
var i: usize = 0;
while (i < THREADS) {
const this_value = value.retain();
errdefer this_value.releaseWithFn(Data.deinit);
errdefer this_value.releaseWithFn(Data.deinit, .{});
handles[i] = try Thread.spawn(.{}, thread_exec, .{this_value});
i += 1;
}
for (handles) |handle| {
handle.join();
}
for (handles) |handle| handle.join();
const owned_value: Data = value.tryUnwrap().?;
defer owned_value.deinit();
std.debug.print("{d}\n", .{owned_value.data.items});
}
fn thread_exec(data: Arc) !void {
defer data.releaseWithFn(Data.deinit);
defer data.releaseWithFn(Data.deinit, .{});
var rng = std.rand.DefaultPrng.init(@bitCast(u64, @truncate(i64, std.time.nanoTimestamp())));
var rng = std.rand.DefaultPrng.init(@as(u64, @bitCast(@as(i64, @truncate(std.time.nanoTimestamp())))));
data.value.mutex.lock();
defer data.value.mutex.unlock();
Expand Down
11 changes: 4 additions & 7 deletions src/example.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,26 @@ test "example" {
std.debug.print("Heap align: {}\n\n", .{Arc.innerAlign()});

var value = try Arc.init(std.testing.allocator, .{});
errdefer value.releaseWithFn(Data.deinit);
errdefer value.releaseWithFn(Data.deinit, .{});

var handles: [THREADS]Thread = undefined;
var i: usize = 0;
while (i < THREADS) {
const this_value = value.retain();
errdefer this_value.releaseWithFn(Data.deinit);
errdefer this_value.releaseWithFn(Data.deinit, .{});
handles[i] = try Thread.spawn(.{}, thread_exec, .{this_value});
i += 1;
}

for (handles) |handle| {
handle.join();
}

for (handles) |handle| handle.join();
const owned_value: Data = value.tryUnwrap().?;
defer owned_value.deinit();

std.debug.print("{d}\n", .{owned_value.data.items});
}

fn thread_exec(data: Arc) !void {
defer data.releaseWithFn(Data.deinit);
defer data.releaseWithFn(Data.deinit, .{});

var rng = std.rand.DefaultPrng.init(@as(u64, @bitCast(@as(i64, @truncate(std.time.nanoTimestamp())))));

Expand Down
8 changes: 4 additions & 4 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub fn RcAligned(comptime T: type, comptime alignment: ?u29) type {

/// Decrements the reference count, deallocating the weak count reaches zero,
/// and executing `f` if the strong count reaches zero.
/// The `f` function has a signature of `fn(*T, ...args)` or `fn(T, ...args)`
/// The `f` function has a signature of `fn(*T, ...args)` or `fn(T, ...args)`.
/// The continued use of the pointer after calling `release` is undefined behaviour.
pub fn releaseWithFn(self: Self, comptime f: anytype, args: anytype) void {
return self.asUnmanaged().releaseWithFn(self.alloc, f, args);
Expand Down Expand Up @@ -291,7 +291,7 @@ pub fn ArcAligned(comptime T: type, comptime alignment: ?u29) type {

/// Decrements the reference count, deallocating the weak count reaches zero,
/// and executing `f` if the strong count reaches zero.
/// The `f` function has a signature of `fn(*T, ...args)` or `fn(T, ...args)`
/// The `f` function has a signature of `fn(*T, ...args)` or `fn(T, ...args)`.
/// The continued use of the pointer after calling `release` is undefined behaviour.
pub fn releaseWithFn(self: Self, comptime f: anytype, args: anytype) void {
return self.asUnmanaged().releaseWithFn(self.alloc, f, args);
Expand Down Expand Up @@ -519,7 +519,7 @@ pub fn RcAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) type {

/// Decrements the reference count, deallocating the weak count reaches zero,
/// and executing `f` if the strong count reaches zero.
/// The `f` function has a signature of `fn(*T, ...args)` or `fn(T, ...args)`
/// The `f` function has a signature of `fn(*T, ...args)` or `fn(T, ...args)`.
/// The continued use of the pointer after calling `release` is undefined behaviour.
pub fn releaseWithFn(self: Self, allocator: Allocator, comptime f: anytype, args: anytype) void {
const ptr = self.innerPtr();
Expand Down Expand Up @@ -767,7 +767,7 @@ pub fn ArcAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) type {

/// Decrements the reference count, deallocating the weak count reaches zero,
/// and executing `f` if the strong count reaches zero.
/// The `f` function has a signature of `fn(*T, ...args)` or `fn(T, ...args)`
/// The `f` function has a signature of `fn(*T, ...args)` or `fn(T, ...args)`.
/// The continued use of the pointer after calling `release` is undefined behaviour.
pub fn releaseWithFn(self: Self, allocator: Allocator, comptime f: anytype, args: anytype) void {
const ptr = self.innerPtr();
Expand Down

0 comments on commit da7e21b

Please sign in to comment.