From 83eabe21408792ece7f8b7f4e35d357a520752ed Mon Sep 17 00:00:00 2001 From: Clay Murray Date: Sat, 11 Jul 2020 18:22:56 -0600 Subject: [PATCH 1/4] Fix pthread_t on Macos. From some testing with directly using C code, pthread_t on macos is 8 bytes. This is my test code: ``` #include #include #include void* PosixThreadMainRoutine(void* data) { // Do some work here. for (int i = 0; i < 2000000000; i++) { } return NULL; } pthread_t LaunchThread() { // Create the thread using POSIX routines. pthread_attr_t attr; pthread_t posixThreadID; int returnVal; returnVal = pthread_attr_init(&attr); assert(!returnVal); returnVal = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); assert(!returnVal); int threadError = pthread_create(&posixThreadID, &attr, &PosixThreadMainRoutine, NULL); returnVal = pthread_attr_destroy(&attr); assert(!returnVal); if (threadError != 0) { // Report an error. } return posixThreadID; } int main() { pthread_t t = LaunchThread(); void ** ret; printf("%d, %d\n", sizeof(t), sizeof(pthread_t)); int val = pthread_join(t, ret); printf("%d", val); return 0; } ``` running this on macos reports `8, 8`. Then I made the proposed changes and errors I was having with threads completely went away. --- core/sys/unix/pthread_darwin.odin | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/core/sys/unix/pthread_darwin.odin b/core/sys/unix/pthread_darwin.odin index 9de64ead4..7f7f59189 100644 --- a/core/sys/unix/pthread_darwin.odin +++ b/core/sys/unix/pthread_darwin.odin @@ -14,11 +14,7 @@ PTHREAD_ONCE_SIZE :: 8; PTHREAD_RWLOCK_SIZE :: 192; PTHREAD_RWLOCKATTR_SIZE :: 16; -pthread_t :: opaque struct #align 16 { - sig: c.long, - cleanup_stack: rawptr, - _: [PTHREAD_SIZE] c.char, -}; +pthread_t :: opaque u64; pthread_attr_t :: opaque struct #align 16 { sig: c.long, From a7e38dc063e19821a4f6c1239f787786d10c82c1 Mon Sep 17 00:00:00 2001 From: Oskar Nordquist Date: Wed, 22 Jul 2020 17:38:13 +0200 Subject: [PATCH 2/4] Implement os.current_thread_id() for Darwin (assumes OSX 10.6 / iOS 3.2 and later) --- core/os/os_darwin.odin | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/os/os_darwin.odin b/core/os/os_darwin.odin index 9813b9b3b..5300979a1 100644 --- a/core/os/os_darwin.odin +++ b/core/os/os_darwin.odin @@ -2,6 +2,7 @@ package os foreign import dl "system:dl" foreign import libc "system:c" +foreign import pthread "system:pthread" import "core:runtime" import "core:strings" @@ -428,8 +429,13 @@ exit :: inline proc(code: int) -> ! { } current_thread_id :: proc "contextless" () -> int { - // return int(_unix_gettid()); - return 0; + 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); } dlopen :: inline proc(filename: string, flags: int) -> rawptr { From 36cac87387552ef0761d22333afc884dadcc4386 Mon Sep 17 00:00:00 2001 From: Oskar Nordquist Date: Wed, 22 Jul 2020 17:41:29 +0200 Subject: [PATCH 3/4] Add .Thread_Id option to log package --- core/log/file_console_logger.odin | 6 ++++++ core/runtime/core.odin | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core/log/file_console_logger.odin b/core/log/file_console_logger.odin index ccecbc8b8..757bb5ff5 100644 --- a/core/log/file_console_logger.odin +++ b/core/log/file_console_logger.odin @@ -83,6 +83,12 @@ file_console_logger_proc :: proc(logger_data: rawptr, level: Level, text: string do_location_header(options, &buf, location); + if .Thread_Id in options { + // NOTE(Oskar): not using context.thread_id here since that could be + // incorrect when replacing context for a thread. + fmt.sbprintf(&buf, "[{}] ", os.current_thread_id()); + } + if data.ident != "" do fmt.sbprintf(&buf, "[%s] ", data.ident); //TODO(Hoej): When we have better atomics and such, make this thread-safe fmt.fprintf(h, "%s %s\n", strings.to_string(buf), text); diff --git a/core/runtime/core.odin b/core/runtime/core.odin index 7d1a7bdd5..1ed16d127 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -279,7 +279,8 @@ Logger_Option :: enum { Long_File_Path, Line, Procedure, - Terminal_Color + Terminal_Color, + Thread_Id } Logger_Options :: bit_set[Logger_Option]; From 5b7e1cd6f64fcf70e968553002abf491fde2984d Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 1 Aug 2020 11:35:56 +0100 Subject: [PATCH 4/4] Update LICENSE --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index f50c5ada3..e9e75e569 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2016-2017 Ginger Bill. All rights reserved. +Copyright (c) 2016-2020 Ginger Bill. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: