Skip to content

Commit 89fc5d9

Browse files
committed
Fix atomics cross-platform
1 parent 5d7bac8 commit 89fc5d9

2 files changed

Lines changed: 62 additions & 58 deletions

File tree

include/mpmt/atomic.h

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424

2525
// TODO: compare exchange, test/set/clear, thread fences and barriers.
26+
// TODO: relaxed barrier functions.
2627

2728
#pragma once
2829

@@ -135,102 +136,102 @@
135136

136137
/***********************************************************************************************************************
137138
* @brief Atomically performs AND operation to the variable that memory points to.
138-
* @return The resulting value of the AND operation.
139+
* @return The current value of the variable that memory points to.
139140
*
140141
* @param[in,out] memory pointer of a variable to which the AND operation is to be performed
141142
* @param value variable whose value is to be used for an AND operation
142143
*/
143-
#define atomicAndFetch8(memory, value) __atomic_and_fetch(memory, value, __ATOMIC_SEQ_CST)
144+
#define atomicFetchAnd8(memory, value) __atomic_fetch_and(memory, value, __ATOMIC_SEQ_CST)
144145
/**
145146
* @brief Atomically performs AND operation to the variable that memory points to.
146-
* @return The resulting value of the AND operation.
147+
* @return The current value of the variable that memory points to.
147148
*
148149
* @param[in,out] memory pointer of a variable to which the AND operation is to be performed
149150
* @param value variable whose value is to be used for an AND operation
150151
*/
151-
#define atomicAndFetch16(memory, value) __atomic_and_fetch(memory, value, __ATOMIC_SEQ_CST)
152+
#define atomicFetchAnd16(memory, value) __atomic_fetch_and(memory, value, __ATOMIC_SEQ_CST)
152153
/**
153154
* @brief Atomically performs AND operation to the variable that memory points to.
154-
* @return The resulting value of the AND operation.
155+
* @return The current value of the variable that memory points to.
155156
*
156157
* @param[in,out] memory pointer of a variable to which the AND operation is to be performed
157158
* @param value variable whose value is to be used for an AND operation
158159
*/
159-
#define atomicAndFetch32(memory, value) __atomic_and_fetch(memory, value, __ATOMIC_SEQ_CST)
160+
#define atomicFetchAnd32(memory, value) __atomic_fetch_and(memory, value, __ATOMIC_SEQ_CST)
160161
/**
161162
* @brief Atomically performs AND operation to the variable that memory points to.
162-
* @return The resulting value of the AND operation.
163+
* @return The current value of the variable that memory points to.
163164
*
164165
* @param[in,out] memory pointer of a variable to which the AND operation is to be performed
165166
* @param value variable whose value is to be used for an AND operation
166167
*/
167-
#define atomicAndFetch64(memory, value) __atomic_and_fetch(memory, value, __ATOMIC_SEQ_CST)
168+
#define atomicFetchAnd64(memory, value) __atomic_fetch_and(memory, value, __ATOMIC_SEQ_CST)
168169

169170
/***********************************************************************************************************************
170171
* @brief Atomically performs OR operation to the variable that memory points to.
171-
* @return The resulting value of the OR operation.
172+
* @return The current value of the variable that memory points to.
172173
*
173174
* @param[in,out] memory pointer of a variable to which the OR operation is to be performed
174175
* @param value variable whose value is to be used for an OR operation
175176
*/
176-
#define atomicOrFetch8(memory, value) __atomic_or_fetch(memory, value, __ATOMIC_SEQ_CST)
177+
#define atomicFetchOr8(memory, value) __atomic_fetch_or(memory, value, __ATOMIC_SEQ_CST)
177178
/**
178179
* @brief Atomically performs OR operation to the variable that memory points to.
179-
* @return The resulting value of the OR operation.
180+
* @return The current value of the variable that memory points to.
180181
*
181182
* @param[in,out] memory pointer of a variable to which the OR operation is to be performed
182183
* @param value variable whose value is to be used for an OR operation
183184
*/
184-
#define atomicOrFetch16(memory, value) __atomic_or_fetch(memory, value, __ATOMIC_SEQ_CST)
185+
#define atomicFetchOr16(memory, value) __atomic_fetch_or(memory, value, __ATOMIC_SEQ_CST)
185186
/**
186187
* @brief Atomically performs OR operation to the variable that memory points to.
187-
* @return The resulting value of the OR operation.
188+
* @return The current value of the variable that memory points to.
188189
*
189190
* @param[in,out] memory pointer of a variable to which the OR operation is to be performed
190191
* @param value variable whose value is to be used for an OR operation
191192
*/
192-
#define atomicOrFetch32(memory, value) __atomic_or_fetch(memory, value, __ATOMIC_SEQ_CST)
193+
#define atomicFetchOr32(memory, value) __atomic_fetch_or(memory, value, __ATOMIC_SEQ_CST)
193194
/**
194195
* @brief Atomically performs OR operation to the variable that memory points to.
195-
* @return The resulting value of the OR operation.
196+
* @return The current value of the variable that memory points to.
196197
*
197198
* @param[in,out] memory pointer of a variable to which the OR operation is to be performed
198199
* @param value variable whose value is to be used for an OR operation
199200
*/
200-
#define atomicOrFetch64(memory, value) __atomic_or_fetch(memory, value, __ATOMIC_SEQ_CST)
201+
#define atomicFetchOr64(memory, value) __atomic_fetch_or(memory, value, __ATOMIC_SEQ_CST)
201202

