Browse Source

Remove `context.thread_id`

gingerBill 4 years ago
parent
commit
8ec2ca9dcd

+ 0 - 3
core/runtime/core.odin

@@ -329,8 +329,6 @@ Context :: struct {
 	assertion_failure_proc: Assertion_Failure_Proc,
 	assertion_failure_proc: Assertion_Failure_Proc,
 	logger:                 Logger,
 	logger:                 Logger,
 
 
-	thread_id:  int,
-
 	user_data:  any,
 	user_data:  any,
 	user_ptr:   rawptr,
 	user_ptr:   rawptr,
 	user_index: int,
 	user_index: int,
@@ -479,7 +477,6 @@ __init_context :: proc "contextless" (c: ^Context) {
 	c.temp_allocator.procedure = default_temp_allocator_proc;
 	c.temp_allocator.procedure = default_temp_allocator_proc;
 	c.temp_allocator.data = &global_default_temp_allocator_data;
 	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.assertion_failure_proc = default_assertion_failure_proc;
 
 
 	c.logger.procedure = default_logger_proc;
 	c.logger.procedure = default_logger_proc;

+ 0 - 4
core/runtime/os_specific.odin

@@ -5,7 +5,3 @@ _OS_Errno :: distinct int;
 os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
 os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
 	return _os_write(data);
 	return _os_write(data);
 }
 }
-
-current_thread_id :: proc "contextless" () -> int {
-	return _current_thread_id();
-}

+ 0 - 4
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);
 	n, err := os.write(os.stderr, data);
 	return int(n), _OS_Errno(err);
 	return int(n), _OS_Errno(err);
 }
 }
-
-_current_thread_id :: proc "contextless" () -> int {
-	return os.current_thread_id();
-}

+ 0 - 4
core/runtime/os_specific_freestanding.odin

@@ -5,7 +5,3 @@ package runtime
 _os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
 _os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
 	return 0, -1;
 	return 0, -1;
 }
 }
-
-_current_thread_id :: proc "contextless" () -> int {
-	return 0;
-}

+ 0 - 8
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 ---
 	WriteFile            :: proc(hFile: rawptr, lpBuffer: rawptr, nNumberOfBytesToWrite: u32, lpNumberOfBytesWritten: ^u32, lpOverlapped: rawptr) -> b32 ---
 	GetLastError         :: proc() -> u32 ---
 	GetLastError         :: proc() -> u32 ---
 
 
-	// current_thread_id
-	GetCurrentThreadId :: proc() -> u32 ---
-
 	// default_allocator
 	// default_allocator
 	GetProcessHeap :: proc() -> rawptr ---
 	GetProcessHeap :: proc() -> rawptr ---
 	HeapAlloc      :: proc(hHeap: rawptr, dwFlags: u32, dwBytes: uint) -> 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;
 	return;
 }
 }
 
 
-_current_thread_id :: proc "contextless" () -> int {
-	return int(GetCurrentThreadId());
-}
-
-
 heap_alloc :: proc "contextless" (size: int) -> rawptr {
 heap_alloc :: proc "contextless" (size: int) -> rawptr {
 	HEAP_ZERO_MEMORY :: 0x00000008;
 	HEAP_ZERO_MEMORY :: 0x00000008;
 	return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, uint(size));
 	return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, uint(size));

+ 3 - 3
core/sync/sync.odin

