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