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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/param.h>
29 #include <sys/types.h>
31 #include <sys/sysmacros.h>
32 #include <sys/systm.h>
34 #include <sys/mutex.h>
35 #include <sys/condvar.h>
36 #include <sys/callb.h>
38 #include <sys/cmn_err.h>
39 #include <sys/debug.h>
41 #include <sys/systm.h> /* for delay() */
42 #include <sys/taskq.h> /* For TASKQ_NAMELEN */
43 #include <sys/kernel.h>
45 #define CB_MAXNAME TASKQ_NAMELEN
48 * The callb mechanism provides generic event scheduling/echoing.
49 * A callb function is registered and called on behalf of the event.
51 typedef struct callb {
52 struct callb *c_next; /* next in class or on freelist */
53 kthread_id_t c_thread; /* ptr to caller's thread struct */
54 char c_flag; /* info about the callb state */
55 uchar_t c_class; /* this callb's class */
56 kcondvar_t c_done_cv; /* signal callb completion */
57 boolean_t (*c_func)(); /* cb function: returns true if ok */
58 void *c_arg; /* arg to c_func */
59 char c_name[CB_MAXNAME+1]; /* debug:max func name length */
63 * callb c_flag bitmap definitions
65 #define CALLB_FREE 0x0
66 #define CALLB_TAKEN 0x1
67 #define CALLB_EXECUTING 0x2
70 * Basic structure for a callb table.
71 * All callbs are organized into different class groups described
73 * The callbs within a class are single-linked and normally run by a
76 typedef struct callb_table {
77 kmutex_t ct_lock; /* protect all callb states */
78 callb_t *ct_freelist; /* free callb structures */
79 int ct_busy; /* != 0 prevents additions */
80 kcondvar_t ct_busy_cv; /* to wait for not busy */
81 int ct_ncallb; /* num of callbs allocated */
82 callb_t *ct_first_cb[NCBCLASS]; /* ptr to 1st callb in a class */
85 int callb_timeout_sec = CPR_KTHREAD_TIMEOUT_SEC;
87 static callb_id_t callb_add_common(boolean_t (*)(void *, int),
88 void *, int, char *, kthread_id_t);
90 static callb_table_t callb_table; /* system level callback table */
91 static callb_table_t *ct = &callb_table;
92 static kmutex_t callb_safe_mutex;
93 callb_cpr_t callb_cprinfo_safe = {
94 &callb_safe_mutex, CALLB_CPR_ALWAYS_SAFE, 0, 0, 0 };
97 * Init all callb tables in the system.
100 callb_init(void *dummy __unused)
102 callb_table.ct_busy = 0; /* mark table open for additions */
103 mutex_init(&callb_safe_mutex, NULL, MUTEX_DEFAULT, NULL);
104 mutex_init(&callb_table.ct_lock, NULL, MUTEX_DEFAULT, NULL);
108 callb_fini(void *dummy __unused)
113 mutex_enter(&ct->ct_lock);
114 for (i = 0; i < 16; i++) {
115 while ((cp = ct->ct_freelist) != NULL) {
116 ct->ct_freelist = cp->c_next;
118 kmem_free(cp, sizeof (callb_t));
120 if (ct->ct_ncallb == 0)
122 /* Not all callbacks finished, waiting for the rest. */
123 mutex_exit(&ct->ct_lock);
124 tsleep(ct, 0, "callb", hz / 4);
125 mutex_enter(&ct->ct_lock);
127 if (ct->ct_ncallb > 0)
128 printf("%s: Leaked %d callbacks!\n", __func__, ct->ct_ncallb);
129 mutex_exit(&ct->ct_lock);
130 mutex_destroy(&callb_safe_mutex);
131 mutex_destroy(&callb_table.ct_lock);
135 * callout_add() is called to register func() be called later.
138 callb_add_common(boolean_t (*func)(void *arg, int code),
139 void *arg, int class, char *name, kthread_id_t t)
143 ASSERT(class < NCBCLASS);
145 mutex_enter(&ct->ct_lock);
147 cv_wait(&ct->ct_busy_cv, &ct->ct_lock);
148 if ((cp = ct->ct_freelist) == NULL) {
150 cp = (callb_t *)kmem_zalloc(sizeof (callb_t), KM_SLEEP);
152 ct->ct_freelist = cp->c_next;
156 cp->c_class = (uchar_t)class;
157 cp->c_flag |= CALLB_TAKEN;
159 if (strlen(name) > CB_MAXNAME)
160 cmn_err(CE_WARN, "callb_add: name of callback function '%s' "
161 "too long -- truncated to %d chars",
164 (void) strncpy(cp->c_name, name, CB_MAXNAME);
165 cp->c_name[CB_MAXNAME] = '\0';
168 * Insert the new callb at the head of its class list.
170 cp->c_next = ct->ct_first_cb[class];
171 ct->ct_first_cb[class] = cp;
173 mutex_exit(&ct->ct_lock);
174 return ((callb_id_t)cp);
178 * The default function to add an entry to the callback table. Since
179 * it uses curthread as the thread identifier to store in the table,
180 * it should be used for the normal case of a thread which is calling
181 * to add ITSELF to the table.
184 callb_add(boolean_t (*func)(void *arg, int code),
185 void *arg, int class, char *name)
187 return (callb_add_common(func, arg, class, name, curthread));
191 * A special version of callb_add() above for use by threads which
192 * might be adding an entry to the table on behalf of some other
193 * thread (for example, one which is constructed but not yet running).
194 * In this version the thread id is an argument.
197 callb_add_thread(boolean_t (*func)(void *arg, int code),
198 void *arg, int class, char *name, kthread_id_t t)
200 return (callb_add_common(func, arg, class, name, t));
204 * callout_delete() is called to remove an entry identified by id
205 * that was originally placed there by a call to callout_add().
206 * return -1 if fail to delete a callb entry otherwise return 0.
209 callb_delete(callb_id_t id)
212 callb_t *me = (callb_t *)id;
214 mutex_enter(&ct->ct_lock);
217 pp = &ct->ct_first_cb[me->c_class];
218 while (*pp != NULL && *pp != me)
223 cmn_err(CE_WARN, "callb delete bogus entry 0x%p",
225 mutex_exit(&ct->ct_lock);
231 * It is not allowed to delete a callb in the middle of
232 * executing otherwise, the callb_execute() will be confused.
234 if (!(me->c_flag & CALLB_EXECUTING))
237 cv_wait(&me->c_done_cv, &ct->ct_lock);
239 /* relink the class list */
242 /* clean up myself and return the free callb to the head of freelist */
243 me->c_flag = CALLB_FREE;
244 me->c_next = ct->ct_freelist;
245 ct->ct_freelist = me;
247 mutex_exit(&ct->ct_lock);
252 * class: indicates to execute all callbs in the same class;
253 * code: optional argument for the callb functions.
254 * return: = 0: success
255 * != 0: ptr to string supplied when callback was registered
258 callb_execute_class(int class, int code)
263 ASSERT(class < NCBCLASS);
265 mutex_enter(&ct->ct_lock);
267 for (cp = ct->ct_first_cb[class];
268 cp != NULL && ret == 0; cp = cp->c_next) {
269 while (cp->c_flag & CALLB_EXECUTING)
270 cv_wait(&cp->c_done_cv, &ct->ct_lock);
272 * cont if the callb is deleted while we're sleeping
274 if (cp->c_flag == CALLB_FREE)
276 cp->c_flag |= CALLB_EXECUTING;
279 printf("callb_execute: name=%s func=%p arg=%p\n",
280 cp->c_name, (void *)cp->c_func, (void *)cp->c_arg);
281 #endif /* CALLB_DEBUG */
283 mutex_exit(&ct->ct_lock);
284 /* If callback function fails, pass back client's name */
285 if (!(*cp->c_func)(cp->c_arg, code))
287 mutex_enter(&ct->ct_lock);
289 cp->c_flag &= ~CALLB_EXECUTING;
290 cv_broadcast(&cp->c_done_cv);
292 mutex_exit(&ct->ct_lock);
297 * callers make sure no recursive entries to this func.
298 * dp->cc_lockp is registered by callb_add to protect callb_cpr_t structure.
300 * When calling to stop a kernel thread (code == CB_CODE_CPR_CHKPT) we
301 * use a cv_timedwait() in case the kernel thread is blocked.
303 * Note that this is a generic callback handler for daemon CPR and
304 * should NOT be changed to accommodate any specific requirement in a daemon.
305 * Individual daemons that require changes to the handler shall write
306 * callback routines in their own daemon modules.
309 callb_generic_cpr(void *arg, int code)
311 callb_cpr_t *cp = (callb_cpr_t *)arg;
312 clock_t ret = 0; /* assume success */
314 mutex_enter(cp->cc_lockp);
317 case CB_CODE_CPR_CHKPT:
318 cp->cc_events |= CALLB_CPR_START;
319 #ifdef CPR_NOT_THREAD_SAFE
320 while (!(cp->cc_events & CALLB_CPR_SAFE))
321 /* cv_timedwait() returns -1 if it times out. */
322 if ((ret = cv_timedwait(&cp->cc_callb_cv,
324 callb_timeout_sec * hz)) == -1)
329 case CB_CODE_CPR_RESUME:
330 cp->cc_events &= ~CALLB_CPR_START;
331 cv_signal(&cp->cc_stop_cv);
334 mutex_exit(cp->cc_lockp);
339 * The generic callback function associated with kernel threads which
340 * are always considered safe.
344 callb_generic_cpr_safe(void *arg, int code)
349 * Prevent additions to callback table.
352 callb_lock_table(void)
354 mutex_enter(&ct->ct_lock);
355 ASSERT(ct->ct_busy == 0);
357 mutex_exit(&ct->ct_lock);
361 * Allow additions to callback table.
364 callb_unlock_table(void)
366 mutex_enter(&ct->ct_lock);
367 ASSERT(ct->ct_busy != 0);
369 cv_broadcast(&ct->ct_busy_cv);
370 mutex_exit(&ct->ct_lock);
373 SYSINIT(sol_callb, SI_SUB_DRIVERS, SI_ORDER_FIRST, callb_init, NULL);
374 SYSUNINIT(sol_callb, SI_SUB_DRIVERS, SI_ORDER_FIRST, callb_fini, NULL);