From af38c420db770db4d809ca1a21e11c47b663fe56 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sun, 28 Nov 2021 17:14:48 -0600 Subject: [PATCH] added a couple test cases --- src/onyx.c | 5 +++ tests/atomics | 13 ++++++ tests/atomics.onyx | 67 +++++++++++++++++++++++++++++ tests/poly_struct_in_type_info | 4 ++ tests/poly_struct_in_type_info.onyx | 18 ++++++++ 5 files changed, 107 insertions(+) create mode 100644 tests/atomics create mode 100644 tests/atomics.onyx create mode 100644 tests/poly_struct_in_type_info create mode 100644 tests/poly_struct_in_type_info.onyx diff --git a/src/onyx.c b/src/onyx.c index 2ded60d7..f0f44786 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -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 index 00000000..a2ba3c65 --- /dev/null +++ b/tests/atomics @@ -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 index 00000000..892913ca --- /dev/null +++ b/tests/atomics.onyx @@ -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 index 00000000..297a8210 --- /dev/null +++ b/tests/poly_struct_in_type_info @@ -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 index 00000000..eb321036 --- /dev/null +++ b/tests/poly_struct_in_type_info.onyx @@ -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 -- 2.25.1