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