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