xref: /onnv-gate/usr/src/uts/common/os/serializer.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Kernel protection serializers: general purpose synchronization mechanism.
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * Serializers provide a simple way to serialize access to some resource. They
33*0Sstevel@tonic-gate  * can be used as an alternative to locks or STREAMS perimeters. They scale
34*0Sstevel@tonic-gate  * much better than STREAMS outer serializers.
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * Serializer is an abstraction that guarantees that all functions executed
37*0Sstevel@tonic-gate  * within the serializer are serialized: they are executed in the order they
38*0Sstevel@tonic-gate  * entered serializer one at a time.
39*0Sstevel@tonic-gate  *
40*0Sstevel@tonic-gate  * INTERFACES:
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  * serializer_t *serializer_create(flags);
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  *	Create a serializer. The flags may be either SER_SLEEP or SER_NOSLEEP
45*0Sstevel@tonic-gate  *	which are the same as KM_SLEEP and KM_NOSLEEP respectively.
46*0Sstevel@tonic-gate  *
47*0Sstevel@tonic-gate  * serializer_enter(serializer, proc, mblk, arg);
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  *	Execute 'proc(mblk, arg)' within the serializer.
50*0Sstevel@tonic-gate  *
51*0Sstevel@tonic-gate  * serializer_wait(serializer);
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  *	Wait for pending serializer jobs to complete. This function should never
54*0Sstevel@tonic-gate  *	be called within the serializer or it will deadlock.
55*0Sstevel@tonic-gate  *
56*0Sstevel@tonic-gate  * serializer_destroy(serializer);
57*0Sstevel@tonic-gate  *
58*0Sstevel@tonic-gate  *	Destroy serializer.
59*0Sstevel@tonic-gate  *
60*0Sstevel@tonic-gate  * Serializers export three DTrace SDT probes:
61*0Sstevel@tonic-gate  *
62*0Sstevel@tonic-gate  *	serializer-enqueue(serializer, mblk, arg, proc)
63*0Sstevel@tonic-gate  *
64*0Sstevel@tonic-gate  *		The probe triggers when serializer is busy and the request is
65*0Sstevel@tonic-gate  *		queued.
66*0Sstevel@tonic-gate  *
67*0Sstevel@tonic-gate  *	serializer-exec-start(serializer, mblk, arg, proc)
68*0Sstevel@tonic-gate  *
69*0Sstevel@tonic-gate  *		The probe triggers before the request is executed
70*0Sstevel@tonic-gate  *
71*0Sstevel@tonic-gate  *	serializer-exec-end(serializer, mblk, arg, proc)
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  *		The probe triggers after the request is executed
74*0Sstevel@tonic-gate  *
75*0Sstevel@tonic-gate  *
76*0Sstevel@tonic-gate  * IMPLEMENTATION.
77*0Sstevel@tonic-gate  *
78*0Sstevel@tonic-gate  * Serializer consists of a "owner" and a list of queued jobs. The first thread
79*0Sstevel@tonic-gate  * entering serializer sets the owner and executes its job directly without
80*0Sstevel@tonic-gate  * context switch. Then it processes jobs which may have been enqueued while it
81*0Sstevel@tonic-gate  * was executing a job and drops the owner, leaving the serializer empty.  Any
82*0Sstevel@tonic-gate  * thread entering an owned serializer enqueues its job and returns immediately.
83*0Sstevel@tonic-gate  *
84*0Sstevel@tonic-gate  * Serializer data structure holds several fields used for debugging only. They
85*0Sstevel@tonic-gate  * are not relevant for the proper serializer functioning.
86*0Sstevel@tonic-gate  *
87*0Sstevel@tonic-gate  * When new requests arrive faster then they are processed it is possible that a
88*0Sstevel@tonic-gate  * thread that started processing serializer will continue doing so for a long
89*0Sstevel@tonic-gate  * time. To avoid such pathological behavior the amount of requests drained by
90*0Sstevel@tonic-gate  * serializer_enter() is limited by `serializer_credit' value. After the credit
91*0Sstevel@tonic-gate  * is expired serializer_enter() schedules a taskq request to continue draining.
92*0Sstevel@tonic-gate  * The taskq thread draining is not limited by serializer_credit. Note that it
93*0Sstevel@tonic-gate  * is possible that another serializer_enter() will drain the serializer before
94*0Sstevel@tonic-gate  * a taskq thread will get to it.
95*0Sstevel@tonic-gate  */
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate #include <sys/types.h>
98*0Sstevel@tonic-gate #include <sys/kmem.h>
99*0Sstevel@tonic-gate #include <sys/thread.h>
100*0Sstevel@tonic-gate #include <sys/mutex.h>
101*0Sstevel@tonic-gate #include <sys/systm.h>
102*0Sstevel@tonic-gate #include <sys/debug.h>
103*0Sstevel@tonic-gate #include <sys/taskq.h>
104*0Sstevel@tonic-gate #include <sys/sdt.h>
105*0Sstevel@tonic-gate #include <sys/serializer.h>
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate #define	SERIALIZER_NAMELEN 31
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate  * Serializer abstraction.
111*0Sstevel@tonic-gate  * Fields marked (D) are used for debugging purposes only.
112*0Sstevel@tonic-gate  */
113*0Sstevel@tonic-gate struct serializer_s {
114*0Sstevel@tonic-gate 	kmutex_t	ser_lock;	/* Protects state and the list */
115*0Sstevel@tonic-gate 	kthread_t	*ser_owner;	/* Thread executing serializer */
116*0Sstevel@tonic-gate 	ushort_t	ser_taskq;	/* Serializer scheduled for taskq */
117*0Sstevel@tonic-gate 	kcondvar_t	ser_cv;		/* For serializer-wait */
118*0Sstevel@tonic-gate 	uint_t		ser_count;	/* # of queued requests (D) */
119*0Sstevel@tonic-gate 	mblk_t		*ser_first;	/* First message in the queue */
120*0Sstevel@tonic-gate 	mblk_t		*ser_last;	/* Last message in the queue */
121*0Sstevel@tonic-gate 	srproc_t	*ser_proc;	/* Currently executing proc (D) */
122*0Sstevel@tonic-gate 	mblk_t		*ser_curr;	/* Currently executing msg (D) */
123*0Sstevel@tonic-gate 	void		*ser_arg;	/* Currently executing arg (D) */
124*0Sstevel@tonic-gate };
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate static kmem_cache_t *serializer_cache;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate /*
129*0Sstevel@tonic-gate  * How many drains are allowed before we switch to taskq processing.
130*0Sstevel@tonic-gate  */
131*0Sstevel@tonic-gate #define	SERIALIZER_CREDIT 10
132*0Sstevel@tonic-gate static int serializer_credit = SERIALIZER_CREDIT;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate /* Statistics for debugging */
135*0Sstevel@tonic-gate static int perim_context_swtch = 0;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate static int serializer_constructor(void *, void *, int);
138*0Sstevel@tonic-gate static void serializer_destructor(void *, void *);
139*0Sstevel@tonic-gate static void serializer_exec(serializer_t *, srproc_t, mblk_t *, void *);
140*0Sstevel@tonic-gate static void serializer_enqueue(serializer_t *, srproc_t, mblk_t *, void *);
141*0Sstevel@tonic-gate static void serializer_drain(serializer_t *, int);
142*0Sstevel@tonic-gate static void serializer_drain_completely(serializer_t *);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate /*
145*0Sstevel@tonic-gate  * SERIALIZER Implementation.
146*0Sstevel@tonic-gate  */
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate /*
149*0Sstevel@tonic-gate  * Record debugging information and execute single request.
150*0Sstevel@tonic-gate  */
151*0Sstevel@tonic-gate static void
serializer_exec(serializer_t * s,srproc_t proc,mblk_t * mp,void * arg)152*0Sstevel@tonic-gate serializer_exec(serializer_t *s, srproc_t proc, mblk_t *mp, void *arg)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&s->ser_lock));
155*0Sstevel@tonic-gate 	ASSERT(s->ser_owner == curthread);
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	ASSERT(proc != NULL);
158*0Sstevel@tonic-gate 	ASSERT(mp != NULL);
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	s->ser_curr = mp;
161*0Sstevel@tonic-gate 	s->ser_arg = arg;
162*0Sstevel@tonic-gate 	s->ser_proc = proc;
163*0Sstevel@tonic-gate 	proc(mp, arg);
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate /*
167*0Sstevel@tonic-gate  * Enqueue a single request on serializer.
168*0Sstevel@tonic-gate  */
169*0Sstevel@tonic-gate static void
serializer_enqueue(serializer_t * s,srproc_t proc,mblk_t * mp,void * arg)170*0Sstevel@tonic-gate serializer_enqueue(serializer_t *s, srproc_t proc, mblk_t *mp, void *arg)
171*0Sstevel@tonic-gate {
172*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&s->ser_lock));
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	DTRACE_PROBE4(serializer__enqueue, serializer_t *, s,
175*0Sstevel@tonic-gate 	    mblk_t *, mp, void *, arg, srproc_t, proc);
176*0Sstevel@tonic-gate 	s->ser_count++;
177*0Sstevel@tonic-gate 	mp->b_queue = (queue_t *)proc;
178*0Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)arg;
179*0Sstevel@tonic-gate 	if (s->ser_last != NULL)
180*0Sstevel@tonic-gate 		s->ser_last->b_next = mp;
181*0Sstevel@tonic-gate 	else
182*0Sstevel@tonic-gate 		s->ser_first = mp;
183*0Sstevel@tonic-gate 	s->ser_last = mp;
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate /*
187*0Sstevel@tonic-gate  * Drain serializer, limiting drain to `credit' requests at most.
188*0Sstevel@tonic-gate  */
189*0Sstevel@tonic-gate static void
serializer_drain(serializer_t * s,int credit)190*0Sstevel@tonic-gate serializer_drain(serializer_t *s, int credit)
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate 	mblk_t *mp = s->ser_first;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&s->ser_lock));
195*0Sstevel@tonic-gate 	ASSERT(s->ser_owner == curthread);
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	for (; mp != NULL && credit-- != 0; mp = s->ser_first) {
198*0Sstevel@tonic-gate 		srproc_t *proc = (srproc_t *)mp->b_queue;
199*0Sstevel@tonic-gate 		void *arg = mp->b_prev;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 		if ((s->ser_first = s->ser_first->b_next) == NULL) {
202*0Sstevel@tonic-gate 			s->ser_last = NULL;
203*0Sstevel@tonic-gate 		} else {
204*0Sstevel@tonic-gate 			mp->b_next = NULL;
205*0Sstevel@tonic-gate 		}
206*0Sstevel@tonic-gate 		ASSERT(s->ser_count != 0);
207*0Sstevel@tonic-gate 		s->ser_count--;
208*0Sstevel@tonic-gate 		mp->b_queue = NULL;
209*0Sstevel@tonic-gate 		mp->b_prev = NULL;
210*0Sstevel@tonic-gate 		mutex_exit(&s->ser_lock);
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 		DTRACE_PROBE4(serializer__exec__start, serializer_t *, s,
213*0Sstevel@tonic-gate 		    mblk_t *, mp, void *, arg, srproc_t, proc);
214*0Sstevel@tonic-gate 		serializer_exec(s, proc, mp, arg);
215*0Sstevel@tonic-gate 		DTRACE_PROBE4(serializer__exec__end, serializer_t *, s,
216*0Sstevel@tonic-gate 		    mblk_t *, mp, void *, arg, srproc_t, proc);
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 		mutex_enter(&s->ser_lock);
219*0Sstevel@tonic-gate 	}
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate /*
223*0Sstevel@tonic-gate  * Drain serializer completely if serializer is free.
224*0Sstevel@tonic-gate  */
225*0Sstevel@tonic-gate static void
serializer_drain_completely(serializer_t * s)226*0Sstevel@tonic-gate serializer_drain_completely(serializer_t *s)
227*0Sstevel@tonic-gate {
228*0Sstevel@tonic-gate 	mutex_enter(&s->ser_lock);
229*0Sstevel@tonic-gate 	ASSERT(s->ser_taskq);
230*0Sstevel@tonic-gate 	if (s->ser_owner == NULL) {
231*0Sstevel@tonic-gate 		s->ser_owner = curthread;
232*0Sstevel@tonic-gate 		while (s->ser_first != NULL)
233*0Sstevel@tonic-gate 			serializer_drain(s, INT_MAX);
234*0Sstevel@tonic-gate 		s->ser_owner = NULL;
235*0Sstevel@tonic-gate 		s->ser_curr = NULL;
236*0Sstevel@tonic-gate 		s->ser_proc = NULL;
237*0Sstevel@tonic-gate 		s->ser_arg = NULL;
238*0Sstevel@tonic-gate 	}
239*0Sstevel@tonic-gate 	s->ser_taskq = B_FALSE;
240*0Sstevel@tonic-gate 	/*
241*0Sstevel@tonic-gate 	 * Wake up serializer_wait().
242*0Sstevel@tonic-gate 	 */
243*0Sstevel@tonic-gate 	cv_signal(&s->ser_cv);
244*0Sstevel@tonic-gate 	mutex_exit(&s->ser_lock);
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate /*
248*0Sstevel@tonic-gate  * Call proc(mp, arg) within serializer.
249*0Sstevel@tonic-gate  *
250*0Sstevel@tonic-gate  * If serializer is empty and not owned, proc(mp, arg) is called right
251*0Sstevel@tonic-gate  * away. Otherwise the request is queued.
252*0Sstevel@tonic-gate  */
253*0Sstevel@tonic-gate void
serializer_enter(serializer_t * s,srproc_t proc,mblk_t * mp,void * arg)254*0Sstevel@tonic-gate serializer_enter(serializer_t *s, srproc_t proc, mblk_t *mp, void *arg)
255*0Sstevel@tonic-gate {
256*0Sstevel@tonic-gate 	ASSERT(proc != NULL);
257*0Sstevel@tonic-gate 	ASSERT(mp != NULL);
258*0Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL);
259*0Sstevel@tonic-gate 	ASSERT(mp->b_prev == NULL);
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&s->ser_lock));
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	mutex_enter(&s->ser_lock);
264*0Sstevel@tonic-gate 	if (s->ser_owner != NULL) {
265*0Sstevel@tonic-gate 		/*
266*0Sstevel@tonic-gate 		 * Serializer is owned. Enqueue and return.
267*0Sstevel@tonic-gate 		 */
268*0Sstevel@tonic-gate 		serializer_enqueue(s, proc, mp, arg);
269*0Sstevel@tonic-gate 	} else {
270*0Sstevel@tonic-gate 		taskqid_t tid = 0;
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 		/*
273*0Sstevel@tonic-gate 		 * If the request list is empty, can process right away,
274*0Sstevel@tonic-gate 		 * otherwise enqueue and process.
275*0Sstevel@tonic-gate 		 */
276*0Sstevel@tonic-gate 		s->ser_owner = curthread;
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 		if (s->ser_first != NULL) {
279*0Sstevel@tonic-gate 			ASSERT(s->ser_count != 0);
280*0Sstevel@tonic-gate 			serializer_enqueue(s, proc, mp, arg);
281*0Sstevel@tonic-gate 		} else {
282*0Sstevel@tonic-gate 			ASSERT(s->ser_count == 0);
283*0Sstevel@tonic-gate 			mutex_exit(&s->ser_lock);
284*0Sstevel@tonic-gate 			/*
285*0Sstevel@tonic-gate 			 * Execute request
286*0Sstevel@tonic-gate 			 */
287*0Sstevel@tonic-gate 			DTRACE_PROBE4(serializer__exec__start,
288*0Sstevel@tonic-gate 			    serializer_t *, s, mblk_t *, mp,
289*0Sstevel@tonic-gate 			    void *, arg, srproc_t, proc);
290*0Sstevel@tonic-gate 			serializer_exec(s, proc, mp, arg);
291*0Sstevel@tonic-gate 			DTRACE_PROBE4(serializer__exec__end,
292*0Sstevel@tonic-gate 			    serializer_t *, s, mblk_t *, mp,
293*0Sstevel@tonic-gate 			    void *, arg, srproc_t, proc);
294*0Sstevel@tonic-gate 			mutex_enter(&s->ser_lock);
295*0Sstevel@tonic-gate 		}
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 		/*
298*0Sstevel@tonic-gate 		 * Drain whatever has arrived in the meantime.
299*0Sstevel@tonic-gate 		 * If we spend too much time draining, continue draining by the
300*0Sstevel@tonic-gate 		 * taskq thread.
301*0Sstevel@tonic-gate 		 */
302*0Sstevel@tonic-gate 		while ((s->ser_first != NULL) && (tid == 0)) {
303*0Sstevel@tonic-gate 			serializer_drain(s, serializer_credit);
304*0Sstevel@tonic-gate 			if (s->ser_first != NULL) {
305*0Sstevel@tonic-gate 				perim_context_swtch++;
306*0Sstevel@tonic-gate 				/*
307*0Sstevel@tonic-gate 				 * If there is a taskq pending for this
308*0Sstevel@tonic-gate 				 * serializer, no need to schedule a new one.
309*0Sstevel@tonic-gate 				 */
310*0Sstevel@tonic-gate 				if (s->ser_taskq) {
311*0Sstevel@tonic-gate 					break;
312*0Sstevel@tonic-gate 				} else {
313*0Sstevel@tonic-gate 					tid = taskq_dispatch(system_taskq,
314*0Sstevel@tonic-gate 					    (task_func_t *)
315*0Sstevel@tonic-gate 					    serializer_drain_completely,
316*0Sstevel@tonic-gate 					    s, TQ_NOSLEEP | TQ_NOQUEUE);
317*0Sstevel@tonic-gate 					if (tid != 0)
318*0Sstevel@tonic-gate 						s->ser_taskq = B_TRUE;
319*0Sstevel@tonic-gate 				}
320*0Sstevel@tonic-gate 			}
321*0Sstevel@tonic-gate 		}
322*0Sstevel@tonic-gate 		s->ser_owner = NULL;
323*0Sstevel@tonic-gate 		s->ser_curr = NULL;
324*0Sstevel@tonic-gate 		s->ser_proc = NULL;
325*0Sstevel@tonic-gate 		s->ser_arg = NULL;
326*0Sstevel@tonic-gate 	}
327*0Sstevel@tonic-gate 	/*
328*0Sstevel@tonic-gate 	 * Wakeup serializer_wait().
329*0Sstevel@tonic-gate 	 */
330*0Sstevel@tonic-gate 	cv_signal(&s->ser_cv);
331*0Sstevel@tonic-gate 	mutex_exit(&s->ser_lock);
332*0Sstevel@tonic-gate }
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate /*
335*0Sstevel@tonic-gate  * Wait for pending serializer jobs to complete. This function should never be
336*0Sstevel@tonic-gate  * called within the serializer or it will deadlock.
337*0Sstevel@tonic-gate  */
338*0Sstevel@tonic-gate void
serializer_wait(serializer_t * s)339*0Sstevel@tonic-gate serializer_wait(serializer_t *s)
340*0Sstevel@tonic-gate {
341*0Sstevel@tonic-gate 	mutex_enter(&s->ser_lock);
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	ASSERT(s->ser_owner != curthread);
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	while ((s->ser_owner != NULL) || s->ser_taskq || (s->ser_first != NULL))
346*0Sstevel@tonic-gate 		cv_wait(&s->ser_cv, &s->ser_lock);
347*0Sstevel@tonic-gate 	ASSERT((s->ser_first == NULL) && (s->ser_last == NULL));
348*0Sstevel@tonic-gate 	/*
349*0Sstevel@tonic-gate 	 * Wakeup other potential waiters.
350*0Sstevel@tonic-gate 	 */
351*0Sstevel@tonic-gate 	cv_signal(&s->ser_cv);
352*0Sstevel@tonic-gate 	mutex_exit(&s->ser_lock);
353*0Sstevel@tonic-gate }
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate /*
356*0Sstevel@tonic-gate  * Create a new serializer.
357*0Sstevel@tonic-gate  */
358*0Sstevel@tonic-gate serializer_t *
serializer_create(int flags)359*0Sstevel@tonic-gate serializer_create(int flags)
360*0Sstevel@tonic-gate {
361*0Sstevel@tonic-gate 	return (kmem_cache_alloc(serializer_cache, flags));
362*0Sstevel@tonic-gate }
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate /*
365*0Sstevel@tonic-gate  * Wait for all pending entries to drain and then destroy serializer.
366*0Sstevel@tonic-gate  */
367*0Sstevel@tonic-gate void
serializer_destroy(serializer_t * s)368*0Sstevel@tonic-gate serializer_destroy(serializer_t *s)
369*0Sstevel@tonic-gate {
370*0Sstevel@tonic-gate 	serializer_wait(s);
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	ASSERT(s->ser_owner == NULL);
373*0Sstevel@tonic-gate 	ASSERT(s->ser_taskq == 0);
374*0Sstevel@tonic-gate 	ASSERT(s->ser_count == 0);
375*0Sstevel@tonic-gate 	ASSERT(s->ser_first == NULL);
376*0Sstevel@tonic-gate 	ASSERT(s->ser_last == NULL);
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	kmem_cache_free(serializer_cache, s);
379*0Sstevel@tonic-gate }
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate /*ARGSUSED*/
382*0Sstevel@tonic-gate static int
serializer_constructor(void * buf,void * cdrarg,int kmflags)383*0Sstevel@tonic-gate serializer_constructor(void *buf, void *cdrarg, int kmflags)
384*0Sstevel@tonic-gate {
385*0Sstevel@tonic-gate 	serializer_t *s = buf;
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	mutex_init(&s->ser_lock, NULL, MUTEX_DEFAULT, NULL);
388*0Sstevel@tonic-gate 	cv_init(&s->ser_cv, NULL, CV_DEFAULT, NULL);
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 	s->ser_taskq = 0;
391*0Sstevel@tonic-gate 	s->ser_count = 0;
392*0Sstevel@tonic-gate 	s->ser_first = s->ser_last = s->ser_curr = NULL;
393*0Sstevel@tonic-gate 	s->ser_proc = NULL;
394*0Sstevel@tonic-gate 	s->ser_arg = NULL;
395*0Sstevel@tonic-gate 	s->ser_owner = NULL;
396*0Sstevel@tonic-gate 	return (0);
397*0Sstevel@tonic-gate }
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate /*ARGSUSED*/
400*0Sstevel@tonic-gate static void
serializer_destructor(void * buf,void * cdrarg)401*0Sstevel@tonic-gate serializer_destructor(void *buf, void *cdrarg)
402*0Sstevel@tonic-gate {
403*0Sstevel@tonic-gate 	serializer_t *s = buf;
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 	mutex_destroy(&s->ser_lock);
406*0Sstevel@tonic-gate 	cv_destroy(&s->ser_cv);
407*0Sstevel@tonic-gate }
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate void
serializer_init(void)410*0Sstevel@tonic-gate serializer_init(void)
411*0Sstevel@tonic-gate {
412*0Sstevel@tonic-gate 	serializer_cache = kmem_cache_create("serializer_cache",
413*0Sstevel@tonic-gate 	    sizeof (serializer_t), 0, serializer_constructor,
414*0Sstevel@tonic-gate 	    serializer_destructor, NULL, NULL, NULL, 0);
415*0Sstevel@tonic-gate }
416