Changeset View
Changeset View
Standalone View
Standalone View
lib/libthr/thread/thr_info.c
Show All 37 Lines | |||||
#include <pthread.h> | #include <pthread.h> | ||||
#include <pthread_np.h> | #include <pthread_np.h> | ||||
#include "un-namespace.h" | #include "un-namespace.h" | ||||
#include "thr_private.h" | #include "thr_private.h" | ||||
__weak_reference(_pthread_set_name_np, pthread_set_name_np); | __weak_reference(_pthread_set_name_np, pthread_set_name_np); | ||||
static void | |||||
thr_set_name_np(struct pthread *thread, const char *name) | |||||
{ | |||||
size_t len; | |||||
free(thread->name); | |||||
len = strlen(name); | |||||
thread->name = malloc(len + 1); | |||||
arichardson: Could this be `thread->name = strdup(name)` instead? It will also set the value to null on… | |||||
if (thread->name != NULL) | |||||
memcpy(thread->name, name, len + 1); | |||||
} | |||||
/* Set the thread name for debug. */ | /* Set the thread name for debug. */ | ||||
void | void | ||||
_pthread_set_name_np(pthread_t thread, const char *name) | _pthread_set_name_np(pthread_t thread, const char *name) | ||||
{ | { | ||||
struct pthread *curthread = _get_curthread(); | struct pthread *curthread; | ||||
int ret = 0; | |||||
curthread = _get_curthread(); | |||||
if (curthread == thread) { | if (curthread == thread) { | ||||
if (thr_set_name(thread->tid, name)) | THR_THREAD_LOCK(curthread, thread); | ||||
ret = errno; | thr_set_name(thread->tid, name); | ||||
thr_set_name_np(thread, name); | |||||
THR_THREAD_UNLOCK(curthread, thread); | |||||
} else { | } else { | ||||
if ((ret=_thr_find_thread(curthread, thread, 0)) == 0) { | if (_thr_find_thread(curthread, thread, 0) == 0) { | ||||
if (thread->state != PS_DEAD) { | if (thread->state != PS_DEAD) { | ||||
if (thr_set_name(thread->tid, name)) | thr_set_name(thread->tid, name); | ||||
ret = errno; | thr_set_name_np(thread, name); | ||||
} | } | ||||
THR_THREAD_UNLOCK(curthread, thread); | THR_THREAD_UNLOCK(curthread, thread); | ||||
} | } | ||||
} | } | ||||
#if 0 | } | ||||
/* XXX should return error code. */ | |||||
return (ret); | static void | ||||
#endif | thr_get_name_np(struct pthread *thread, char *buf, size_t len) | ||||
{ | |||||
if (thread->name != NULL) | |||||
strlcpy(buf, thread->name, len); | |||||
} | |||||
Done Inline ActionsWhat about when thread->name is NULL? Doesn't this need handling, like... else if (len > 0) buf[0] = 0; ian: What about when thread->name is NULL? Doesn't this need handling, like...
else if (len > 0)… | |||||
__weak_reference(_pthread_get_name_np, pthread_get_name_np); | |||||
void | |||||
_pthread_get_name_np(pthread_t thread, char *buf, size_t len) | |||||
{ | |||||
struct pthread *curthread; | |||||
curthread = _get_curthread(); | |||||
if (curthread == thread) { | |||||
THR_THREAD_LOCK(curthread, thread); | |||||
thr_get_name_np(thread, buf, len); | |||||
THR_THREAD_UNLOCK(curthread, thread); | |||||
} else { | |||||
if (_thr_find_thread(curthread, thread, 0) == 0) { | |||||
if (thread->state != PS_DEAD) | |||||
thr_get_name_np(thread, buf, len); | |||||
THR_THREAD_UNLOCK(curthread, thread); | |||||
} else if (len > 0) | |||||
buf[0] = '\0'; | |||||
} | |||||
} | } |
Could this be thread->name = strdup(name) instead? It will also set the value to null on allocation failure.