queue.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011-2013  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* $Id$ */
00018 
00019 /*
00020  * This is a generic implementation of a two-lock concurrent queue.
00021  * There are built-in mutex locks for the head and tail of the queue,
00022  * allowing elements to be safely added and removed at the same time.
00023  *
00024  * NULL is "end of list"
00025  * -1 is "not linked"
00026  */
00027 
00028 #ifndef ISC_QUEUE_H
00029 #define ISC_QUEUE_H 1
00030 #include <isc/assertions.h>
00031 #include <isc/boolean.h>
00032 #include <isc/mutex.h>
00033 
00034 #ifdef ISC_QUEUE_CHECKINIT
00035 #define ISC_QLINK_INSIST(x) ISC_INSIST(x)
00036 #else
00037 #define ISC_QLINK_INSIST(x) (void)0
00038 #endif
00039 
00040 #define ISC_QLINK(type) struct { type *prev, *next; }
00041 
00042 #define ISC_QLINK_INIT(elt, link) \
00043         do { \
00044                 (elt)->link.next = (elt)->link.prev = (void *)(-1); \
00045         } while(0)
00046 
00047 #define ISC_QLINK_LINKED(elt, link) ((void*)(elt)->link.next != (void*)(-1))
00048 
00049 #define ISC_QUEUE(type) struct { \
00050         type *head, *tail; \
00051         isc_mutex_t headlock, taillock; \
00052 }
00053 
00054 #define ISC_QUEUE_INIT(queue, link) \
00055         do { \
00056                 (void) isc_mutex_init(&(queue).taillock); \
00057                 (void) isc_mutex_init(&(queue).headlock); \
00058                 (queue).tail = (queue).head = NULL; \
00059         } while (0)
00060 
00061 #define ISC_QUEUE_EMPTY(queue) ISC_TF((queue).head == NULL)
00062 
00063 #define ISC_QUEUE_DESTROY(queue) \
00064         do { \
00065                 ISC_QLINK_INSIST(ISC_QUEUE_EMPTY(queue)); \
00066                 (void) isc_mutex_destroy(&(queue).taillock); \
00067                 (void) isc_mutex_destroy(&(queue).headlock); \
00068         } while (0)
00069 
00070 /*
00071  * queues are meant to separate the locks at either end.  For best effect, that
00072  * means keeping the ends separate - i.e. non-empty queues work best.
00073  *
00074  * a push to an empty queue has to take the pop lock to update
00075  * the pop side of the queue.
00076  * Popping the last entry has to take the push lock to update
00077  * the push side of the queue.
00078  *
00079  * The order is (pop, push), because a pop is presumably in the
00080  * latency path and a push is when we're done.
00081  *
00082  * We do an MT hot test in push to see if we need both locks, so we can
00083  * acquire them in order.  Hopefully that makes the case where we get
00084  * the push lock and find we need the pop lock (and have to release it) rare.
00085  *
00086  * > 1 entry - no collision, push works on one end, pop on the other
00087  *   0 entry - headlock race
00088  *     pop wins - return(NULL), push adds new as both head/tail
00089  *     push wins - updates head/tail, becomes 1 entry case.
00090  *   1 entry - taillock race
00091  *     pop wins - return(pop) sets head/tail NULL, becomes 0 entry case
00092  *     push wins - updates {head,tail}->link.next, pop updates head
00093  *                 with new ->link.next and doesn't update tail
00094  *
00095  */
00096 #define ISC_QUEUE_PUSH(queue, elt, link) \
00097         do { \
00098                 isc_boolean_t headlocked = ISC_FALSE; \
00099                 ISC_QLINK_INSIST(!ISC_QLINK_LINKED(elt, link)); \
00100                 if ((queue).head == NULL) { \
00101                         LOCK(&(queue).headlock); \
00102                         headlocked = ISC_TRUE; \
00103                 } \
00104                 LOCK(&(queue).taillock); \
00105                 if ((queue).tail == NULL && !headlocked) { \
00106                         UNLOCK(&(queue).taillock); \
00107                         LOCK(&(queue).headlock); \
00108                         LOCK(&(queue).taillock); \
00109                         headlocked = ISC_TRUE; \
00110                 } \
00111                 (elt)->link.prev = (queue).tail; \
00112                 (elt)->link.next = NULL; \
00113                 if ((queue).tail != NULL) \
00114                         (queue).tail->link.next = (elt); \
00115                 (queue).tail = (elt); \
00116                 UNLOCK(&(queue).taillock); \
00117                 if (headlocked) { \
00118                         if ((queue).head == NULL) \
00119                                 (queue).head = (elt); \
00120                         UNLOCK(&(queue).headlock); \
00121                 } \
00122         } while (0)
00123 
00124 #define ISC_QUEUE_POP(queue, link, ret) \
00125         do { \
00126                 LOCK(&(queue).headlock); \
00127                 ret = (queue).head; \
00128                 while (ret != NULL) { \
00129                         if (ret->link.next == NULL) { \
00130                                 LOCK(&(queue).taillock); \
00131                                 if (ret->link.next == NULL) { \
00132                                         (queue).head = (queue).tail = NULL; \
00133                                         UNLOCK(&(queue).taillock); \
00134                                         break; \
00135                                 }\
00136                                 UNLOCK(&(queue).taillock); \
00137                         } \
00138                         (queue).head = ret->link.next; \
00139                         (queue).head->link.prev = NULL; \
00140                         break; \
00141                 } \
00142                 UNLOCK(&(queue).headlock); \
00143                 if (ret != NULL) \
00144                         (ret)->link.next = (ret)->link.prev = (void *)(-1); \
00145         } while(0)
00146 
00147 #define ISC_QUEUE_UNLINK(queue, elt, link) \
00148         do { \
00149                 ISC_QLINK_INSIST(ISC_QLINK_LINKED(elt, link)); \
00150                 LOCK(&(queue).headlock); \
00151                 LOCK(&(queue).taillock); \
00152                 if ((elt)->link.prev == NULL) \
00153                         (queue).head = (elt)->link.next; \
00154                 else \
00155                         (elt)->link.prev->link.next = (elt)->link.next; \
00156                 if ((elt)->link.next == NULL) \
00157                         (queue).tail = (elt)->link.prev; \
00158                 else \
00159                         (elt)->link.next->link.prev = (elt)->link.prev; \
00160                 UNLOCK(&(queue).taillock); \
00161                 UNLOCK(&(queue).headlock); \
00162                 (elt)->link.next = (elt)->link.prev = (void *)(-1); \
00163         } while(0)
00164 
00165 #endif /* ISC_QUEUE_H */

Generated on Tue Apr 28 17:41:04 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha