diff options
| author | daurnimator <quae@daurnimator.com> | 2020-11-19 00:58:13 +1100 |
|---|---|---|
| committer | daurnimator <quae@daurnimator.com> | 2020-11-19 00:58:13 +1100 |
| commit | 767dd772c091f072f4b446365e6053021fa67c26 (patch) | |
| tree | 2479f21dba826d8462f2a4760344d18afb914305 | |
| parent | d89d6374bed73683fa92d3c409016fb3f260a7c1 (diff) | |
| download | zig-767dd772c091f072f4b446365e6053021fa67c26.tar.gz zig-767dd772c091f072f4b446365e6053021fa67c26.zip | |
std: add std.atomic.Bool
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | lib/std/atomic.zig | 2 | ||||
| -rw-r--r-- | lib/std/atomic/bool.zig | 43 |
3 files changed, 46 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e5a907ffe..16e3d7a347 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -318,6 +318,7 @@ set(ZIG_STAGE2_SOURCES "${CMAKE_SOURCE_DIR}/lib/std/array_list.zig" "${CMAKE_SOURCE_DIR}/lib/std/ascii.zig" "${CMAKE_SOURCE_DIR}/lib/std/atomic.zig" + "${CMAKE_SOURCE_DIR}/lib/std/atomic/bool.zig" "${CMAKE_SOURCE_DIR}/lib/std/atomic/int.zig" "${CMAKE_SOURCE_DIR}/lib/std/atomic/queue.zig" "${CMAKE_SOURCE_DIR}/lib/std/atomic/stack.zig" diff --git a/lib/std/atomic.zig b/lib/std/atomic.zig index c465708ffa..4402ca462b 100644 --- a/lib/std/atomic.zig +++ b/lib/std/atomic.zig @@ -5,10 +5,12 @@ // and substantial portions of the software. pub const Stack = @import("atomic/stack.zig").Stack; pub const Queue = @import("atomic/queue.zig").Queue; +pub const Bool = @import("atomic/bool.zig").Bool; pub const Int = @import("atomic/int.zig").Int; test "std.atomic" { _ = @import("atomic/stack.zig"); _ = @import("atomic/queue.zig"); + _ = @import("atomic/bool.zig"); _ = @import("atomic/int.zig"); } diff --git a/lib/std/atomic/bool.zig b/lib/std/atomic/bool.zig new file mode 100644 index 0000000000..c686fdfae0 --- /dev/null +++ b/lib/std/atomic/bool.zig @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2015-2020 Zig Contributors +// This file is part of [zig](https://ziglang.org/), which is MIT licensed. +// The MIT license requires this copyright notice to be included in all copies +// and substantial portions of the software. + +const std = @import("std"); +const builtin = std.builtin; +const testing = std.testing; + +/// Thread-safe, lock-free boolean +pub const Bool = extern struct { + unprotected_value: bool, + + pub const Self = @This(); + + pub fn init(init_val: bool) Self { + return Self{ .unprotected_value = init_val }; + } + + // xchg is only valid rmw operation for a bool + /// Atomically modifies memory and then returns the previous value. + pub fn xchg(self: *Self, operand: bool, comptime ordering: std.builtin.AtomicOrder) bool { + return @atomicRmw(bool, &self.unprotected_value, .Xchg, operand, ordering); + } + + pub fn load(self: *Self, comptime ordering: std.builtin.AtomicOrder) bool { + return @atomicLoad(bool, &self.unprotected_value, ordering); + } + + pub fn store(self: *Self, value: bool, comptime ordering: std.builtin.AtomicOrder) void { + @atomicStore(bool, &self.unprotected_value, value, ordering); + } +}; + +test "std.atomic.Bool" { + var a = Bool.init(false); + testing.expectEqual(false, a.xchg(false, .SeqCst)); + testing.expectEqual(false, a.load(.SeqCst)); + a.store(true, .SeqCst); + testing.expectEqual(true, a.xchg(false, .SeqCst)); + testing.expectEqual(false, a.load(.SeqCst)); +} |