202203
/***********************************************************************************************************************
203204
* @brief Atomically performs XOR operation to the variable that memory points to.
204-
* @return The resulting value of the XOR operation.
205+
* @return The current value of the variable that memory points to.
205206
*
206207
* @param[in,out] memory pointer of a variable to which the XOR operation is to be performed
207208
* @param value variable whose value is to be used for an XOR operation
208209
*/
209-
#define atomicXorFetch8(memory, value) __atomic_xor_fetch(memory, value, __ATOMIC_SEQ_CST)
210+
#define atomicFetchXor8(memory, value) __atomic_fetch_xor(memory, value, __ATOMIC_SEQ_CST)
210211
/**
211212
* @brief Atomically performs XOR operation to the variable that memory points to.
212-
* @return The resulting value of the XOR operation.
213+
* @return The current value of the variable that memory points to.
213214
*
214215
* @param[in,out] memory pointer of a variable to which the XOR operation is to be performed
215216
* @param value variable whose value is to be used for an XOR operation
216217
*/
217-
#define atomicXorFetch16(memory, value) __atomic_xor_fetch(memory, value, __ATOMIC_SEQ_CST)
218+
#define atomicFetchXor16(memory, value) __atomic_fetch_xor(memory, value, __ATOMIC_SEQ_CST)
218219
/**
219220
* @brief Atomically performs XOR operation to the variable that memory points to.
220-
* @return The resulting value of the XOR operation.
221+
* @return The current value of the variable that memory points to.
221222
*
222223
* @param[in,out] memory pointer of a variable to which the XOR operation is to be performed
223224
* @param value variable whose value is to be used for an XOR operation
224225
*/
225-
#define atomicXorFetch32(memory, value) __atomic_xor_fetch(memory, value, __ATOMIC_SEQ_CST)
226+
#define atomicFetchXor32(memory, value) __atomic_fetch_xor(memory, value, __ATOMIC_SEQ_CST)
226227
/**
227228
* @brief Atomically performs XOR operation to the variable that memory points to.
228-
* @return The resulting value of the XOR operation.
229+
* @return The current value of the variable that memory points to.
229230
*
230231
* @param[in,out] memory pointer of a variable to which the XOR operation is to be performed
231232
* @param value variable whose value is to be used for an XOR operation
232233
*/
233-
#define atomicXorFetch64(memory, value) __atomic_xor_fetch(memory, value, __ATOMIC_SEQ_CST)
234+
#define atomicFetchXor64(memory, value) __atomic_fetch_xor(memory, value, __ATOMIC_SEQ_CST)
234235

