mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 07:08:48 +00:00
[core:thread] Seeing if this fixes network tests
This commit is contained in:
+14
-9
@@ -10,8 +10,16 @@ Thread_Proc :: #type proc(^Thread)
|
|||||||
|
|
||||||
MAX_USER_ARGUMENTS :: 8
|
MAX_USER_ARGUMENTS :: 8
|
||||||
|
|
||||||
|
Thread_State :: enum u8 {
|
||||||
|
Started,
|
||||||
|
Joined,
|
||||||
|
Done,
|
||||||
|
Self_Cleanup,
|
||||||
|
}
|
||||||
|
|
||||||
Thread :: struct {
|
Thread :: struct {
|
||||||
using specific: Thread_Os_Specific,
|
using specific: Thread_Os_Specific,
|
||||||
|
flags: bit_set[Thread_State; u8],
|
||||||
id: int,
|
id: int,
|
||||||
procedure: Thread_Proc,
|
procedure: Thread_Proc,
|
||||||
|
|
||||||
@@ -47,9 +55,6 @@ Thread :: struct {
|
|||||||
*/
|
*/
|
||||||
init_context: Maybe(runtime.Context),
|
init_context: Maybe(runtime.Context),
|
||||||
|
|
||||||
// Indicates whether the thread will free itself when it completes
|
|
||||||
self_cleanup: bool,
|
|
||||||
|
|
||||||
creation_allocator: mem.Allocator,
|
creation_allocator: mem.Allocator,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +147,7 @@ create_and_start :: proc(fn: proc(), init_context: Maybe(runtime.Context) = nil,
|
|||||||
}
|
}
|
||||||
t := create(thread_proc, priority)
|
t := create(thread_proc, priority)
|
||||||
t.data = rawptr(fn)
|
t.data = rawptr(fn)
|
||||||
t.self_cleanup = self_cleanup
|
if self_cleanup do t.flags += {.Self_Cleanup}
|
||||||
t.init_context = init_context
|
t.init_context = init_context
|
||||||
start(t)
|
start(t)
|
||||||
return t
|
return t
|
||||||
@@ -162,7 +167,7 @@ create_and_start_with_data :: proc(data: rawptr, fn: proc(data: rawptr), init_co
|
|||||||
t.data = rawptr(fn)
|
t.data = rawptr(fn)
|
||||||
t.user_index = 1
|
t.user_index = 1
|
||||||
t.user_args = data
|
t.user_args = data
|
||||||
t.self_cleanup = self_cleanup
|
if self_cleanup do t.flags += {.Self_Cleanup}
|
||||||
t.init_context = init_context
|
t.init_context = init_context
|
||||||
start(t)
|
start(t)
|
||||||
return t
|
return t
|
||||||
@@ -181,7 +186,7 @@ create_and_start_with_poly_data :: proc(data: $T, fn: proc(data: T), init_contex
|
|||||||
t.user_index = 1
|
t.user_index = 1
|
||||||
data := data
|
data := data
|
||||||
mem.copy(&t.user_args[0], &data, size_of(data))
|
mem.copy(&t.user_args[0], &data, size_of(data))
|
||||||
t.self_cleanup = self_cleanup
|
if self_cleanup do t.flags += {.Self_Cleanup}
|
||||||
t.init_context = init_context
|
t.init_context = init_context
|
||||||
start(t)
|
start(t)
|
||||||
return t
|
return t
|
||||||
@@ -203,7 +208,7 @@ create_and_start_with_poly_data2 :: proc(arg1: $T1, arg2: $T2, fn: proc(T1, T2),
|
|||||||
arg1, arg2 := arg1, arg2
|
arg1, arg2 := arg1, arg2
|
||||||
mem.copy(&t.user_args[0], &arg1, size_of(arg1))
|
mem.copy(&t.user_args[0], &arg1, size_of(arg1))
|
||||||
mem.copy(&t.user_args[1], &arg2, size_of(arg2))
|
mem.copy(&t.user_args[1], &arg2, size_of(arg2))
|
||||||
t.self_cleanup = self_cleanup
|
if self_cleanup do t.flags += {.Self_Cleanup}
|
||||||
t.init_context = init_context
|
t.init_context = init_context
|
||||||
start(t)
|
start(t)
|
||||||
return t
|
return t
|
||||||
@@ -228,7 +233,7 @@ create_and_start_with_poly_data3 :: proc(arg1: $T1, arg2: $T2, arg3: $T3, fn: pr
|
|||||||
mem.copy(&t.user_args[0], &arg1, size_of(arg1))
|
mem.copy(&t.user_args[0], &arg1, size_of(arg1))
|
||||||
mem.copy(&t.user_args[1], &arg2, size_of(arg2))
|
mem.copy(&t.user_args[1], &arg2, size_of(arg2))
|
||||||
mem.copy(&t.user_args[2], &arg3, size_of(arg3))
|
mem.copy(&t.user_args[2], &arg3, size_of(arg3))
|
||||||
t.self_cleanup = self_cleanup
|
if self_cleanup do t.flags += {.Self_Cleanup}
|
||||||
t.init_context = init_context
|
t.init_context = init_context
|
||||||
start(t)
|
start(t)
|
||||||
return t
|
return t
|
||||||
@@ -254,7 +259,7 @@ create_and_start_with_poly_data4 :: proc(arg1: $T1, arg2: $T2, arg3: $T3, arg4:
|
|||||||
mem.copy(&t.user_args[1], &arg2, size_of(arg2))
|
mem.copy(&t.user_args[1], &arg2, size_of(arg2))
|
||||||
mem.copy(&t.user_args[2], &arg3, size_of(arg3))
|
mem.copy(&t.user_args[2], &arg3, size_of(arg3))
|
||||||
mem.copy(&t.user_args[3], &arg4, size_of(arg4))
|
mem.copy(&t.user_args[3], &arg4, size_of(arg4))
|
||||||
t.self_cleanup = self_cleanup
|
if self_cleanup do t.flags += {.Self_Cleanup}
|
||||||
t.init_context = init_context
|
t.init_context = init_context
|
||||||
start(t)
|
start(t)
|
||||||
return t
|
return t
|
||||||
|
|||||||
@@ -5,15 +5,7 @@ import "core:intrinsics"
|
|||||||
import "core:sync"
|
import "core:sync"
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
|
|
||||||
Thread_State :: enum u8 {
|
Thread_Os_Specific :: struct {}
|
||||||
Started,
|
|
||||||
Joined,
|
|
||||||
Done,
|
|
||||||
}
|
|
||||||
|
|
||||||
Thread_Os_Specific :: struct {
|
|
||||||
flags: bit_set[Thread_State; u8],
|
|
||||||
}
|
|
||||||
|
|
||||||
_thread_priority_map := [Thread_Priority]i32{
|
_thread_priority_map := [Thread_Priority]i32{
|
||||||
.Normal = 0,
|
.Normal = 0,
|
||||||
|
|||||||
@@ -8,19 +8,12 @@ import "core:sys/unix"
|
|||||||
|
|
||||||
CAS :: intrinsics.atomic_compare_exchange_strong
|
CAS :: intrinsics.atomic_compare_exchange_strong
|
||||||
|
|
||||||
Thread_State :: enum u8 {
|
|
||||||
Started,
|
|
||||||
Joined,
|
|
||||||
Done,
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t.
|
// NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t.
|
||||||
// Also see core/sys/darwin/mach_darwin.odin/semaphore_t.
|
// Also see core/sys/darwin/mach_darwin.odin/semaphore_t.
|
||||||
Thread_Os_Specific :: struct #align 16 {
|
Thread_Os_Specific :: struct #align 16 {
|
||||||
unix_thread: unix.pthread_t, // NOTE: very large on Darwin, small on Linux.
|
unix_thread: unix.pthread_t, // NOTE: very large on Darwin, small on Linux.
|
||||||
cond: sync.Cond,
|
cond: sync.Cond,
|
||||||
mutex: sync.Mutex,
|
mutex: sync.Mutex,
|
||||||
flags: bit_set[Thread_State; u8],
|
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
// Creates a thread which will run the given procedure.
|
// Creates a thread which will run the given procedure.
|
||||||
@@ -67,7 +60,7 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
|
|||||||
|
|
||||||
sync.unlock(&t.mutex)
|
sync.unlock(&t.mutex)
|
||||||
|
|
||||||
if t.self_cleanup {
|
if .Self_Cleanup in t.flags {
|
||||||
t.unix_thread = {}
|
t.unix_thread = {}
|
||||||
// NOTE(ftphikari): It doesn't matter which context 'free' received, right?
|
// NOTE(ftphikari): It doesn't matter which context 'free' received, right?
|
||||||
context = {}
|
context = {}
|
||||||
|
|||||||
@@ -6,17 +6,10 @@ import "core:intrinsics"
|
|||||||
import "core:sync"
|
import "core:sync"
|
||||||
import win32 "core:sys/windows"
|
import win32 "core:sys/windows"
|
||||||
|
|
||||||
Thread_State :: enum u8 {
|
|
||||||
Started,
|
|
||||||
Joined,
|
|
||||||
Done,
|
|
||||||
}
|
|
||||||
|
|
||||||
Thread_Os_Specific :: struct {
|
Thread_Os_Specific :: struct {
|
||||||
win32_thread: win32.HANDLE,
|
win32_thread: win32.HANDLE,
|
||||||
win32_thread_id: win32.DWORD,
|
win32_thread_id: win32.DWORD,
|
||||||
mutex: sync.Mutex,
|
mutex: sync.Mutex,
|
||||||
flags: bit_set[Thread_State; u8],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_thread_priority_map := [Thread_Priority]i32{
|
_thread_priority_map := [Thread_Priority]i32{
|
||||||
@@ -47,7 +40,7 @@ _create :: proc(procedure: Thread_Proc, priority: Thread_Priority) -> ^Thread {
|
|||||||
|
|
||||||
intrinsics.atomic_store(&t.flags, t.flags + {.Done})
|
intrinsics.atomic_store(&t.flags, t.flags + {.Done})
|
||||||
|
|
||||||
if t.self_cleanup {
|
if .Self_Cleanup in t.flags {
|
||||||
win32.CloseHandle(t.win32_thread)
|
win32.CloseHandle(t.win32_thread)
|
||||||
t.win32_thread = win32.INVALID_HANDLE
|
t.win32_thread = win32.INVALID_HANDLE
|
||||||
// NOTE(ftphikari): It doesn't matter which context 'free' received, right?
|
// NOTE(ftphikari): It doesn't matter which context 'free' received, right?
|
||||||
|
|||||||
Reference in New Issue
Block a user