thr_kill.c revision 144518
11573Srgrimes/*
21573Srgrimes * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
31573Srgrimes * All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 3. All advertising materials mentioning features or use of this software
141573Srgrimes *    must display the following acknowledgement:
151573Srgrimes *	This product includes software developed by John Birrell.
16148834Sstefanf * 4. Neither the name of the author nor the names of any co-contributors
171573Srgrimes *    may be used to endorse or promote products derived from this software
181573Srgrimes *    without specific prior written permission.
191573Srgrimes *
201573Srgrimes * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301573Srgrimes * SUCH DAMAGE.
3184260Sobrien *
32268782Spfg * $FreeBSD: head/lib/libthr/thread/thr_kill.c 144518 2005-04-02 01:20:00Z davidxu $
331573Srgrimes */
341573Srgrimes
351573Srgrimes#include <errno.h>
361573Srgrimes#include <signal.h>
371573Srgrimes#include <pthread.h>
3884260Sobrien
3984260Sobrien#include "thr_private.h"
401573Srgrimes
411573Srgrimes__weak_reference(_pthread_kill, pthread_kill);
421573Srgrimes
431573Srgrimesint
441573Srgrimes_pthread_kill(pthread_t pthread, int sig)
451573Srgrimes{
461573Srgrimes	struct pthread *curthread = _get_curthread();
471573Srgrimes	int ret;
4884260Sobrien
4984260Sobrien	/* Check for invalid signal numbers: */
501573Srgrimes	if (sig < 0 || sig > _SIG_MAXSIG)
511573Srgrimes		/* Invalid signal: */
521573Srgrimes		ret = EINVAL;
531573Srgrimes	/*
541573Srgrimes	 * Ensure the thread is in the list of active threads, and the
551573Srgrimes	 * signal is valid (signal 0 specifies error checking only) and
56148834Sstefanf	 * not being ignored:
571573Srgrimes	 */
5884260Sobrien	else if ((ret = _thr_ref_add(curthread, pthread, /*include dead*/0))
5984260Sobrien	    == 0) {
6084260Sobrien		if (sig > 0)
611573Srgrimes			_thr_send_sig(pthread, sig);
621573Srgrimes		_thr_ref_delete(curthread, pthread);
631573Srgrimes	}
6484260Sobrien
6584260Sobrien	/* Return the completion status: */
6684260Sobrien	return (ret);
6784260Sobrien}
6884260Sobrien