235236
/***********************************************************************************************************************
236237
* @brief Atomically adds the value to the variable that memory points to.
@@ -411,102 +412,102 @@ static inline void atomicStore64(atomic_int64* memory, atomic_int64 value)
411412

412413
/***********************************************************************************************************************
413414
* @brief Atomically performs AND operation to the variable that memory points to.
414-
* @return The resulting value of the AND operation.
415+
* @return The current value of the variable that memory points to.
415416
*
416417
* @param[in,out] memory pointer of a variable to which the AND operation is to be performed
417418
* @param value variable whose value is to be used for an AND operation
418419
*/
419-
#define atomicAndFetch8(memory, value) _InterlockedAnd8(memory, value)
420+
#define atomicFetchAnd8(memory, value) _InterlockedAnd8(memory, value)
420421
/**
421422
* @brief Atomically performs AND operation to the variable that memory points to.
422-
* @return The resulting value of the AND operation.
423+
* @return The current value of the variable that memory points to.
423424
*
424425
* @param[in,out] memory pointer of a variable to which the AND operation is to be performed
425426
* @param value variable whose value is to be used for an AND operation
426427
*/
427-
#define atomicAndFetch16(memory, value) _InterlockedAnd16(memory, value)
428+
#define atomicFetchAnd16(memory, value) _InterlockedAnd16(memory, value)
428429
/**
429430
* @brief Atomically performs AND operation to the variable that memory points to.
430-
* @return The resulting value of the AND operation.
431+
* @return The current value of the variable that memory points to.
431432
*
432433
* @param[in,out] memory pointer of a variable to which the AND operation is to be performed
433434
* @param value variable whose value is to be used for an AND operation
434435
*/
435-
#define atomicAndFetch32(memory, value) _InterlockedAnd(memory, value)
436+
#define atomicFetchAnd32(memory, value) _InterlockedAnd(memory, value)
436437
/**
437438
* @brief Atomically performs AND operation to the variable that memory points to.
438-
* @return The resulting value of the AND operation.
439+
* @return The current value of the variable that memory points to.
439440
*
440441
* @param[in,out] memory pointer of a variable to which the AND operation is to be performed
441442
* @param value variable whose value is to be used for an AND operation
442443
*/
443-
#define atomicAndFetch64(memory, value) _InterlockedAnd64(memory, value)
444+
#define atomicFetchAnd64(memory, value) _InterlockedAnd64(memory, value)
444445

445446
/***********************************************************************************************************************
446447
* @brief Atomically performs OR operation to the variable that memory points to.
447-
* @return The resulting value of the OR operation.
448+
* @return The current value of the variable that memory points to.
448449
*
449450
* @param[in,out] memory pointer of a variable to which the OR operation is to be performed
450451
* @param value variable whose value is to be used for an OR operation
451452
*/
452-
#define atomicOrFetch8(memory, value) _InterlockedOr8(memory, value)
453+
#define atomicFetchOr8(memory, value) _InterlockedOr8(memory, value)
453454
/**
454455
* @brief Atomically performs OR operation to the variable that memory points to.
455-
* @return The resulting value of the OR operation.
456+
* @return The current value of the variable that memory points to.
456457
*
457458
* @param[in,out] memory pointer of a variable to which the OR operation is to be performed
458459
* @param value variable whose value is to be used for an OR operation
459460
*/
460-
#define atomicOrFetch16(memory, value) _InterlockedOr16(memory, value)
461+
#define atomicFetchOr16(memory, value) _InterlockedOr16(memory, value)
461462
/**
462463
* @brief Atomically performs OR operation to the variable that memory points to.
463-
* @return The resulting value of the OR operation.
464+
* @return The current value of the variable that memory points to.
464465
*
465466
* @param[in,out] memory pointer of a variable to which the OR operation is to be performed
466467
* @param value variable whose value is to be used for an OR operation
467468
*/
468-
#define atomicOrFetch32(memory, value) _InterlockedOr(memory, value)
469+
#define atomicFetchOr32(memory, value) _InterlockedOr(memory, value)
469470
/**
470471
* @brief Atomically performs OR operation to the variable that memory points to.
471-
* @return The resulting value of the OR operation.
472+
* @return The current value of the variable that memory points to.
472473
*
473474
* @param[in,out] memory pointer of a variable to which the OR operation is to be performed
474475
* @param value variable whose value is to be used for an OR operation
475476
*/
476-
#define atomicOrFetch64(memory, value) _InterlockedOr64(memory, value)
477+
#define atomicFetchOr64(memory, value) _InterlockedOr64(memory, value)
477478

