thr_find_thread.c revision 71581
1106266Sjulian/*
2106266Sjulian * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>.
3106266Sjulian * All rights reserved.
4106266Sjulian *
5106266Sjulian * Redistribution and use in source and binary forms, with or without
6106266Sjulian * modification, are permitted provided that the following conditions
7106266Sjulian * are met:
8106266Sjulian * 1. Redistributions of source code must retain the above copyright
9106266Sjulian *    notice, this list of conditions and the following disclaimer.
10106266Sjulian * 2. Redistributions in binary form must reproduce the above copyright
11106266Sjulian *    notice, this list of conditions and the following disclaimer in the
12106266Sjulian *    documentation and/or other materials provided with the distribution.
13106266Sjulian * 3. All advertising materials mentioning features or use of this software
14106266Sjulian *    must display the following acknowledgement:
15106266Sjulian *	This product includes software developed by John Birrell.
16106266Sjulian * 4. Neither the name of the author nor the names of any co-contributors
17106266Sjulian *    may be used to endorse or promote products derived from this software
18106266Sjulian *    without specific prior written permission.
19106266Sjulian *
20106266Sjulian * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21106266Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22106266Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23106266Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24106266Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25106266Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26106266Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27106266Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28106266Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29106266Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30106266Sjulian * SUCH DAMAGE.
31106266Sjulian *
32106266Sjulian * $FreeBSD: head/lib/libkse/thread/thr_find_thread.c 71581 2001-01-24 13:03:38Z deischen $
33106266Sjulian */
34106266Sjulian#include <errno.h>
35106266Sjulian#include <pthread.h>
36106266Sjulian#include "pthread_private.h"
37106266Sjulian
38106266Sjulian/* Find a thread in the linked list of active threads: */
39106266Sjulianint
40106266Sjulian_find_thread(pthread_t pthread)
41106266Sjulian{
42106266Sjulian	pthread_t pthread1;
43106266Sjulian
44106266Sjulian	/* Check if the caller has specified an invalid thread: */
45106266Sjulian	if (pthread == NULL || pthread->magic != PTHREAD_MAGIC)
46106266Sjulian		/* Invalid thread: */
47106266Sjulian		return(EINVAL);
48106266Sjulian
49106266Sjulian	/*
50106266Sjulian	 * Defer signals to protect the thread list from access
51106266Sjulian	 * by the signal handler:
52106266Sjulian	 */
53106266Sjulian	_thread_kern_sig_defer();
54106266Sjulian
55106266Sjulian	/* Search for the specified thread: */
56106266Sjulian	TAILQ_FOREACH(pthread1, &_thread_list, tle) {
57106266Sjulian		if (pthread == pthread1)
58106266Sjulian			break;
59106266Sjulian	}
60106266Sjulian
61106266Sjulian	/* Undefer and handle pending signals, yielding if necessary: */
62106266Sjulian	_thread_kern_sig_undefer();
63106266Sjulian
64106266Sjulian	/* Return zero if the thread exists: */
65106266Sjulian	return ((pthread1 != NULL) ? 0:ESRCH);
66106266Sjulian}
67106266Sjulian
68106266Sjulian/* Find a thread in the linked list of dead threads: */
69106266Sjulianint
70106266Sjulian_find_dead_thread(pthread_t pthread)
71106266Sjulian{
72106266Sjulian	pthread_t pthread1;
73106266Sjulian
74106266Sjulian	/* Check if the caller has specified an invalid thread: */
75106266Sjulian	if (pthread == NULL || pthread->magic != PTHREAD_MAGIC)
76106266Sjulian		/* Invalid thread: */
77106266Sjulian		return(EINVAL);
78106266Sjulian
79106266Sjulian	/*
80106266Sjulian	 * Lock the garbage collector mutex to ensure that the garbage
81106266Sjulian	 * collector is not using the dead thread list.
82106266Sjulian	 */
83106266Sjulian	if (pthread_mutex_lock(&_gc_mutex) != 0)
84106266Sjulian		PANIC("Cannot lock gc mutex");
85106266Sjulian
86106266Sjulian	/* Search for the specified thread: */
87106266Sjulian	TAILQ_FOREACH(pthread1, &_dead_list, dle) {
88106266Sjulian		if (pthread1 == pthread)
89106266Sjulian			break;
90106266Sjulian	}
91106266Sjulian
92106266Sjulian	/* Unlock the garbage collector mutex: */
93106266Sjulian	if (pthread_mutex_unlock(&_gc_mutex) != 0)
94106266Sjulian		PANIC("Cannot lock gc mutex");
95106266Sjulian
96106266Sjulian	/* Return zero if the thread exists: */
97106266Sjulian	return ((pthread1 != NULL) ? 0:ESRCH);
98106266Sjulian}
99106266Sjulian