added a couple test cases
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 28 Nov 2021 23:14:48 +0000 (17:14 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 28 Nov 2021 23:14:48 +0000 (17:14 -0600)
src/onyx.c
tests/atomics [new file with mode: 0644]
tests/atomics.onyx [new file with mode: 0644]
tests/poly_struct_in_type_info [new file with mode: 0644]
tests/poly_struct_in_type_info.onyx [new file with mode: 0644]

index 2ded60d7737e4abd46c8fac44abfd0d7655273c4..f0f44786c2bca4c36b1fda95382c7a94924274b3 100644 (file)
@@ -176,6 +176,11 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg
         }
     }
 
+    // NOTE: Always enable multi-threading for the Onyx runtime.
+    if (options.runtime == Runtime_Onyx) {
+        options.use_multi_threading = 1;
+    }
+
     return options;
 }
 
diff --git a/tests/atomics b/tests/atomics
new file mode 100644 (file)
index 0000000..a2ba3c6
--- /dev/null
@@ -0,0 +1,13 @@
+1234
+Spawned thread 1
+Spawned thread 2
+Spawned thread 3
+Spawned thread 4
+Waiting...
+Thread 1 joined!
+Thread 2 joined!
+Thread 3 joined!
+Thread 4 joined!
+[ 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000, 40000 ]
+On the main thread: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
+1234
diff --git a/tests/atomics.onyx b/tests/atomics.onyx
new file mode 100644 (file)
index 0000000..892913c
--- /dev/null
@@ -0,0 +1,67 @@
+#load "core/std"
+#load "core/intrinsics/atomics"
+
+use package core
+use package core.intrinsics.atomics
+
+Shared_Data :: struct {
+    arr: [] i32;
+}
+
+// This is a shared global
+shared_mutex : sync.Mutex;
+
+// This is a thread-local global
+#thread_local partial_arr : [] i32
+
+// This is shared, and exists to test that initializing new threads
+// does not re-execute the intialization segments.
+test := 5678;
+
+print_from_other_thread :: (sd) => {
+    // Creating high contention for the shared resource
+    for i: 10000 {
+        sync.scoped_mutex(^shared_mutex);
+        for ^v: sd.arr {
+            *v += 1;
+        }
+    }
+
+    // Using the thread-local variable
+    memory.alloc_slice(^partial_arr, 5);
+    for i: 5 do partial_arr[i] = i * i;
+    
+    // Not printing on threads since the test case looks for EXACT string matches
+    // printf("On a worker thread: {}\n", partial_arr);
+}
+
+main :: (args) => {
+    sync.mutex_init(^shared_mutex);
+
+    test = 1234;
+    println(test);
+
+    sd: Shared_Data;
+    sd.arr = memory.make_slice(i32, 1000);
+    memory.fill_slice(sd.arr, 0);
+
+    threads : [4] thread.Thread;
+    for ^t: threads {
+        thread.spawn(t, ^sd, print_from_other_thread);
+        printf("Spawned thread {}\n", t.id);
+    }
+
+    memory.alloc_slice(^partial_arr, 10);
+    for i: 10 do partial_arr[i] = i;
+
+    printf("Waiting...\n");
+    for ^t: threads {
+        thread.join(t);
+        printf("Thread {} joined!\n", t.id);
+    }
+    printf("{}\n", sd.arr);
+    printf("On the main thread: {}\n", partial_arr);
+    println(test);
+}
+
+
diff --git a/tests/poly_struct_in_type_info b/tests/poly_struct_in_type_info
new file mode 100644 (file)
index 0000000..297a821
--- /dev/null
@@ -0,0 +1,4 @@
+Base
+Base([] u8) { const = 10, dynamic = "Hello" }
+true
+false
diff --git a/tests/poly_struct_in_type_info.onyx b/tests/poly_struct_in_type_info.onyx
new file mode 100644 (file)
index 0000000..eb32103
--- /dev/null
@@ -0,0 +1,18 @@
+#load "core/std"
+use package core
+
+Base :: struct (T: type_expr) {
+    const: i32;
+    dynamic: T;
+}
+
+main :: (args) => {
+    printf("{}\n", Base);
+
+    x := Base(str).{ 10, "Hello" };
+    printf("{}\n", x);
+    printf("{}\n", type_info.struct_constructed_from(typeof x, Base));
+
+    y: Allocator;
+    printf("{}\n", type_info.struct_constructed_from(typeof y, Base));
+}
\ No newline at end of file