8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / fm / fmd / common / fmd_thread.c
blobcb0cb0745d2a1ee69595f0e58f67ba8746eabf4b
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <signal.h>
27 #include <fmd_alloc.h>
28 #include <fmd_thread.h>
29 #include <fmd_module.h>
30 #include <fmd_error.h>
31 #include <fmd_subr.h>
32 #include <fmd.h>
34 fmd_thread_t *
35 fmd_thread_xcreate(fmd_module_t *mp, pthread_t tid)
37 fmd_thread_t *tp = fmd_alloc(sizeof (fmd_thread_t), FMD_SLEEP);
39 tp->thr_mod = mp;
40 tp->thr_tid = tid;
41 tp->thr_func = NULL;
42 tp->thr_arg = NULL;
43 tp->thr_trdata = fmd_trace_create();
44 tp->thr_trfunc = (fmd_tracebuf_f *)fmd.d_thr_trace;
45 tp->thr_errdepth = 0;
46 tp->thr_isdoor = 0;
48 (void) pthread_mutex_lock(&fmd.d_thr_lock);
49 fmd_list_append(&fmd.d_thr_list, tp);
50 (void) pthread_mutex_unlock(&fmd.d_thr_lock);
52 return (tp);
55 static void *
56 fmd_thread_start(void *arg)
58 fmd_thread_t *tp = arg;
60 if (pthread_setspecific(fmd.d_key, tp) != 0)
61 fmd_panic("failed to initialize thread key to %p", arg);
63 if (!tp->thr_isdoor) {
64 (void) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
65 (void) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
68 tp->thr_func(tp->thr_arg);
69 return (NULL);
72 static fmd_thread_t *
73 fmd_thread_create_cmn(fmd_module_t *mp, fmd_thread_f *func, void *arg,
74 int isdoor)
76 fmd_thread_t *tp = fmd_alloc(sizeof (fmd_thread_t), FMD_SLEEP);
77 sigset_t oset, nset;
78 int err;
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 tp->thr_isdoor = isdoor;
88 (void) sigfillset(&nset);
89 (void) sigdelset(&nset, SIGABRT); /* always unblocked for fmd_panic() */
90 if (!isdoor)
91 (void) sigdelset(&nset, fmd.d_thr_sig); /* fmd_thr_signal() */
93 (void) pthread_sigmask(SIG_SETMASK, &nset, &oset);
94 err = pthread_create(&tp->thr_tid, NULL, fmd_thread_start, tp);
95 (void) pthread_sigmask(SIG_SETMASK, &oset, NULL);
97 if (err != 0) {
98 fmd_free(tp, sizeof (fmd_thread_t));
99 return (NULL);
102 (void) pthread_mutex_lock(&fmd.d_thr_lock);
103 fmd_list_append(&fmd.d_thr_list, tp);
104 (void) pthread_mutex_unlock(&fmd.d_thr_lock);
106 return (tp);
109 fmd_thread_t *
110 fmd_thread_create(fmd_module_t *mp, fmd_thread_f *func, void *arg)
112 return (fmd_thread_create_cmn(mp, func, arg, 0));
115 fmd_thread_t *
116 fmd_doorthread_create(fmd_module_t *mp, fmd_thread_f *func, void *arg)
118 return (fmd_thread_create_cmn(mp, func, arg, 1));
121 void
122 fmd_thread_destroy(fmd_thread_t *tp, int flag)
124 if (flag == FMD_THREAD_JOIN && tp->thr_tid != pthread_self() &&
125 pthread_join(tp->thr_tid, NULL) != 0) {
126 fmd_error(EFMD_MOD_JOIN, "failed to join thread for module "
127 "%s (tid %u)\n", tp->thr_mod->mod_name, tp->thr_tid);
130 (void) pthread_mutex_lock(&fmd.d_thr_lock);
131 fmd_list_delete(&fmd.d_thr_list, tp);
132 (void) pthread_mutex_unlock(&fmd.d_thr_lock);
134 fmd_trace_destroy(tp->thr_trdata);
135 fmd_free(tp, sizeof (fmd_thread_t));