@@ -80,7 +80,7 @@ recursive_benaphore_destroy :: proc(b: ^Recursive_Benaphore) {
 }
 }
 
 
 recursive_benaphore_lock :: 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 intrinsics.atomic_add_acq(&b.counter, 1) > 1 {
 		if tid != b.owner {
 		if tid != b.owner {
 			semaphore_wait_for(&b.sema);
 			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 {
 recursive_benaphore_try_lock :: proc(b: ^Recursive_Benaphore) -> bool {
-	tid := runtime.current_thread_id();
+	tid := current_thread_id();
 	if b.owner == tid {
 	if b.owner == tid {
 		intrinsics.atomic_add_acq(&b.counter, 1);
 		intrinsics.atomic_add_acq(&b.counter, 1);
 	} else {
 	} else {
@@ -108,7 +108,7 @@ recursive_benaphore_try_lock :: proc(b: ^Recursive_Benaphore) -> bool {
 }
 }
 
 
 recursive_benaphore_unlock :: proc(b: ^Recursive_Benaphore) {
 recursive_benaphore_unlock :: proc(b: ^Recursive_Benaphore) {
-	tid := runtime.current_thread_id();
+	tid := current_thread_id();
 	assert(tid == b.owner);
 	assert(tid == b.owner);
 	b.recursion -= 1;
 	b.recursion -= 1;
 	recursion := b.recursion;
 	recursion := b.recursion;

+ 3 - 3
core/sync/sync2/extended.odin

@@ -201,7 +201,7 @@ Recursive_Benaphore :: struct {
 }
 }
 
 
 recursive_benaphore_lock :: proc(b: ^Recursive_Benaphore) {
 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 atomic_add_acquire(&b.counter, 1) > 1 {
 		if tid != b.owner {
 		if tid != b.owner {
 			sema_wait(&b.sema);
 			sema_wait(&b.sema);
@@ -213,7 +213,7 @@ recursive_benaphore_lock :: proc(b: ^Recursive_Benaphore) {
 }
 }
 
 
 recursive_benaphore_try_lock :: proc(b: ^Recursive_Benaphore) -> bool {
 recursive_benaphore_try_lock :: proc(b: ^Recursive_Benaphore) -> bool {
-	tid := runtime.current_thread_id();
+	tid := current_thread_id();
 	if b.owner == tid {
 	if b.owner == tid {
 		atomic_add_acquire(&b.counter, 1);
 		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) {
 recursive_benaphore_unlock :: proc(b: ^Recursive_Benaphore) {
-	tid := runtime.current_thread_id();
+	tid := current_thread_id();
 	assert(tid == b.owner);
 	assert(tid == b.owner);
 	b.recursion -= 1;
 	b.recursion -= 1;
 	recursion := b.recursion;
 	recursion := b.recursion;

+ 4 - 0
core/sync/sync2/primitives.odin

@@ -2,6 +2,10 @@ package sync2
 
 
 import "core:time"
 import "core:time"
 
 
+current_thread_id :: proc "contextless" () -> int {
+	return _current_thread_id();
+}
+
 // A Mutex is a mutual exclusion lock
 // A Mutex is a mutual exclusion lock
 // The zero value for a Mutex is an unlocked mutex
 // The zero value for a Mutex is an unlocked mutex
 //
 //

+ 3 - 3
core/sync/sync2/primitives_atomic.odin

@@ -233,7 +233,7 @@ Atomic_Recursive_Mutex :: struct {
 }
 }
 
 
 atomic_recursive_mutex_lock :: proc(m: ^Atomic_Recursive_Mutex) {
 atomic_recursive_mutex_lock :: proc(m: ^Atomic_Recursive_Mutex) {
-	tid := runtime.current_thread_id();
+	tid := current_thread_id();
 	if tid != m.owner {
 	if tid != m.owner {
 		mutex_lock(&m.mutex);
 		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) {
 atomic_recursive_mutex_unlock :: proc(m: ^Atomic_Recursive_Mutex) {
-	tid := runtime.current_thread_id();
+	tid := current_thread_id();
 	assert(tid == m.owner);
 	assert(tid == m.owner);
 	m.recursion -= 1;
 	m.recursion -= 1;
 	recursion := m.recursion;
 	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 {
 atomic_recursive_mutex_try_lock :: proc(m: ^Atomic_Recursive_Mutex) -> bool {
-	tid := runtime.current_thread_id();
+	tid := current_thread_id();
 	if m.owner == tid {
 	if m.owner == tid {
 		return mutex_try_lock(&m.mutex);
 		return mutex_try_lock(&m.mutex);
 	}
 	}

+ 4 - 0
core/sync/sync2/primitives_windows.odin

@@ -5,6 +5,10 @@ package sync2
 import "core:time"
 import "core:time"
 import win32 "core:sys/windows"
 import win32 "core:sys/windows"
 
 
+_current_thread_id :: proc "contextless" () -> int {
+	return int(win32.GetCurrentThreadId());
+}
+
 _Mutex :: struct {
 _Mutex :: struct {
 	srwlock: win32.SRWLOCK,
 	srwlock: win32.SRWLOCK,
 }
 }

+ 13 - 0
core/sync/sync_darwin.odin

@@ -4,6 +4,19 @@ import "core:sys/darwin"
 
 
 import "core:c"
 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:
 // The Darwin docs say it best:
 // A semaphore is much like a lock, except that a finite number of threads can hold it simultaneously.
 // 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, 
 // Semaphores can be thought of as being much like piles of tokens; multiple threads can take these tokens, 

+ 12 - 0
core/sync/sync_freebsd.odin

@@ -2,6 +2,18 @@ package sync
 
 
 import "core:sys/unix"
 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:
 // The Darwin docs say it best:
 // A semaphore is much like a lock, except that a finite number of threads can hold it simultaneously.
 // 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, 
 // Semaphores can be thought of as being much like piles of tokens; multiple threads can take these tokens, 

+ 12 - 0
core/sync/sync_linux.odin

@@ -2,6 +2,18 @@ package sync
 
 
 import "core:sys/unix"
 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:
 // The Darwin docs say it best:
 // A semaphore is much like a lock, except that a finite number of threads can hold it simultaneously.
 // 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, 
 // Semaphores can be thought of as being much like piles of tokens; multiple threads can take these tokens, 

+ 4 - 0
core/sync/sync_windows.odin

@@ -4,6 +4,10 @@ package sync
 import win32 "core:sys/windows"
 import win32 "core:sys/windows"
 import "core:time"
 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.
 // 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.
 // Posting to the semaphore increases the count by one, or the provided amount.