Home | History | Annotate | Line # | Download | only in kern
subr_pcq.c revision 1.8.4.1
      1  1.8.4.1     skrll /*	$NetBSD: subr_pcq.c,v 1.8.4.1 2015/04/06 15:18:20 skrll Exp $	*/
      2      1.3     rmind 
      3      1.1      matt /*-
      4      1.4     rmind  * Copyright (c) 2009 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.4     rmind  * by Andrew Doran.
      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.4     rmind /*
     33      1.4     rmind  * Lockless producer/consumer queue.
     34      1.4     rmind  */
     35      1.4     rmind 
     36      1.1      matt #include <sys/cdefs.h>
     37  1.8.4.1     skrll __KERNEL_RCSID(0, "$NetBSD: subr_pcq.c,v 1.8.4.1 2015/04/06 15:18:20 skrll Exp $");
     38      1.1      matt 
     39      1.1      matt #include <sys/param.h>
     40      1.1      matt #include <sys/types.h>
     41      1.1      matt #include <sys/atomic.h>
     42      1.1      matt #include <sys/kmem.h>
     43      1.1      matt 
     44      1.1      matt #include <sys/pcq.h>
     45      1.1      matt 
     46      1.4     rmind /*
     47      1.4     rmind  * Internal producer-consumer queue structure.  Note: providing a separate
     48      1.4     rmind  * cache-line both for pcq_t::pcq_pc and pcq_t::pcq_items.
     49      1.4     rmind  */
     50      1.1      matt struct pcq {
     51      1.4     rmind 	u_int			pcq_nitems;
     52      1.4     rmind 	uint8_t			pcq_pad1[COHERENCY_UNIT - sizeof(u_int)];
     53      1.4     rmind 	volatile uint32_t	pcq_pc;
     54      1.4     rmind 	uint8_t			pcq_pad2[COHERENCY_UNIT - sizeof(uint32_t)];
     55      1.4     rmind 	void * volatile		pcq_items[];
     56      1.1      matt };
     57      1.1      matt 
     58      1.4     rmind /*
     59      1.4     rmind  * Producer (p) - stored in the lower 16 bits of pcq_t::pcq_pc.
     60      1.4     rmind  * Consumer (c) - in the higher 16 bits.
     61      1.4     rmind  *
     62      1.4     rmind  * We have a limitation of 16 bits i.e. 0xffff items in the queue.
     63      1.8     rmind  * The PCQ_MAXLEN constant is set accordingly.
     64      1.4     rmind  */
     65      1.4     rmind 
     66      1.4     rmind static inline void
     67      1.4     rmind pcq_split(uint32_t v, u_int *p, u_int *c)
     68      1.1      matt {
     69      1.3     rmind 
     70      1.4     rmind 	*p = v & 0xffff;
     71      1.4     rmind 	*c = v >> 16;
     72      1.4     rmind }
     73      1.1      matt 
     74      1.4     rmind static inline uint32_t
     75      1.4     rmind pcq_combine(u_int p, u_int c)
     76      1.4     rmind {
     77      1.4     rmind 
     78      1.4     rmind 	return p | (c << 16);
     79      1.4     rmind }
     80      1.4     rmind 
     81      1.4     rmind static inline u_int
     82      1.4     rmind pcq_advance(pcq_t *pcq, u_int pc)
     83      1.4     rmind {
     84      1.4     rmind 
     85      1.4     rmind 	if (__predict_false(++pc == pcq->pcq_nitems)) {
     86      1.4     rmind 		return 0;
     87      1.4     rmind 	}
     88      1.4     rmind 	return pc;
     89      1.1      matt }
     90      1.1      matt 
     91      1.4     rmind /*
     92      1.4     rmind  * pcq_put: place an item at the end of the queue.
     93      1.4     rmind  */
     94      1.1      matt bool
     95      1.1      matt pcq_put(pcq_t *pcq, void *item)
     96      1.1      matt {
     97      1.4     rmind 	uint32_t v, nv;
     98      1.4     rmind 	u_int op, p, c;
     99      1.1      matt 
    100      1.1      matt 	KASSERT(item != NULL);
    101      1.1      matt 
    102      1.4     rmind 	do {
    103      1.4     rmind 		v = pcq->pcq_pc;
    104      1.4     rmind 		pcq_split(v, &op, &c);
    105      1.4     rmind 		p = pcq_advance(pcq, op);
    106      1.4     rmind 		if (p == c) {
    107      1.4     rmind 			/* Queue is full. */
    108      1.4     rmind 			return false;
    109      1.4     rmind 		}
    110      1.4     rmind 		nv = pcq_combine(p, c);
    111      1.4     rmind 	} while (atomic_cas_32(&pcq->pcq_pc, v, nv) != v);
    112      1.4     rmind 
    113      1.1      matt 	/*
    114      1.4     rmind 	 * Ensure that the update to pcq_pc is globally visible before the
    115      1.4     rmind 	 * data item.  See pcq_get().  This also ensures that any changes
    116      1.4     rmind 	 * that the caller made to the data item are globally visible
    117      1.4     rmind 	 * before we put it onto the list.
    118      1.1      matt 	 */
    119      1.7  riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
    120      1.4     rmind 	membar_producer();
    121      1.4     rmind #endif
    122      1.4     rmind 	pcq->pcq_items[op] = item;
    123      1.4     rmind 
    124      1.4     rmind 	/*
    125      1.4     rmind 	 * Synchronization activity to wake up the consumer will ensure
    126      1.4     rmind 	 * that the update to pcq_items[] is visible before the wakeup
    127      1.4     rmind 	 * arrives.  So, we do not need an additonal memory barrier here.
    128      1.4     rmind 	 */
    129      1.4     rmind 	return true;
    130      1.4     rmind }
    131      1.1      matt 
    132      1.4     rmind /*
    133      1.4     rmind  * pcq_peek: return the next item from the queue without removal.
    134      1.4     rmind  */
    135      1.4     rmind void *
    136      1.4     rmind pcq_peek(pcq_t *pcq)
    137      1.4     rmind {
    138      1.4     rmind 	const uint32_t v = pcq->pcq_pc;
    139      1.4     rmind 	u_int p, c;
    140      1.1      matt 
    141      1.4     rmind 	pcq_split(v, &p, &c);
    142      1.1      matt 
    143      1.4     rmind 	/* See comment on race below in pcq_get(). */
    144  1.8.4.1     skrll 	return (p == c) ? NULL :
    145  1.8.4.1     skrll 	    (membar_datadep_consumer(), pcq->pcq_items[c]);
    146      1.1      matt }
    147      1.1      matt 
    148      1.1      matt /*
    149      1.4     rmind  * pcq_get: remove and return the next item for consumption or NULL if empty.
    150      1.4     rmind  *
    151      1.4     rmind  * => The caller must prevent concurrent gets from occuring.
    152      1.1      matt  */
    153      1.1      matt void *
    154      1.1      matt pcq_get(pcq_t *pcq)
    155      1.1      matt {
    156      1.4     rmind 	uint32_t v, nv;
    157      1.4     rmind 	u_int p, c;
    158      1.1      matt 	void *item;
    159      1.1      matt 
    160      1.4     rmind 	v = pcq->pcq_pc;
    161      1.4     rmind 	pcq_split(v, &p, &c);
    162      1.4     rmind 	if (p == c) {
    163      1.4     rmind 		/* Queue is empty: nothing to return. */
    164      1.4     rmind 		return NULL;
    165      1.4     rmind 	}
    166  1.8.4.1     skrll 	/* Make sure we read pcq->pcq_pc before pcq->pcq_items[c].  */
    167  1.8.4.1     skrll 	membar_datadep_consumer();
    168      1.4     rmind 	item = pcq->pcq_items[c];
    169      1.4     rmind 	if (item == NULL) {
    170      1.4     rmind 		/*
    171      1.4     rmind 		 * Raced with sender: we rely on a notification (e.g. softint
    172      1.4     rmind 		 * or wakeup) being generated after the producer's pcq_put(),
    173      1.4     rmind 		 * causing us to retry pcq_get() later.
    174      1.4     rmind 		 */
    175      1.1      matt 		return NULL;
    176      1.4     rmind 	}
    177      1.4     rmind 	pcq->pcq_items[c] = NULL;
    178      1.4     rmind 	c = pcq_advance(pcq, c);
    179      1.4     rmind 	nv = pcq_combine(p, c);
    180      1.1      matt 
    181      1.1      matt 	/*
    182      1.4     rmind 	 * Ensure that update to pcq_items[] becomes globally visible
    183      1.4     rmind 	 * before the update to pcq_pc.  If it were reodered to occur
    184      1.4     rmind 	 * after it, we could in theory wipe out a modification made
    185      1.4     rmind 	 * to pcq_items[] by pcq_put().
    186      1.1      matt 	 */
    187      1.7  riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
    188      1.1      matt 	membar_producer();
    189      1.4     rmind #endif
    190      1.4     rmind 	while (__predict_false(atomic_cas_32(&pcq->pcq_pc, v, nv) != v)) {
    191      1.4     rmind 		v = pcq->pcq_pc;
    192      1.4     rmind 		pcq_split(v, &p, &c);
    193      1.4     rmind 		c = pcq_advance(pcq, c);
    194      1.4     rmind 		nv = pcq_combine(p, c);
    195      1.4     rmind 	}
    196      1.1      matt 	return item;
    197      1.1      matt }
    198      1.1      matt 
    199      1.1      matt pcq_t *
    200      1.4     rmind pcq_create(size_t nitems, km_flag_t kmflags)
    201      1.1      matt {
    202      1.1      matt 	pcq_t *pcq;
    203      1.1      matt 
    204      1.8     rmind 	KASSERT(nitems > 0 || nitems <= PCQ_MAXLEN);
    205      1.1      matt 
    206      1.6     alnsn 	pcq = kmem_zalloc(offsetof(pcq_t, pcq_items[nitems]), kmflags);
    207      1.4     rmind 	if (pcq == NULL) {
    208      1.1      matt 		return NULL;
    209      1.4     rmind 	}
    210      1.4     rmind 	pcq->pcq_nitems = nitems;
    211      1.1      matt 	return pcq;
    212      1.1      matt }
    213      1.1      matt 
    214      1.1      matt void
    215      1.1      matt pcq_destroy(pcq_t *pcq)
    216      1.1      matt {
    217      1.3     rmind 
    218      1.4     rmind 	kmem_free(pcq, offsetof(pcq_t, pcq_items[pcq->pcq_nitems]));
    219      1.4     rmind }
    220      1.4     rmind 
    221      1.4     rmind size_t
    222      1.4     rmind pcq_maxitems(pcq_t *pcq)
    223      1.4     rmind {
    224      1.1      matt 
    225      1.4     rmind 	return pcq->pcq_nitems;
    226      1.1      matt }
    227