xref: /onnv-gate/usr/src/cmd/fm/fmd/common/fmd_thread.c (revision 12967:ab9ae749152f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*12967Sgavin.maltby@oracle.com  * Common Development and Distribution License (the "License").
6*12967Sgavin.maltby@oracle.com  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12967Sgavin.maltby@oracle.com  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <signal.h>
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <fmd_alloc.h>
280Sstevel@tonic-gate #include <fmd_thread.h>
290Sstevel@tonic-gate #include <fmd_module.h>
300Sstevel@tonic-gate #include <fmd_error.h>
310Sstevel@tonic-gate #include <fmd_subr.h>
320Sstevel@tonic-gate #include <fmd.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate fmd_thread_t *
fmd_thread_xcreate(fmd_module_t * mp,pthread_t tid)350Sstevel@tonic-gate fmd_thread_xcreate(fmd_module_t *mp, pthread_t tid)
360Sstevel@tonic-gate {
370Sstevel@tonic-gate 	fmd_thread_t *tp = fmd_alloc(sizeof (fmd_thread_t), FMD_SLEEP);
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 	tp->thr_mod = mp;
400Sstevel@tonic-gate 	tp->thr_tid = tid;
410Sstevel@tonic-gate 	tp->thr_func = NULL;
420Sstevel@tonic-gate 	tp->thr_arg = NULL;
430Sstevel@tonic-gate 	tp->thr_trdata = fmd_trace_create();
440Sstevel@tonic-gate 	tp->thr_trfunc = (fmd_tracebuf_f *)fmd.d_thr_trace;
450Sstevel@tonic-gate 	tp->thr_errdepth = 0;
46*12967Sgavin.maltby@oracle.com 	tp->thr_isdoor = 0;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 	(void) pthread_mutex_lock(&fmd.d_thr_lock);
490Sstevel@tonic-gate 	fmd_list_append(&fmd.d_thr_list, tp);
500Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&fmd.d_thr_lock);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	return (tp);
530Sstevel@tonic-gate }
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static void *
fmd_thread_start(void * arg)560Sstevel@tonic-gate fmd_thread_start(void *arg)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	fmd_thread_t *tp = arg;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	if (pthread_setspecific(fmd.d_key, tp) != 0)
610Sstevel@tonic-gate 		fmd_panic("failed to initialize thread key to %p", arg);
620Sstevel@tonic-gate 
63*12967Sgavin.maltby@oracle.com 	if (!tp->thr_isdoor) {
64*12967Sgavin.maltby@oracle.com 		(void) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
65*12967Sgavin.maltby@oracle.com 		(void) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
66*12967Sgavin.maltby@oracle.com 	}
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	tp->thr_func(tp->thr_arg);
690Sstevel@tonic-gate 	return (NULL);
700Sstevel@tonic-gate }
710Sstevel@tonic-gate 
72*12967Sgavin.maltby@oracle.com static fmd_thread_t *
fmd_thread_create_cmn(fmd_module_t * mp,fmd_thread_f * func,void * arg,int isdoor)73*12967Sgavin.maltby@oracle.com fmd_thread_create_cmn(fmd_module_t *mp, fmd_thread_f *func, void *arg,
74*12967Sgavin.maltby@oracle.com     int isdoor)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	fmd_thread_t *tp = fmd_alloc(sizeof (fmd_thread_t), FMD_SLEEP);
770Sstevel@tonic-gate 	sigset_t oset, nset;
780Sstevel@tonic-gate 	int err;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	tp->thr_mod = mp;
810Sstevel@tonic-gate 	tp->thr_func = func;
820Sstevel@tonic-gate 	tp->thr_arg = arg;
830Sstevel@tonic-gate 	tp->thr_trdata = fmd_trace_create();
840Sstevel@tonic-gate 	tp->thr_trfunc = (fmd_tracebuf_f *)fmd.d_thr_trace;
850Sstevel@tonic-gate 	tp->thr_errdepth = 0;
86*12967Sgavin.maltby@oracle.com 	tp->thr_isdoor = isdoor;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	(void) sigfillset(&nset);
890Sstevel@tonic-gate 	(void) sigdelset(&nset, SIGABRT); /* always unblocked for fmd_panic() */
90*12967Sgavin.maltby@oracle.com 	if (!isdoor)
91*12967Sgavin.maltby@oracle.com 		(void) sigdelset(&nset, fmd.d_thr_sig); /* fmd_thr_signal() */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &nset, &oset);
940Sstevel@tonic-gate 	err = pthread_create(&tp->thr_tid, NULL, fmd_thread_start, tp);
950Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &oset, NULL);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	if (err != 0) {
980Sstevel@tonic-gate 		fmd_free(tp, sizeof (fmd_thread_t));
990Sstevel@tonic-gate 		return (NULL);
1000Sstevel@tonic-gate 	}
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	(void) pthread_mutex_lock(&fmd.d_thr_lock);
1030Sstevel@tonic-gate 	fmd_list_append(&fmd.d_thr_list, tp);
1040Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&fmd.d_thr_lock);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	return (tp);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
109*12967Sgavin.maltby@oracle.com fmd_thread_t *
fmd_thread_create(fmd_module_t * mp,fmd_thread_f * func,void * arg)110*12967Sgavin.maltby@oracle.com fmd_thread_create(fmd_module_t *mp, fmd_thread_f *func, void *arg)
111*12967Sgavin.maltby@oracle.com {
112*12967Sgavin.maltby@oracle.com 	return (fmd_thread_create_cmn(mp, func, arg, 0));
113*12967Sgavin.maltby@oracle.com }
114*12967Sgavin.maltby@oracle.com 
115*12967Sgavin.maltby@oracle.com fmd_thread_t *
fmd_doorthread_create(fmd_module_t * mp,fmd_thread_f * func,void * arg)116*12967Sgavin.maltby@oracle.com fmd_doorthread_create(fmd_module_t *mp, fmd_thread_f *func, void *arg)
117*12967Sgavin.maltby@oracle.com {
118*12967Sgavin.maltby@oracle.com 	return (fmd_thread_create_cmn(mp, func, arg, 1));
119*12967Sgavin.maltby@oracle.com }
120*12967Sgavin.maltby@oracle.com 
1210Sstevel@tonic-gate void
fmd_thread_destroy(fmd_thread_t * tp,int flag)1220Sstevel@tonic-gate fmd_thread_destroy(fmd_thread_t *tp, int flag)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	if (flag == FMD_THREAD_JOIN && tp->thr_tid != pthread_self() &&
1250Sstevel@tonic-gate 	    pthread_join(tp->thr_tid, NULL) != 0) {
1260Sstevel@tonic-gate 		fmd_error(EFMD_MOD_JOIN, "failed to join thread for module "
1270Sstevel@tonic-gate 		    "%s (tid %u)\n", tp->thr_mod->mod_name, tp->thr_tid);
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	(void) pthread_mutex_lock(&fmd.d_thr_lock);
1310Sstevel@tonic-gate 	fmd_list_delete(&fmd.d_thr_list, tp);
1320Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&fmd.d_thr_lock);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	fmd_trace_destroy(tp->thr_trdata);
1350Sstevel@tonic-gate 	fmd_free(tp, sizeof (fmd_thread_t));
1360Sstevel@tonic-gate }
137