From 8ec2ca9dcd138f6f5e49cd3f6c6bb575a7e434e7 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 8 Jun 2021 15:57:00 +0100 Subject: [PATCH] Remove `context.thread_id` --- core/runtime/core.odin | 3 --- core/runtime/os_specific.odin | 4 ---- core/runtime/os_specific_any.odin | 4 ---- core/runtime/os_specific_freestanding.odin | 4 ---- core/runtime/os_specific_windows.odin | 8 -------- core/sync/sync.odin | 6 +++--- core/sync/sync2/extended.odin | 6 +++--- core/sync/sync2/primitives.odin | 4 ++++ core/sync/sync2/primitives_atomic.odin | 6 +++--- core/sync/sync2/primitives_windows.odin | 4 ++++ core/sync/sync_darwin.odin | 13 +++++++++++++ core/sync/sync_freebsd.odin | 12 ++++++++++++ core/sync/sync_linux.odin | 12 ++++++++++++ core/sync/sync_windows.odin | 4 ++++ 14 files changed, 58 insertions(+), 32 deletions(-) diff --git a/core/runtime/core.odin b/core/runtime/core.odin index 9c454cea9..8e71e1fa0 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -329,8 +329,6 @@ Context :: struct { assertion_failure_proc: Assertion_Failure_Proc, logger: Logger, - thread_id: int, - user_data: any, user_ptr: rawptr, user_index: int, @@ -479,7 +477,6 @@ __init_context :: proc "contextless" (c: ^Context) { c.temp_allocator.procedure = default_temp_allocator_proc; c.temp_allocator.data = &global_default_temp_allocator_data; - c.thread_id = current_thread_id(); // NOTE(bill): This is "contextless" so it is okay to call c.assertion_failure_proc = default_assertion_failure_proc; c.logger.procedure = default_logger_proc; diff --git a/core/runtime/os_specific.odin b/core/runtime/os_specific.odin index 03acf4279..0f0e77dad 100644 --- a/core/runtime/os_specific.odin +++ b/core/runtime/os_specific.odin @@ -5,7 +5,3 @@ _OS_Errno :: distinct int; os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) { return _os_write(data); } - -current_thread_id :: proc "contextless" () -> int { - return _current_thread_id(); -} diff --git a/core/runtime/os_specific_any.odin b/core/runtime/os_specific_any.odin index 9c2686661..7d6f05d87 100644 --- a/core/runtime/os_specific_any.odin +++ b/core/runtime/os_specific_any.odin @@ -11,7 +11,3 @@ _os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) { n, err := os.write(os.stderr, data); return int(n), _OS_Errno(err); } - -_current_thread_id :: proc "contextless" () -> int { - return os.current_thread_id(); -} diff --git a/core/runtime/os_specific_freestanding.odin b/core/runtime/os_specific_freestanding.odin index cae41daae..15323f763 100644 --- a/core/runtime/os_specific_freestanding.odin +++ b/core/runtime/os_specific_freestanding.odin @@ -5,7 +5,3 @@ package runtime _os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) { return 0, -1; } - -_current_thread_id :: proc "contextless" () -> int { - return 0; -} diff --git a/core/runtime/os_specific_windows.odin b/core/runtime/os_specific_windows.odin index 3fa79182f..936ea9dbe 100644 --- a/core/runtime/os_specific_windows.odin +++ b/core/runtime/os_specific_windows.odin @@ -17,9 +17,6 @@ foreign kernel32 { WriteFile :: proc(hFile: rawptr, lpBuffer: rawptr, nNumberOfBytesToWrite: u32, lpNumberOfBytesWritten: ^u32, lpOverlapped: rawptr) -> b32 --- GetLastError :: proc() -> u32 --- - // current_thread_id - GetCurrentThreadId :: proc() -> u32 --- - // default_allocator GetProcessHeap :: proc() -> rawptr --- HeapAlloc :: proc(hHeap: rawptr, dwFlags: u32, dwBytes: uint) -> rawptr --- @@ -61,11 +58,6 @@ _os_write :: proc "contextless" (data: []byte) -> (n: int, err: _OS_Errno) { return; } -_current_thread_id :: proc "contextless" () -> int { - return int(GetCurrentThreadId()); -} - - heap_alloc :: proc "contextless" (size: int) -> rawptr { HEAP_ZERO_MEMORY :: 0x00000008; return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, uint(size)); diff --git a/core/sync/sync.odin b/core/sync/sync.odin index 06ec043bc..60d226d20 100644 --- a/core/sync/sync.odin +++ b/core/sync/sync.odin @@ -80,7 +80,7 @@ recursive_benaphore_destroy :: proc(b: ^Recursive_Benaphore) { } recursive_benaphore_lock :: proc(b: ^Recursive_Benaphore) { - tid := runtime.current_thread_id(); + tid := current_thread_id(); if intrinsics.atomic_add_acq(&b.counter, 1) > 1 { if tid != b.owner { semaphore_wait_for(&b.sema); @@ -92,7 +92,7 @@ recursive_benaphore_lock :: proc(b: ^Recursive_Benaphore) { } recursive_benaphore_try_lock :: proc(b: ^Recursive_Benaphore) -> bool { - tid := runtime.current_thread_id(); + tid := current_thread_id(); if b.owner == tid { intrinsics.atomic_add_acq(&b.counter, 1); } else { @@ -108,7 +108,7 @@ recursive_benaphore_try_lock :: proc(b: ^Recursive_Benaphore) -> bool { } recursive_benaphore_unlock :: proc(b: ^Recursive_Benaphore) { - tid := runtime.current_thread_id(); + tid := current_thread_id(); assert(tid == b.owner); b.recursion -= 1; recursion := b.recursion; diff --git a/core/sync/sync2/extended.odin b/core/sync/sync2/extended.odin index 06051c822..9ee796057 100644 --- a/core/sync/sync2/extended.odin +++ b/core/sync/sync2/extended.odin @@ -201,7 +201,7 @@ Recursive_Benaphore :: struct { } recursive_benaphore_lock :: proc(b: ^Recursive_Benaphore) { - tid := runtime.current_thread_id(); + tid := current_thread_id(); if atomic_add_acquire(&b.counter, 1) > 1 { if tid != b.owner { sema_wait(&b.sema); @@ -213,7 +213,7 @@ recursive_benaphore_lock :: proc(b: ^Recursive_Benaphore) { } recursive_benaphore_try_lock :: proc(b: ^Recursive_Benaphore) -> bool { - tid := runtime.current_thread_id(); + tid := current_thread_id(); if b.owner == tid { atomic_add_acquire(&b.counter, 1); } @@ -228,7 +228,7 @@ recursive_benaphore_try_lock :: proc(b: ^Recursive_Benaphore) -> bool { } recursive_benaphore_unlock :: proc(b: ^Recursive_Benaphore) { - tid := runtime.current_thread_id(); + tid := current_thread_id(); assert(tid == b.owner); b.recursion -= 1; recursion := b.recursion; diff --git a/core/sync/sync2/primitives.odin b/core/sync/sync2/primitives.odin index e524586ec..d0a4439d2 100644 --- a/core/sync/sync2/primitives.odin +++ b/core/sync/sync2/primitives.odin @@ -2,6 +2,10 @@ package sync2 import "core:time" +current_thread_id :: proc "contextless" () -> int { + return _current_thread_id(); +} + // A Mutex is a mutual exclusion lock // The zero value for a Mutex is an unlocked mutex // diff --git a/core/sync/sync2/primitives_atomic.odin b/core/sync/sync2/primitives_atomic.odin index aed01eb1f..cfd06130d 100644 --- a/core/sync/sync2/primitives_atomic.odin +++ b/core/sync/sync2/primitives_atomic.odin @@ -233,7 +233,7 @@ Atomic_Recursive_Mutex :: struct { } atomic_recursive_mutex_lock :: proc(m: ^Atomic_Recursive_Mutex) { - tid := runtime.current_thread_id(); + tid := current_thread_id(); if tid != m.owner { mutex_lock(&m.mutex); } @@ -243,7 +243,7 @@ atomic_recursive_mutex_lock :: proc(m: ^Atomic_Recursive_Mutex) { } atomic_recursive_mutex_unlock :: proc(m: ^Atomic_Recursive_Mutex) { - tid := runtime.current_thread_id(); + tid := current_thread_id(); assert(tid == m.owner); m.recursion -= 1; recursion := m.recursion; @@ -258,7 +258,7 @@ atomic_recursive_mutex_unlock :: proc(m: ^Atomic_Recursive_Mutex) { } atomic_recursive_mutex_try_lock :: proc(m: ^Atomic_Recursive_Mutex) -> bool { - tid := runtime.current_thread_id(); + tid := current_thread_id(); if m.owner == tid { return mutex_try_lock(&m.mutex); } diff --git a/core/sync/sync2/primitives_windows.odin b/core/sync/sync2/primitives_windows.odin index 219af0162..582572aa9 100644 --- a/core/sync/sync2/primitives_windows.odin +++ b/core/sync/sync2/primitives_windows.odin @@ -5,6 +5,10 @@ package sync2 import "core:time" import win32 "core:sys/windows" +_current_thread_id :: proc "contextless" () -> int { + return int(win32.GetCurrentThreadId()); +} + _Mutex :: struct { srwlock: win32.SRWLOCK, } diff --git a/core/sync/sync_darwin.odin b/core/sync/sync_darwin.odin index 8153b4b37..c8e9632be 100644 --- a/core/sync/sync_darwin.odin +++ b/core/sync/sync_darwin.odin @@ -4,6 +4,19 @@ import "core:sys/darwin" import "core:c" +foreign import pthread "System.framework" + +current_thread_id :: proc "contextless" () -> int { + tid: u64; + // NOTE(Oskar): available from OSX 10.6 and iOS 3.2. + // For older versions there is `syscall(SYS_thread_selfid)`, but not really + // the same thing apparently. + foreign pthread { pthread_threadid_np :: proc "c" (rawptr, ^u64) -> c.int ---; } + pthread_threadid_np(nil, &tid); + return int(tid); +} + + // The Darwin docs say it best: // A semaphore is much like a lock, except that a finite number of threads can hold it simultaneously. // Semaphores can be thought of as being much like piles of tokens; multiple threads can take these tokens, diff --git a/core/sync/sync_freebsd.odin b/core/sync/sync_freebsd.odin index 7d1816cfe..87532621d 100644 --- a/core/sync/sync_freebsd.odin +++ b/core/sync/sync_freebsd.odin @@ -2,6 +2,18 @@ package sync import "core:sys/unix" +foreign import libc "system:c" + +current_thread_id :: proc "contextless" () -> int { + foreign libc { + syscall :: proc(number: i32, #c_vararg args: ..any) -> i32 --- + } + + SYS_GETTID :: 186; + return int(syscall(SYS_GETTID)); +} + + // The Darwin docs say it best: // A semaphore is much like a lock, except that a finite number of threads can hold it simultaneously. // Semaphores can be thought of as being much like piles of tokens; multiple threads can take these tokens, diff --git a/core/sync/sync_linux.odin b/core/sync/sync_linux.odin index 74f2b1e87..aa3c7a068 100644 --- a/core/sync/sync_linux.odin +++ b/core/sync/sync_linux.odin @@ -2,6 +2,18 @@ package sync import "core:sys/unix" +foreign import libc "system:c" + +current_thread_id :: proc "contextless" () -> int { + foreign libc { + syscall :: proc(number: i32, #c_vararg args: ..any) -> i32 --- + } + + SYS_GETTID :: 186; + return int(syscall(SYS_GETTID)); +} + + // The Darwin docs say it best: // A semaphore is much like a lock, except that a finite number of threads can hold it simultaneously. // Semaphores can be thought of as being much like piles of tokens; multiple threads can take these tokens, diff --git a/core/sync/sync_windows.odin b/core/sync/sync_windows.odin index 095f4a3d3..2bfd82567 100644 --- a/core/sync/sync_windows.odin +++ b/core/sync/sync_windows.odin @@ -4,6 +4,10 @@ package sync import win32 "core:sys/windows" import "core:time" +current_thread_id :: proc "contextless" () -> int { + return int(win32.GetCurrentThreadId()); +} + // When waited upon, blocks until the internal count is greater than zero, then subtracts one. // Posting to the semaphore increases the count by one, or the provided amount.