478479
/***********************************************************************************************************************
479480
* @brief Atomically performs XOR operation to the variable that memory points to.
480-
* @return The resulting value of the XOR operation.
481+
* @return The current value of the variable that memory points to.
481482
*
482483
* @param[in,out] memory pointer of a variable to which the XOR operation is to be performed
483484
* @param value variable whose value is to be used for an XOR operation
484485
*/
485-
#define atomicXorFetch8(memory, value) _InterlockedXor8(memory, value)
486+
#define atomicFetchXor8(memory, value) _InterlockedXor8(memory, value)
486487
/**
487488
* @brief Atomically performs XOR operation to the variable that memory points to.
488-
* @return The resulting value of the XOR operation.
489+
* @return The current value of the variable that memory points to.
489490
*
490491
* @param[in,out] memory pointer of a variable to which the XOR operation is to be performed
491492
* @param value variable whose value is to be used for an XOR operation
492493
*/
493-
#define atomicXorFetch16(memory, value) _InterlockedXor16(memory, value)
494+
#define atomicFetchXor16(memory, value) _InterlockedXor16(memory, value)
494495
/**
495496
* @brief Atomically performs XOR operation to the variable that memory points to.
496-
* @return The resulting value of the XOR operation.
497+
* @return The current value of the variable that memory points to.
497498
*
498499
* @param[in,out] memory pointer of a variable to which the XOR operation is to be performed
499500
* @param value variable whose value is to be used for an XOR operation
500501
*/
501-
#define atomicXorFetch32(memory, value) _InterlockedXor(memory, value)
502+
#define atomicFetchXor32(memory, value) _InterlockedXor(memory, value)
502503
/**
503504
* @brief Atomically performs XOR operation to the variable that memory points to.
504-
* @return The resulting value of the XOR operation.
505+
* @return The current value of the variable that memory points to.
505506
*
506507
* @param[in,out] memory pointer of a variable to which the XOR operation is to be performed
507508
* @param value variable whose value is to be used for an XOR operation
508509
*/
509-
#define atomicXorFetch64(memory, value) _InterlockedXor64(memory, value)
510+
#define atomicFetchXor64(memory, value) _InterlockedXor64(memory, value)
510511

511512
/***********************************************************************************************************************
512513
* @brief Atomically adds the value to the variable that memory points to.

tests/test_atomic.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
1818

1919
int main()
2020
{
21-
atomic_int32 value32 = 0; atomic_int64 value64 = 0;
22-
atomicStore32(&value32, 123); atomicStore64(&value64, 456);
23-
bool result = value32 == 123 && value64 == 456;
24-
result &= atomicLoad32(&value32) == 123 && atomicLoad64(&value64) == 456;
25-
result &= atomicFetchAdd32(&value32, 1) == 123 && atomicFetchAdd64(&value64, 1) == 456;
26-
result &= value32 == 124 && value64 == 457;
27-
result &= atomicExchange32(&value32, 0) == 124 && atomicExchange64(&value64, 0) == 457;
28-
result &= atomicOrFetch32(&value32, 111) == 111 && atomicOrFetch64(&value64, 2222) == 2222;
29-
result &= atomicAndFetch32(&value32, 1) == 1 && atomicAndFetch64(&value64, 0) == 0;
21+
atomic_int32* value32 = calloc(1, sizeof(atomic_int32));
22+
atomic_int64* value64 = calloc(1, sizeof(atomic_int64));
23+
atomicStore32(value32, 123); atomicStore64(value64, 456);
24+
bool result = atomicLoad32(value32) == 123 && atomicLoad64(value64) == 456;
25+
result &= atomicLoad32(value32) == 123 && atomicLoad64(value64) == 456;
26+
result &= atomicFetchAdd32(value32, 1) == 123 && atomicFetchAdd64(value64, 1) == 456;
27+
result &= atomicLoad32(value32) == 124 && atomicLoad64(value64) == 457;
28+
result &= atomicExchange32(value32, 0) == 124 && atomicExchange64(value64, 0) == 457;
29+
atomicFetchOr32(value32, 111); atomicFetchOr64(value64, 2222);
30+
result &= atomicLoad32(value32) == 111 && atomicLoad64(value64) == 2222;
31+
atomicFetchAnd32(value32, 1); atomicFetchAnd64(value64, 0);
32+
result &= atomicLoad32(value32) == 1 && atomicLoad64(value64) == 0;
3033
return result ? EXIT_SUCCESS : EXIT_FAILURE;
31-
}
34+
}

0 commit comments

Comments
 (0)