1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <signal.h> 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #include <fmd_alloc.h> 32*0Sstevel@tonic-gate #include <fmd_thread.h> 33*0Sstevel@tonic-gate #include <fmd_module.h> 34*0Sstevel@tonic-gate #include <fmd_error.h> 35*0Sstevel@tonic-gate #include <fmd_subr.h> 36*0Sstevel@tonic-gate #include <fmd.h> 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate fmd_thread_t * 39*0Sstevel@tonic-gate fmd_thread_xcreate(fmd_module_t *mp, pthread_t tid) 40*0Sstevel@tonic-gate { 41*0Sstevel@tonic-gate fmd_thread_t *tp = fmd_alloc(sizeof (fmd_thread_t), FMD_SLEEP); 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate tp->thr_mod = mp; 44*0Sstevel@tonic-gate tp->thr_tid = tid; 45*0Sstevel@tonic-gate tp->thr_func = NULL; 46*0Sstevel@tonic-gate tp->thr_arg = NULL; 47*0Sstevel@tonic-gate tp->thr_trdata = fmd_trace_create(); 48*0Sstevel@tonic-gate tp->thr_trfunc = (fmd_tracebuf_f *)fmd.d_thr_trace; 49*0Sstevel@tonic-gate tp->thr_errdepth = 0; 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate (void) pthread_mutex_lock(&fmd.d_thr_lock); 52*0Sstevel@tonic-gate fmd_list_append(&fmd.d_thr_list, tp); 53*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&fmd.d_thr_lock); 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate return (tp); 56*0Sstevel@tonic-gate } 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate static void * 59*0Sstevel@tonic-gate fmd_thread_start(void *arg) 60*0Sstevel@tonic-gate { 61*0Sstevel@tonic-gate fmd_thread_t *tp = arg; 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate if (pthread_setspecific(fmd.d_key, tp) != 0) 64*0Sstevel@tonic-gate fmd_panic("failed to initialize thread key to %p", arg); 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate (void) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); 67*0Sstevel@tonic-gate (void) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate tp->thr_func(tp->thr_arg); 70*0Sstevel@tonic-gate return (NULL); 71*0Sstevel@tonic-gate } 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate fmd_thread_t * 74*0Sstevel@tonic-gate fmd_thread_create(fmd_module_t *mp, fmd_thread_f *func, void *arg) 75*0Sstevel@tonic-gate { 76*0Sstevel@tonic-gate fmd_thread_t *tp = fmd_alloc(sizeof (fmd_thread_t), FMD_SLEEP); 77*0Sstevel@tonic-gate sigset_t oset, nset; 78*0Sstevel@tonic-gate int err; 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate tp->thr_mod = mp; 81*0Sstevel@tonic-gate tp->thr_func = func; 82*0Sstevel@tonic-gate tp->thr_arg = arg; 83*0Sstevel@tonic-gate tp->thr_trdata = fmd_trace_create(); 84*0Sstevel@tonic-gate tp->thr_trfunc = (fmd_tracebuf_f *)fmd.d_thr_trace; 85*0Sstevel@tonic-gate tp->thr_errdepth = 0; 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate (void) sigfillset(&nset); 88*0Sstevel@tonic-gate (void) sigdelset(&nset, SIGABRT); /* always unblocked for fmd_panic() */ 89*0Sstevel@tonic-gate (void) sigdelset(&nset, fmd.d_thr_sig); /* for fmd_thr_signal() */ 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &nset, &oset); 92*0Sstevel@tonic-gate err = pthread_create(&tp->thr_tid, NULL, fmd_thread_start, tp); 93*0Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &oset, NULL); 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate if (err != 0) { 96*0Sstevel@tonic-gate fmd_free(tp, sizeof (fmd_thread_t)); 97*0Sstevel@tonic-gate return (NULL); 98*0Sstevel@tonic-gate } 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate (void) pthread_mutex_lock(&fmd.d_thr_lock); 101*0Sstevel@tonic-gate fmd_list_append(&fmd.d_thr_list, tp); 102*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&fmd.d_thr_lock); 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate return (tp); 105*0Sstevel@tonic-gate } 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate void 108*0Sstevel@tonic-gate fmd_thread_destroy(fmd_thread_t *tp, int flag) 109*0Sstevel@tonic-gate { 110*0Sstevel@tonic-gate if (flag == FMD_THREAD_JOIN && tp->thr_tid != pthread_self() && 111*0Sstevel@tonic-gate pthread_join(tp->thr_tid, NULL) != 0) { 112*0Sstevel@tonic-gate fmd_error(EFMD_MOD_JOIN, "failed to join thread for module " 113*0Sstevel@tonic-gate "%s (tid %u)\n", tp->thr_mod->mod_name, tp->thr_tid); 114*0Sstevel@tonic-gate } 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate (void) pthread_mutex_lock(&fmd.d_thr_lock); 117*0Sstevel@tonic-gate fmd_list_delete(&fmd.d_thr_list, tp); 118*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&fmd.d_thr_lock); 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate fmd_trace_destroy(tp->thr_trdata); 121*0Sstevel@tonic-gate fmd_free(tp, sizeof (fmd_thread_t)); 122*0Sstevel@tonic-gate } 123