cleaned up initialization process
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 18 Oct 2021 00:29:45 +0000 (19:29 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 18 Oct 2021 00:29:45 +0000 (19:29 -0500)
bin/onyx
core/builtin.onyx
core/runtime/common.onyx
core/runtime/js.onyx
core/runtime/wasi.onyx
core/stdio.onyx
src/parser.c

index c80c6ba0896a0365a656f0d060b485d9c2c8af30..4e59708766129272c5fb780f1c061a316a4eef0e 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 620721034f66d16795a4d18490dd507de3a6f07f..f20a4805dbf834f351a279f9d46ed915605dd719 100644 (file)
@@ -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);
index d63f573ac9c3b26a3d4459d6d15c10264fef61fe..6d6f5e4dd1ee9ca8d2d14ff3bcea6faf3a68b863 100644 (file)
@@ -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();
 }
 
index bfddb133b39c4b4881ee2afcb4af712250134461..18f3e18ad9bab964c77fc86edaa7fac932edc6ec 100644 (file)
@@ -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);
 
index 9f8b44202c2e2023afa142ea30097c5c7cc1a03f..be8f7fad57fedd562b421a48ea3935a4a0d3f516 100644 (file)
@@ -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;
index 72fe20dd5fab9f0a130dcca0787a8bc58d3fb9ba..4fb13f105aa3b758dbbd91c0bf0cf4cea52b14ea 100644 (file)
@@ -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;
 }
index 643c7b5b7105d5d0873a74870c3406a5de6d6693..3e2103e6a12142a5f53605217904c68dc416fbd3 100644 (file)
@@ -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);