Changeset View
Changeset View
Standalone View
Standalone View
head/lib/libthr/thread/thr_info.c
Show All 31 Lines | |||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||||
* SUCH DAMAGE. | * SUCH DAMAGE. | ||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include "namespace.h" | #include "namespace.h" | ||||
#include <sys/errno.h> | |||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#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); | |||||
static void | static void | ||||
thr_set_name_np(struct pthread *thread, const char *name) | thr_set_name_np(struct pthread *thread, char **tmp_name) | ||||
{ | { | ||||
free(thread->name); | free(thread->name); | ||||
thread->name = name != NULL ? strdup(name) : NULL; | thread->name = *tmp_name; | ||||
*tmp_name = NULL; | |||||
} | } | ||||
/* Set the thread name for debug. */ | /* Set the thread name. */ | ||||
void | __weak_reference(_pthread_setname_np, pthread_setname_np); | ||||
_pthread_set_name_np(pthread_t thread, const char *name) | int | ||||
_pthread_setname_np(pthread_t thread, const char *name) | |||||
{ | { | ||||
struct pthread *curthread; | struct pthread *curthread; | ||||
char *tmp_name; | |||||
int res; | |||||
if (name != NULL) { | |||||
tmp_name = strdup(name); | |||||
if (tmp_name == NULL) | |||||
return (ENOMEM); | |||||
} else { | |||||
tmp_name = NULL; | |||||
} | |||||
curthread = _get_curthread(); | curthread = _get_curthread(); | ||||
if (curthread == thread) { | if (curthread == thread) { | ||||
res = 0; | |||||
THR_THREAD_LOCK(curthread, thread); | THR_THREAD_LOCK(curthread, thread); | ||||
thr_set_name(thread->tid, name); | if (thr_set_name(thread->tid, name) == -1) | ||||
thr_set_name_np(thread, name); | res = errno; | ||||
else | |||||
thr_set_name_np(thread, &tmp_name); | |||||
THR_THREAD_UNLOCK(curthread, thread); | THR_THREAD_UNLOCK(curthread, thread); | ||||
} else { | } else { | ||||
res = ESRCH; | |||||
if (_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) { | ||||
thr_set_name(thread->tid, name); | if (thr_set_name(thread->tid, name) == -1) { | ||||
thr_set_name_np(thread, name); | res = errno; | ||||
} else { | |||||
thr_set_name_np(thread, &tmp_name); | |||||
res = 0; | |||||
} | } | ||||
} | |||||
THR_THREAD_UNLOCK(curthread, thread); | THR_THREAD_UNLOCK(curthread, thread); | ||||
} | } | ||||
} | } | ||||
free(tmp_name); | |||||
return (res); | |||||
} | } | ||||
/* Set the thread name for debug. */ | |||||
__weak_reference(_pthread_set_name_np, pthread_set_name_np); | |||||
void | |||||
_pthread_set_name_np(pthread_t thread, const char *name) | |||||
{ | |||||
(void)_pthread_setname_np(thread, name); | |||||
} | |||||
static void | static void | ||||
thr_get_name_np(struct pthread *thread, char *buf, size_t len) | thr_get_name_np(struct pthread *thread, char *buf, size_t len) | ||||
{ | { | ||||
if (thread->name != NULL) | if (thread->name != NULL) | ||||
strlcpy(buf, thread->name, len); | strlcpy(buf, thread->name, len); | ||||
else if (len > 0) | else if (len > 0) | ||||
buf[0] = '\0'; | buf[0] = '\0'; | ||||
} | } | ||||
__weak_reference(_pthread_get_name_np, pthread_get_name_np); | __weak_reference(_pthread_getname_np, pthread_getname_np); | ||||
int | |||||
void | _pthread_getname_np(pthread_t thread, char *buf, size_t len) | ||||
_pthread_get_name_np(pthread_t thread, char *buf, size_t len) | |||||
{ | { | ||||
struct pthread *curthread; | struct pthread *curthread; | ||||
int res; | |||||
res = 0; | |||||
curthread = _get_curthread(); | curthread = _get_curthread(); | ||||
if (curthread == thread) { | if (curthread == thread) { | ||||
THR_THREAD_LOCK(curthread, thread); | THR_THREAD_LOCK(curthread, thread); | ||||
thr_get_name_np(thread, buf, len); | thr_get_name_np(thread, buf, len); | ||||
THR_THREAD_UNLOCK(curthread, thread); | THR_THREAD_UNLOCK(curthread, thread); | ||||
} else { | } else { | ||||
if (_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) | ||||
thr_get_name_np(thread, buf, len); | thr_get_name_np(thread, buf, len); | ||||
else | |||||
res = ESRCH; | |||||
THR_THREAD_UNLOCK(curthread, thread); | THR_THREAD_UNLOCK(curthread, thread); | ||||
} else if (len > 0) | } else { | ||||
res = ESRCH; | |||||
if (len > 0) | |||||
buf[0] = '\0'; | buf[0] = '\0'; | ||||
} | } | ||||
} | |||||
return (res); | |||||
} | |||||
__weak_reference(_pthread_get_name_np, pthread_get_name_np); | |||||
void | |||||
_pthread_get_name_np(pthread_t thread, char *buf, size_t len) | |||||
{ | |||||
(void)_pthread_getname_np(thread, buf, len); | |||||
} | } |