Skip to content

Commit

Permalink
chore: fix nightly build 0.12.0-dev.1773+8a8fd47d2
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanaasagi committed Dec 9, 2023
1 parent 13ac103 commit 567930d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.name = "once",
.version = "0.1.0",
.dependencies = .{},
.paths = .{""},
}
26 changes: 13 additions & 13 deletions src/multithread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ pub fn OnceCell(comptime T: type) type {

cell: T,
mutex: std.Thread.Mutex,
done: std.atomic.Atomic(u32),
done: std.atomic.Value(u32),
const Self = @This();

/// Creates a new empty cell.
pub fn empty() Self {
return Self{
.cell = undefined,
.mutex = std.Thread.Mutex{},
.done = std.atomic.Atomic(u32).init(0b00),
.done = std.atomic.Value(u32).init(0b00),
};
}

Expand All @@ -51,7 +51,7 @@ pub fn OnceCell(comptime T: type) type {
return Self{
.cell = value,
.mutex = std.Thread.Mutex{},
.done = std.atomic.Atomic(u32).init(0b01),
.done = std.atomic.Value(u32).init(0b01),
};
}

Expand Down Expand Up @@ -93,7 +93,7 @@ pub fn OnceCell(comptime T: type) type {
if (self.isInitialize()) {
defer self.done.store(0b00, .Release);

var cell = self.cell;
const cell = self.cell;
self.cell = undefined;
return cell;
}
Expand Down Expand Up @@ -128,7 +128,7 @@ pub fn OnceCell(comptime T: type) type {
defer self.mutex.unlock();

// The first thread to acquire the mutex gets to run the initializer
if (self.done.loadUnchecked() == 0b00) {
if (self.done.raw == 0b00) {
self.cell = f();
defer self.done.store(0b01, .Release);
std.Thread.Futex.wake(&self.done, 1000);
Expand Down Expand Up @@ -160,15 +160,15 @@ var globalMap = OnceCell(std.StringHashMap(i32)).empty();

test "test global map" {
_ = globalMap.getOrInit(returnMap);
var r1 = globalMap.get().?;
const r1 = globalMap.get().?;
try r1.*.put("a", 1);

try testing.expect(r1.*.get("b") != null);
try testing.expect(r1.*.get("b").? == 2);

// must be same hashmap
_ = globalMap.getOrInit(returnMap);
var r2 = globalMap.get().?;
const r2 = globalMap.get().?;

try testing.expect(r2.*.get("a") != null);
try testing.expect(r2.*.get("a").? == 1);
Expand All @@ -186,7 +186,7 @@ test "test global map take" {
try testing.expect(r1.get("b") != null);
try testing.expect(r1.get("b").? == 2);

var r2 = globalMap2.take();
const r2 = globalMap2.take();
try testing.expect(r2 == null);

_ = globalMap2.getOrInit(returnMap);
Expand Down Expand Up @@ -274,11 +274,11 @@ var cell4 = OnceCell(i32).empty();
var LazyMap = Lazy(std.StringHashMap(i32), returnMap).init();

test "test lazy" {
var map = LazyMap.get();
const map = LazyMap.get();
defer map.*.deinit();
try map.*.put("c", 3);

var map2 = LazyMap.get();
const map2 = LazyMap.get();

try testing.expect(map2.*.get("c") != null);
try testing.expect(map2.*.get("c").? == 3);
Expand Down Expand Up @@ -317,10 +317,10 @@ fn returnMutexMap() MutexStringHashMap {
var LazyMutexMap = Lazy(MutexStringHashMap, returnMutexMap).init();

test "test lazy mutex map" {
var obj = LazyMutexMap.get();
const obj = LazyMutexMap.get();
defer obj.*.deinit();

var map = obj.*.borrow();
const map = obj.*.borrow();
try map.*.put("v", 0);
obj.*.restore();

Expand All @@ -330,7 +330,7 @@ test "test lazy mutex map" {
handle.* = try std.Thread.spawn(.{}, struct {
fn thread_fn(x: u8) !void {
_ = x;
var o = LazyMutexMap.get();
const o = LazyMutexMap.get();
var m = o.*.borrow();

const v = m.get("v").?;
Expand Down
8 changes: 4 additions & 4 deletions src/singlethread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn OnceCell(comptime T: type) type {
return null;
}

var cell = self.cell.?;
const cell = self.cell.?;
self.cell = null;
return cell;
}
Expand All @@ -89,7 +89,7 @@ var cell3 = OnceCell(i32).empty();
test "test init once" {
_ = cell3.getOrInit(incrShared);
_ = cell3.getOrInit(incrShared);
var v = cell3.get();
const v = cell3.get();

try testing.expect(v != null);
try testing.expect(v.?.* == 1);
Expand All @@ -105,11 +105,11 @@ fn returnMap() std.StringHashMap(i32) {
var LazyMap = Lazy(std.StringHashMap(i32), returnMap).init();

test "test lazy" {
var map = LazyMap.get();
const map = LazyMap.get();
defer map.*.deinit();
try map.*.put("c", 3);

var map2 = LazyMap.get();
const map2 = LazyMap.get();

try testing.expect(map2.*.get("c") != null);
try testing.expect(map2.*.get("c").? == 3);
Expand Down

0 comments on commit 567930d

Please sign in to comment.