4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas <matt@3am-software.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: subr_pcq.c,v 1.2 2008/11/11 20:37:15 snj Exp $");
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/atomic.h>
38 #include <sys/errno.h>
43 typedef void * volatile pcq_entry_t
;
46 pcq_entry_t
*pcq_consumer
;
47 pcq_entry_t
*pcq_producer
;
48 pcq_entry_t
*pcq_limit
;
49 pcq_entry_t pcq_base
[];
52 static inline pcq_entry_t
*
53 pcq_advance(pcq_t
*pcq
, pcq_entry_t
*ptr
)
56 if (__predict_false(++ptr
== pcq
->pcq_limit
))
63 pcq_put(pcq_t
*pcq
, void *item
)
65 pcq_entry_t
*producer
;
67 KASSERT(item
!= NULL
);
70 * Get our starting point, While we are doing this, it is
71 * imperative that pcq->pcq_base/pcq->pcq_limit not change
72 * in value. If you need to resize a pcq, init a new pcq
73 * with the right size and swap pointers to it.
75 membar_consumer(); /* see updates to pcq_producer */
76 producer
= pcq
->pcq_producer
;
79 * Preadvance so we reduce the window on updates.
81 pcq_entry_t
* const new_producer
= pcq_advance(pcq
, producer
);
84 * Try to fill an empty slot
86 if (NULL
== atomic_cas_ptr(producer
, NULL
, item
)) {
88 * We need to use atomic_cas_ptr since another thread
89 * might have inserted between these two cas operations
90 * and we don't want to overwrite a producer that's
93 atomic_cas_ptr(&pcq
->pcq_producer
,
94 __UNVOLATILE(producer
),
95 __UNVOLATILE(new_producer
));
97 * Tell them we were able to enqueue it.
99 #ifndef __HAVE_ATOMIC_AS_MEMBAR
106 * If we've reached the consumer, we've filled all the
107 * slots and there's no more room so return false.
109 #ifndef __HAVE_ATOMIC_AS_MEMBAR
110 membar_consumer(); /* see updates to pcq_consumer */
112 if (producer
== pcq
->pcq_consumer
)
116 * Let's see if the next slot is free...
118 producer
= new_producer
;
123 * It's assumed that the enclosing structure that contains the pcq will
124 * provide appropriate locking to prevent concurrent gets from occurring.
129 pcq_entry_t
* const consumer
= pcq
->pcq_consumer
;
133 * Updates to pcq_consumer doesn't matter since we control it but we
134 * want to make sure that any stores to what it references have
140 * If there's nothing to return, just return.
142 if ((item
= *consumer
) == NULL
)
146 * Update the consumer and free the slot.
147 * Update the consumer pointer first so when producer == consumer
148 * the right thing happens.
150 * 1) until the slot set to NULL, pcq_put will fail since
151 * the slot != NULL && producer == consumer.
152 * 2) consumer is advanced but the slot is still not NULL,
153 * pcq_put will advance by one, see that producer == consumer,
155 * 4) Once the slot is set to NULL, the producer can fill the slot
156 * and advance the producer.
158 * and then we are back to 1.
160 pcq
->pcq_consumer
= pcq_advance(pcq
, consumer
);
173 membar_consumer(); /* see updates to *pcq_consumer */
174 return *pcq
->pcq_consumer
;
178 pcq_maxitems(pcq_t
*pcq
)
181 return pcq
->pcq_limit
- pcq
->pcq_base
;
185 pcq_create(size_t maxitems
, km_flag_t kmflags
)
189 KASSERT(maxitems
> 0);
191 pcq
= kmem_zalloc(offsetof(pcq_t
, pcq_base
[maxitems
]), kmflags
);
192 if (__predict_false(pcq
== NULL
))
195 pcq
->pcq_limit
= pcq
->pcq_base
+ maxitems
;
196 pcq
->pcq_producer
= pcq
->pcq_base
;
197 pcq
->pcq_consumer
= pcq
->pcq_producer
;
203 pcq_destroy(pcq_t
*pcq
)
206 KASSERT(*pcq
->pcq_consumer
== NULL
);
208 kmem_free(pcq
, (uintptr_t)pcq
->pcq_limit
- (uintptr_t)pcq
);