2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("std.zig");
|
2018-07-16 17:52:50 -07:00
|
|
|
const assert = std.debug.assert;
|
2019-02-08 15:18:47 -08:00
|
|
|
const testing = std.testing;
|
2018-07-16 17:52:50 -07:00
|
|
|
|
|
|
|
/// Thread-safe initialization of global data.
|
|
|
|
/// TODO use a mutex instead of a spinlock
|
|
|
|
pub fn lazyInit(comptime T: type) LazyInit(T) {
|
2018-11-13 05:08:37 -08:00
|
|
|
return LazyInit(T){
|
2018-07-16 17:52:50 -07:00
|
|
|
.data = undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn LazyInit(comptime T: type) type {
|
2018-11-13 05:08:37 -08:00
|
|
|
return struct {
|
2019-11-12 07:40:21 -08:00
|
|
|
state: State = .NotResolved,
|
2018-07-16 17:52:50 -07:00
|
|
|
data: Data,
|
|
|
|
|
2019-11-12 07:40:21 -08:00
|
|
|
const State = enum(u8) {
|
|
|
|
NotResolved,
|
|
|
|
Resolving,
|
|
|
|
Resolved,
|
|
|
|
};
|
|
|
|
|
2018-09-13 13:34:33 -07:00
|
|
|
const Self = @This();
|
2018-07-16 17:52:50 -07:00
|
|
|
|
|
|
|
// TODO this isn't working for void, investigate and then remove this special case
|
|
|
|
const Data = if (@sizeOf(T) == 0) u8 else T;
|
|
|
|
const Ptr = if (T == void) void else *T;
|
|
|
|
|
|
|
|
/// Returns a usable pointer to the initialized data,
|
|
|
|
/// or returns null, indicating that the caller should
|
|
|
|
/// perform the initialization and then call resolve().
|
|
|
|
pub fn get(self: *Self) ?Ptr {
|
|
|
|
while (true) {
|
2019-11-12 07:40:21 -08:00
|
|
|
var state = @cmpxchgWeak(State, &self.state, .NotResolved, .Resolving, .SeqCst, .SeqCst) orelse return null;
|
2018-07-16 17:52:50 -07:00
|
|
|
switch (state) {
|
2019-11-12 07:40:21 -08:00
|
|
|
.NotResolved => continue,
|
|
|
|
.Resolving => {
|
2018-07-16 17:52:50 -07:00
|
|
|
// TODO mutex instead of a spinlock
|
|
|
|
continue;
|
|
|
|
},
|
2019-11-12 07:40:21 -08:00
|
|
|
.Resolved => {
|
2018-07-16 17:52:50 -07:00
|
|
|
if (@sizeOf(T) == 0) {
|
2019-11-07 15:52:09 -08:00
|
|
|
return @as(T, undefined);
|
2018-07-16 17:52:50 -07:00
|
|
|
} else {
|
|
|
|
return &self.data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve(self: *Self) void {
|
2019-11-12 07:40:21 -08:00
|
|
|
const prev = @atomicRmw(State, &self.state, .Xchg, .Resolved, .SeqCst);
|
|
|
|
assert(prev != .Resolved); // resolve() called twice
|
2018-07-16 17:52:50 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var global_number = lazyInit(i32);
|
|
|
|
|
|
|
|
test "std.lazyInit" {
|
|
|
|
if (global_number.get()) |_| @panic("bad") else {
|
|
|
|
global_number.data = 1234;
|
|
|
|
global_number.resolve();
|
|
|
|
}
|
|
|
|
if (global_number.get()) |x| {
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(x.* == 1234);
|
2018-07-16 17:52:50 -07:00
|
|
|
} else {
|
|
|
|
@panic("bad");
|
|
|
|
}
|
|
|
|
if (global_number.get()) |x| {
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(x.* == 1234);
|
2018-07-16 17:52:50 -07:00
|
|
|
} else {
|
|
|
|
@panic("bad");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var global_void = lazyInit(void);
|
|
|
|
|
|
|
|
test "std.lazyInit(void)" {
|
|
|
|
if (global_void.get()) |_| @panic("bad") else {
|
|
|
|
global_void.resolve();
|
|
|
|
}
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(global_void.get() != null);
|
|
|
|
testing.expect(global_void.get() != null);
|
2018-07-16 17:52:50 -07:00
|
|
|
}
|