subr_pcq.c revision 1.3.8.2 1 1.3.8.2 skrll /* $NetBSD: subr_pcq.c,v 1.3.8.2 2009/01/19 13:19:39 skrll Exp $ */
2 1.3.8.2 skrll
3 1.3.8.2 skrll /*-
4 1.3.8.2 skrll * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.3.8.2 skrll * All rights reserved.
6 1.3.8.2 skrll *
7 1.3.8.2 skrll * This code is derived from software contributed to The NetBSD Foundation
8 1.3.8.2 skrll * by Matt Thomas <matt (at) 3am-software.com>
9 1.3.8.2 skrll *
10 1.3.8.2 skrll * Redistribution and use in source and binary forms, with or without
11 1.3.8.2 skrll * modification, are permitted provided that the following conditions
12 1.3.8.2 skrll * are met:
13 1.3.8.2 skrll * 1. Redistributions of source code must retain the above copyright
14 1.3.8.2 skrll * notice, this list of conditions and the following disclaimer.
15 1.3.8.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.8.2 skrll * notice, this list of conditions and the following disclaimer in the
17 1.3.8.2 skrll * documentation and/or other materials provided with the distribution.
18 1.3.8.2 skrll *
19 1.3.8.2 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3.8.2 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3.8.2 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3.8.2 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3.8.2 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3.8.2 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3.8.2 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3.8.2 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3.8.2 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3.8.2 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3.8.2 skrll * POSSIBILITY OF SUCH DAMAGE.
30 1.3.8.2 skrll */
31 1.3.8.2 skrll
32 1.3.8.2 skrll #include <sys/cdefs.h>
33 1.3.8.2 skrll __KERNEL_RCSID(0, "$NetBSD: subr_pcq.c,v 1.3.8.2 2009/01/19 13:19:39 skrll Exp $");
34 1.3.8.2 skrll
35 1.3.8.2 skrll #include <sys/param.h>
36 1.3.8.2 skrll #include <sys/types.h>
37 1.3.8.2 skrll #include <sys/atomic.h>
38 1.3.8.2 skrll #include <sys/errno.h>
39 1.3.8.2 skrll #include <sys/kmem.h>
40 1.3.8.2 skrll
41 1.3.8.2 skrll #include <sys/pcq.h>
42 1.3.8.2 skrll
43 1.3.8.2 skrll typedef void * volatile pcq_entry_t;
44 1.3.8.2 skrll
45 1.3.8.2 skrll struct pcq {
46 1.3.8.2 skrll pcq_entry_t *pcq_consumer;
47 1.3.8.2 skrll pcq_entry_t *pcq_producer;
48 1.3.8.2 skrll pcq_entry_t *pcq_limit;
49 1.3.8.2 skrll pcq_entry_t pcq_base[];
50 1.3.8.2 skrll };
51 1.3.8.2 skrll
52 1.3.8.2 skrll static inline pcq_entry_t *
53 1.3.8.2 skrll pcq_advance(pcq_t *pcq, pcq_entry_t *ptr)
54 1.3.8.2 skrll {
55 1.3.8.2 skrll
56 1.3.8.2 skrll if (__predict_false(++ptr == pcq->pcq_limit))
57 1.3.8.2 skrll return pcq->pcq_base;
58 1.3.8.2 skrll
59 1.3.8.2 skrll return ptr;
60 1.3.8.2 skrll }
61 1.3.8.2 skrll
62 1.3.8.2 skrll bool
63 1.3.8.2 skrll pcq_put(pcq_t *pcq, void *item)
64 1.3.8.2 skrll {
65 1.3.8.2 skrll pcq_entry_t *producer;
66 1.3.8.2 skrll
67 1.3.8.2 skrll KASSERT(item != NULL);
68 1.3.8.2 skrll
69 1.3.8.2 skrll /*
70 1.3.8.2 skrll * Get our starting point, While we are doing this, it is
71 1.3.8.2 skrll * imperative that pcq->pcq_base/pcq->pcq_limit not change
72 1.3.8.2 skrll * in value. If you need to resize a pcq, init a new pcq
73 1.3.8.2 skrll * with the right size and swap pointers to it.
74 1.3.8.2 skrll */
75 1.3.8.2 skrll membar_consumer(); /* see updates to pcq_producer */
76 1.3.8.2 skrll producer = pcq->pcq_producer;
77 1.3.8.2 skrll for (;;) {
78 1.3.8.2 skrll /*
79 1.3.8.2 skrll * Preadvance so we reduce the window on updates.
80 1.3.8.2 skrll */
81 1.3.8.2 skrll pcq_entry_t * const new_producer = pcq_advance(pcq, producer);
82 1.3.8.2 skrll
83 1.3.8.2 skrll /*
84 1.3.8.2 skrll * Try to fill an empty slot
85 1.3.8.2 skrll */
86 1.3.8.2 skrll if (NULL == atomic_cas_ptr(producer, NULL, item)) {
87 1.3.8.2 skrll /*
88 1.3.8.2 skrll * We need to use atomic_cas_ptr since another thread
89 1.3.8.2 skrll * might have inserted between these two cas operations
90 1.3.8.2 skrll * and we don't want to overwrite a producer that's
91 1.3.8.2 skrll * more up-to-date.
92 1.3.8.2 skrll */
93 1.3.8.2 skrll atomic_cas_ptr(&pcq->pcq_producer,
94 1.3.8.2 skrll __UNVOLATILE(producer),
95 1.3.8.2 skrll __UNVOLATILE(new_producer));
96 1.3.8.2 skrll /*
97 1.3.8.2 skrll * Tell them we were able to enqueue it.
98 1.3.8.2 skrll */
99 1.3.8.2 skrll #ifndef __HAVE_ATOMIC_AS_MEMBAR
100 1.3.8.2 skrll membar_producer();
101 1.3.8.2 skrll #endif
102 1.3.8.2 skrll return true;
103 1.3.8.2 skrll }
104 1.3.8.2 skrll
105 1.3.8.2 skrll /*
106 1.3.8.2 skrll * If we've reached the consumer, we've filled all the
107 1.3.8.2 skrll * slots and there's no more room so return false.
108 1.3.8.2 skrll */
109 1.3.8.2 skrll #ifndef __HAVE_ATOMIC_AS_MEMBAR
110 1.3.8.2 skrll membar_consumer(); /* see updates to pcq_consumer */
111 1.3.8.2 skrll #endif
112 1.3.8.2 skrll if (producer == pcq->pcq_consumer)
113 1.3.8.2 skrll return false;
114 1.3.8.2 skrll
115 1.3.8.2 skrll /*
116 1.3.8.2 skrll * Let's see if the next slot is free...
117 1.3.8.2 skrll */
118 1.3.8.2 skrll producer = new_producer;
119 1.3.8.2 skrll }
120 1.3.8.2 skrll }
121 1.3.8.2 skrll
122 1.3.8.2 skrll /*
123 1.3.8.2 skrll * It's assumed that the enclosing structure that contains the pcq will
124 1.3.8.2 skrll * provide appropriate locking to prevent concurrent gets from occurring.
125 1.3.8.2 skrll */
126 1.3.8.2 skrll void *
127 1.3.8.2 skrll pcq_get(pcq_t *pcq)
128 1.3.8.2 skrll {
129 1.3.8.2 skrll pcq_entry_t * const consumer = pcq->pcq_consumer;
130 1.3.8.2 skrll void *item;
131 1.3.8.2 skrll
132 1.3.8.2 skrll /*
133 1.3.8.2 skrll * Updates to pcq_consumer doesn't matter since we control it but we
134 1.3.8.2 skrll * want to make sure that any stores to what it references have
135 1.3.8.2 skrll * completed.
136 1.3.8.2 skrll */
137 1.3.8.2 skrll membar_consumer();
138 1.3.8.2 skrll
139 1.3.8.2 skrll /*
140 1.3.8.2 skrll * If there's nothing to return, just return.
141 1.3.8.2 skrll */
142 1.3.8.2 skrll if ((item = *consumer) == NULL)
143 1.3.8.2 skrll return NULL;
144 1.3.8.2 skrll
145 1.3.8.2 skrll /*
146 1.3.8.2 skrll * Update the consumer and free the slot.
147 1.3.8.2 skrll * Update the consumer pointer first so when producer == consumer
148 1.3.8.2 skrll * the right thing happens.
149 1.3.8.2 skrll *
150 1.3.8.2 skrll * 1) until the slot set to NULL, pcq_put will fail since
151 1.3.8.2 skrll * the slot != NULL && producer == consumer.
152 1.3.8.2 skrll * 2) consumer is advanced but the slot is still not NULL,
153 1.3.8.2 skrll * pcq_put will advance by one, see that producer == consumer,
154 1.3.8.2 skrll * and fail.
155 1.3.8.2 skrll * 4) Once the slot is set to NULL, the producer can fill the slot
156 1.3.8.2 skrll * and advance the producer.
157 1.3.8.2 skrll *
158 1.3.8.2 skrll * and then we are back to 1.
159 1.3.8.2 skrll */
160 1.3.8.2 skrll pcq->pcq_consumer = pcq_advance(pcq, consumer);
161 1.3.8.2 skrll membar_producer();
162 1.3.8.2 skrll
163 1.3.8.2 skrll *consumer = NULL;
164 1.3.8.2 skrll membar_producer();
165 1.3.8.2 skrll
166 1.3.8.2 skrll return item;
167 1.3.8.2 skrll }
168 1.3.8.2 skrll
169 1.3.8.2 skrll void *
170 1.3.8.2 skrll pcq_peek(pcq_t *pcq)
171 1.3.8.2 skrll {
172 1.3.8.2 skrll
173 1.3.8.2 skrll membar_consumer(); /* see updates to *pcq_consumer */
174 1.3.8.2 skrll return *pcq->pcq_consumer;
175 1.3.8.2 skrll }
176 1.3.8.2 skrll
177 1.3.8.2 skrll size_t
178 1.3.8.2 skrll pcq_maxitems(pcq_t *pcq)
179 1.3.8.2 skrll {
180 1.3.8.2 skrll
181 1.3.8.2 skrll return pcq->pcq_limit - pcq->pcq_base;
182 1.3.8.2 skrll }
183 1.3.8.2 skrll
184 1.3.8.2 skrll pcq_t *
185 1.3.8.2 skrll pcq_create(size_t maxitems, km_flag_t kmflags)
186 1.3.8.2 skrll {
187 1.3.8.2 skrll pcq_t *pcq;
188 1.3.8.2 skrll
189 1.3.8.2 skrll KASSERT(maxitems > 0);
190 1.3.8.2 skrll
191 1.3.8.2 skrll pcq = kmem_zalloc(offsetof(pcq_t, pcq_base[maxitems]), kmflags);
192 1.3.8.2 skrll if (__predict_false(pcq == NULL))
193 1.3.8.2 skrll return NULL;
194 1.3.8.2 skrll
195 1.3.8.2 skrll pcq->pcq_limit = pcq->pcq_base + maxitems;
196 1.3.8.2 skrll pcq->pcq_producer = pcq->pcq_base;
197 1.3.8.2 skrll pcq->pcq_consumer = pcq->pcq_producer;
198 1.3.8.2 skrll
199 1.3.8.2 skrll return pcq;
200 1.3.8.2 skrll }
201 1.3.8.2 skrll
202 1.3.8.2 skrll void
203 1.3.8.2 skrll pcq_destroy(pcq_t *pcq)
204 1.3.8.2 skrll {
205 1.3.8.2 skrll
206 1.3.8.2 skrll KASSERT(*pcq->pcq_consumer == NULL);
207 1.3.8.2 skrll
208 1.3.8.2 skrll kmem_free(pcq, (uintptr_t)pcq->pcq_limit - (uintptr_t)pcq);
209 1.3.8.2 skrll }
210