authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-11-18 21:48:17+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-11-18 21:48:17+02:00
log2418c8206a81d1410850283ea6046c053a24851b
treecc9ddd3f4b7cbc8102884c6ae1db9c80872c6a14
parentcf7de64f1a136c9208284362db6cc1d3c81f1111
parent767dd772c091f072f4b446365e6053021fa67c26
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #7154 from daurnimator/std.atomic

std.atomic.{Int,Bool}

4 files changed, 67 insertions(+), 3 deletions(-)

CMakeLists.txt+1
......@@ -318,6 +318,7 @@ set(ZIG_STAGE2_SOURCES
318318 "${CMAKE_SOURCE_DIR}/lib/std/array_list.zig"
319319 "${CMAKE_SOURCE_DIR}/lib/std/ascii.zig"
320320 "${CMAKE_SOURCE_DIR}/lib/std/atomic.zig"
321 "${CMAKE_SOURCE_DIR}/lib/std/atomic/bool.zig"
321322 "${CMAKE_SOURCE_DIR}/lib/std/atomic/int.zig"
322323 "${CMAKE_SOURCE_DIR}/lib/std/atomic/queue.zig"
323324 "${CMAKE_SOURCE_DIR}/lib/std/atomic/stack.zig"
lib/std/atomic.zig+2
......@@ -5,10 +5,12 @@
55// and substantial portions of the software.
66pub const Stack = @import("atomic/stack.zig").Stack;
77pub const Queue = @import("atomic/queue.zig").Queue;
8pub const Bool = @import("atomic/bool.zig").Bool;
89pub const Int = @import("atomic/int.zig").Int;
910
1011test "std.atomic" {
1112 _ = @import("atomic/stack.zig");
1213 _ = @import("atomic/queue.zig");
14 _ = @import("atomic/bool.zig");
1315 _ = @import("atomic/int.zig");
1416}
lib/std/atomic/bool.zig created+43
......@@ -0,0 +1,43 @@
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2015-2020 Zig Contributors
3// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.
6
7const std = @import("std");
8const builtin = std.builtin;
9const testing = std.testing;
10
11/// Thread-safe, lock-free boolean
12pub const Bool = extern struct {
13 unprotected_value: bool,
14
15 pub const Self = @This();
16
17 pub fn init(init_val: bool) Self {
18 return Self{ .unprotected_value = init_val };
19 }
20
21 // xchg is only valid rmw operation for a bool
22 /// Atomically modifies memory and then returns the previous value.
23 pub fn xchg(self: *Self, operand: bool, comptime ordering: std.builtin.AtomicOrder) bool {
24 return @atomicRmw(bool, &self.unprotected_value, .Xchg, operand, ordering);
25 }
26
27 pub fn load(self: *Self, comptime ordering: std.builtin.AtomicOrder) bool {
28 return @atomicLoad(bool, &self.unprotected_value, ordering);
29 }
30
31 pub fn store(self: *Self, value: bool, comptime ordering: std.builtin.AtomicOrder) void {
32 @atomicStore(bool, &self.unprotected_value, value, ordering);
33 }
34};
35
36test "std.atomic.Bool" {
37 var a = Bool.init(false);
38 testing.expectEqual(false, a.xchg(false, .SeqCst));
39 testing.expectEqual(false, a.load(.SeqCst));
40 a.store(true, .SeqCst);
41 testing.expectEqual(true, a.xchg(false, .SeqCst));
42 testing.expectEqual(false, a.load(.SeqCst));
43}
lib/std/atomic/int.zig+21-3
......@@ -4,11 +4,13 @@
44// The MIT license requires this copyright notice to be included in all copies
55// and substantial portions of the software.
66
7const builtin = @import("std").builtin;
7const std = @import("std");
8const builtin = std.builtin;
9const testing = std.testing;
810
911/// Thread-safe, lock-free integer
1012pub fn Int(comptime T: type) type {
11 return struct {
13 return extern struct {
1214 unprotected_value: T,
1315
1416 pub const Self = @This();
......@@ -19,7 +21,7 @@ pub fn Int(comptime T: type) type {
1921
2022 /// Read, Modify, Write
2123 pub fn rmw(self: *Self, comptime op: builtin.AtomicRmwOp, operand: T, comptime ordering: builtin.AtomicOrder) T {
22 return @atomicRmw(T, &self.unprotected_value, operand, ordering);
24 return @atomicRmw(T, &self.unprotected_value, op, operand, ordering);
2325 }
2426
2527 pub fn load(self: *Self, comptime ordering: builtin.AtomicOrder) T {
......@@ -30,11 +32,13 @@ pub fn Int(comptime T: type) type {
3032 @atomicStore(T, &self.unprotected_value, value, ordering);
3133 }
3234
35 /// Twos complement wraparound increment
3336 /// Returns previous value
3437 pub fn incr(self: *Self) T {
3538 return self.rmw(.Add, 1, .SeqCst);
3639 }
3740
41 /// Twos complement wraparound decrement
3842 /// Returns previous value
3943 pub fn decr(self: *Self) T {
4044 return self.rmw(.Sub, 1, .SeqCst);
......@@ -52,8 +56,22 @@ pub fn Int(comptime T: type) type {
5256 return self.rmw(.Xchg, new_value, .SeqCst);
5357 }
5458
59 /// Twos complement wraparound add
60 /// Returns previous value
5561 pub fn fetchAdd(self: *Self, op: T) T {
5662 return self.rmw(.Add, op, .SeqCst);
5763 }
5864 };
5965}
66
67test "std.atomic.Int" {
68 var a = Int(u8).init(0);
69 testing.expectEqual(@as(u8, 0), a.incr());
70 testing.expectEqual(@as(u8, 1), a.load(.SeqCst));
71 a.store(42, .SeqCst);
72 testing.expectEqual(@as(u8, 42), a.decr());
73 testing.expectEqual(@as(u8, 41), a.xchg(100));
74 testing.expectEqual(@as(u8, 100), a.fetchAdd(5));
75 testing.expectEqual(@as(u8, 105), a.get());
76 a.set(200);
77}