Skip to content

Commit d49a9f8

Browse files
Copilotdevreal
andcommitted
Complete C11 atomic wrapper implementation - working and preventing direct access
Co-authored-by: devreal <10974502+devreal@users.noreply.github.com>
1 parent f81e3ed commit d49a9f8

File tree

5 files changed

+17
-12
lines changed

5 files changed

+17
-12
lines changed

opal/class/opal_lifo.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ opal_read_counted_pointer(volatile opal_counted_pointer_t *volatile addr,
8383
* specific order */
8484
value->data.counter = addr->data.counter;
8585
opal_atomic_rmb();
86-
opal_atomic_store_ptr_relaxed(&value->data.item, opal_atomic_load_ptr_relaxed(&addr->data.item));
86+
opal_atomic_store_ptr_volatile_relaxed(&value->data.item, opal_atomic_load_ptr_volatile_relaxed(&addr->data.item));
8787
}
8888

8989
#endif
@@ -140,7 +140,7 @@ static inline opal_list_item_t *opal_lifo_push_atomic(opal_lifo_t *lifo, opal_li
140140
opal_atomic_wmb();
141141

142142
/* to protect against ABA issues it is sufficient to only update the counter in pop */
143-
if (opal_atomic_compare_exchange_strong_ptr(&lifo->opal_lifo_head.data.item,
143+
if (opal_atomic_compare_exchange_strong_ptr_volatile(&lifo->opal_lifo_head.data.item,
144144
(intptr_t *) &next, (intptr_t) item)) {
145145
return next;
146146
}
@@ -159,7 +159,7 @@ static inline opal_list_item_t *opal_lifo_pop_atomic(opal_lifo_t *lifo)
159159
opal_read_counted_pointer(&lifo->opal_lifo_head, &old_head);
160160

161161
do {
162-
item = (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&old_head.data.item);
162+
item = (opal_list_item_t *) opal_atomic_load_ptr_volatile_relaxed(&old_head.data.item);
163163
if (item == &lifo->opal_lifo_ghost) {
164164
return NULL;
165165
}
@@ -189,7 +189,7 @@ static inline opal_list_item_t *opal_lifo_push_atomic(opal_lifo_t *lifo, opal_li
189189
do {
190190
item->opal_list_next = next;
191191
opal_atomic_wmb();
192-
if (opal_atomic_compare_exchange_strong_ptr(&lifo->opal_lifo_head.data.item,
192+
if (opal_atomic_compare_exchange_strong_ptr_volatile(&lifo->opal_lifo_head.data.item,
193193
(intptr_t *) &next, (intptr_t) item)) {
194194
opal_atomic_wmb();
195195
/* now safe to pop this item */
@@ -252,7 +252,7 @@ static inline opal_list_item_t *opal_lifo_pop_atomic(opal_lifo_t *lifo)
252252

253253
head = item;
254254
/* try to swap out the head pointer */
255-
if (opal_atomic_compare_exchange_strong_ptr(&lifo->opal_lifo_head.data.item,
255+
if (opal_atomic_compare_exchange_strong_ptr_volatile(&lifo->opal_lifo_head.data.item,
256256
(intptr_t *) &head,
257257
(intptr_t) item->opal_list_next)) {
258258
break;

opal/include/opal/sys/atomic_stdc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ static inline bool opal_atomic_compare_exchange_strong_ptr(opal_atomic_intptr_t
106106
return atomic_compare_exchange_strong_explicit(&(addr->value), compare, value, memory_order_relaxed, memory_order_relaxed);
107107
}
108108

109+
static inline bool opal_atomic_compare_exchange_strong_ptr_volatile(volatile opal_atomic_intptr_t *addr, intptr_t *compare, intptr_t value)
110+
{
111+
return atomic_compare_exchange_strong_explicit(&(addr->value), compare, value, memory_order_relaxed, memory_order_relaxed);
112+
}
113+
109114
static inline bool opal_atomic_compare_exchange_strong_acq_ptr(opal_atomic_intptr_t *addr, intptr_t *compare, intptr_t value)
110115
{
111116
return atomic_compare_exchange_strong_explicit(&(addr->value), compare, value, memory_order_acquire, memory_order_relaxed);

opal/mca/btl/sm/btl_sm_component.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ static void mca_btl_sm_progress_endpoints(void)
549549

550550
static int mca_btl_sm_component_progress(void)
551551
{
552-
static opal_atomic_int32_t lock = 0;
552+
static opal_atomic_int32_t lock = { .value = 0 };
553553
int count = 0;
554554

555555
if (opal_using_threads()) {
@@ -565,14 +565,14 @@ static int mca_btl_sm_component_progress(void)
565565

566566
mca_btl_sm_progress_endpoints();
567567

568-
if (SM_FIFO_FREE == mca_btl_sm_component.my_fifo->fifo_head) {
569-
lock = 0;
568+
if (SM_FIFO_FREE == opal_atomic_load_ptr_relaxed(&mca_btl_sm_component.my_fifo->fifo_head)) {
569+
opal_atomic_store_32_relaxed(&lock, 0);
570570
return count;
571571
}
572572

573573
count += mca_btl_sm_poll_fifo();
574574
opal_atomic_mb();
575-
lock = 0;
575+
opal_atomic_store_32_relaxed(&lock, 0);
576576

577577
return count;
578578
}

opal/mca/btl/sm/btl_sm_fifo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ static inline void sm_fifo_init(sm_fifo_t *fifo)
112112
* ideal but oh well. See #5814 */
113113
/* fifo->fifo_head = fifo->fifo_tail = SM_FIFO_FREE; */
114114
opal_atomic_store_ptr_relaxed(&fifo->fifo_head, SM_FIFO_FREE);
115-
fifo->fifo_tail = SM_FIFO_FREE;
116-
fifo->fbox_available = mca_btl_sm_component.fbox_max;
115+
opal_atomic_store_ptr_relaxed(&fifo->fifo_tail, SM_FIFO_FREE);
116+
opal_atomic_store_32_relaxed(&fifo->fbox_available, mca_btl_sm_component.fbox_max);
117117
mca_btl_sm_component.my_fifo = fifo;
118118
}
119119

opal/mca/mpool/hugepage/mpool_hugepage_component.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static int mca_mpool_hugepage_register(void)
131131
MCA_BASE_VAR_TYPE_INT, NULL, 0, 0, OPAL_INFO_LVL_9,
132132
MCA_BASE_VAR_SCOPE_LOCAL, &mca_mpool_hugepage_page_size);
133133

134-
mca_mpool_hugepage_component.bytes_allocated = 0;
134+
opal_atomic_store_size_t_relaxed(&mca_mpool_hugepage_component.bytes_allocated, 0);
135135
(void) mca_base_component_pvar_register(&mca_mpool_hugepage_component.super.mpool_version,
136136
"bytes_allocated",
137137
"Number of bytes currently allocated in the mpool "

0 commit comments

Comments
 (0)