From: Brendan Hansen Date: Tue, 29 Nov 2022 12:01:29 +0000 (-0600) Subject: added sync.Once; code cleanup X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=6f9be0d563db59e662182733f444ec030430092c;p=onyx.git added sync.Once; code cleanup --- diff --git a/compiler/src/wasm_emit.c b/compiler/src/wasm_emit.c index b8a046bc..ecced22e 100644 --- a/compiler/src/wasm_emit.c +++ b/compiler/src/wasm_emit.c @@ -4220,6 +4220,7 @@ static b32 emit_constexpr_(ConstExprContext *ctx, AstTyped *node, u32 offset) { patch.index = ctx->data_id; patch.location = offset; patch.offset = 0; + patch.data_id = 0; // Here, we cannot use the data_id property of the // memory reservation because there is no guarantee that diff --git a/core/io/reader.onyx b/core/io/reader.onyx index f695029b..bcd4663e 100644 --- a/core/io/reader.onyx +++ b/core/io/reader.onyx @@ -1,6 +1,6 @@ package core.io -use core {memory, math, array} +use core {memory, math, array, iter} Reader :: struct { stream : ^Stream; @@ -552,25 +552,16 @@ skip_bytes :: (use reader: ^Reader, bytes: u32) -> (skipped: i32, err: Error) { return 0, .None; } -lines :: (r: ^Reader, inplace := false) -> Iterator(str) { - Context :: struct { - r: ^Reader; - inplace: bool; - } - - c := new(Context, allocator=context.temp_allocator); - c.r = r; - c.inplace = inplace; +lines :: (r: ^Reader, inplace := false) => + iter.generator(^.{ + r = r, inplace = inplace - next :: (use c: ^Context) -> (str, bool) { - line := r->read_line(consume_newline=true, inplace=inplace); + }, (ctx: $C) -> (str, bool) { + line := ctx.r->read_line(consume_newline=true, inplace=ctx.inplace); if line.count > 0 do return line, true; else do return "", false; - } - - return .{ c, next }; -} + }); #local reader_consume_error :: (use reader: ^Reader) -> Error { diff --git a/core/std.onyx b/core/std.onyx index 7d79a1ab..1537974f 100644 --- a/core/std.onyx +++ b/core/std.onyx @@ -92,6 +92,7 @@ package core #load "./sync/condition_variable" #load "./sync/semaphore" #load "./sync/barrier" + #load "./sync/once" #load "./threads/thread" } diff --git a/core/string/string.onyx b/core/string/string.onyx index 668a020f..ef901b8e 100644 --- a/core/string/string.onyx +++ b/core/string/string.onyx @@ -2,8 +2,20 @@ package core.string use core +#doc "Generic procedure for turning something into a string." as_str :: #match {} +#local HasAsStrMethod :: interface (t: $T) { + { T.as_str(t) } -> str; +} + +#overload #precedence 10000 +as_str :: macro (t: $T/HasAsStrMethod) -> str { + return T.as_str(t); +} + + + free :: (s: str, allocator := context.allocator) do raw_free(allocator, s.data); alloc_copy :: (original: str, allocator := context.allocator) -> str { diff --git a/core/sync/once.onyx b/core/sync/once.onyx new file mode 100644 index 00000000..9c87fd09 --- /dev/null +++ b/core/sync/once.onyx @@ -0,0 +1,28 @@ +package core.sync + +Once :: struct { + done: bool; + mutex: Mutex; +} + +#inject Once.exec :: #match #local {} + +#overload +Once.exec :: (o: ^Once, f: () -> $R) { + if o.done do return; + + mutex_lock(^o.mutex); + o.done = true; + f(); + mutex_unlock(^o.mutex); +} + +#overload +Once.exec :: (o: ^Once, ctx: $Ctx, f: (Ctx) -> $R) { + if o.done do return; + + mutex_lock(^o.mutex); + o.done = true; + f(ctx); + mutex_unlock(^o.mutex); +}