2018-04-28 14:53:06 -07:00
|
|
|
const builtin = @import("builtin");
|
|
|
|
const AtomicOrder = builtin.AtomicOrder;
|
|
|
|
|
2018-04-27 16:27:58 -07:00
|
|
|
/// Many reader, many writer, non-allocating, thread-safe, lock-free
|
|
|
|
pub fn Stack(comptime T: type) type {
|
|
|
|
return struct {
|
2018-05-31 07:56:59 -07:00
|
|
|
root: ?*Node,
|
2018-04-27 16:27:58 -07:00
|
|
|
|
|
|
|
pub const Self = this;
|
|
|
|
|
|
|
|
pub const Node = struct {
|
2018-05-31 07:56:59 -07:00
|
|
|
next: ?*Node,
|
2018-04-27 16:27:58 -07:00
|
|
|
data: T,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn init() Self {
|
2018-05-09 21:29:49 -07:00
|
|
|
return Self{ .root = null };
|
2018-04-27 16:27:58 -07: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 07:56:59 -07:00
|
|
|
pub fn pushFirst(self: *Self, node: *Node) ?*Node {
|
2018-04-27 16:27:58 -07:00
|
|
|
node.next = null;
|
2018-05-31 07:56:59 -07:00
|
|
|
return @cmpxchgStrong(?*Node, &self.root, null, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst);
|
2018-04-27 16:27:58 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn push(self: *Self, node: *Node) void {
|
|
|
|
var root = @atomicLoad(?*Node, &self.root, AtomicOrder.SeqCst);
|
2018-04-27 16:27:58 -07:00
|
|
|
while (true) {
|
|
|
|
node.next = root;
|
2018-06-09 22:13:51 -07:00
|
|
|
root = @cmpxchgWeak(?*Node, &self.root, root, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) orelse break;
|
2018-04-27 16:27:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn pop(self: *Self) ?*Node {
|
|
|
|
var root = @atomicLoad(?*Node, &self.root, AtomicOrder.SeqCst);
|
2018-04-27 16:27:58 -07:00
|
|
|
while (true) {
|
2018-06-09 22:13:51 -07:00
|
|
|
root = @cmpxchgWeak(?*Node, &self.root, root, (root orelse return null).next, AtomicOrder.SeqCst, AtomicOrder.SeqCst) orelse return root;
|
2018-04-27 16:27:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn isEmpty(self: *Self) bool {
|
|
|
|
return @atomicLoad(?*Node, &self.root, AtomicOrder.SeqCst) == null;
|
2018-04-27 16:27:58 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-04-28 14:53:06 -07:00
|
|
|
|
|
|
|
const std = @import("std");
|
|
|
|
const Context = struct {
|
2018-05-31 07:56:59 -07:00
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
stack: *Stack(i32),
|
2018-04-28 14:53:06 -07:00
|
|
|
put_sum: isize,
|
|
|
|
get_sum: isize,
|
2018-04-28 15:00:51 -07:00
|
|
|
get_count: usize,
|
2018-04-28 14:53:06 -07:00
|
|
|
puts_done: u8, // TODO make this a bool
|
|
|
|
};
|
2018-05-02 18:34:34 -07: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 14:53:06 -07:00
|
|
|
const put_thread_count = 3;
|
|
|
|
|
|
|
|
test "std.atomic.stack" {
|
|
|
|
var direct_allocator = std.heap.DirectAllocator.init();
|
|
|
|
defer direct_allocator.deinit();
|
|
|
|
|
2018-05-02 18:34:34 -07:00
|
|
|
var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 300 * 1024);
|
2018-04-28 14:53:06 -07:00
|
|
|
defer direct_allocator.allocator.free(plenty_of_memory);
|
|
|
|
|
|
|
|
var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
|
|
|
|
var a = &fixed_buffer_allocator.allocator;
|
|
|
|
|
|
|
|
var stack = Stack(i32).init();
|
2018-05-09 21:29:49 -07:00
|
|
|
var context = Context{
|
2018-04-28 14:53:06 -07:00
|
|
|
.allocator = a,
|
|
|
|
.stack = &stack,
|
|
|
|
.put_sum = 0,
|
|
|
|
.get_sum = 0,
|
|
|
|
.puts_done = 0,
|
2018-04-28 15:00:51 -07:00
|
|
|
.get_count = 0,
|
2018-04-28 14:53:06 -07:00
|
|
|
};
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
var putters: [put_thread_count]*std.os.Thread = undefined;
|
2018-04-28 14:53:06 -07:00
|
|
|
for (putters) |*t| {
|
2018-05-09 21:29:49 -07:00
|
|
|
t.* = try std.os.spawnThread(&context, startPuts);
|
2018-04-28 14:53:06 -07:00
|
|
|
}
|
2018-05-31 07:56:59 -07:00
|
|
|
var getters: [put_thread_count]*std.os.Thread = undefined;
|
2018-04-28 14:53:06 -07:00
|
|
|
for (getters) |*t| {
|
2018-05-09 21:29:49 -07:00
|
|
|
t.* = try std.os.spawnThread(&context, startGets);
|
2018-04-28 14:53:06 -07:00
|
|
|
}
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
for (putters) |t|
|
|
|
|
t.wait();
|
2018-04-28 14:53:06 -07:00
|
|
|
_ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
|
2018-05-09 21:29:49 -07:00
|
|
|
for (getters) |t|
|
|
|
|
t.wait();
|
2018-04-28 14:53:06 -07:00
|
|
|
|
|
|
|
std.debug.assert(context.put_sum == context.get_sum);
|
2018-04-28 15:00:51 -07:00
|
|
|
std.debug.assert(context.get_count == puts_per_thread * put_thread_count);
|
2018-04-28 14:53:06 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn startPuts(ctx: *Context) u8 {
|
2018-04-28 14:53:06 -07:00
|
|
|
var put_count: usize = puts_per_thread;
|
|
|
|
var r = std.rand.DefaultPrng.init(0xdeadbeef);
|
|
|
|
while (put_count != 0) : (put_count -= 1) {
|
|
|
|
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
|
|
|
|
const x = @bitCast(i32, r.random.scalar(u32));
|
|
|
|
const node = ctx.allocator.create(Stack(i32).Node) catch unreachable;
|
|
|
|
node.data = x;
|
|
|
|
ctx.stack.push(node);
|
|
|
|
_ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn startGets(ctx: *Context) u8 {
|
2018-04-28 14:53:06 -07:00
|
|
|
while (true) {
|
|
|
|
while (ctx.stack.pop()) |node| {
|
|
|
|
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
|
|
|
|
_ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
|
2018-04-28 15:00:51 -07:00
|
|
|
_ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
|
2018-04-28 14:53:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (@atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|