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]
22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
27 #include <fmd_alloc.h>
28 #include <fmd_thread.h>
29 #include <fmd_module.h>
30 #include <fmd_error.h>
35 fmd_thread_xcreate(fmd_module_t
*mp
, pthread_t tid
)
37 fmd_thread_t
*tp
= fmd_alloc(sizeof (fmd_thread_t
), FMD_SLEEP
);
43 tp
->thr_trdata
= fmd_trace_create();
44 tp
->thr_trfunc
= (fmd_tracebuf_f
*)fmd
.d_thr_trace
;
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
);
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
);
73 fmd_thread_create_cmn(fmd_module_t
*mp
, fmd_thread_f
*func
, void *arg
,
76 fmd_thread_t
*tp
= fmd_alloc(sizeof (fmd_thread_t
), FMD_SLEEP
);
83 tp
->thr_trdata
= fmd_trace_create();
84 tp
->thr_trfunc
= (fmd_tracebuf_f
*)fmd
.d_thr_trace
;
86 tp
->thr_isdoor
= isdoor
;
88 (void) sigfillset(&nset
);
89 (void) sigdelset(&nset
, SIGABRT
); /* always unblocked for fmd_panic() */
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
);
98 fmd_free(tp
, sizeof (fmd_thread_t
));
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
);
110 fmd_thread_create(fmd_module_t
*mp
, fmd_thread_f
*func
, void *arg
)
112 return (fmd_thread_create_cmn(mp
, func
, arg
, 0));
116 fmd_doorthread_create(fmd_module_t
*mp
, fmd_thread_f
*func
, void *arg
)
118 return (fmd_thread_create_cmn(mp
, func
, arg
, 1));
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
));