17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*f6e214c7SGavin Maltby * Common Development and Distribution License (the "License"). 6*f6e214c7SGavin Maltby * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*f6e214c7SGavin Maltby * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate #include <signal.h> 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <fmd_alloc.h> 287c478bd9Sstevel@tonic-gate #include <fmd_thread.h> 297c478bd9Sstevel@tonic-gate #include <fmd_module.h> 307c478bd9Sstevel@tonic-gate #include <fmd_error.h> 317c478bd9Sstevel@tonic-gate #include <fmd_subr.h> 327c478bd9Sstevel@tonic-gate #include <fmd.h> 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate fmd_thread_t * 357c478bd9Sstevel@tonic-gate fmd_thread_xcreate(fmd_module_t *mp, pthread_t tid) 367c478bd9Sstevel@tonic-gate { 377c478bd9Sstevel@tonic-gate fmd_thread_t *tp = fmd_alloc(sizeof (fmd_thread_t), FMD_SLEEP); 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate tp->thr_mod = mp; 407c478bd9Sstevel@tonic-gate tp->thr_tid = tid; 417c478bd9Sstevel@tonic-gate tp->thr_func = NULL; 427c478bd9Sstevel@tonic-gate tp->thr_arg = NULL; 437c478bd9Sstevel@tonic-gate tp->thr_trdata = fmd_trace_create(); 447c478bd9Sstevel@tonic-gate tp->thr_trfunc = (fmd_tracebuf_f *)fmd.d_thr_trace; 457c478bd9Sstevel@tonic-gate tp->thr_errdepth = 0; 46*f6e214c7SGavin Maltby tp->thr_isdoor = 0; 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&fmd.d_thr_lock); 497c478bd9Sstevel@tonic-gate fmd_list_append(&fmd.d_thr_list, tp); 507c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&fmd.d_thr_lock); 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate return (tp); 537c478bd9Sstevel@tonic-gate } 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate static void * 567c478bd9Sstevel@tonic-gate fmd_thread_start(void *arg) 577c478bd9Sstevel@tonic-gate { 587c478bd9Sstevel@tonic-gate fmd_thread_t *tp = arg; 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate if (pthread_setspecific(fmd.d_key, tp) != 0) 617c478bd9Sstevel@tonic-gate fmd_panic("failed to initialize thread key to %p", arg); 627c478bd9Sstevel@tonic-gate 63*f6e214c7SGavin Maltby if (!tp->thr_isdoor) { 647c478bd9Sstevel@tonic-gate (void) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); 657c478bd9Sstevel@tonic-gate (void) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); 66*f6e214c7SGavin Maltby } 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate tp->thr_func(tp->thr_arg); 697c478bd9Sstevel@tonic-gate return (NULL); 707c478bd9Sstevel@tonic-gate } 717c478bd9Sstevel@tonic-gate 72*f6e214c7SGavin Maltby static fmd_thread_t * 73*f6e214c7SGavin Maltby fmd_thread_create_cmn(fmd_module_t *mp, fmd_thread_f *func, void *arg, 74*f6e214c7SGavin Maltby int isdoor) 757c478bd9Sstevel@tonic-gate { 767c478bd9Sstevel@tonic-gate fmd_thread_t *tp = fmd_alloc(sizeof (fmd_thread_t), FMD_SLEEP); 777c478bd9Sstevel@tonic-gate sigset_t oset, nset; 787c478bd9Sstevel@tonic-gate int err; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate tp->thr_mod = mp; 817c478bd9Sstevel@tonic-gate tp->thr_func = func; 827c478bd9Sstevel@tonic-gate tp->thr_arg = arg; 837c478bd9Sstevel@tonic-gate tp->thr_trdata = fmd_trace_create(); 847c478bd9Sstevel@tonic-gate tp->thr_trfunc = (fmd_tracebuf_f *)fmd.d_thr_trace; 857c478bd9Sstevel@tonic-gate tp->thr_errdepth = 0; 86*f6e214c7SGavin Maltby tp->thr_isdoor = isdoor; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate (void) sigfillset(&nset); 897c478bd9Sstevel@tonic-gate (void) sigdelset(&nset, SIGABRT); /* always unblocked for fmd_panic() */ 90*f6e214c7SGavin Maltby if (!isdoor) 91*f6e214c7SGavin Maltby (void) sigdelset(&nset, fmd.d_thr_sig); /* fmd_thr_signal() */ 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &nset, &oset); 947c478bd9Sstevel@tonic-gate err = pthread_create(&tp->thr_tid, NULL, fmd_thread_start, tp); 957c478bd9Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &oset, NULL); 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate if (err != 0) { 987c478bd9Sstevel@tonic-gate fmd_free(tp, sizeof (fmd_thread_t)); 997c478bd9Sstevel@tonic-gate return (NULL); 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&fmd.d_thr_lock); 1037c478bd9Sstevel@tonic-gate fmd_list_append(&fmd.d_thr_list, tp); 1047c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&fmd.d_thr_lock); 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate return (tp); 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 109*f6e214c7SGavin Maltby fmd_thread_t * 110*f6e214c7SGavin Maltby fmd_thread_create(fmd_module_t *mp, fmd_thread_f *func, void *arg) 111*f6e214c7SGavin Maltby { 112*f6e214c7SGavin Maltby return (fmd_thread_create_cmn(mp, func, arg, 0)); 113*f6e214c7SGavin Maltby } 114*f6e214c7SGavin Maltby 115*f6e214c7SGavin Maltby fmd_thread_t * 116*f6e214c7SGavin Maltby fmd_doorthread_create(fmd_module_t *mp, fmd_thread_f *func, void *arg) 117*f6e214c7SGavin Maltby { 118*f6e214c7SGavin Maltby return (fmd_thread_create_cmn(mp, func, arg, 1)); 119*f6e214c7SGavin Maltby } 120*f6e214c7SGavin Maltby 1217c478bd9Sstevel@tonic-gate void 1227c478bd9Sstevel@tonic-gate fmd_thread_destroy(fmd_thread_t *tp, int flag) 1237c478bd9Sstevel@tonic-gate { 1247c478bd9Sstevel@tonic-gate if (flag == FMD_THREAD_JOIN && tp->thr_tid != pthread_self() && 1257c478bd9Sstevel@tonic-gate pthread_join(tp->thr_tid, NULL) != 0) { 1267c478bd9Sstevel@tonic-gate fmd_error(EFMD_MOD_JOIN, "failed to join thread for module " 1277c478bd9Sstevel@tonic-gate "%s (tid %u)\n", tp->thr_mod->mod_name, tp->thr_tid); 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&fmd.d_thr_lock); 1317c478bd9Sstevel@tonic-gate fmd_list_delete(&fmd.d_thr_list, tp); 1327c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&fmd.d_thr_lock); 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate fmd_trace_destroy(tp->thr_trdata); 1357c478bd9Sstevel@tonic-gate fmd_free(tp, sizeof (fmd_thread_t)); 1367c478bd9Sstevel@tonic-gate } 137