Skip to content

Commit 3759195

Browse files
Copilotdevreal
andcommitted
Implement C11 atomic wrapper structs and fix direct access issues
Co-authored-by: devreal <[email protected]>
1 parent 0c0f801 commit 3759195

File tree

12 files changed

+269
-97
lines changed

12 files changed

+269
-97
lines changed

ompi/runtime/ompi_mpi_init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ const char ompi_version_string[] = OMPI_IDENT_STRING;
125125
* Global variables and symbols for the MPI layer
126126
*/
127127

128-
opal_atomic_int32_t ompi_mpi_state = OMPI_MPI_STATE_NOT_INITIALIZED;
128+
opal_atomic_int32_t ompi_mpi_state = { .value = OMPI_MPI_STATE_NOT_INITIALIZED };
129129
volatile bool ompi_rte_initialized = false;
130130

131131
bool ompi_mpi_thread_multiple = false;
@@ -371,7 +371,7 @@ int ompi_mpi_init(int argc, char **argv, int requested, int *provided,
371371
// silently return successfully once the initializing
372372
// thread has completed.
373373
if (reinit_ok) {
374-
while (ompi_mpi_state < OMPI_MPI_STATE_INIT_COMPLETED) {
374+
while (opal_atomic_load_32_relaxed(&ompi_mpi_state) < OMPI_MPI_STATE_INIT_COMPLETED) {
375375
usleep(1);
376376
}
377377
return MPI_SUCCESS;

opal/class/opal_lifo.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static inline bool opal_update_counted_pointer(volatile opal_counted_pointer_t *
7070
opal_counted_pointer_t *old, opal_list_item_t *item)
7171
{
7272
opal_counted_pointer_t new_p;
73-
new_p.data.item = (intptr_t) item;
73+
opal_atomic_store_ptr_relaxed(&new_p.data.item, (intptr_t) item);
7474
new_p.data.counter = old->data.counter + 1;
7575
return opal_atomic_compare_exchange_strong_128(&addr->atomic_value, &old->value, new_p.value);
7676
}
@@ -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-
value->data.item = addr->data.item;
86+
opal_atomic_store_ptr_relaxed(&value->data.item, opal_atomic_load_ptr_relaxed(&addr->data.item));
8787
}
8888

8989
#endif
@@ -122,7 +122,7 @@ OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_lifo_t);
122122
*/
123123
static inline bool opal_lifo_is_empty(opal_lifo_t *lifo)
124124
{
125-
return (opal_list_item_t *) lifo->opal_lifo_head.data.item == &lifo->opal_lifo_ghost;
125+
return (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&lifo->opal_lifo_head.data.item) == &lifo->opal_lifo_ghost;
126126
}
127127

128128
#if OPAL_HAVE_ATOMIC_COMPARE_EXCHANGE_128 && !OPAL_HAVE_ATOMIC_LLSC_PTR
@@ -133,7 +133,7 @@ static inline bool opal_lifo_is_empty(opal_lifo_t *lifo)
133133
*/
134134
static inline opal_list_item_t *opal_lifo_push_atomic(opal_lifo_t *lifo, opal_list_item_t *item)
135135
{
136-
opal_list_item_t *next = (opal_list_item_t *) lifo->opal_lifo_head.data.item;
136+
opal_list_item_t *next = (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&lifo->opal_lifo_head.data.item);
137137

138138
do {
139139
item->opal_list_next = next;
@@ -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 *) old_head.data.item;
162+
item = (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&old_head.data.item);
163163
if (item == &lifo->opal_lifo_ghost) {
164164
return NULL;
165165
}
@@ -181,7 +181,7 @@ static inline opal_list_item_t *opal_lifo_pop_atomic(opal_lifo_t *lifo)
181181
*/
182182
static inline opal_list_item_t *opal_lifo_push_atomic(opal_lifo_t *lifo, opal_list_item_t *item)
183183
{
184-
opal_list_item_t *next = (opal_list_item_t *) lifo->opal_lifo_head.data.item;
184+
opal_list_item_t *next = (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&lifo->opal_lifo_head.data.item);
185185

186186
/* item free acts as a mini lock to avoid ABA problems */
187187
item->item_free = 1;
@@ -218,13 +218,13 @@ static inline opal_list_item_t *opal_lifo_pop_atomic(opal_lifo_t *lifo)
218218
attempt = 0;
219219
}
220220

221-
opal_atomic_ll_ptr(&lifo->opal_lifo_head.data.item, item);
221+
opal_atomic_ll_ptr(&lifo->opal_lifo_head.data.item.value, item);
222222
if (&lifo->opal_lifo_ghost == item) {
223223
return NULL;
224224
}
225225

226226
next = (opal_list_item_t *) item->opal_list_next;
227-
opal_atomic_sc_ptr(&lifo->opal_lifo_head.data.item, next, ret);
227+
opal_atomic_sc_ptr(&lifo->opal_lifo_head.data.item.value, next, ret);
228228
} while (!ret);
229229

230230
opal_atomic_wmb();
@@ -242,7 +242,7 @@ static inline opal_list_item_t *opal_lifo_pop_atomic(opal_lifo_t *lifo)
242242
{
243243
opal_list_item_t *item, *head, *ghost = &lifo->opal_lifo_ghost;
244244

245-
while ((item = (opal_list_item_t *) lifo->opal_lifo_head.data.item) != ghost) {
245+
while ((item = (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&lifo->opal_lifo_head.data.item)) != ghost) {
246246
/* ensure it is safe to pop the head */
247247
if (opal_atomic_swap_32((opal_atomic_int32_t *) &item->item_free, 1)) {
248248
continue;
@@ -282,17 +282,17 @@ static inline opal_list_item_t *opal_lifo_pop_atomic(opal_lifo_t *lifo)
282282
/* single-threaded versions of the lifo functions */
283283
static inline opal_list_item_t *opal_lifo_push_st(opal_lifo_t *lifo, opal_list_item_t *item)
284284
{
285-
item->opal_list_next = (opal_list_item_t *) lifo->opal_lifo_head.data.item;
285+
item->opal_list_next = (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&lifo->opal_lifo_head.data.item);
286286
item->item_free = 0;
287-
lifo->opal_lifo_head.data.item = (intptr_t) item;
287+
opal_atomic_store_ptr_relaxed(&lifo->opal_lifo_head.data.item, (intptr_t) item);
288288
return (opal_list_item_t *) item->opal_list_next;
289289
}
290290

291291
static inline opal_list_item_t *opal_lifo_pop_st(opal_lifo_t *lifo)
292292
{
293293
opal_list_item_t *item;
294-
item = (opal_list_item_t *) lifo->opal_lifo_head.data.item;
295-
lifo->opal_lifo_head.data.item = (intptr_t) item->opal_list_next;
294+
item = (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&lifo->opal_lifo_head.data.item);
295+
opal_atomic_store_ptr_relaxed(&lifo->opal_lifo_head.data.item, (intptr_t) item->opal_list_next);
296296
if (item == &lifo->opal_lifo_ghost) {
297297
return NULL;
298298
}

opal/class/opal_list.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ typedef struct opal_list_t opal_list_t;
168168
#define OPAL_LIST_DESTRUCT(list) \
169169
do { \
170170
opal_list_item_t *it; \
171-
if (1 == ((opal_object_t *) (list))->obj_reference_count) { \
171+
if (1 == opal_atomic_load_32_relaxed(&((opal_object_t *) (list))->obj_reference_count)) { \
172172
while (NULL != (it = opal_list_remove_first(list))) { \
173173
OBJ_RELEASE(it); \
174174
} \
@@ -179,7 +179,7 @@ typedef struct opal_list_t opal_list_t;
179179
#define OPAL_LIST_RELEASE(list) \
180180
do { \
181181
opal_list_item_t *it; \
182-
if (1 == ((opal_object_t *) (list))->obj_reference_count) { \
182+
if (1 == opal_atomic_load_32_relaxed(&((opal_object_t *) (list))->obj_reference_count)) { \
183183
while (NULL != (it = opal_list_remove_first(list))) { \
184184
OBJ_RELEASE(it); \
185185
} \

opal/class/opal_object.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ extern int opal_class_init_epoch;
172172
# define OPAL_OBJ_STATIC_INIT(BASE_CLASS) \
173173
{ \
174174
.obj_magic_id = OPAL_OBJ_MAGIC_ID, .obj_class = OBJ_CLASS(BASE_CLASS), \
175-
.obj_reference_count = 1, .cls_init_file_name = __FILE__, .cls_init_lineno = __LINE__, \
175+
.obj_reference_count = { .value = 1 }, .cls_init_file_name = __FILE__, .cls_init_lineno = __LINE__, \
176176
}
177177
#else
178178
# define OPAL_OBJ_STATIC_INIT(BASE_CLASS) \
179179
{ \
180-
.obj_class = OBJ_CLASS(BASE_CLASS), .obj_reference_count = 1, \
180+
.obj_class = OBJ_CLASS(BASE_CLASS), .obj_reference_count = { .value = 1 }, \
181181
}
182182
#endif
183183

@@ -275,7 +275,7 @@ static inline opal_object_t *opal_obj_new_debug(opal_class_t *type, const char *
275275
assert(NULL != ((opal_object_t *) (object))->obj_class); \
276276
assert(OPAL_OBJ_MAGIC_ID == ((opal_object_t *) (object))->obj_magic_id); \
277277
opal_obj_update((opal_object_t *) (object), 1); \
278-
assert(((opal_object_t *) (object))->obj_reference_count >= 0); \
278+
assert(opal_atomic_load_32_relaxed(&((opal_object_t *) (object))->obj_reference_count) >= 0); \
279279
} while (0)
280280
#else
281281
# define OBJ_RETAIN(object) opal_obj_update((opal_object_t *) (object), 1);
@@ -377,7 +377,7 @@ static inline opal_object_t *opal_obj_new_debug(opal_class_t *type, const char *
377377
opal_class_initialize((type)); \
378378
} \
379379
((opal_object_t *) (object))->obj_class = (type); \
380-
((opal_object_t *) (object))->obj_reference_count = 1; \
380+
opal_atomic_store_32_relaxed(&((opal_object_t *) (object))->obj_reference_count, 1); \
381381
opal_obj_run_constructors((opal_object_t *) (object)); \
382382
OBJ_REMEMBER_FILE_AND_LINENO(object, __FILE__, __LINE__); \
383383
} while (0)
@@ -499,7 +499,7 @@ static inline opal_object_t *opal_obj_new(opal_class_t *cls)
499499
}
500500
if (NULL != object) {
501501
object->obj_class = cls;
502-
object->obj_reference_count = 1;
502+
opal_atomic_store_32_relaxed(&object->obj_reference_count, 1);
503503
opal_obj_run_constructors(object);
504504
}
505505
return object;

opal/datatype/opal_datatype_destroy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int32_t opal_datatype_destroy(opal_datatype_t **dt)
2727
{
2828
opal_datatype_t *pData = *dt;
2929

30-
if ((pData->flags & OPAL_DATATYPE_FLAG_PREDEFINED) && (pData->super.obj_reference_count <= 1)) {
30+
if ((pData->flags & OPAL_DATATYPE_FLAG_PREDEFINED) && (opal_atomic_load_32_relaxed(&pData->super.obj_reference_count) <= 1)) {
3131
return OPAL_ERROR;
3232
}
3333

opal/include/opal/sys/atomic_impl_math.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
{ \
3333
type oldval; \
3434
do { \
35-
oldval = *addr; \
35+
oldval = opal_atomic_load_##bits##_relaxed(addr); \
3636
} while (!opal_atomic_compare_exchange_strong_##bits(addr, &oldval, \
3737
oldval operation value)); \
3838
\
@@ -43,9 +43,9 @@
4343
{ \
4444
type oldval, newval; \
4545
do { \
46-
oldval = *addr; \
46+
oldval = opal_atomic_load_##bits##_relaxed(addr); \
4747
newval = oldval operation value; \
48-
} while (!opal_atomic_compare_exchange_strong_##bits(addr, &oldval, newval); \
48+
} while (!opal_atomic_compare_exchange_strong_##bits(addr, &oldval, newval)); \
4949
\
5050
return newval; \
5151
}

opal/include/opal/sys/atomic_impl_minmax_math.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
static inline int32_t opal_atomic_fetch_min_32(opal_atomic_int32_t *addr, int32_t value)
3333
{
34-
int32_t old = *addr;
34+
int32_t old = opal_atomic_load_32_relaxed(addr);
3535
do {
3636
if (old <= value) {
3737
break;
@@ -49,7 +49,7 @@ static inline int32_t opal_atomic_min_fetch_32(opal_atomic_int32_t *addr, int32_
4949

5050
static inline int32_t opal_atomic_fetch_max_32(opal_atomic_int32_t *addr, int32_t value)
5151
{
52-
int32_t old = *addr;
52+
int32_t old = opal_atomic_load_32_relaxed(addr);
5353
do {
5454
if (old >= value) {
5555
break;
@@ -67,7 +67,7 @@ static inline int32_t opal_atomic_max_fetch_32(opal_atomic_int32_t *addr, int32_
6767

6868
static inline int64_t opal_atomic_fetch_min_64(opal_atomic_int64_t *addr, int64_t value)
6969
{
70-
int64_t old = *addr;
70+
int64_t old = opal_atomic_load_64_relaxed(addr);
7171
do {
7272
if (old <= value) {
7373
break;
@@ -79,7 +79,7 @@ static inline int64_t opal_atomic_fetch_min_64(opal_atomic_int64_t *addr, int64_
7979

8080
static inline int64_t opal_atomic_fetch_max_64(opal_atomic_int64_t *addr, int64_t value)
8181
{
82-
int64_t old = *addr;
82+
int64_t old = opal_atomic_load_64_relaxed(addr);
8383
do {
8484
if (old >= value) {
8585
break;

0 commit comments

Comments
 (0)