Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #204: fix get pthread id problem. #205

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/include/libks/ks_threadmutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ typedef void *(*ks_thread_function_t) (ks_thread_t *, void *);
#ifdef KS_PLAT_WIN
typedef void * ks_thread_os_handle_t;
typedef DWORD ks_pid_t;
#else
#elif KS_PLAT_LIN
typedef pid_t ks_pid_t;
typedef pthread_t ks_thread_os_handle_t;
#else
typedef uint64_t ks_pid_t;
typedef pthread_t ks_thread_os_handle_t;
#endif

typedef enum {
Expand Down
15 changes: 12 additions & 3 deletions src/ks_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,16 @@ KS_DECLARE(ks_pid_t) ks_thread_self_id(void)
#elif KS_PLAT_LIN
return syscall(SYS_gettid);
#else
return pthread_self();
uint64_t tid;
int r = pthread_threadid_np(NULL, &tid);
if (!r) {
return tid;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will overflow as returning uint64_t while function returns ks_pid_t that is pid_t aka int.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try fix it on commit : d2cae9c

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note there are places in the code under questions now:

uint32_t id = (uint32_t)ks_thread_self_id();

used += snprintf(buf + used - 1, bufSize - used, "#%8.8X ", id);

uint32_t id = (uint32_t)ks_thread_self_id();

or

static inline unsigned long ks_ssl_thread_id(void)
{
	return ks_thread_self_id();
}
CRYPTO_set_id_callback(ks_ssl_thread_id);

} else {
ks_log(KS_LOG_CRIT, "pthread_threadid_np error, return %d", r);
ks_log(KS_LOG_CRIT, "BACKTRACE:");
ks_debug_dump_backtrace();
abort();
}
#endif
}

Expand Down Expand Up @@ -165,10 +174,10 @@ static void *KS_THREAD_CALLING_CONVENTION thread_launch(void *args)
if (thread->tag)
SetThreadName(thread->id, thread->tag);
#elif KS_PLAT_MAC
if (thread->tag && pthread_setname_np)
if (thread->tag && &pthread_setname_np)
suzp1984 marked this conversation as resolved.
Show resolved Hide resolved
pthread_setname_np(thread->tag);
#else
if (thread->tag && pthread_setname_np)
if (thread->tag && &pthread_setname_np)
pthread_setname_np(pthread_self(), thread->tag);
#endif

Expand Down