| 1 |
/* |
| 2 |
* Copyright (C) by Argonne National Laboratory |
| 3 |
* See COPYRIGHT in top-level directory |
| 4 |
*/ |
| 5 |
|
| 6 |
/* TODO figure out how to rewrite some/all of this queue code to use |
| 7 |
* explicit relaxed atomic operations */ |
| 8 |
|
| 9 |
#ifndef MPID_NEM_QUEUE_H_INCLUDED |
| 10 |
#define MPID_NEM_QUEUE_H_INCLUDED |
| 11 |
#include "mpid_nem_datatypes.h" |
| 12 |
#include "mpid_nem_defs.h" |
| 13 |
#include "mpid_nem_atomics.h" |
| 14 |
|
| 15 |
int MPID_nem_network_poll(int in_blocking_progress); |
| 16 |
|
| 17 |
/* Assertion macros for nemesis queues. We don't use the normal |
| 18 |
* assertion macros because we don't usually want to assert several |
| 19 |
* times per queue operation. These assertions serve more as structured |
| 20 |
* comments that can easily transformed into being real assertions */ |
| 21 |
#if 0 |
| 22 |
#define MPID_nem_q_assert(a_) \ |
| 23 |
do { \ |
| 24 |
if (unlikely(!(a_))) { \ |
| 25 |
MPID_nem_q_assert_fail(#a_, __FILE__, __LINE__); \ |
| 26 |
} \ |
| 27 |
} while (0) |
| 28 |
#define MPID_nem_q_assert_fail(a_str_, file_, line_) \ |
| 29 |
do {/*nothing*/} while(0) |
| 30 |
#else |
| 31 |
#define MPID_nem_q_assert(a_) \ |
| 32 |
do {/*nothing*/} while (0) |
| 33 |
#endif |
| 34 |
|
| 35 |
static inline void MPID_nem_cell_init(MPID_nem_cell_ptr_t cell) |
| 36 |
{ |
| 37 |
|
| 38 |
MPIR_FUNC_ENTER; |
| 39 |
|
| 40 |
MPID_NEM_SET_REL_NULL(cell->next); |
| 41 |
memset((void *)&cell->header, 0, sizeof(MPID_nem_pkt_header_t)); |
| 42 |
|
| 43 |
MPIR_FUNC_EXIT; |
| 44 |
} |
| 45 |
|
| 46 |
static inline void MPID_nem_queue_init(MPID_nem_queue_ptr_t qhead) |
| 47 |
{ |
| 48 |
|
| 49 |
MPIR_FUNC_ENTER; |
| 50 |
|
| 51 |
MPID_NEM_SET_REL_NULL(qhead->head); |
| 52 |
MPID_NEM_SET_REL_NULL(qhead->my_head); |
| 53 |
MPID_NEM_SET_REL_NULL(qhead->tail); |
| 54 |
|
| 55 |
MPIR_FUNC_EXIT; |
| 56 |
} |
| 57 |
|
| 58 |
#define MPID_NEM_USE_SHADOW_HEAD |
| 59 |
|
| 60 |
static inline MPID_nem_cell_rel_ptr_t MPID_NEM_SWAP_REL (MPID_nem_cell_rel_ptr_t *ptr, MPID_nem_cell_rel_ptr_t val) |
| 61 |
{ |
| 62 |
MPID_nem_cell_rel_ptr_t ret; |
| 63 |
MPL_atomic_release_store_ptr(&ret.p, MPL_atomic_swap_ptr(&(ptr->p), MPL_atomic_acquire_load_ptr(&val.p))); |
| 64 |
return ret; |
| 65 |
} |
| 66 |
|
| 67 |
/* do a compare-and-swap with MPID_NEM_RELNULL */ |
| 68 |
static inline MPID_nem_cell_rel_ptr_t MPID_NEM_CAS_REL_NULL (MPID_nem_cell_rel_ptr_t *ptr, MPID_nem_cell_rel_ptr_t oldv) |
| 69 |
{ |
| 70 |
MPID_nem_cell_rel_ptr_t ret; |
| 71 |
MPL_atomic_release_store_ptr(&ret.p, MPL_atomic_cas_ptr(&(ptr->p), MPL_atomic_acquire_load_ptr(&oldv.p), MPID_NEM_REL_NULL)); |
| 72 |
return ret; |
| 73 |
} |
| 74 |
|
| 75 |
static inline void |
| 76 |
MPID_nem_queue_enqueue (MPID_nem_queue_ptr_t qhead, MPID_nem_cell_ptr_t element) |
| 77 |
{ |
| 78 |
MPID_nem_cell_rel_ptr_t r_prev; |
| 79 |
MPID_nem_cell_rel_ptr_t r_element = MPID_NEM_ABS_TO_REL (element); |
| 80 |
|
| 81 |
/* the _dequeue can break if this does not hold */ |
| 82 |
MPID_nem_q_assert(MPID_NEM_IS_REL_NULL(element->next)); |
| 83 |
|
| 84 |
/* Orders payload and e->next=NULL w.r.t. the SWAP, updating head, and |
| 85 |
* updating prev->next. We assert e->next==NULL above, but it may have been |
| 86 |
* done by us in the preceding _dequeue operation. |
| 87 |
* |
| 88 |
* The SWAP itself does not need to be ordered w.r.t. the payload because |
| 89 |
* the consumer does not directly inspect the tail. But the subsequent |
| 90 |
* update to the head or e->next field does need to be ordered w.r.t. the |
| 91 |
* payload or the consumer may read incorrect data. */ |
| 92 |
MPL_atomic_write_barrier(); |
| 93 |
|
| 94 |
/* enqueue at tail */ |
| 95 |
r_prev = MPID_NEM_SWAP_REL (&(qhead->tail), r_element); |
| 96 |
if (MPID_NEM_IS_REL_NULL (r_prev)) |
| 97 |
{ |
| 98 |
/* queue was empty, element is the new head too */ |
| 99 |
|
| 100 |
/* no write barrier needed, we believe atomic SWAP with a control |
| 101 |
* dependence (if) will enforce ordering between the SWAP and the head |
| 102 |
* assignment */ |
| 103 |
qhead->head = r_element; |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
/* queue was not empty, swing old tail's next field to point to |
| 108 |
* our element */ |
| 109 |
MPID_nem_q_assert(MPID_NEM_IS_REL_NULL(MPID_NEM_REL_TO_ABS(r_prev)->next)); |
| 110 |
|
| 111 |
/* no write barrier needed, we believe atomic SWAP with a control |
| 112 |
* dependence (if/else) will enforce ordering between the SWAP and the |
| 113 |
* prev->next assignment */ |
| 114 |
MPID_NEM_REL_TO_ABS (r_prev)->next = r_element; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/* This operation is only safe because this is a single-dequeuer queue impl. |
| 119 |
Assumes that MPID_nem_queue_empty was called immediately prior to fix up any |
| 120 |
shadow head issues. */ |
| 121 |
static inline MPID_nem_cell_ptr_t |
| 122 |
MPID_nem_queue_head (MPID_nem_queue_ptr_t qhead) |
| 123 |
{ |
| 124 |
MPID_nem_q_assert(MPID_NEM_IS_REL_NULL(qhead->head)); |
| 125 |
return MPID_NEM_REL_TO_ABS(qhead->my_head); |
| 126 |
} |
| 127 |
|
| 128 |
static inline int |
| 129 |
MPID_nem_queue_empty (MPID_nem_queue_ptr_t qhead) |
| 130 |
{ |
| 131 |
/* outside of this routine my_head and head should never both |
| 132 |
* contain a non-null value */ |
| 133 |
MPID_nem_q_assert(MPID_NEM_IS_REL_NULL(qhead->my_head) || |
| 134 |
MPID_NEM_IS_REL_NULL(qhead->head)); |
| 135 |
|
| 136 |
if (MPID_NEM_IS_REL_NULL (qhead->my_head)) |
| 137 |
{ |
| 138 |
/* the order of comparison between my_head and head does not |
| 139 |
* matter, no read barrier needed here */ |
| 140 |
if (MPID_NEM_IS_REL_NULL (qhead->head)) |
| 141 |
{ |
| 142 |
/* both null, nothing in queue */ |
| 143 |
return 1; |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
/* shadow head null and head has value, move the value to |
| 148 |
* our private shadow head and zero the real head */ |
| 149 |
qhead->my_head = qhead->head; |
| 150 |
/* no barrier needed, my_head is entirely private to consumer */ |
| 151 |
MPID_NEM_SET_REL_NULL (qhead->head); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/* the following assertions are present at the beginning of _dequeue: |
| 156 |
MPID_nem_q_assert(!MPID_NEM_IS_REL_NULL(qhead->my_head)); |
| 157 |
MPID_nem_q_assert( MPID_NEM_IS_REL_NULL(qhead->head)); |
| 158 |
*/ |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
/* Gets the head */ |
| 164 |
static inline void |
| 165 |
MPID_nem_queue_dequeue (MPID_nem_queue_ptr_t qhead, MPID_nem_cell_ptr_t *e) |
| 166 |
{ |
| 167 |
MPID_nem_cell_ptr_t _e; |
| 168 |
MPID_nem_cell_rel_ptr_t _r_e; |
| 169 |
|
| 170 |
/* _empty always called first, moving head-->my_head */ |
| 171 |
MPID_nem_q_assert(!MPID_NEM_IS_REL_NULL(qhead->my_head)); |
| 172 |
MPID_nem_q_assert( MPID_NEM_IS_REL_NULL(qhead->head)); |
| 173 |
|
| 174 |
_r_e = qhead->my_head; |
| 175 |
_e = MPID_NEM_REL_TO_ABS (_r_e); |
| 176 |
|
| 177 |
/* no barrier needed, my_head is private to consumer, plus |
| 178 |
* head/my_head and _e->next are ordered by a data dependency */ |
| 179 |
if (!MPID_NEM_IS_REL_NULL(_e->next)) |
| 180 |
{ |
| 181 |
qhead->my_head = _e->next; |
| 182 |
} |
| 183 |
else |
| 184 |
{ |
| 185 |
/* we've reached the end (tail) of the queue */ |
| 186 |
MPID_nem_cell_rel_ptr_t old_tail; |
| 187 |
|
| 188 |
MPID_NEM_SET_REL_NULL (qhead->my_head); |
| 189 |
/* no barrier needed, the caller doesn't need any ordering w.r.t. |
| 190 |
* my_head or the tail */ |
| 191 |
old_tail = MPID_NEM_CAS_REL_NULL (&(qhead->tail), _r_e); |
| 192 |
|
| 193 |
if (!MPID_NEM_REL_ARE_EQUAL (old_tail, _r_e)) |
| 194 |
{ |
| 195 |
/* FIXME is a barrier needed here because of the control-only dependency? */ |
| 196 |
while (MPID_NEM_IS_REL_NULL (_e->next)) |
| 197 |
{ |
| 198 |
SKIP; |
| 199 |
} |
| 200 |
/* no read barrier needed between loads from the same location */ |
| 201 |
qhead->my_head = _e->next; |
| 202 |
} |
| 203 |
} |
| 204 |
MPID_NEM_SET_REL_NULL (_e->next); |
| 205 |
|
| 206 |
/* Conservative read barrier here to ensure loads from head are ordered |
| 207 |
* w.r.t. payload reads by the caller. The McKenney "whymb" document's |
| 208 |
* Figure 11 indicates that we don't need a barrier, but we are currently |
| 209 |
* unconvinced of this. Further work, ideally using more formal methods, |
| 210 |
* should justify removing this. (note that this barrier won't cost us |
| 211 |
* anything on many platforms, esp. x86) */ |
| 212 |
MPL_atomic_read_barrier(); |
| 213 |
|
| 214 |
*e = _e; |
| 215 |
} |
| 216 |
|
| 217 |
#endif /* MPID_NEM_QUEUE_H_INCLUDED */ |