authorgravatar for isnt@haze.coolHaze Booth <isnt@haze.cool> 2020-04-21 14:26:41-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-21 18:04:37-04:00
log78e2a203e3d80222e7049ddb8ec6ceec7d39a6c9
tree67fd6892337f25bb117065bc9f508c01a0a2476e
parentaca6b7018443bfc012f39be486f848d6e62c532c

Remove std.lazyInit


3 files changed, 2 insertions(+), 94 deletions(-)

lib/std/lazy_init.zig deleted-88
...@@ -1,88 +0,0 @@
1const std = @import("std.zig");
2const assert = std.debug.assert;
3const testing = std.testing;
4
5/// Thread-safe initialization of global data.
6/// TODO use a mutex instead of a spinlock
7pub fn lazyInit(comptime T: type) LazyInit(T) {
8 return LazyInit(T){
9 .data = undefined,
10 };
11}
12
13fn LazyInit(comptime T: type) type {
14 return struct {
15 state: State = .NotResolved,
16 data: Data,
17
18 const State = enum(u8) {
19 NotResolved,
20 Resolving,
21 Resolved,
22 };
23
24 const Self = @This();
25
26 // TODO this isn't working for void, investigate and then remove this special case
27 const Data = if (@sizeOf(T) == 0) u8 else T;
28 const Ptr = if (T == void) void else *T;
29
30 /// Returns a usable pointer to the initialized data,
31 /// or returns null, indicating that the caller should
32 /// perform the initialization and then call resolve().
33 pub fn get(self: *Self) ?Ptr {
34 while (true) {
35 var state = @cmpxchgWeak(State, &self.state, .NotResolved, .Resolving, .SeqCst, .SeqCst) orelse return null;
36 switch (state) {
37 .NotResolved => continue,
38 .Resolving => {
39 // TODO mutex instead of a spinlock
40 continue;
41 },
42 .Resolved => {
43 if (@sizeOf(T) == 0) {
44 return @as(T, undefined);
45 } else {
46 return &self.data;
47 }
48 },
49 else => unreachable,
50 }
51 }
52 }
53
54 pub fn resolve(self: *Self) void {
55 const prev = @atomicRmw(State, &self.state, .Xchg, .Resolved, .SeqCst);
56 assert(prev != .Resolved); // resolve() called twice
57 }
58 };
59}
60
61var global_number = lazyInit(i32);
62
63test "std.lazyInit" {
64 if (global_number.get()) |_| @panic("bad") else {
65 global_number.data = 1234;
66 global_number.resolve();
67 }
68 if (global_number.get()) |x| {
69 testing.expect(x.* == 1234);
70 } else {
71 @panic("bad");
72 }
73 if (global_number.get()) |x| {
74 testing.expect(x.* == 1234);
75 } else {
76 @panic("bad");
77 }
78}
79
80var global_void = lazyInit(void);
81
82test "std.lazyInit(void)" {
83 if (global_void.get()) |_| @panic("bad") else {
84 global_void.resolve();
85 }
86 testing.expect(global_void.get() != null);
87 testing.expect(global_void.get() != null);
88}
lib/std/std.zig-1
...@@ -45,7 +45,6 @@ pub const heap = @import("heap.zig");...@@ -45,7 +45,6 @@ pub const heap = @import("heap.zig");
45pub const http = @import("http.zig");45pub const http = @import("http.zig");
46pub const io = @import("io.zig");46pub const io = @import("io.zig");
47pub const json = @import("json.zig");47pub const json = @import("json.zig");
48pub const lazyInit = @import("lazy_init.zig").lazyInit;
49pub const macho = @import("macho.zig");48pub const macho = @import("macho.zig");
50pub const math = @import("math.zig");49pub const math = @import("math.zig");
51pub const mem = @import("mem.zig");50pub const mem = @import("mem.zig");
src-self-hosted/compilation.zig+2-5
...@@ -45,13 +45,10 @@ pub const ZigCompiler = struct {...@@ -45,13 +45,10 @@ pub const ZigCompiler = struct {
4545
46 native_libc: event.Future(LibCInstallation),46 native_libc: event.Future(LibCInstallation),
4747
48 var lazy_init_targets = std.lazyInit(void);48 var lazy_init_targets = std.once(util.initializeAllTargets);
4949
50 pub fn init(allocator: *Allocator) !ZigCompiler {50 pub fn init(allocator: *Allocator) !ZigCompiler {
51 lazy_init_targets.get() orelse {51 lazy_init_targets.call();
52 util.initializeAllTargets();
53 lazy_init_targets.resolve();
54 };
5552
56 var seed_bytes: [@sizeOf(u64)]u8 = undefined;53 var seed_bytes: [@sizeOf(u64)]u8 = undefined;
57 try std.crypto.randomBytes(seed_bytes[0..]);54 try std.crypto.randomBytes(seed_bytes[0..]);