| ... | ... | @@ -0,0 +1,522 @@ |
| 1 | // SPDX-License-Identifier: MIT |
| 2 | // Copyright (c) 2015-2021 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 | |
| 7 | const std = @import("../std.zig"); |
| 8 | |
| 9 | const testing = std.testing; |
| 10 | const target = std.Target.current; |
| 11 | const Ordering = std.atomic.Ordering; |
| 12 | |
| 13 | pub fn Atomic(comptime T: type) type { |
| 14 | return extern struct { |
| 15 | value: T, |
| 16 | |
| 17 | const Self = @This(); |
| 18 | |
| 19 | pub fn init(value: T) Self { |
| 20 | return .{ .value = value }; |
| 21 | } |
| 22 | |
| 23 | /// Non-atomically load from the atomic value without synchronization. |
| 24 | /// Care must be taken to avoid data-races when interacting with other atomic operations. |
| 25 | pub fn loadUnchecked(self: Self) T { |
| 26 | return self.value; |
| 27 | } |
| 28 | |
| 29 | /// Non-atomically store to the atomic value without synchronization. |
| 30 | /// Care must be taken to avoid data-races when interacting with other atomic operations. |
| 31 | pub fn storeUnchecked(self: *Self, value: T) void { |
| 32 | self.value = value; |
| 33 | } |
| 34 | |
| 35 | pub fn load(self: *const Self, comptime ordering: Ordering) T { |
| 36 | return switch (ordering) { |
| 37 | .AcqRel => @compileError(@tagName(ordering) ++ " implies " ++ @tagName(Ordering.Release) ++ " which is only allowed on atomic stores"), |
| 38 | .Release => @compileError(@tagName(ordering) ++ " is only allowed on atomic stores"), |
| 39 | else => @atomicLoad(T, &self.value, ordering), |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | pub fn store(self: *Self, value: T, comptime ordering: Ordering) void { |
| 44 | return switch (ordering) { |
| 45 | .AcqRel => @compileError(@tagName(ordering) ++ " implies " ++ @tagName(Ordering.Acquire) ++ " which is only allowed on atomic loads"), |
| 46 | .Acquire => @compileError(@tagName(ordering) ++ " is only allowed on atomic loads"), |
| 47 | else => @atomicStore(T, &self.value, value, ordering), |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | pub fn swap(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { |
| 52 | return self.rmw(.Xchg, value, ordering); |
| 53 | } |
| 54 | |
| 55 | pub fn compareAndSwap( |
| 56 | self: *Self, |
| 57 | compare: T, |
| 58 | exchange: T, |
| 59 | comptime success: Ordering, |
| 60 | comptime failure: Ordering, |
| 61 | ) callconv(.Inline) ?T { |
| 62 | return self.cmpxchg(true, compare, exchange, success, failure); |
| 63 | } |
| 64 | |
| 65 | pub fn tryCompareAndSwap( |
| 66 | self: *Self, |
| 67 | compare: T, |
| 68 | exchange: T, |
| 69 | comptime success: Ordering, |
| 70 | comptime failure: Ordering, |
| 71 | ) callconv(.Inline) ?T { |
| 72 | return self.cmpxchg(false, compare, exchange, success, failure); |
| 73 | } |
| 74 | |
| 75 | fn cmpxchg( |
| 76 | self: *Self, |
| 77 | comptime is_strong: bool, |
| 78 | compare: T, |
| 79 | exchange: T, |
| 80 | comptime success: Ordering, |
| 81 | comptime failure: Ordering, |
| 82 | ) callconv(.Inline) ?T { |
| 83 | if (success == .Unordered or failure == .Unordered) { |
| 84 | @compileError(@tagName(Ordering.Unordered) ++ " is only allowed on atomic loads and stores"); |
| 85 | } |
| 86 | |
| 87 | comptime var success_is_stronger = switch (failure) { |
| 88 | .SeqCst => success == .SeqCst, |
| 89 | .AcqRel => @compileError(@tagName(failure) ++ " implies " ++ @tagName(Ordering.Release) ++ " which is only allowed on success"), |
| 90 | .Acquire => success == .SeqCst or success == .AcqRel or success == .Acquire, |
| 91 | .Release => @compileError(@tagName(failure) ++ " is only allowed on success"), |
| 92 | .Monotonic => true, |
| 93 | .Unordered => unreachable, |
| 94 | }; |
| 95 | |
| 96 | if (!success_is_stronger) { |
| 97 | @compileError(@tagName(success) ++ " must be stronger than " ++ @tagName(failure)); |
| 98 | } |
| 99 | |
| 100 | return switch (is_strong) { |
| 101 | true => @cmpxchgStrong(T, &self.value, compare, exchange, success, failure), |
| 102 | false => @cmpxchgWeak(T, &self.value, compare, exchange, success, failure), |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | fn rmw( |
| 107 | self: *Self, |
| 108 | comptime op: std.builtin.AtomicRmwOp, |
| 109 | value: T, |
| 110 | comptime ordering: Ordering, |
| 111 | ) callconv(.Inline) T { |
| 112 | return @atomicRmw(T, &self.value, op, value, ordering); |
| 113 | } |
| 114 | |
| 115 | fn exportWhen(comptime condition: bool, comptime functions: type) type { |
| 116 | return if (condition) functions else struct {}; |
| 117 | } |
| 118 | |
| 119 | pub usingnamespace exportWhen(std.meta.trait.isNumber(T), struct { |
| 120 | pub fn fetchAdd(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { |
| 121 | return self.rmw(.Add, value, ordering); |
| 122 | } |
| 123 | |
| 124 | pub fn fetchSub(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { |
| 125 | return self.rmw(.Sub, value, ordering); |
| 126 | } |
| 127 | |
| 128 | pub fn fetchMin(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { |
| 129 | return self.rmw(.Min, value, ordering); |
| 130 | } |
| 131 | |
| 132 | pub fn fetchMax(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { |
| 133 | return self.rmw(.Max, value, ordering); |
| 134 | } |
| 135 | }); |
| 136 | |
| 137 | pub usingnamespace exportWhen(std.meta.trait.isIntegral(T), struct { |
| 138 | pub fn fetchAnd(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { |
| 139 | return self.rmw(.And, value, ordering); |
| 140 | } |
| 141 | |
| 142 | pub fn fetchNand(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { |
| 143 | return self.rmw(.Nand, value, ordering); |
| 144 | } |
| 145 | |
| 146 | pub fn fetchOr(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { |
| 147 | return self.rmw(.Or, value, ordering); |
| 148 | } |
| 149 | |
| 150 | pub fn fetchXor(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { |
| 151 | return self.rmw(.Xor, value, ordering); |
| 152 | } |
| 153 | |
| 154 | const Bit = std.math.Log2Int(T); |
| 155 | const BitRmwOp = enum { |
| 156 | Set, |
| 157 | Reset, |
| 158 | Toggle, |
| 159 | }; |
| 160 | |
| 161 | pub fn bitSet(self: *Self, bit: Bit, comptime ordering: Ordering) callconv(.Inline) u1 { |
| 162 | return bitRmw(self, .Set, bit, ordering); |
| 163 | } |
| 164 | |
| 165 | pub fn bitReset(self: *Self, bit: Bit, comptime ordering: Ordering) callconv(.Inline) u1 { |
| 166 | return bitRmw(self, .Reset, bit, ordering); |
| 167 | } |
| 168 | |
| 169 | pub fn bitToggle(self: *Self, bit: Bit, comptime ordering: Ordering) callconv(.Inline) u1 { |
| 170 | return bitRmw(self, .Toggle, bit, ordering); |
| 171 | } |
| 172 | |
| 173 | fn bitRmw( |
| 174 | self: *Self, |
| 175 | comptime op: BitRmwOp, |
| 176 | bit: Bit, |
| 177 | comptime ordering: Ordering, |
| 178 | ) callconv(.Inline) u1 { |
| 179 | // x86 supports dedicated bitwise instructions |
| 180 | if (comptime target.cpu.arch.isX86() and @sizeOf(T) >= 2 and @sizeOf(T) <= 8) { |
| 181 | const instruction = switch (op) { |
| 182 | .Set => "lock bts", |
| 183 | .Reset => "lock btr", |
| 184 | .Toggle => "lock btc", |
| 185 | }; |
| 186 | |
| 187 | const suffix = switch (@sizeOf(T)) { |
| 188 | 2 => "w", |
| 189 | 4 => "l", |
| 190 | 8 => "q", |
| 191 | else => @compileError("Invalid atomic type " ++ @typeName(T)), |
| 192 | }; |
| 193 | |
| 194 | const old_bit = asm volatile (instruction ++ suffix ++ " %[bit], %[ptr]" |
| 195 | : [result] "={@ccc}" (-> u8) // LLVM doesn't support u1 flag register return values |
| 196 | : [ptr] "*p" (&self.value), |
| 197 | [bit] "X" (@as(T, bit)) |
| 198 | : "cc", "memory" |
| 199 | ); |
| 200 | |
| 201 | return @intCast(u1, old_bit); |
| 202 | } |
| 203 | |
| 204 | const mask = @as(T, 1) << bit; |
| 205 | const value = switch (op) { |
| 206 | .Set => self.fetchOr(mask, ordering), |
| 207 | .Reset => self.fetchAnd(~mask, ordering), |
| 208 | .Toggle => self.fetchXor(mask, ordering), |
| 209 | }; |
| 210 | |
| 211 | return @boolToInt(value & mask != 0); |
| 212 | } |
| 213 | }); |
| 214 | }; |
| 215 | } |
| 216 | |
| 217 | fn atomicIntTypes() []const type { |
| 218 | comptime var bytes = 1; |
| 219 | comptime var types: []const type = &[_]type{}; |
| 220 | inline while (bytes <= @sizeOf(usize)) : (bytes *= 2) { |
| 221 | types = types ++ &[_]type{std.meta.Int(.unsigned, bytes * 8)}; |
| 222 | } |
| 223 | return types; |
| 224 | } |
| 225 | |
| 226 | test "Atomic.loadUnchecked" { |
| 227 | inline for (atomicIntTypes()) |Int| { |
| 228 | var x = Atomic(Int).init(5); |
| 229 | try testing.expectEqual(x.loadUnchecked(), 5); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | test "Atomic.storeUnchecked" { |
| 234 | inline for (atomicIntTypes()) |Int| { |
| 235 | var x = Atomic(usize).init(5); |
| 236 | x.storeUnchecked(10); |
| 237 | try testing.expectEqual(x.loadUnchecked(), 10); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | test "Atomic.load" { |
| 242 | inline for (atomicIntTypes()) |Int| { |
| 243 | inline for (.{ .Unordered, .Monotonic, .Acquire, .SeqCst }) |ordering| { |
| 244 | var x = Atomic(Int).init(5); |
| 245 | try testing.expectEqual(x.load(ordering), 5); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | test "Atomic.store" { |
| 251 | inline for (atomicIntTypes()) |Int| { |
| 252 | inline for (.{ .Unordered, .Monotonic, .Release, .SeqCst }) |ordering| { |
| 253 | var x = Atomic(usize).init(5); |
| 254 | x.store(10, ordering); |
| 255 | try testing.expectEqual(x.load(.SeqCst), 10); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | const atomic_rmw_orderings = [_]Ordering{ |
| 261 | .Monotonic, |
| 262 | .Acquire, |
| 263 | .Release, |
| 264 | .AcqRel, |
| 265 | .SeqCst, |
| 266 | }; |
| 267 | |
| 268 | test "Atomic.swap" { |
| 269 | inline for (atomic_rmw_orderings) |ordering| { |
| 270 | var x = Atomic(usize).init(5); |
| 271 | try testing.expectEqual(x.swap(10, ordering), 5); |
| 272 | try testing.expectEqual(x.load(.SeqCst), 10); |
| 273 | |
| 274 | var y = Atomic(enum(usize) { a, b, c }).init(.c); |
| 275 | try testing.expectEqual(y.swap(.a, ordering), .c); |
| 276 | try testing.expectEqual(y.load(.SeqCst), .a); |
| 277 | |
| 278 | var z = Atomic(f32).init(5.0); |
| 279 | try testing.expectEqual(z.swap(10.0, ordering), 5.0); |
| 280 | try testing.expectEqual(z.load(.SeqCst), 10.0); |
| 281 | |
| 282 | var a = Atomic(bool).init(false); |
| 283 | try testing.expectEqual(a.swap(true, ordering), false); |
| 284 | try testing.expectEqual(a.load(.SeqCst), true); |
| 285 | |
| 286 | var b = Atomic(?*u8).init(null); |
| 287 | try testing.expectEqual(b.swap(@intToPtr(?*u8, @alignOf(u8)), ordering), null); |
| 288 | try testing.expectEqual(b.load(.SeqCst), @intToPtr(?*u8, @alignOf(u8))); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | const atomic_cmpxchg_orderings = [_][2]Ordering{ |
| 293 | .{ .Monotonic, .Monotonic }, |
| 294 | .{ .Acquire, .Monotonic }, |
| 295 | .{ .Acquire, .Acquire }, |
| 296 | .{ .Release, .Monotonic }, |
| 297 | // Although accepted by LLVM, acquire failure implies AcqRel success |
| 298 | // .{ .Release, .Acquire }, |
| 299 | .{ .AcqRel, .Monotonic }, |
| 300 | .{ .AcqRel, .Acquire }, |
| 301 | .{ .SeqCst, .Monotonic }, |
| 302 | .{ .SeqCst, .Acquire }, |
| 303 | .{ .SeqCst, .SeqCst }, |
| 304 | }; |
| 305 | |
| 306 | test "Atomic.compareAndSwap" { |
| 307 | inline for (atomicIntTypes()) |Int| { |
| 308 | inline for (atomic_cmpxchg_orderings) |ordering| { |
| 309 | var x = Atomic(Int).init(0); |
| 310 | try testing.expectEqual(x.compareAndSwap(1, 0, ordering[0], ordering[1]), 0); |
| 311 | try testing.expectEqual(x.load(.SeqCst), 0); |
| 312 | try testing.expectEqual(x.compareAndSwap(0, 1, ordering[0], ordering[1]), null); |
| 313 | try testing.expectEqual(x.load(.SeqCst), 1); |
| 314 | try testing.expectEqual(x.compareAndSwap(1, 0, ordering[0], ordering[1]), null); |
| 315 | try testing.expectEqual(x.load(.SeqCst), 0); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | test "Atomic.tryCompareAndSwap" { |
| 321 | inline for (atomicIntTypes()) |Int| { |
| 322 | inline for (atomic_cmpxchg_orderings) |ordering| { |
| 323 | var x = Atomic(Int).init(0); |
| 324 | |
| 325 | try testing.expectEqual(x.tryCompareAndSwap(1, 0, ordering[0], ordering[1]), 0); |
| 326 | try testing.expectEqual(x.load(.SeqCst), 0); |
| 327 | |
| 328 | while (x.tryCompareAndSwap(0, 1, ordering[0], ordering[1])) |_| {} |
| 329 | try testing.expectEqual(x.load(.SeqCst), 1); |
| 330 | |
| 331 | while (x.tryCompareAndSwap(1, 0, ordering[0], ordering[1])) |_| {} |
| 332 | try testing.expectEqual(x.load(.SeqCst), 0); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | test "Atomic.fetchAdd" { |
| 338 | inline for (atomicIntTypes()) |Int| { |
| 339 | inline for (atomic_rmw_orderings) |ordering| { |
| 340 | var x = Atomic(Int).init(5); |
| 341 | try testing.expectEqual(x.fetchAdd(5, ordering), 5); |
| 342 | try testing.expectEqual(x.load(.SeqCst), 10); |
| 343 | try testing.expectEqual(x.fetchAdd(std.math.maxInt(Int), ordering), 10); |
| 344 | try testing.expectEqual(x.load(.SeqCst), 9); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | test "Atomic.fetchSub" { |
| 350 | inline for (atomicIntTypes()) |Int| { |
| 351 | inline for (atomic_rmw_orderings) |ordering| { |
| 352 | var x = Atomic(Int).init(5); |
| 353 | try testing.expectEqual(x.fetchSub(5, ordering), 5); |
| 354 | try testing.expectEqual(x.load(.SeqCst), 0); |
| 355 | try testing.expectEqual(x.fetchSub(1, ordering), 0); |
| 356 | try testing.expectEqual(x.load(.SeqCst), std.math.maxInt(Int)); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | test "Atomic.fetchMin" { |
| 362 | inline for (atomicIntTypes()) |Int| { |
| 363 | inline for (atomic_rmw_orderings) |ordering| { |
| 364 | var x = Atomic(Int).init(5); |
| 365 | try testing.expectEqual(x.fetchMin(0, ordering), 5); |
| 366 | try testing.expectEqual(x.load(.SeqCst), 0); |
| 367 | try testing.expectEqual(x.fetchMin(10, ordering), 0); |
| 368 | try testing.expectEqual(x.load(.SeqCst), 0); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | test "Atomic.fetchMax" { |
| 374 | inline for (atomicIntTypes()) |Int| { |
| 375 | inline for (atomic_rmw_orderings) |ordering| { |
| 376 | var x = Atomic(Int).init(5); |
| 377 | try testing.expectEqual(x.fetchMax(10, ordering), 5); |
| 378 | try testing.expectEqual(x.load(.SeqCst), 10); |
| 379 | try testing.expectEqual(x.fetchMax(5, ordering), 10); |
| 380 | try testing.expectEqual(x.load(.SeqCst), 10); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | test "Atomic.fetchAnd" { |
| 386 | inline for (atomicIntTypes()) |Int| { |
| 387 | inline for (atomic_rmw_orderings) |ordering| { |
| 388 | var x = Atomic(Int).init(0b11); |
| 389 | try testing.expectEqual(x.fetchAnd(0b10, ordering), 0b11); |
| 390 | try testing.expectEqual(x.load(.SeqCst), 0b10); |
| 391 | try testing.expectEqual(x.fetchAnd(0b00, ordering), 0b10); |
| 392 | try testing.expectEqual(x.load(.SeqCst), 0b00); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | test "Atomic.fetchNand" { |
| 398 | inline for (atomicIntTypes()) |Int| { |
| 399 | inline for (atomic_rmw_orderings) |ordering| { |
| 400 | var x = Atomic(Int).init(0b11); |
| 401 | try testing.expectEqual(x.fetchNand(0b10, ordering), 0b11); |
| 402 | try testing.expectEqual(x.load(.SeqCst), ~@as(Int, 0b10)); |
| 403 | try testing.expectEqual(x.fetchNand(0b00, ordering), ~@as(Int, 0b10)); |
| 404 | try testing.expectEqual(x.load(.SeqCst), ~@as(Int, 0b00)); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | test "Atomic.fetchOr" { |
| 410 | inline for (atomicIntTypes()) |Int| { |
| 411 | inline for (atomic_rmw_orderings) |ordering| { |
| 412 | var x = Atomic(Int).init(0b11); |
| 413 | try testing.expectEqual(x.fetchOr(0b100, ordering), 0b11); |
| 414 | try testing.expectEqual(x.load(.SeqCst), 0b111); |
| 415 | try testing.expectEqual(x.fetchOr(0b010, ordering), 0b111); |
| 416 | try testing.expectEqual(x.load(.SeqCst), 0b111); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | test "Atomic.fetchXor" { |
| 422 | inline for (atomicIntTypes()) |Int| { |
| 423 | inline for (atomic_rmw_orderings) |ordering| { |
| 424 | var x = Atomic(Int).init(0b11); |
| 425 | try testing.expectEqual(x.fetchXor(0b10, ordering), 0b11); |
| 426 | try testing.expectEqual(x.load(.SeqCst), 0b01); |
| 427 | try testing.expectEqual(x.fetchXor(0b01, ordering), 0b01); |
| 428 | try testing.expectEqual(x.load(.SeqCst), 0b00); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | test "Atomic.bitSet" { |
| 434 | inline for (atomicIntTypes()) |Int| { |
| 435 | inline for (atomic_rmw_orderings) |ordering| { |
| 436 | var x = Atomic(Int).init(0); |
| 437 | const bit_array = @as([std.meta.bitCount(Int)]void, undefined); |
| 438 | |
| 439 | for (bit_array) |_, bit_index| { |
| 440 | const bit = @intCast(std.math.Log2Int(Int), bit_index); |
| 441 | const mask = @as(Int, 1) << bit; |
| 442 | |
| 443 | // setting the bit should change the bit |
| 444 | try testing.expect(x.load(.SeqCst) & mask == 0); |
| 445 | try testing.expectEqual(x.bitSet(bit, ordering), 0); |
| 446 | try testing.expect(x.load(.SeqCst) & mask != 0); |
| 447 | |
| 448 | // setting it again shouldn't change the bit |
| 449 | try testing.expectEqual(x.bitSet(bit, ordering), 1); |
| 450 | try testing.expect(x.load(.SeqCst) & mask != 0); |
| 451 | |
| 452 | // all the previous bits should have not changed (still be set) |
| 453 | for (bit_array[0..bit_index]) |_, prev_bit_index| { |
| 454 | const prev_bit = @intCast(std.math.Log2Int(Int), prev_bit_index); |
| 455 | const prev_mask = @as(Int, 1) << prev_bit; |
| 456 | try testing.expect(x.load(.SeqCst) & prev_mask != 0); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | test "Atomic.bitReset" { |
| 464 | inline for (atomicIntTypes()) |Int| { |
| 465 | inline for (atomic_rmw_orderings) |ordering| { |
| 466 | var x = Atomic(Int).init(0); |
| 467 | const bit_array = @as([std.meta.bitCount(Int)]void, undefined); |
| 468 | |
| 469 | for (bit_array) |_, bit_index| { |
| 470 | const bit = @intCast(std.math.Log2Int(Int), bit_index); |
| 471 | const mask = @as(Int, 1) << bit; |
| 472 | x.storeUnchecked(x.loadUnchecked() | mask); |
| 473 | |
| 474 | // unsetting the bit should change the bit |
| 475 | try testing.expect(x.load(.SeqCst) & mask != 0); |
| 476 | try testing.expectEqual(x.bitReset(bit, ordering), 1); |
| 477 | try testing.expect(x.load(.SeqCst) & mask == 0); |
| 478 | |
| 479 | // unsetting it again shouldn't change the bit |
| 480 | try testing.expectEqual(x.bitReset(bit, ordering), 0); |
| 481 | try testing.expect(x.load(.SeqCst) & mask == 0); |
| 482 | |
| 483 | // all the previous bits should have not changed (still be reset) |
| 484 | for (bit_array[0..bit_index]) |_, prev_bit_index| { |
| 485 | const prev_bit = @intCast(std.math.Log2Int(Int), prev_bit_index); |
| 486 | const prev_mask = @as(Int, 1) << prev_bit; |
| 487 | try testing.expect(x.load(.SeqCst) & prev_mask == 0); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | test "Atomic.bitToggle" { |
| 495 | inline for (atomicIntTypes()) |Int| { |
| 496 | inline for (atomic_rmw_orderings) |ordering| { |
| 497 | var x = Atomic(Int).init(0); |
| 498 | const bit_array = @as([std.meta.bitCount(Int)]void, undefined); |
| 499 | |
| 500 | for (bit_array) |_, bit_index| { |
| 501 | const bit = @intCast(std.math.Log2Int(Int), bit_index); |
| 502 | const mask = @as(Int, 1) << bit; |
| 503 | |
| 504 | // toggling the bit should change the bit |
| 505 | try testing.expect(x.load(.SeqCst) & mask == 0); |
| 506 | try testing.expectEqual(x.bitToggle(bit, ordering), 0); |
| 507 | try testing.expect(x.load(.SeqCst) & mask != 0); |
| 508 | |
| 509 | // toggling it again *should* change the bit |
| 510 | try testing.expectEqual(x.bitToggle(bit, ordering), 1); |
| 511 | try testing.expect(x.load(.SeqCst) & mask == 0); |
| 512 | |
| 513 | // all the previous bits should have not changed (still be toggled back) |
| 514 | for (bit_array[0..bit_index]) |_, prev_bit_index| { |
| 515 | const prev_bit = @intCast(std.math.Log2Int(Int), prev_bit_index); |
| 516 | const prev_mask = @as(Int, 1) << prev_bit; |
| 517 | try testing.expect(x.load(.SeqCst) & prev_mask == 0); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | } |