authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-28 18:00:51-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-28 18:00:51-04:00
log5d6e44b3f2bf37deaf09ce4a473fa5b52a6fb760
tree18333571eb948b8a8f65aa384e8f967e72369c04
parent96ecb402590df7a02526009f1630f27e14a0e77c

add tests for std.atomic Queue and Stack


2 files changed, 88 insertions(+), 1 deletions(-)

std/atomic/queue.zig+84-1
...@@ -1,3 +1,7 @@...@@ -1,3 +1,7 @@
1const builtin = @import("builtin");
2const AtomicOrder = builtin.AtomicOrder;
3const AtomicRmwOp = builtin.AtomicRmwOp;
4
1/// Many reader, many writer, non-allocating, thread-safe, lock-free5/// Many reader, many writer, non-allocating, thread-safe, lock-free
2pub fn Queue(comptime T: type) type {6pub fn Queue(comptime T: type) type {
3 return struct {7 return struct {
...@@ -12,7 +16,7 @@ pub fn Queue(comptime T: type) type {...@@ -12,7 +16,7 @@ pub fn Queue(comptime T: type) type {
12 data: T,16 data: T,
13 };17 };
1418
15 // TODO: well defined copy elision19 // TODO: well defined copy elision: https://github.com/zig-lang/zig/issues/287
16 pub fn init(self: &Self) void {20 pub fn init(self: &Self) void {
17 self.root.next = null;21 self.root.next = null;
18 self.head = &self.root;22 self.head = &self.root;
...@@ -35,3 +39,82 @@ pub fn Queue(comptime T: type) type {...@@ -35,3 +39,82 @@ pub fn Queue(comptime T: type) type {
35 }39 }
36 };40 };
37}41}
42
43const std = @import("std");
44const Context = struct {
45 allocator: &std.mem.Allocator,
46 queue: &Queue(i32),
47 put_sum: isize,
48 get_sum: isize,
49 get_count: usize,
50 puts_done: u8, // TODO make this a bool
51};
52const puts_per_thread = 10000;
53const put_thread_count = 3;
54
55test "std.atomic.queue" {
56 var direct_allocator = std.heap.DirectAllocator.init();
57 defer direct_allocator.deinit();
58
59 var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 64 * 1024 * 1024);
60 defer direct_allocator.allocator.free(plenty_of_memory);
61
62 var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
63 var a = &fixed_buffer_allocator.allocator;
64
65 var queue: Queue(i32) = undefined;
66 queue.init();
67 var context = Context {
68 .allocator = a,
69 .queue = &queue,
70 .put_sum = 0,
71 .get_sum = 0,
72 .puts_done = 0,
73 .get_count = 0,
74 };
75
76 var putters: [put_thread_count]&std.os.Thread = undefined;
77 for (putters) |*t| {
78 *t = try std.os.spawnThreadAllocator(a, &context, startPuts);
79 }
80 var getters: [put_thread_count]&std.os.Thread = undefined;
81 for (getters) |*t| {
82 *t = try std.os.spawnThreadAllocator(a, &context, startGets);
83 }
84
85 for (putters) |t| t.wait();
86 _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
87 for (getters) |t| t.wait();
88
89 std.debug.assert(context.put_sum == context.get_sum);
90 std.debug.assert(context.get_count == puts_per_thread * put_thread_count);
91}
92
93fn startPuts(ctx: &Context) u8 {
94 var put_count: usize = puts_per_thread;
95 var r = std.rand.DefaultPrng.init(0xdeadbeef);
96 while (put_count != 0) : (put_count -= 1) {
97 std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
98 const x = @bitCast(i32, r.random.scalar(u32));
99 const node = ctx.allocator.create(Queue(i32).Node) catch unreachable;
100 node.data = x;
101 ctx.queue.put(node);
102 _ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst);
103 }
104 return 0;
105}
106
107fn startGets(ctx: &Context) u8 {
108 while (true) {
109 while (ctx.queue.get()) |node| {
110 std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
111 _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
112 _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
113 }
114
115 if (@atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1) {
116 break;
117 }
118 }
119 return 0;
120}
std/atomic/stack.zig+4
...@@ -53,6 +53,7 @@ const Context = struct {...@@ -53,6 +53,7 @@ const Context = struct {
53 stack: &Stack(i32),53 stack: &Stack(i32),
54 put_sum: isize,54 put_sum: isize,
55 get_sum: isize,55 get_sum: isize,
56 get_count: usize,
56 puts_done: u8, // TODO make this a bool57 puts_done: u8, // TODO make this a bool
57};58};
58const puts_per_thread = 1000;59const puts_per_thread = 1000;
...@@ -75,6 +76,7 @@ test "std.atomic.stack" {...@@ -75,6 +76,7 @@ test "std.atomic.stack" {
75 .put_sum = 0,76 .put_sum = 0,
76 .get_sum = 0,77 .get_sum = 0,
77 .puts_done = 0,78 .puts_done = 0,
79 .get_count = 0,
78 };80 };
7981
80 var putters: [put_thread_count]&std.os.Thread = undefined;82 var putters: [put_thread_count]&std.os.Thread = undefined;
...@@ -91,6 +93,7 @@ test "std.atomic.stack" {...@@ -91,6 +93,7 @@ test "std.atomic.stack" {
91 for (getters) |t| t.wait();93 for (getters) |t| t.wait();
9294
93 std.debug.assert(context.put_sum == context.get_sum);95 std.debug.assert(context.put_sum == context.get_sum);
96 std.debug.assert(context.get_count == puts_per_thread * put_thread_count);
94}97}
9598
96fn startPuts(ctx: &Context) u8 {99fn startPuts(ctx: &Context) u8 {
...@@ -112,6 +115,7 @@ fn startGets(ctx: &Context) u8 {...@@ -112,6 +115,7 @@ fn startGets(ctx: &Context) u8 {
112 while (ctx.stack.pop()) |node| {115 while (ctx.stack.pop()) |node| {
113 std.os.time.sleep(0, 1); // let the os scheduler be our fuzz116 std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
114 _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);117 _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
118 _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
115 }119 }
116120
117 if (@atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1) {121 if (@atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1) {