mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-28 18:30:06 +00:00
Fix sync.Channel code; add thread.run_with_poly_data and run_with_poly_data(2|3|4) procedures
This commit is contained in:
+93
-7
@@ -2,17 +2,26 @@ package thread
|
||||
|
||||
import "core:runtime"
|
||||
import "core:sync"
|
||||
import "core:intrinsics"
|
||||
import "core:mem"
|
||||
import "intrinsics"
|
||||
|
||||
_ :: intrinsics;
|
||||
|
||||
Thread_Proc :: #type proc(^Thread);
|
||||
|
||||
MAX_USER_ARGUMENTS :: 8;
|
||||
|
||||
Thread :: struct {
|
||||
using specific: Thread_Os_Specific,
|
||||
procedure: Thread_Proc,
|
||||
data: rawptr,
|
||||
user_index: int,
|
||||
using specific: Thread_Os_Specific,
|
||||
procedure: Thread_Proc,
|
||||
data: rawptr,
|
||||
user_index: int,
|
||||
user_args: [MAX_USER_ARGUMENTS]rawptr,
|
||||
|
||||
init_context: Maybe(runtime.Context),
|
||||
|
||||
|
||||
creation_allocator: mem.Allocator,
|
||||
}
|
||||
|
||||
#assert(size_of(Thread{}.user_index) == size_of(uintptr));
|
||||
@@ -34,17 +43,94 @@ run :: proc(fn: proc(), init_context: Maybe(runtime.Context) = nil, priority :=
|
||||
run_with_data :: proc(data: rawptr, fn: proc(data: rawptr), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal) {
|
||||
thread_proc :: proc(t: ^Thread) {
|
||||
fn := cast(proc(rawptr))t.data;
|
||||
data := rawptr(uintptr(t.user_index));
|
||||
assert(t.user_index >= 1);
|
||||
data := t.user_args[0];
|
||||
fn(data);
|
||||
destroy(t);
|
||||
}
|
||||
t := create(thread_proc, priority);
|
||||
t.data = rawptr(fn);
|
||||
t.user_index = int(uintptr(data));
|
||||
t.user_index = 1;
|
||||
t.user_args = data;
|
||||
t.init_context = init_context;
|
||||
start(t);
|
||||
}
|
||||
|
||||
run_with_poly_data :: proc(data: $T, fn: proc(data: T), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal)
|
||||
where intrinsics.type_is_pointer(T) || size_of(T) == size_of(rawptr) {
|
||||
thread_proc :: proc(t: ^Thread) {
|
||||
fn := cast(proc(rawptr))t.data;
|
||||
assert(t.user_index >= 1);
|
||||
data := t.user_args[0];
|
||||
fn(data);
|
||||
destroy(t);
|
||||
}
|
||||
t := create(thread_proc, priority);
|
||||
t.data = rawptr(fn);
|
||||
t.user_index = 1;
|
||||
t.user_args[0] = transmute(rawptr)data;
|
||||
t.init_context = init_context;
|
||||
start(t);
|
||||
}
|
||||
|
||||
run_with_poly_data2 :: proc(arg1: $T1, arg2: $T2, fn: proc(T1, T2), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal)
|
||||
where intrinsics.type_is_pointer(T1) || size_of(T1) == size_of(rawptr),
|
||||
intrinsics.type_is_pointer(T2) || size_of(T2) == size_of(rawptr) {
|
||||
thread_proc :: proc(t: ^Thread) {
|
||||
fn := cast(proc(rawptr, rawptr))t.data;
|
||||
assert(t.user_index >= 2);
|
||||
fn(t.user_args[0], t.user_args[1]);
|
||||
destroy(t);
|
||||
}
|
||||
t := create(thread_proc, priority);
|
||||
t.data = rawptr(fn);
|
||||
t.user_index = 2;
|
||||
t.user_args[0] = transmute(rawptr)arg1;
|
||||
t.user_args[1] = transmute(rawptr)arg2;
|
||||
t.init_context = init_context;
|
||||
start(t);
|
||||
}
|
||||
|
||||
run_with_poly_data3 :: proc(arg1: $T1, arg2: $T2, arg3: $T3, fn: proc(arg1: T1, arg2: T2, arg3: T3), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal)
|
||||
where intrinsics.type_is_pointer(T1) || size_of(T1) == size_of(rawptr),
|
||||
intrinsics.type_is_pointer(T2) || size_of(T2) == size_of(rawptr),
|
||||
intrinsics.type_is_pointer(T3) || size_of(T3) == size_of(rawptr) {
|
||||
thread_proc :: proc(t: ^Thread) {
|
||||
fn := cast(proc(rawptr, rawptr, rawptr))t.data;
|
||||
assert(t.user_index >= 3);
|
||||
fn(t.user_args[0], t.user_args[1], t.user_args[2]);
|
||||
destroy(t);
|
||||
}
|
||||
t := create(thread_proc, priority);
|
||||
t.data = rawptr(fn);
|
||||
t.user_index = 3;
|
||||
t.user_args[0] = transmute(rawptr)arg1;
|
||||
t.user_args[1] = transmute(rawptr)arg2;
|
||||
t.user_args[2] = transmute(rawptr)arg3;
|
||||
t.init_context = init_context;
|
||||
start(t);
|
||||
}
|
||||
run_with_poly_data4 :: proc(arg1: $T1, arg2: $T2, arg3: $T3, arg4: $T4, fn: proc(arg1: T1, arg2: T2, arg3: T3, arg4: T4), init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal)
|
||||
where intrinsics.type_is_pointer(T1) || size_of(T1) == size_of(rawptr),
|
||||
intrinsics.type_is_pointer(T2) || size_of(T2) == size_of(rawptr),
|
||||
intrinsics.type_is_pointer(T3) || size_of(T3) == size_of(rawptr) {
|
||||
thread_proc :: proc(t: ^Thread) {
|
||||
fn := cast(proc(rawptr, rawptr, rawptr))t.data;
|
||||
assert(t.user_index >= 3);
|
||||
fn(t.user_args[0], t.user_args[1], t.user_args[2]);
|
||||
destroy(t);
|
||||
}
|
||||
t := create(thread_proc, priority);
|
||||
t.data = rawptr(fn);
|
||||
t.user_index = 3;
|
||||
t.user_args[0] = transmute(rawptr)arg1;
|
||||
t.user_args[1] = transmute(rawptr)arg2;
|
||||
t.user_args[2] = transmute(rawptr)arg3;
|
||||
t.init_context = init_context;
|
||||
start(t);
|
||||
}
|
||||
|
||||
|
||||
|
||||
create_and_start :: proc(fn: Thread_Proc, init_context: Maybe(runtime.Context) = nil, priority := Thread_Priority.Normal) -> ^Thread {
|
||||
t := create(fn, priority);
|
||||
|
||||
@@ -85,6 +85,7 @@ create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^T
|
||||
if thread == nil {
|
||||
return nil;
|
||||
}
|
||||
thread.creation_allocator = context.allocator;
|
||||
|
||||
// Set thread priority.
|
||||
policy: i32;
|
||||
@@ -106,7 +107,7 @@ create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^T
|
||||
sync.mutex_init(&thread.start_mutex);
|
||||
sync.condition_init(&thread.start_gate, &thread.start_mutex);
|
||||
if unix.pthread_create(&thread.unix_thread, &attrs, __linux_thread_entry_proc, thread) != 0 {
|
||||
free(thread);
|
||||
free(thread, thread.creation_allocator);
|
||||
return nil;
|
||||
}
|
||||
thread.procedure = procedure;
|
||||
@@ -172,7 +173,7 @@ join_multiple :: proc(threads: ..^Thread) {
|
||||
destroy :: proc(t: ^Thread) {
|
||||
join(t);
|
||||
t.unix_thread = {};
|
||||
free(t);
|
||||
free(t, t.creation_allocator);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -49,10 +49,14 @@ create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^T
|
||||
|
||||
|
||||
thread := new(Thread);
|
||||
if thread == nil {
|
||||
return nil;
|
||||
}
|
||||
thread.creation_allocator = context.allocator;
|
||||
|
||||
win32_thread := win32.CreateThread(nil, 0, __windows_thread_entry_proc, thread, win32.CREATE_SUSPENDED, &win32_thread_id);
|
||||
if win32_thread == nil {
|
||||
free(thread);
|
||||
free(thread, thread.creation_allocator);
|
||||
return nil;
|
||||
}
|
||||
thread.procedure = procedure;
|
||||
@@ -111,7 +115,7 @@ join_multiple :: proc(threads: ..^Thread) {
|
||||
|
||||
destroy :: proc(thread: ^Thread) {
|
||||
join(thread);
|
||||
free(thread);
|
||||
free(thread, thread.creation_allocator);
|
||||
}
|
||||
|
||||
terminate :: proc(using thread : ^Thread, exit_code: u32) {
|
||||
|
||||
Reference in New Issue
Block a user