1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2007 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2007 Voltaire All rights reserved.
* Copyright (c) 2010 IBM Corporation. All rights reserved.
* Copyright (c) 2014-2018 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2021 Triad National Security, LLC. All rights reserved.
* Copyright (c) 2021 Google, LLC. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#ifndef OPAL_FIFO_H_HAS_BEEN_INCLUDED
#define OPAL_FIFO_H_HAS_BEEN_INCLUDED
#include "opal_config.h"
#include "opal/class/opal_lifo.h"
#include "opal/mca/threads/mutex.h"
#include "opal/sys/atomic.h"
BEGIN_C_DECLS
/* Atomic First In First Out lists. If we are in a multi-threaded environment then the
* atomicity is insured via the compare-and-swap operation, if not we simply do a read
* and/or a write.
*
* There is a trick. The ghost element at the end of the list. This ghost element has
* the next pointer pointing to itself, therefore we cannot go past the end of the list.
* With this approach we will never have a NULL element in the list, so we never have
* to test for the NULL.
*/
struct opal_fifo_t {
opal_object_t super;
/** first element on the fifo */
volatile opal_counted_pointer_t opal_fifo_head;
/** last element on the fifo */
volatile opal_counted_pointer_t opal_fifo_tail;
/** list sentinel (always points to self) */
opal_list_item_t opal_fifo_ghost;
};
typedef struct opal_fifo_t opal_fifo_t;
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_fifo_t);
static inline opal_list_item_t *opal_fifo_head(opal_fifo_t *fifo)
{
return (opal_list_item_t *) fifo->opal_fifo_head.data.item;
}
static inline opal_list_item_t *opal_fifo_tail(opal_fifo_t *fifo)
{
return (opal_list_item_t *) fifo->opal_fifo_tail.data.item;
}
/* The ghost pointer will never change. The head will change via an atomic
* compare-and-swap. On most architectures the reading of a pointer is an
* atomic operation so we don't have to protect it.
*/
static inline bool opal_fifo_is_empty(opal_fifo_t *fifo)
{
return opal_fifo_head(fifo) == &fifo->opal_fifo_ghost;
}
#if OPAL_HAVE_ATOMIC_COMPARE_EXCHANGE_128 && !OPAL_HAVE_ATOMIC_LLSC_PTR
/* Add one element to the FIFO. We will return the last head of the list
* to allow the upper level to detect if this element is the first one in the
* list (if the list was empty before this operation).
*/
static inline opal_list_item_t *opal_fifo_push_atomic(opal_fifo_t *fifo, opal_list_item_t *item)
{
opal_counted_pointer_t tail = {.value = fifo->opal_fifo_tail.value};
const opal_list_item_t *const ghost = &fifo->opal_fifo_ghost;
item->opal_list_next = (opal_list_item_t *) ghost;
opal_atomic_wmb();
do {
if (opal_update_counted_pointer(&fifo->opal_fifo_tail, &tail, item)) {
break;
}
} while (1);
opal_atomic_wmb();
if ((intptr_t) ghost == tail.data.item) {
/* update the head */
opal_counted_pointer_t head = {.value = fifo->opal_fifo_head.value};
opal_update_counted_pointer(&fifo->opal_fifo_head, &head, item);
} else {
/* update previous item */
((opal_list_item_t *) tail.data.item)->opal_list_next = item;
}
return (opal_list_item_t *) tail.data.item;
}
/* Retrieve one element from the FIFO. If we reach the ghost element then the FIFO
* is empty so we return NULL.
*/
static inline opal_list_item_t *opal_fifo_pop_atomic(opal_fifo_t *fifo)
{
opal_list_item_t *item, *next, *ghost = &fifo->opal_fifo_ghost;
opal_counted_pointer_t head, tail;
opal_read_counted_pointer(&fifo->opal_fifo_head, &head);
do {
tail.value = fifo->opal_fifo_tail.value;
opal_atomic_rmb();
item = (opal_list_item_t *) head.data.item;
next = (opal_list_item_t *) item->opal_list_next;
if ((intptr_t) ghost == tail.data.item && ghost == item) {
return NULL;
}
/* the head or next pointer are in an inconsistent state. keep looping. */
if (tail.data.item != (intptr_t) item && (intptr_t) ghost != tail.data.item
&& ghost == next) {
opal_read_counted_pointer(&fifo->opal_fifo_head, &head);
continue;
}
/* try popping the head */
if (opal_update_counted_pointer(&fifo->opal_fifo_head, &head, next)) {
break;
}
} while (1);
opal_atomic_wmb();
/* check for tail and head consistency */
if (ghost == next) {
/* the head was just set to &fifo->opal_fifo_ghost. try to update the tail as well */
if (!opal_update_counted_pointer(&fifo->opal_fifo_tail, &tail, ghost)) {
/* tail was changed by a push operation. wait for the item's next pointer to be se then
* update the head */
/* wait for next pointer to be updated by push */
do {
opal_atomic_rmb();
} while (ghost == item->opal_list_next);
/* update the head with the real next value. note that no other thread
* will be attempting to update the head until after it has been updated
* with the next pointer. push will not see an empty list and other pop
* operations will loop until the head is consistent. */
fifo->opal_fifo_head.data.item = (intptr_t) item->opal_list_next;
opal_atomic_wmb();
}
}
item->opal_list_next = NULL;
return item;
}
#else
/* When compare-and-set 128 is not available we avoid the ABA problem by
* using a spin-lock on the head (using the head counter). Otherwise
* the algorithm is identical to the compare-and-set 128 version. */
static inline opal_list_item_t *opal_fifo_push_atomic(opal_fifo_t *fifo, opal_list_item_t *item)
{
const opal_list_item_t *const ghost = &fifo->opal_fifo_ghost;
opal_list_item_t *tail_item;
item->opal_list_next = (opal_list_item_t *) ghost;
opal_atomic_wmb();
/* try to get the tail */
tail_item = (opal_list_item_t *) opal_atomic_swap_ptr(&fifo->opal_fifo_tail.data.item,
(intptr_t) item);
opal_atomic_wmb();
if (ghost == tail_item) {
/* update the head */
fifo->opal_fifo_head.data.item = (intptr_t) item;
} else {
/* update previous item */
tail_item->opal_list_next = item;
}
opal_atomic_wmb();
return (opal_list_item_t *) tail_item;
}
/* Retrieve one element from the FIFO. If we reach the ghost element then the FIFO
* is empty so we return NULL.
*/
static inline opal_list_item_t *opal_fifo_pop_atomic(opal_fifo_t *fifo)
{
const opal_list_item_t *const ghost = &fifo->opal_fifo_ghost;
# if OPAL_HAVE_ATOMIC_LLSC_PTR
register opal_list_item_t *item, *next;
int attempt = 0, ret = 0;
/* use load-linked store-conditional to avoid ABA issues */
do {
if (++attempt == 5) {
/* deliberately suspend this thread to allow other threads to run. this should
* only occur during periods of contention on the lifo. */
_opal_lifo_release_cpu();
attempt = 0;
}
opal_atomic_ll_ptr(&fifo->opal_fifo_head.data.item, item);
if (ghost == item) {
if ((intptr_t) ghost == fifo->opal_fifo_tail.data.item) {
return NULL;
}
/* fifo does not appear empty. wait for the fifo to be made
* consistent by conflicting thread. */
continue;
}
next = (opal_list_item_t *) item->opal_list_next;
opal_atomic_sc_ptr(&fifo->opal_fifo_head.data.item, next, ret);
} while (!ret);
# else
opal_list_item_t *item, *next;
/* protect against ABA issues by "locking" the head */
do {
if (!opal_atomic_swap_32((opal_atomic_int32_t *) &fifo->opal_fifo_head.data.counter, 1)) {
break;
}
opal_atomic_wmb();
} while (1);
opal_atomic_wmb();
item = opal_fifo_head(fifo);
if (ghost == item) {
fifo->opal_fifo_head.data.counter = 0;
return NULL;
}
next = (opal_list_item_t *) item->opal_list_next;
fifo->opal_fifo_head.data.item = (uintptr_t) next;
# endif
if (ghost == next) {
void *tmp = item;
if (!opal_atomic_compare_exchange_strong_ptr(&fifo->opal_fifo_tail.data.item,
(intptr_t *) &tmp, (intptr_t) ghost)) {
do {
opal_atomic_rmb();
} while (ghost == item->opal_list_next);
fifo->opal_fifo_head.data.item = (intptr_t) item->opal_list_next;
}
}
opal_atomic_wmb();
/* unlock the head */
fifo->opal_fifo_head.data.counter = 0;
item->opal_list_next = NULL;
return item;
}
#endif
/* single threaded versions of push/pop */
static inline opal_list_item_t *opal_fifo_push_st(opal_fifo_t *fifo, opal_list_item_t *item)
{
opal_list_item_t *prev = opal_fifo_tail(fifo);
item->opal_list_next = &fifo->opal_fifo_ghost;
fifo->opal_fifo_tail.data.item = (intptr_t) item;
if (&fifo->opal_fifo_ghost == opal_fifo_head(fifo)) {
fifo->opal_fifo_head.data.item = (intptr_t) item;
} else {
prev->opal_list_next = item;
}
return (opal_list_item_t *) item->opal_list_next;
}
static inline opal_list_item_t *opal_fifo_pop_st(opal_fifo_t *fifo)
{
opal_list_item_t *item = opal_fifo_head(fifo);
if (item == &fifo->opal_fifo_ghost) {
return NULL;
}
fifo->opal_fifo_head.data.item = (intptr_t) item->opal_list_next;
if (&fifo->opal_fifo_ghost == opal_fifo_head(fifo)) {
fifo->opal_fifo_tail.data.item = (intptr_t) &fifo->opal_fifo_ghost;
}
item->opal_list_next = NULL;
return item;
}
/* push/pop versions conditioned off opal_using_threads() */
static inline opal_list_item_t *opal_fifo_push(opal_fifo_t *fifo, opal_list_item_t *item)
{
if (opal_using_threads()) {
return opal_fifo_push_atomic(fifo, item);
}
return opal_fifo_push_st(fifo, item);
}
static inline opal_list_item_t *opal_fifo_pop(opal_fifo_t *fifo)
{
if (opal_using_threads()) {
return opal_fifo_pop_atomic(fifo);
}
return opal_fifo_pop_st(fifo);
}
END_C_DECLS
#endif /* OPAL_FIFO_H_HAS_BEEN_INCLUDED */
|