xref: /freebsd/lib/libthr/thread/thr_info.c (revision fed32d75449d7d05afdd7ddb7703bfa69a3027f3)
1bb535300SJeff Roberson /*
2bb535300SJeff Roberson  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3bb535300SJeff Roberson  * All rights reserved.
4bb535300SJeff Roberson  *
5bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
6bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
7bb535300SJeff Roberson  * are met:
8bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
9bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer.
10bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
11bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
12bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
13fed32d75SWarner Losh  * 3. Neither the name of the author nor the names of any co-contributors
14bb535300SJeff Roberson  *    may be used to endorse or promote products derived from this software
15bb535300SJeff Roberson  *    without specific prior written permission.
16bb535300SJeff Roberson  *
17bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27bb535300SJeff Roberson  * SUCH DAMAGE.
28bb535300SJeff Roberson  *
29bb535300SJeff Roberson  * $FreeBSD$
30bb535300SJeff Roberson  */
31a091d823SDavid Xu 
3237a6356bSDavid Xu #include "namespace.h"
33bb535300SJeff Roberson #include <stdlib.h>
34bb535300SJeff Roberson #include <string.h>
3537a6356bSDavid Xu #include <pthread.h>
3637a6356bSDavid Xu #include <pthread_np.h>
3737a6356bSDavid Xu #include "un-namespace.h"
38a091d823SDavid Xu 
39bb535300SJeff Roberson #include "thr_private.h"
40bb535300SJeff Roberson 
415f9df006SDavid Xu __weak_reference(_pthread_set_name_np, pthread_set_name_np);
425f9df006SDavid Xu 
43e35e2ebdSDavid Xu /* Set the thread name for debug. */
44bb535300SJeff Roberson void
4537a6356bSDavid Xu _pthread_set_name_np(pthread_t thread, const char *name)
46bb535300SJeff Roberson {
47e35e2ebdSDavid Xu 	struct pthread *curthread = _get_curthread();
483f7dda33SDavid Xu 	int ret = 0;
49e35e2ebdSDavid Xu 
503f7dda33SDavid Xu 	if (curthread == thread) {
513f7dda33SDavid Xu 		if (thr_set_name(thread->tid, name))
523f7dda33SDavid Xu 			ret = errno;
533f7dda33SDavid Xu 	} else {
543f7dda33SDavid Xu 		if (_thr_ref_add(curthread, thread, 0) == 0) {
553f7dda33SDavid Xu 			THR_LOCK(thread);
563f7dda33SDavid Xu 			if (thread->state != PS_DEAD) {
573f7dda33SDavid Xu 				if (thr_set_name(thread->tid, name))
583f7dda33SDavid Xu 					ret = errno;
59bb535300SJeff Roberson 			}
603f7dda33SDavid Xu 			THR_UNLOCK(thread);
613f7dda33SDavid Xu 			_thr_ref_delete(curthread, thread);
623f7dda33SDavid Xu 		} else {
633f7dda33SDavid Xu 			ret = ESRCH;
64bb535300SJeff Roberson 		}
653f7dda33SDavid Xu 	}
663f7dda33SDavid Xu #if 0
673f7dda33SDavid Xu 	/* XXX should return error code. */
683f7dda33SDavid Xu 	return (ret);
69e35e2ebdSDavid Xu #endif
70bb535300SJeff Roberson }
71