From: Brendan Hansen Date: Mon, 18 Oct 2021 00:29:45 +0000 (-0500) Subject: cleaned up initialization process X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=d6b85a6238431f4536cb57f5f2ff6e267053ab2f;p=onyx.git cleaned up initialization process --- diff --git a/bin/onyx b/bin/onyx index c80c6ba0..4e597087 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/builtin.onyx b/core/builtin.onyx index 62072103..f20a4805 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -70,14 +70,7 @@ OnyxContext :: struct { } } -// @Robustness -// Currently, because the only compilation target is WebAssembly, which is only -// single threaded for the moment, it is safe to store the context in a global -// variable. If other compilations targets are added, or if WebAssembly standardizes -// a multi-threading proposal, this context will need to be changed. Either it will -// need to be a thread-local variable, or it needs to be implicitly passed as the -// first parameter to ALL procedures in a compilation unit. -context : OnyxContext; +#thread_local context : OnyxContext; assert :: (cond: bool, msg: str, site := #callsite) { if !cond do context.assert_handler(msg, site); diff --git a/core/runtime/common.onyx b/core/runtime/common.onyx index d63f573a..6d6f5e4d 100644 --- a/core/runtime/common.onyx +++ b/core/runtime/common.onyx @@ -19,18 +19,23 @@ __assert_handler :: (msg: str, site: CallSite) { __exit(1); } -// Use this procedure to initialize everything needed in the -// standard library when you are dropped directly into a function. __runtime_initialize :: () { alloc.init(); + __thread_initialize(); + + #if Multi_Threading_Enabled do thread.__initialize(); +} + +// Use this procedure to initialize everything needed in the +// standard library when you are dropped directly into a function. +__thread_initialize :: () { + __tls_base = raw_alloc(alloc.heap_allocator, __tls_size); __initialize(^context); context.allocator = alloc.heap_allocator; context.temp_allocator = alloc.temp_allocator; context.assert_handler = __assert_handler; - #if Multi_Threading_Enabled { - thread.__initialize(); - } + __stdio_init(); } diff --git a/core/runtime/js.onyx b/core/runtime/js.onyx index bfddb133..18f3e18a 100644 --- a/core/runtime/js.onyx +++ b/core/runtime/js.onyx @@ -12,9 +12,6 @@ __exit :: (status: i32) -> void #foreign "host" "exit" --- #export "_start" () { __runtime_initialize(); - __tls_base = raw_alloc(context.allocator, __tls_size); - __stdio_init(); - args: [] cstr = .{ null, 0 }; (package main).main(args); @@ -26,9 +23,7 @@ __exit :: (status: i32) -> void #foreign "host" "exit" --- #export "_thread_start" (func: (data: rawptr) -> void, data: rawptr) { __stack_top = raw_alloc(context.allocator, 1 << 20); - - __tls_base = raw_alloc(context.allocator, __tls_size); - __stdio_init(); + __thread_initialize(); func(data); diff --git a/core/runtime/wasi.onyx b/core/runtime/wasi.onyx index 9f8b4420..be8f7fad 100644 --- a/core/runtime/wasi.onyx +++ b/core/runtime/wasi.onyx @@ -22,9 +22,6 @@ __exit :: (status: i32) do proc_exit(status); // Sets up everything needed for execution. #export "_start" () { __runtime_initialize(); - - __tls_base = raw_alloc(context.allocator, __tls_size); - __stdio_init(); args : [] cstr; argv_buf_size : Size; diff --git a/core/stdio.onyx b/core/stdio.onyx index 72fe20dd..4fb13f10 100644 --- a/core/stdio.onyx +++ b/core/stdio.onyx @@ -100,7 +100,7 @@ byte_dump :: (ptr: rawptr, byte_count: u32, bytes_per_line := 8) { // Private and internal things // -#threadlocal stdio : struct { +#thread_local stdio : struct { print_stream : io.DynamicStringStream; print_writer : io.Writer; } diff --git a/src/parser.c b/src/parser.c index 643c7b5b..3e2103e6 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1479,7 +1479,7 @@ static AstNode* parse_statement(OnyxParser* parser) { } if (parse_possible_directive(parser, "persist")) { - b32 thread_local = parse_possible_directive(parser, "threadlocal"); + b32 thread_local = parse_possible_directive(parser, "thread_local"); OnyxToken* symbol = expect_token(parser, Token_Type_Symbol); AstMemRes* memres = parse_memory_reservation(parser, symbol, thread_local); @@ -2897,7 +2897,7 @@ static void parse_top_level_statement(OnyxParser* parser) { ENTITY_SUBMIT(tag); return; } - else if (parse_possible_directive(parser, "threadlocal")) { + else if (parse_possible_directive(parser, "thread_local")) { OnyxToken* symbol = expect_token(parser, Token_Type_Symbol); AstMemRes* memres = parse_memory_reservation(parser, symbol, 1);