xref: /onnv-gate/usr/src/uts/common/os/callb.c (revision 11066:cebb50cbe4f9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55295Srandyf  * Common Development and Distribution License (the "License").
65295Srandyf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11066Srafael.vanoni@sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/t_lock.h>
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/time.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <sys/systm.h>
320Sstevel@tonic-gate #include <sys/cpuvar.h>
330Sstevel@tonic-gate #include <sys/user.h>
340Sstevel@tonic-gate #include <sys/proc.h>
350Sstevel@tonic-gate #include <sys/callb.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/cmn_err.h>
380Sstevel@tonic-gate #include <sys/swap.h>
390Sstevel@tonic-gate #include <sys/vmsystm.h>
400Sstevel@tonic-gate #include <sys/class.h>
410Sstevel@tonic-gate #include <sys/debug.h>
420Sstevel@tonic-gate #include <sys/thread.h>
430Sstevel@tonic-gate #include <sys/kobj.h>
440Sstevel@tonic-gate #include <sys/ddi.h>	/* for delay() */
450Sstevel@tonic-gate #include <sys/taskq.h>  /* For TASKQ_NAMELEN */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define	CB_MAXNAME	TASKQ_NAMELEN
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * The callb mechanism provides generic event scheduling/echoing.
510Sstevel@tonic-gate  * A callb function is registered and called on behalf of the event.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate typedef struct callb {
540Sstevel@tonic-gate 	struct callb	*c_next; 	/* next in class or on freelist */
550Sstevel@tonic-gate 	kthread_id_t	c_thread;	/* ptr to caller's thread struct */
560Sstevel@tonic-gate 	char		c_flag;		/* info about the callb state */
570Sstevel@tonic-gate 	uchar_t		c_class;	/* this callb's class */
580Sstevel@tonic-gate 	kcondvar_t	c_done_cv;	/* signal callb completion */
590Sstevel@tonic-gate 	boolean_t	(*c_func)();	/* cb function: returns true if ok */
600Sstevel@tonic-gate 	void		*c_arg;		/* arg to c_func */
610Sstevel@tonic-gate 	char		c_name[CB_MAXNAME+1]; /* debug:max func name length */
620Sstevel@tonic-gate } callb_t;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate  * callb c_flag bitmap definitions
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate #define	CALLB_FREE		0x0
680Sstevel@tonic-gate #define	CALLB_TAKEN		0x1
690Sstevel@tonic-gate #define	CALLB_EXECUTING		0x2
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * Basic structure for a callb table.
730Sstevel@tonic-gate  * All callbs are organized into different class groups described
740Sstevel@tonic-gate  * by ct_class array.
750Sstevel@tonic-gate  * The callbs within a class are single-linked and normally run by a
760Sstevel@tonic-gate  * serial execution.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate typedef struct callb_table {
790Sstevel@tonic-gate 	kmutex_t ct_lock;		/* protect all callb states */
800Sstevel@tonic-gate 	callb_t	*ct_freelist; 		/* free callb structures */
810Sstevel@tonic-gate 	int	ct_busy;		/* != 0 prevents additions */
820Sstevel@tonic-gate 	kcondvar_t ct_busy_cv;		/* to wait for not busy    */
830Sstevel@tonic-gate 	int	ct_ncallb; 		/* num of callbs allocated */
840Sstevel@tonic-gate 	callb_t	*ct_first_cb[NCBCLASS];	/* ptr to 1st callb in a class */
850Sstevel@tonic-gate } callb_table_t;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate int callb_timeout_sec = CPR_KTHREAD_TIMEOUT_SEC;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static callb_id_t callb_add_common(boolean_t (*)(void *, int),
900Sstevel@tonic-gate     void *, int, char *, kthread_id_t);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static callb_table_t callb_table;	/* system level callback table */
930Sstevel@tonic-gate static callb_table_t *ct = &callb_table;
940Sstevel@tonic-gate static kmutex_t	callb_safe_mutex;
950Sstevel@tonic-gate callb_cpr_t	callb_cprinfo_safe = {
960Sstevel@tonic-gate 	&callb_safe_mutex, CALLB_CPR_ALWAYS_SAFE, 0, 0, 0 };
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * Init all callb tables in the system.
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate void
callb_init()1020Sstevel@tonic-gate callb_init()
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	callb_table.ct_busy = 0;	/* mark table open for additions */
1050Sstevel@tonic-gate 	mutex_init(&callb_safe_mutex, NULL, MUTEX_DEFAULT, NULL);
1060Sstevel@tonic-gate 	mutex_init(&callb_table.ct_lock, NULL, MUTEX_DEFAULT, NULL);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * callout_add() is called to register func() be called later.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate static callb_id_t
callb_add_common(boolean_t (* func)(void * arg,int code),void * arg,int class,char * name,kthread_id_t t)1130Sstevel@tonic-gate callb_add_common(boolean_t (*func)(void *arg, int code),
1140Sstevel@tonic-gate     void *arg, int class, char *name, kthread_id_t t)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	callb_t *cp;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	ASSERT(class < NCBCLASS);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
1210Sstevel@tonic-gate 	while (ct->ct_busy)
1220Sstevel@tonic-gate 		cv_wait(&ct->ct_busy_cv, &ct->ct_lock);
1230Sstevel@tonic-gate 	if ((cp = ct->ct_freelist) == NULL) {
1240Sstevel@tonic-gate 		ct->ct_ncallb++;
1250Sstevel@tonic-gate 		cp = (callb_t *)kmem_zalloc(sizeof (callb_t), KM_SLEEP);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 	ct->ct_freelist = cp->c_next;
1280Sstevel@tonic-gate 	cp->c_thread = t;
1290Sstevel@tonic-gate 	cp->c_func = func;
1300Sstevel@tonic-gate 	cp->c_arg = arg;
1310Sstevel@tonic-gate 	cp->c_class = (uchar_t)class;
1320Sstevel@tonic-gate 	cp->c_flag |= CALLB_TAKEN;
1330Sstevel@tonic-gate #ifdef DEBUG
1340Sstevel@tonic-gate 	if (strlen(name) > CB_MAXNAME)
1350Sstevel@tonic-gate 		cmn_err(CE_WARN, "callb_add: name of callback function '%s' "
1360Sstevel@tonic-gate 		    "too long -- truncated to %d chars",
1370Sstevel@tonic-gate 		    name, CB_MAXNAME);
1380Sstevel@tonic-gate #endif
1390Sstevel@tonic-gate 	(void) strncpy(cp->c_name, name, CB_MAXNAME);
1400Sstevel@tonic-gate 	cp->c_name[CB_MAXNAME] = '\0';
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/*
1430Sstevel@tonic-gate 	 * Insert the new callb at the head of its class list.
1440Sstevel@tonic-gate 	 */
1450Sstevel@tonic-gate 	cp->c_next = ct->ct_first_cb[class];
1460Sstevel@tonic-gate 	ct->ct_first_cb[class] = cp;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
1490Sstevel@tonic-gate 	return ((callb_id_t)cp);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * The default function to add an entry to the callback table.  Since
1540Sstevel@tonic-gate  * it uses curthread as the thread identifier to store in the table,
1550Sstevel@tonic-gate  * it should be used for the normal case of a thread which is calling
1560Sstevel@tonic-gate  * to add ITSELF to the table.
1570Sstevel@tonic-gate  */
1580Sstevel@tonic-gate callb_id_t
callb_add(boolean_t (* func)(void * arg,int code),void * arg,int class,char * name)1590Sstevel@tonic-gate callb_add(boolean_t (*func)(void *arg, int code),
1600Sstevel@tonic-gate     void *arg, int class, char *name)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	return (callb_add_common(func, arg, class, name, curthread));
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate  * A special version of callb_add() above for use by threads which
1670Sstevel@tonic-gate  * might be adding an entry to the table on behalf of some other
1680Sstevel@tonic-gate  * thread (for example, one which is constructed but not yet running).
1690Sstevel@tonic-gate  * In this version the thread id is an argument.
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate callb_id_t
callb_add_thread(boolean_t (* func)(void * arg,int code),void * arg,int class,char * name,kthread_id_t t)1720Sstevel@tonic-gate callb_add_thread(boolean_t (*func)(void *arg, int code),
1730Sstevel@tonic-gate     void *arg, int class, char *name, kthread_id_t t)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	return (callb_add_common(func, arg, class, name, t));
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate  * callout_delete() is called to remove an entry identified by id
1800Sstevel@tonic-gate  * that was originally placed there by a call to callout_add().
1810Sstevel@tonic-gate  * return -1 if fail to delete a callb entry otherwise return 0.
1820Sstevel@tonic-gate  */
1830Sstevel@tonic-gate int
callb_delete(callb_id_t id)1840Sstevel@tonic-gate callb_delete(callb_id_t id)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	callb_t **pp;
1870Sstevel@tonic-gate 	callb_t *me = (callb_t *)id;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	for (;;) {
1920Sstevel@tonic-gate 		pp = &ct->ct_first_cb[me->c_class];
1930Sstevel@tonic-gate 		while (*pp != NULL && *pp != me)
1940Sstevel@tonic-gate 			pp = &(*pp)->c_next;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate #ifdef DEBUG
1970Sstevel@tonic-gate 		if (*pp != me) {
1980Sstevel@tonic-gate 			cmn_err(CE_WARN, "callb delete bogus entry 0x%p",
1990Sstevel@tonic-gate 			    (void *)me);
2000Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
2010Sstevel@tonic-gate 			return (-1);
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate #endif /* DEBUG */
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		/*
2060Sstevel@tonic-gate 		 * It is not allowed to delete a callb in the middle of
2070Sstevel@tonic-gate 		 * executing otherwise, the callb_execute() will be confused.
2080Sstevel@tonic-gate 		 */
2090Sstevel@tonic-gate 		if (!(me->c_flag & CALLB_EXECUTING))
2100Sstevel@tonic-gate 			break;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		cv_wait(&me->c_done_cv, &ct->ct_lock);
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 	/* relink the class list */
2150Sstevel@tonic-gate 	*pp = me->c_next;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/* clean up myself and return the free callb to the head of freelist */
2180Sstevel@tonic-gate 	me->c_flag = CALLB_FREE;
2190Sstevel@tonic-gate 	me->c_next = ct->ct_freelist;
2200Sstevel@tonic-gate 	ct->ct_freelist = me;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
2230Sstevel@tonic-gate 	return (0);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate  * class:	indicates to execute all callbs in the same class;
2280Sstevel@tonic-gate  * code:	optional argument for the callb functions.
2290Sstevel@tonic-gate  * return:	 = 0: success
2300Sstevel@tonic-gate  *		!= 0: ptr to string supplied when callback was registered
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate void *
callb_execute_class(int class,int code)2330Sstevel@tonic-gate callb_execute_class(int class, int code)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	callb_t *cp;
2360Sstevel@tonic-gate 	void *ret = NULL;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	ASSERT(class < NCBCLASS);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	for (cp = ct->ct_first_cb[class];
2430Sstevel@tonic-gate 	    cp != NULL && ret == 0; cp = cp->c_next) {
2440Sstevel@tonic-gate 		while (cp->c_flag & CALLB_EXECUTING)
2450Sstevel@tonic-gate 			cv_wait(&cp->c_done_cv, &ct->ct_lock);
2460Sstevel@tonic-gate 		/*
2470Sstevel@tonic-gate 		 * cont if the callb is deleted while we're sleeping
2480Sstevel@tonic-gate 		 */
2490Sstevel@tonic-gate 		if (cp->c_flag == CALLB_FREE)
2500Sstevel@tonic-gate 			continue;
2510Sstevel@tonic-gate 		cp->c_flag |= CALLB_EXECUTING;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate #ifdef CALLB_DEBUG
2540Sstevel@tonic-gate 		printf("callb_execute: name=%s func=%p arg=%p\n",
2555295Srandyf 		    cp->c_name, (void *)cp->c_func, (void *)cp->c_arg);
2560Sstevel@tonic-gate #endif /* CALLB_DEBUG */
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
2590Sstevel@tonic-gate 		/* If callback function fails, pass back client's name */
2600Sstevel@tonic-gate 		if (!(*cp->c_func)(cp->c_arg, code))
2610Sstevel@tonic-gate 			ret = cp->c_name;
2620Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		cp->c_flag &= ~CALLB_EXECUTING;
2650Sstevel@tonic-gate 		cv_broadcast(&cp->c_done_cv);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
2680Sstevel@tonic-gate 	return (ret);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate /*
2720Sstevel@tonic-gate  * callers make sure no recursive entries to this func.
2730Sstevel@tonic-gate  * dp->cc_lockp is registered by callb_add to protect callb_cpr_t structure.
2740Sstevel@tonic-gate  *
2750Sstevel@tonic-gate  * When calling to stop a kernel thread (code == CB_CODE_CPR_CHKPT) we
2760Sstevel@tonic-gate  * use a cv_timedwait() in case the kernel thread is blocked.
2770Sstevel@tonic-gate  *
2780Sstevel@tonic-gate  * Note that this is a generic callback handler for daemon CPR and
2790Sstevel@tonic-gate  * should NOT be changed to accommodate any specific requirement in a daemon.
2800Sstevel@tonic-gate  * Individual daemons that require changes to the handler shall write
2810Sstevel@tonic-gate  * callback routines in their own daemon modules.
2820Sstevel@tonic-gate  */
2830Sstevel@tonic-gate boolean_t
callb_generic_cpr(void * arg,int code)2840Sstevel@tonic-gate callb_generic_cpr(void *arg, int code)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	callb_cpr_t *cp = (callb_cpr_t *)arg;
2870Sstevel@tonic-gate 	clock_t ret = 0;			/* assume success */
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	mutex_enter(cp->cc_lockp);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	switch (code) {
2920Sstevel@tonic-gate 	case CB_CODE_CPR_CHKPT:
2930Sstevel@tonic-gate 		cp->cc_events |= CALLB_CPR_START;
2945295Srandyf #ifdef CPR_NOT_THREAD_SAFE
2950Sstevel@tonic-gate 		while (!(cp->cc_events & CALLB_CPR_SAFE))
2960Sstevel@tonic-gate 			/* cv_timedwait() returns -1 if it times out. */
297*11066Srafael.vanoni@sun.com 			if ((ret = cv_reltimedwait(&cp->cc_callb_cv,
298*11066Srafael.vanoni@sun.com 			    cp->cc_lockp, (callb_timeout_sec * hz),
299*11066Srafael.vanoni@sun.com 			    TR_CLOCK_TICK)) == -1)
3000Sstevel@tonic-gate 				break;
3015295Srandyf #endif
3020Sstevel@tonic-gate 		break;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	case CB_CODE_CPR_RESUME:
3050Sstevel@tonic-gate 		cp->cc_events &= ~CALLB_CPR_START;
3060Sstevel@tonic-gate 		cv_signal(&cp->cc_stop_cv);
3070Sstevel@tonic-gate 		break;
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 	mutex_exit(cp->cc_lockp);
3100Sstevel@tonic-gate 	return (ret != -1);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate  * The generic callback function associated with kernel threads which
3150Sstevel@tonic-gate  * are always considered safe.
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate /* ARGSUSED */
3180Sstevel@tonic-gate boolean_t
callb_generic_cpr_safe(void * arg,int code)3190Sstevel@tonic-gate callb_generic_cpr_safe(void *arg, int code)
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate 	return (B_TRUE);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate /*
3240Sstevel@tonic-gate  * Prevent additions to callback table.
3250Sstevel@tonic-gate  */
3260Sstevel@tonic-gate void
callb_lock_table(void)3270Sstevel@tonic-gate callb_lock_table(void)
3280Sstevel@tonic-gate {
3290Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
3300Sstevel@tonic-gate 	ASSERT(ct->ct_busy == 0);
3310Sstevel@tonic-gate 	ct->ct_busy = 1;
3320Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate /*
3360Sstevel@tonic-gate  * Allow additions to callback table.
3370Sstevel@tonic-gate  */
3380Sstevel@tonic-gate void
callb_unlock_table(void)3390Sstevel@tonic-gate callb_unlock_table(void)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
3420Sstevel@tonic-gate 	ASSERT(ct->ct_busy != 0);
3430Sstevel@tonic-gate 	ct->ct_busy = 0;
3440Sstevel@tonic-gate 	cv_broadcast(&ct->ct_busy_cv);
3450Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate /*
3490Sstevel@tonic-gate  * Return a boolean value indicating whether a particular kernel thread is
3500Sstevel@tonic-gate  * stopped in accordance with the cpr callback protocol.  If returning
3510Sstevel@tonic-gate  * false, also return a pointer to the thread name via the 2nd argument.
3520Sstevel@tonic-gate  */
3530Sstevel@tonic-gate boolean_t
callb_is_stopped(kthread_id_t tp,caddr_t * thread_name)3540Sstevel@tonic-gate callb_is_stopped(kthread_id_t tp, caddr_t *thread_name)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate 	callb_t *cp;
3570Sstevel@tonic-gate 	boolean_t ret_val;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	for (cp = ct->ct_first_cb[CB_CL_CPR_DAEMON];
3620Sstevel@tonic-gate 	    cp != NULL && tp != cp->c_thread; cp = cp->c_next)
3630Sstevel@tonic-gate 		;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	ret_val = (cp != NULL);
3660Sstevel@tonic-gate 	if (ret_val) {
3670Sstevel@tonic-gate 		/*
3680Sstevel@tonic-gate 		 * We found the thread in the callback table and have
3690Sstevel@tonic-gate 		 * provisionally set the return value to true.  Now
3700Sstevel@tonic-gate 		 * see if it is marked "safe" and is sleeping or stopped.
3710Sstevel@tonic-gate 		 */
3720Sstevel@tonic-gate 		callb_cpr_t *ccp = (callb_cpr_t *)cp->c_arg;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 		*thread_name = cp->c_name;	/* in case not stopped */
3750Sstevel@tonic-gate 		mutex_enter(ccp->cc_lockp);
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		if (ccp->cc_events & CALLB_CPR_SAFE) {
3780Sstevel@tonic-gate 			int retry;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 			mutex_exit(ccp->cc_lockp);
3810Sstevel@tonic-gate 			for (retry = 0; retry < CALLB_MAX_RETRY; retry++) {
3820Sstevel@tonic-gate 				thread_lock(tp);
3830Sstevel@tonic-gate 				if (tp->t_state & (TS_SLEEP | TS_STOPPED)) {
3840Sstevel@tonic-gate 					thread_unlock(tp);
3850Sstevel@tonic-gate 					break;
3860Sstevel@tonic-gate 				}
3870Sstevel@tonic-gate 				thread_unlock(tp);
3880Sstevel@tonic-gate 				delay(CALLB_THREAD_DELAY);
3890Sstevel@tonic-gate 			}
3900Sstevel@tonic-gate 			ret_val = retry < CALLB_MAX_RETRY;
3910Sstevel@tonic-gate 		} else {
3920Sstevel@tonic-gate 			ret_val =
3930Sstevel@tonic-gate 			    (ccp->cc_events & CALLB_CPR_ALWAYS_SAFE) != 0;
3940Sstevel@tonic-gate 			mutex_exit(ccp->cc_lockp);
3950Sstevel@tonic-gate 		}
3960Sstevel@tonic-gate 	} else {
3970Sstevel@tonic-gate 		/*
3980Sstevel@tonic-gate 		 * Thread not found in callback table.  Make the best
3990Sstevel@tonic-gate 		 * attempt to identify the thread in the error message.
4000Sstevel@tonic-gate 		 */
4010Sstevel@tonic-gate 		ulong_t offset;
4020Sstevel@tonic-gate 		char *sym = kobj_getsymname((uintptr_t)tp->t_startpc,
4030Sstevel@tonic-gate 		    &offset);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 		*thread_name = sym ? sym : "*unknown*";
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
4090Sstevel@tonic-gate 	return (ret_val);
4100Sstevel@tonic-gate }
411