2020-04-18 13:26:12 +02:00
|
|
|
const std = @import("std.zig");
|
2021-10-04 23:47:27 -07:00
|
|
|
const builtin = @import("builtin");
|
2020-04-18 13:26:12 +02:00
|
|
|
const testing = std.testing;
|
|
|
|
|
|
|
|
|
|
pub fn once(comptime f: fn () void) Once(f) {
|
|
|
|
|
return Once(f){};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// An object that executes the function `f` just once.
|
2024-04-15 13:57:41 +01:00
|
|
|
/// It is undefined behavior if `f` re-enters the same Once instance.
|
2020-04-18 13:26:12 +02:00
|
|
|
pub fn Once(comptime f: fn () void) type {
|
|
|
|
|
return struct {
|
|
|
|
|
done: bool = false,
|
2021-01-14 20:41:37 -07:00
|
|
|
mutex: std.Thread.Mutex = std.Thread.Mutex{},
|
2020-04-18 13:26:12 +02:00
|
|
|
|
|
|
|
|
/// Call the function `f`.
|
|
|
|
|
/// If `call` is invoked multiple times `f` will be executed only the
|
|
|
|
|
/// first time.
|
|
|
|
|
/// The invocations are thread-safe.
|
|
|
|
|
pub fn call(self: *@This()) void {
|
2024-02-18 21:52:23 -08:00
|
|
|
if (@atomicLoad(bool, &self.done, .acquire))
|
2020-04-18 13:26:12 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
return self.callSlow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn callSlow(self: *@This()) void {
|
2024-08-24 16:16:53 +01:00
|
|
|
@branchHint(.cold);
|
2020-04-18 13:26:12 +02:00
|
|
|
|
2021-11-09 18:27:12 -07:00
|
|
|
self.mutex.lock();
|
|
|
|
|
defer self.mutex.unlock();
|
2020-04-18 13:26:12 +02:00
|
|
|
|
|
|
|
|
// The first thread to acquire the mutex gets to run the initializer
|
|
|
|
|
if (!self.done) {
|
|
|
|
|
f();
|
2024-02-18 21:52:23 -08:00
|
|
|
@atomicStore(bool, &self.done, true, .release);
|
2020-04-18 13:26:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var global_number: i32 = 0;
|
|
|
|
|
var global_once = once(incr);
|
|
|
|
|
|
|
|
|
|
fn incr() void {
|
|
|
|
|
global_number += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test "Once executes its function just once" {
|
|
|
|
|
if (builtin.single_threaded) {
|
|
|
|
|
global_once.call();
|
|
|
|
|
global_once.call();
|
|
|
|
|
} else {
|
2021-06-19 21:31:43 -05:00
|
|
|
var threads: [10]std.Thread = undefined;
|
2024-04-15 13:57:41 +01:00
|
|
|
var thread_count: usize = 0;
|
|
|
|
|
defer for (threads[0..thread_count]) |handle| handle.join();
|
2020-04-18 13:26:12 +02:00
|
|
|
|
2023-02-18 09:02:57 -07:00
|
|
|
for (&threads) |*handle| {
|
2021-06-19 21:31:43 -05:00
|
|
|
handle.* = try std.Thread.spawn(.{}, struct {
|
2020-04-18 13:26:12 +02:00
|
|
|
fn thread_fn(x: u8) void {
|
2021-06-19 21:10:22 -04:00
|
|
|
_ = x;
|
2020-04-18 13:26:12 +02:00
|
|
|
global_once.call();
|
2024-04-15 13:57:41 +01:00
|
|
|
if (global_number != 1) @panic("memory ordering bug");
|
2020-04-18 13:26:12 +02:00
|
|
|
}
|
2021-06-19 21:31:43 -05:00
|
|
|
}.thread_fn, .{0});
|
2024-04-15 13:57:41 +01:00
|
|
|
thread_count += 1;
|
2020-04-18 13:26:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 20:47:26 +03:00
|
|
|
try testing.expectEqual(@as(i32, 1), global_number);
|
2020-04-18 13:26:12 +02:00
|
|
|
}
|