Skip to content

Commit

Permalink
Merge branch 'dnut/feat/window' into dnut/roots/mindeps2
Browse files Browse the repository at this point in the history
  • Loading branch information
dnut committed Dec 20, 2024
2 parents fc17d7f + d6c86a6 commit c66fc19
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/sync/shared_memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn SharedPointerWindow(
/// call `release` when you're done with the pointer
pub fn get(self: *Self, index: usize) ?*const T {
self.lock.lockShared();
defer self.lock.lockShared();
defer self.lock.unlockShared();

if (self.window.get(index)) |element| {
return element.acquire().payload();
Expand All @@ -81,7 +81,7 @@ pub fn SharedPointerWindow(
/// call `release` when you're done with the pointer
pub fn contains(self: *Self, index: usize) bool {
self.lock.lockShared();
defer self.lock.lockShared();
defer self.lock.unlockShared();

return self.window.contains(index);
}
Expand All @@ -91,7 +91,7 @@ pub fn SharedPointerWindow(

const items_to_release = blk: {
self.lock.lock();
defer self.lock.lock();
defer self.lock.unlock();

self.center.store(new_center, .monotonic);
break :blk self.window.realignGet(new_center, self.discard_buf);
Expand Down
43 changes: 22 additions & 21 deletions src/utils/collections.zig
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ pub fn Window(T: type) type {
}

fn lowest(self: *const Self) usize {
return self.center - self.state.len / 2;
return self.center -| self.state.len / 2;
}

fn getAssumed(self: *Self, index: usize) *?T {
Expand Down Expand Up @@ -772,34 +772,34 @@ test "binarySearch slice of slices" {
}

test "Window starts empty" {
var mgr = try Window(u64).init(std.testing.allocator, 5, 7);
defer mgr.deinit(std.testing.allocator);
var window = try Window(u64).init(std.testing.allocator, 5, 7);
defer window.deinit(std.testing.allocator);
for (0..20) |i| {
try std.testing.expect(null == mgr.get(i));
try std.testing.expect(null == window.get(i));
}
}

test "Window populates and repopulates (odd)" {
var mgr = try Window(u64).init(std.testing.allocator, 5, 7);
defer mgr.deinit(std.testing.allocator);
var window = try Window(u64).init(std.testing.allocator, 5, 7);
defer window.deinit(std.testing.allocator);
for (0..20) |i| {
const result = mgr.put(i, i * 10);
const result = window.put(i, i * 10);
if (i < 5 or i > 9) {
try std.testing.expectError(error.OutOfBounds, result);
} else {
try std.testing.expectEqual(null, try result);
}
}
for (0..20) |i| {
const result = mgr.put(i, i * 100);
const result = window.put(i, i * 100);
if (i < 5 or i > 9) {
try std.testing.expectError(error.OutOfBounds, result);
} else {
try std.testing.expectEqual(i * 10, try result);
}
}
for (0..20) |i| {
const result = mgr.get(i);
const result = window.get(i);
if (i < 5 or i > 9) {
try std.testing.expectEqual(null, result);
} else {
Expand All @@ -809,18 +809,18 @@ test "Window populates and repopulates (odd)" {
}

test "Window populates (even)" {
var mgr = try Window(u64).init(std.testing.allocator, 4, 7);
defer mgr.deinit(std.testing.allocator);
var window = try Window(u64).init(std.testing.allocator, 4, 7);
defer window.deinit(std.testing.allocator);
for (0..20) |i| {
const result = mgr.put(i, i * 10);
const result = window.put(i, i * 10);
if (i < 5 or i > 8) {
try std.testing.expectError(error.OutOfBounds, result);
} else {
try std.testing.expectEqual(null, try result);
}
}
for (0..20) |i| {
const result = mgr.get(i);
const result = window.get(i);
if (i < 5 or i > 8) {
try std.testing.expectEqual(null, result);
} else {
Expand All @@ -830,39 +830,40 @@ test "Window populates (even)" {
}

test "Window realigns" {
var mgr = try Window(u64).init(std.testing.allocator, 4, 7);
defer mgr.deinit(std.testing.allocator);
var window = try Window(u64).init(std.testing.allocator, 4, 0);
defer window.deinit(std.testing.allocator);
window.realign(7);
for (5..9) |i| {
_ = try mgr.put(i, i * 10);
_ = try window.put(i, i * 10);
}
var deletion_buf: [4]?u64 = undefined;

const deletion = mgr.realignGet(8, deletion_buf[0..]);
const deletion = window.realignGet(8, deletion_buf[0..]);
try std.testing.expectEqual(1, deletion.len);
try std.testing.expectEqual(50, deletion[0]);

const deletion2 = mgr.realignGet(6, deletion_buf[0..]);
const deletion2 = window.realignGet(6, deletion_buf[0..]);
try std.testing.expectEqual(2, deletion2.len);
try std.testing.expectEqual(80, deletion2[0]);
try std.testing.expectEqual(null, deletion2[1]);

for (0..20) |i| {
const result = mgr.get(i);
const result = window.get(i);
if (i < 6 or i > 7) {
try std.testing.expectEqual(null, result);
} else {
try std.testing.expectEqual(i * 10, result);
}
}

const deletion3 = mgr.realignGet(20, deletion_buf[0..]);
const deletion3 = window.realignGet(20, deletion_buf[0..]);
try std.testing.expectEqual(4, deletion3.len);
try std.testing.expectEqual(null, deletion3[0]);
try std.testing.expectEqual(null, deletion3[1]);
try std.testing.expectEqual(60, deletion3[2]);
try std.testing.expectEqual(70, deletion3[3]);

for (0..40) |i| {
try std.testing.expectEqual(null, mgr.get(i));
try std.testing.expectEqual(null, window.get(i));
}
}

0 comments on commit c66fc19

Please sign in to comment.