thr_find_thread.c revision 103388
1238384Sjkim/* 2238384Sjkim * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>. 3238384Sjkim * All rights reserved. 4238384Sjkim * 5238384Sjkim * Redistribution and use in source and binary forms, with or without 6238384Sjkim * modification, are permitted provided that the following conditions 7238384Sjkim * are met: 8238384Sjkim * 1. Redistributions of source code must retain the above copyright 9280297Sjkim * notice, this list of conditions and the following disclaimer. 10238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright 11238384Sjkim * notice, this list of conditions and the following disclaimer in the 12238384Sjkim * documentation and/or other materials provided with the distribution. 13238384Sjkim * 3. All advertising materials mentioning features or use of this software 14238384Sjkim * must display the following acknowledgement: 15238384Sjkim * This product includes software developed by John Birrell. 16238384Sjkim * 4. Neither the name of the author nor the names of any co-contributors 17238384Sjkim * may be used to endorse or promote products derived from this software 18238384Sjkim * without specific prior written permission. 19238384Sjkim * 20238384Sjkim * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 21238384Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23238384Sjkim * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24238384Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25238384Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26238384Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28238384Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29238384Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30238384Sjkim * SUCH DAMAGE. 31238384Sjkim * 32238384Sjkim * $FreeBSD: head/lib/libkse/thread/thr_find_thread.c 103388 2002-09-16 08:45:36Z mini $ 33238384Sjkim */ 34238384Sjkim#include <errno.h> 35238384Sjkim#include <pthread.h> 36238384Sjkim#include "thr_private.h" 37238384Sjkim 38238384Sjkim/* Find a thread in the linked list of active threads: */ 39238384Sjkimint 40238384Sjkim_find_thread(pthread_t pthread) 41238384Sjkim{ 42238384Sjkim pthread_t pthread1; 43238384Sjkim 44238384Sjkim /* Check if the caller has specified an invalid thread: */ 45238384Sjkim if (pthread == NULL || pthread->magic != PTHREAD_MAGIC) 46238384Sjkim /* Invalid thread: */ 47238384Sjkim return(EINVAL); 48238384Sjkim 49238384Sjkim /* 50238384Sjkim * Defer signals to protect the thread list from access 51238384Sjkim * by the signal handler: 52238384Sjkim */ 53238384Sjkim _thread_kern_sig_defer(); 54238384Sjkim 55238384Sjkim /* Search for the specified thread: */ 56280297Sjkim TAILQ_FOREACH(pthread1, &_thread_list, tle) { 57238384Sjkim if (pthread == pthread1) 58238384Sjkim break; 59238384Sjkim } 60238384Sjkim 61238384Sjkim /* Undefer and handle pending signals, yielding if necessary: */ 62238384Sjkim _thread_kern_sig_undefer(); 63280297Sjkim 64280297Sjkim /* Return zero if the thread exists: */ 65238384Sjkim return ((pthread1 != NULL) ? 0:ESRCH); 66238384Sjkim} 67238384Sjkim