2021-10-04 23:47:27 -07:00
|
|
|
const std = @import("../std.zig");
|
|
|
|
|
const builtin = @import("builtin");
|
2018-07-11 19:38:01 -04:00
|
|
|
const assert = std.debug.assert;
|
2019-02-08 18:18:47 -05:00
|
|
|
const expect = std.testing.expect;
|
2018-04-28 17:53:06 -04:00
|
|
|
|
2018-07-11 19:38:01 -04:00
|
|
|
/// Many reader, many writer, non-allocating, thread-safe
|
|
|
|
|
/// Uses a spinlock to protect push() and pop()
|
2019-02-01 17:49:29 -05:00
|
|
|
/// When building in single threaded mode, this is a simple linked list.
|
2018-04-27 19:27:58 -04:00
|
|
|
pub fn Stack(comptime T: type) type {
|
2018-11-13 05:08:37 -08:00
|
|
|
return struct {
|
2018-05-31 10:56:59 -04:00
|
|
|
root: ?*Node,
|
2019-12-09 21:56:19 +01:00
|
|
|
lock: @TypeOf(lock_init),
|
2019-02-01 17:49:29 -05:00
|
|
|
|
2020-03-10 22:46:19 +02:00
|
|
|
const lock_init = if (builtin.single_threaded) {} else false;
|
2018-04-27 19:27:58 -04:00
|
|
|
|
2018-09-13 16:34:33 -04:00
|
|
|
pub const Self = @This();
|
2018-04-27 19:27:58 -04:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const Node = struct {
|
2018-05-31 10:56:59 -04:00
|
|
|
next: ?*Node,
|
2018-04-27 19:27:58 -04:00
|
|
|
data: T,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub fn init() Self {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Self{
|
2018-07-11 19:38:01 -04:00
|
|
|
.root = null,
|
2019-02-01 17:49:29 -05:00
|
|
|
.lock = lock_init,
|
2018-07-11 19:38:01 -04:00
|
|
|
};
|
2018-04-27 19:27:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// push operation, but only if you are the first item in the stack. if you did not succeed in
|
|
|
|
|
/// being the first item in the stack, returns the other item that was there.
|
2018-05-31 10:56:59 -04:00
|
|
|
pub fn pushFirst(self: *Self, node: *Node) ?*Node {
|
2018-04-27 19:27:58 -04:00
|
|
|
node.next = null;
|
2020-03-10 22:46:19 +02:00
|
|
|
return @cmpxchgStrong(?*Node, &self.root, null, node, .SeqCst, .SeqCst);
|
2018-04-27 19:27:58 -04:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 10:56:59 -04:00
|
|
|
pub fn push(self: *Self, node: *Node) void {
|
2019-02-01 17:49:29 -05:00
|
|
|
if (builtin.single_threaded) {
|
|
|
|
|
node.next = self.root;
|
|
|
|
|
self.root = node;
|
|
|
|
|
} else {
|
2020-03-12 22:42:01 +02:00
|
|
|
while (@atomicRmw(bool, &self.lock, .Xchg, true, .SeqCst)) {}
|
|
|
|
|
defer assert(@atomicRmw(bool, &self.lock, .Xchg, false, .SeqCst));
|
2019-02-01 17:49:29 -05:00
|
|
|
|
|
|
|
|
node.next = self.root;
|
|
|
|
|
self.root = node;
|
|
|
|
|
}
|
2018-04-27 19:27:58 -04:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 10:56:59 -04:00
|
|
|
pub fn pop(self: *Self) ?*Node {
|
2019-02-01 17:49:29 -05:00
|
|
|
if (builtin.single_threaded) {
|
|
|
|
|
const root = self.root orelse return null;
|
|
|
|
|
self.root = root.next;
|
|
|
|
|
return root;
|
|
|
|
|
} else {
|
2020-03-12 22:42:01 +02:00
|
|
|
while (@atomicRmw(bool, &self.lock, .Xchg, true, .SeqCst)) {}
|
|
|
|
|
defer assert(@atomicRmw(bool, &self.lock, .Xchg, false, .SeqCst));
|
2019-02-01 17:49:29 -05:00
|
|
|
|
|
|
|
|
const root = self.root orelse return null;
|
|
|
|
|
self.root = root.next;
|
|
|
|
|
return root;
|
|
|
|
|
}
|
2018-04-27 19:27:58 -04:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 10:56:59 -04:00
|
|
|
pub fn isEmpty(self: *Self) bool {
|
2020-03-10 22:46:19 +02:00
|
|
|
return @atomicLoad(?*Node, &self.root, .SeqCst) == null;
|
2018-04-27 19:27:58 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-04-28 17:53:06 -04:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const Context = struct {
|
2021-10-29 00:37:25 +01:00
|
|
|
allocator: std.mem.Allocator,
|
2018-05-31 10:56:59 -04:00
|
|
|
stack: *Stack(i32),
|
2018-04-28 17:53:06 -04:00
|
|
|
put_sum: isize,
|
|
|
|
|
get_sum: isize,
|
2018-04-28 18:00:51 -04:00
|
|
|
get_count: usize,
|
2020-03-10 22:46:19 +02:00
|
|
|
puts_done: bool,
|
2018-04-28 17:53:06 -04:00
|
|
|
};
|
2018-05-02 21:34:34 -04:00
|
|
|
// TODO add lazy evaluated build options and then put puts_per_thread behind
|
|
|
|
|
// some option such as: "AggressiveMultithreadedFuzzTest". In the AppVeyor
|
|
|
|
|
// CI we would use a less aggressive setting since at 1 core, while we still
|
|
|
|
|
// want this test to pass, we need a smaller value since there is so much thrashing
|
|
|
|
|
// we would also use a less aggressive setting when running in valgrind
|
|
|
|
|
const puts_per_thread = 500;
|
2018-04-28 17:53:06 -04:00
|
|
|
const put_thread_count = 3;
|
|
|
|
|
|
|
|
|
|
test "std.atomic.stack" {
|
2019-11-25 17:25:06 -05:00
|
|
|
var plenty_of_memory = try std.heap.page_allocator.alloc(u8, 300 * 1024);
|
|
|
|
|
defer std.heap.page_allocator.free(plenty_of_memory);
|
2018-04-28 17:53:06 -04:00
|
|
|
|
2021-10-29 00:37:25 +01:00
|
|
|
var fixed_buffer_allocator = std.heap.FixedBufferAllocator.init(plenty_of_memory);
|
2021-10-29 02:08:41 +01:00
|
|
|
var a = fixed_buffer_allocator.threadSafeAllocator();
|
2018-04-28 17:53:06 -04:00
|
|
|
|
|
|
|
|
var stack = Stack(i32).init();
|
2018-11-13 05:08:37 -08:00
|
|
|
var context = Context{
|
2018-04-28 17:53:06 -04:00
|
|
|
.allocator = a,
|
|
|
|
|
.stack = &stack,
|
|
|
|
|
.put_sum = 0,
|
|
|
|
|
.get_sum = 0,
|
2020-03-10 22:46:19 +02:00
|
|
|
.puts_done = false,
|
2018-04-28 18:00:51 -04:00
|
|
|
.get_count = 0,
|
2018-04-28 17:53:06 -04:00
|
|
|
};
|
|
|
|
|
|
2019-02-01 17:49:29 -05:00
|
|
|
if (builtin.single_threaded) {
|
|
|
|
|
{
|
|
|
|
|
var i: usize = 0;
|
|
|
|
|
while (i < put_thread_count) : (i += 1) {
|
2021-05-04 20:47:26 +03:00
|
|
|
try expect(startPuts(&context) == 0);
|
2019-02-01 17:49:29 -05:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-10 22:46:19 +02:00
|
|
|
context.puts_done = true;
|
2019-02-01 17:49:29 -05:00
|
|
|
{
|
|
|
|
|
var i: usize = 0;
|
|
|
|
|
while (i < put_thread_count) : (i += 1) {
|
2021-05-04 20:47:26 +03:00
|
|
|
try expect(startGets(&context) == 0);
|
2019-02-01 17:49:29 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-06-19 21:31:43 -05:00
|
|
|
var putters: [put_thread_count]std.Thread = undefined;
|
2019-02-01 17:49:29 -05:00
|
|
|
for (putters) |*t| {
|
2021-06-19 21:31:43 -05:00
|
|
|
t.* = try std.Thread.spawn(.{}, startPuts, .{&context});
|
2019-02-01 17:49:29 -05:00
|
|
|
}
|
2021-06-19 21:31:43 -05:00
|
|
|
var getters: [put_thread_count]std.Thread = undefined;
|
2019-02-01 17:49:29 -05:00
|
|
|
for (getters) |*t| {
|
2021-06-19 21:31:43 -05:00
|
|
|
t.* = try std.Thread.spawn(.{}, startGets, .{&context});
|
2019-02-01 17:49:29 -05:00
|
|
|
}
|
2018-04-28 17:53:06 -04:00
|
|
|
|
2019-02-01 17:49:29 -05:00
|
|
|
for (putters) |t|
|
2021-06-19 21:31:43 -05:00
|
|
|
t.join();
|
2020-03-10 22:46:19 +02:00
|
|
|
@atomicStore(bool, &context.puts_done, true, .SeqCst);
|
2019-02-01 17:49:29 -05:00
|
|
|
for (getters) |t|
|
2021-06-19 21:31:43 -05:00
|
|
|
t.join();
|
2019-02-01 17:49:29 -05:00
|
|
|
}
|
2018-04-28 17:53:06 -04:00
|
|
|
|
2018-06-12 15:14:32 -04:00
|
|
|
if (context.put_sum != context.get_sum) {
|
2019-12-08 22:53:51 -05:00
|
|
|
std.debug.panic("failure\nput_sum:{} != get_sum:{}", .{ context.put_sum, context.get_sum });
|
2018-06-12 15:14:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context.get_count != puts_per_thread * put_thread_count) {
|
2019-12-08 22:53:51 -05:00
|
|
|
std.debug.panic("failure\nget_count:{} != puts_per_thread:{} * put_thread_count:{}", .{
|
2018-06-12 15:14:32 -04:00
|
|
|
context.get_count,
|
2019-11-06 23:25:57 -05:00
|
|
|
@as(u32, puts_per_thread),
|
|
|
|
|
@as(u32, put_thread_count),
|
2019-12-08 22:53:51 -05:00
|
|
|
});
|
2018-06-12 15:14:32 -04:00
|
|
|
}
|
2018-04-28 17:53:06 -04:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 10:56:59 -04:00
|
|
|
fn startPuts(ctx: *Context) u8 {
|
2018-04-28 17:53:06 -04:00
|
|
|
var put_count: usize = puts_per_thread;
|
2021-10-27 15:53:29 +01:00
|
|
|
var prng = std.rand.DefaultPrng.init(0xdeadbeef);
|
|
|
|
|
const random = prng.random();
|
2018-04-28 17:53:06 -04:00
|
|
|
while (put_count != 0) : (put_count -= 1) {
|
2019-05-24 22:52:07 -04:00
|
|
|
std.time.sleep(1); // let the os scheduler be our fuzz
|
2021-10-27 15:53:29 +01:00
|
|
|
const x = @bitCast(i32, random.int(u32));
|
2019-02-03 16:13:28 -05:00
|
|
|
const node = ctx.allocator.create(Stack(i32).Node) catch unreachable;
|
|
|
|
|
node.* = Stack(i32).Node{
|
2018-06-20 17:33:29 -04:00
|
|
|
.next = undefined,
|
2018-06-20 17:16:27 -04:00
|
|
|
.data = x,
|
2019-02-03 16:13:28 -05:00
|
|
|
};
|
2018-04-28 17:53:06 -04:00
|
|
|
ctx.stack.push(node);
|
2020-03-10 22:46:19 +02:00
|
|
|
_ = @atomicRmw(isize, &ctx.put_sum, .Add, x, .SeqCst);
|
2018-04-28 17:53:06 -04:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 10:56:59 -04:00
|
|
|
fn startGets(ctx: *Context) u8 {
|
2018-04-28 17:53:06 -04:00
|
|
|
while (true) {
|
2020-03-12 22:42:01 +02:00
|
|
|
const last = @atomicLoad(bool, &ctx.puts_done, .SeqCst);
|
2018-06-13 11:57:57 -04:00
|
|
|
|
2018-04-28 17:53:06 -04:00
|
|
|
while (ctx.stack.pop()) |node| {
|
2019-05-24 22:52:07 -04:00
|
|
|
std.time.sleep(1); // let the os scheduler be our fuzz
|
2020-03-10 22:46:19 +02:00
|
|
|
_ = @atomicRmw(isize, &ctx.get_sum, .Add, node.data, .SeqCst);
|
|
|
|
|
_ = @atomicRmw(usize, &ctx.get_count, .Add, 1, .SeqCst);
|
2018-04-28 17:53:06 -04:00
|
|
|
}
|
|
|
|
|
|
2018-06-13 11:57:57 -04:00
|
|
|
if (last) return 0;
|
2018-04-28 17:53:06 -04:00
|
|
|
}
|
|
|
|
|
}
|