Skip to content

Commit 50c5028

Browse files
authored
Merge pull request #9432 from douzzer/20251114-atomic-default-c
20251114-atomic-default-c
2 parents 10a60fc + 135bb66 commit 50c5028

File tree

2 files changed

+55
-55
lines changed

2 files changed

+55
-55
lines changed

wolfcrypt/src/wc_port.c

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,74 +1277,79 @@ char* wc_strdup_ex(const char *src, int memType) {
12771277

12781278
#elif defined(SINGLE_THREADED)
12791279

1280-
#elif defined(__GNUC__) && defined(__ATOMIC_RELAXED)
1281-
/* direct calls using gcc-style compiler built-ins */
1280+
#elif defined(HAVE_C___ATOMIC) && defined(WOLFSSL_HAVE_ATOMIC_H) && \
1281+
!defined(__cplusplus)
12821282

1283+
/* Default C Implementation */
12831284
void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
12841285
{
1285-
*c = i;
1286+
atomic_init(c, i);
12861287
}
12871288

12881289
void wolfSSL_Atomic_Uint_Init(wolfSSL_Atomic_Uint* c, unsigned int i)
12891290
{
1290-
*c = i;
1291+
atomic_init(c, i);
12911292
}
12921293

12931294
int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
12941295
{
1295-
return __atomic_fetch_add(c, i, __ATOMIC_RELAXED);
1296+
return atomic_fetch_add_explicit(c, i, memory_order_relaxed);
12961297
}
12971298

12981299
int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
12991300
{
1300-
return __atomic_fetch_sub(c, i, __ATOMIC_RELAXED);
1301+
return atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
13011302
}
13021303

13031304
int wolfSSL_Atomic_Int_AddFetch(wolfSSL_Atomic_Int* c, int i)
13041305
{
1305-
return __atomic_add_fetch(c, i, __ATOMIC_RELAXED);
1306+
int ret = atomic_fetch_add_explicit(c, i, memory_order_relaxed);
1307+
return ret + i;
13061308
}
13071309

13081310
int wolfSSL_Atomic_Int_SubFetch(wolfSSL_Atomic_Int* c, int i)
13091311
{
1310-
return __atomic_sub_fetch(c, i, __ATOMIC_RELAXED);
1312+
int ret = atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
1313+
return ret - i;
13111314
}
13121315

1313-
int wolfSSL_Atomic_Int_CompareExchange(wolfSSL_Atomic_Int* c, int *expected_i,
1314-
int new_i)
1316+
int wolfSSL_Atomic_Int_CompareExchange(
1317+
wolfSSL_Atomic_Int* c, int *expected_i, int new_i)
13151318
{
13161319
/* For the success path, use full synchronization with barriers --
13171320
* "Sequentially-consistent ordering" -- so that all threads see the same
13181321
* "single total modification order of all atomic operations" -- but on
13191322
* failure we just need to be sure we acquire the value that changed out
13201323
* from under us.
13211324
*/
1322-
return __atomic_compare_exchange_n(c, expected_i, new_i, 0 /* weak */,
1323-
__ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
1325+
return atomic_compare_exchange_strong_explicit(
1326+
c, expected_i, new_i, memory_order_seq_cst, memory_order_acquire);
13241327
}
13251328

13261329
unsigned int wolfSSL_Atomic_Uint_FetchAdd(wolfSSL_Atomic_Uint* c,
13271330
unsigned int i)
13281331
{
1329-
return __atomic_fetch_add(c, i, __ATOMIC_RELAXED);
1332+
return atomic_fetch_add_explicit(c, i, memory_order_relaxed);
13301333
}
13311334

13321335
unsigned int wolfSSL_Atomic_Uint_FetchSub(wolfSSL_Atomic_Uint* c,
13331336
unsigned int i)
13341337
{
1335-
return __atomic_fetch_sub(c, i, __ATOMIC_RELAXED);
1338+
return atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
13361339
}
13371340

13381341
unsigned int wolfSSL_Atomic_Uint_AddFetch(wolfSSL_Atomic_Uint* c,
13391342
unsigned int i)
13401343
{
1341-
return __atomic_add_fetch(c, i, __ATOMIC_RELAXED);
1344+
unsigned int ret = atomic_fetch_add_explicit(c, i, memory_order_relaxed);
1345+
return ret + i;
13421346
}
13431347

13441348
unsigned int wolfSSL_Atomic_Uint_SubFetch(wolfSSL_Atomic_Uint* c,
13451349
unsigned int i)
13461350
{
1347-
return __atomic_sub_fetch(c, i, __ATOMIC_RELAXED);
1351+
unsigned int ret = atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
1352+
return ret - i;
13481353
}
13491354

13501355
int wolfSSL_Atomic_Uint_CompareExchange(
@@ -1356,91 +1361,90 @@ int wolfSSL_Atomic_Uint_CompareExchange(
13561361
* failure we just need to be sure we acquire the value that changed out
13571362
* from under us.
13581363
*/
1359-
return __atomic_compare_exchange_n(
1360-
c, expected_i, new_i, 0 /* weak */, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
1364+
return atomic_compare_exchange_strong_explicit(
1365+
c, expected_i, new_i, memory_order_seq_cst, memory_order_acquire);
13611366
}
13621367

13631368
int wolfSSL_Atomic_Ptr_CompareExchange(
13641369
void **c, void **expected_ptr, void *new_ptr)
13651370
{
1371+
/* use gcc-built-in __atomic_compare_exchange_n(), not
1372+
* atomic_compare_exchange_strong_explicit(), to sidestep _Atomic type
1373+
* requirements.
1374+
*/
13661375
return __atomic_compare_exchange_n(
13671376
c, expected_ptr, new_ptr, 0 /* weak */,
13681377
__ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
13691378
}
13701379

1371-
#elif defined(HAVE_C___ATOMIC) && defined(WOLFSSL_HAVE_ATOMIC_H) && \
1372-
!defined(__cplusplus)
1380+
#elif defined(__GNUC__) && defined(__ATOMIC_RELAXED)
1381+
/* direct calls using gcc-style compiler built-ins */
13731382

1374-
/* Default C Implementation */
13751383
void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
13761384
{
1377-
atomic_init(c, i);
1385+
*c = i;
13781386
}
13791387

13801388
void wolfSSL_Atomic_Uint_Init(wolfSSL_Atomic_Uint* c, unsigned int i)
13811389
{
1382-
atomic_init(c, i);
1390+
*c = i;
13831391
}
13841392

13851393
int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
13861394
{
1387-
return atomic_fetch_add_explicit(c, i, memory_order_relaxed);
1395+
return __atomic_fetch_add(c, i, __ATOMIC_RELAXED);
13881396
}
13891397

13901398
int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
13911399
{
1392-
return atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
1400+
return __atomic_fetch_sub(c, i, __ATOMIC_RELAXED);
13931401
}
13941402

13951403
int wolfSSL_Atomic_Int_AddFetch(wolfSSL_Atomic_Int* c, int i)
13961404
{
1397-
int ret = atomic_fetch_add_explicit(c, i, memory_order_relaxed);
1398-
return ret + i;
1405+
return __atomic_add_fetch(c, i, __ATOMIC_RELAXED);
13991406
}
14001407

14011408
int wolfSSL_Atomic_Int_SubFetch(wolfSSL_Atomic_Int* c, int i)
14021409
{
1403-
int ret = atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
1404-
return ret - i;
1410+
return __atomic_sub_fetch(c, i, __ATOMIC_RELAXED);
14051411
}
14061412

1407-
int wolfSSL_Atomic_Int_CompareExchange(
1408-
wolfSSL_Atomic_Int* c, int *expected_i, int new_i)
1413+
int wolfSSL_Atomic_Int_CompareExchange(wolfSSL_Atomic_Int* c, int *expected_i,
1414+
int new_i)
14091415
{
14101416
/* For the success path, use full synchronization with barriers --
14111417
* "Sequentially-consistent ordering" -- so that all threads see the same
14121418
* "single total modification order of all atomic operations" -- but on
14131419
* failure we just need to be sure we acquire the value that changed out
14141420
* from under us.
14151421
*/
1416-
return atomic_compare_exchange_strong_explicit(
1417-
c, expected_i, new_i, memory_order_seq_cst, memory_order_acquire);
1422+
return __atomic_compare_exchange_n(c, expected_i, new_i, 0 /* weak */,
1423+
__ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
14181424
}
14191425

14201426
unsigned int wolfSSL_Atomic_Uint_FetchAdd(wolfSSL_Atomic_Uint* c,
14211427
unsigned int i)
14221428
{
1423-
return atomic_fetch_add_explicit(c, i, memory_order_relaxed);
1429+
return __atomic_fetch_add(c, i, __ATOMIC_RELAXED);
14241430
}
14251431

14261432
unsigned int wolfSSL_Atomic_Uint_FetchSub(wolfSSL_Atomic_Uint* c,
14271433
unsigned int i)
14281434
{
1429-
return atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
1435+
return __atomic_fetch_sub(c, i, __ATOMIC_RELAXED);
14301436
}
14311437

14321438
unsigned int wolfSSL_Atomic_Uint_AddFetch(wolfSSL_Atomic_Uint* c,
14331439
unsigned int i)
14341440
{
1435-
unsigned int ret = atomic_fetch_add_explicit(c, i, memory_order_relaxed);
1436-
return ret + i;
1441+
return __atomic_add_fetch(c, i, __ATOMIC_RELAXED);
14371442
}
14381443

14391444
unsigned int wolfSSL_Atomic_Uint_SubFetch(wolfSSL_Atomic_Uint* c,
14401445
unsigned int i)
14411446
{
1442-
unsigned int ret = atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
1443-
return ret - i;
1447+
return __atomic_sub_fetch(c, i, __ATOMIC_RELAXED);
14441448
}
14451449

14461450
int wolfSSL_Atomic_Uint_CompareExchange(
@@ -1452,17 +1456,13 @@ int wolfSSL_Atomic_Uint_CompareExchange(
14521456
* failure we just need to be sure we acquire the value that changed out
14531457
* from under us.
14541458
*/
1455-
return atomic_compare_exchange_strong_explicit(
1456-
c, expected_i, new_i, memory_order_seq_cst, memory_order_acquire);
1459+
return __atomic_compare_exchange_n(
1460+
c, expected_i, new_i, 0 /* weak */, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
14571461
}
14581462

14591463
int wolfSSL_Atomic_Ptr_CompareExchange(
14601464
void **c, void **expected_ptr, void *new_ptr)
14611465
{
1462-
/* use gcc-built-in __atomic_compare_exchange_n(), not
1463-
* atomic_compare_exchange_strong_explicit(), to sidestep _Atomic type
1464-
* requirements.
1465-
*/
14661466
return __atomic_compare_exchange_n(
14671467
c, expected_ptr, new_ptr, 0 /* weak */,
14681468
__ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);

wolfssl/wolfcrypt/wc_port.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -494,16 +494,6 @@
494494
#define WOLFSSL_ATOMIC_LOAD(x) (x)
495495
#define WOLFSSL_ATOMIC_STORE(x, val) (x) = (val)
496496
#define WOLFSSL_ATOMIC_OPS
497-
#elif defined(__GNUC__) && defined(__ATOMIC_CONSUME)
498-
/* direct calls using gcc-style compiler built-ins */
499-
typedef volatile int wolfSSL_Atomic_Int;
500-
typedef volatile unsigned int wolfSSL_Atomic_Uint;
501-
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
502-
#define WOLFSSL_ATOMIC_LOAD(x) __atomic_load_n(&(x), \
503-
__ATOMIC_CONSUME)
504-
#define WOLFSSL_ATOMIC_STORE(x, val) __atomic_store_n(&(x), \
505-
val, __ATOMIC_RELEASE)
506-
#define WOLFSSL_ATOMIC_OPS
507497
#elif defined(HAVE_C___ATOMIC) && defined(WOLFSSL_HAVE_ATOMIC_H) && \
508498
!defined(__cplusplus)
509499
/* Default C Implementation */
@@ -514,6 +504,16 @@
514504
#define WOLFSSL_ATOMIC_LOAD(x) atomic_load(&(x))
515505
#define WOLFSSL_ATOMIC_STORE(x, val) atomic_store(&(x), val)
516506
#define WOLFSSL_ATOMIC_OPS
507+
#elif defined(__GNUC__) && defined(__ATOMIC_CONSUME)
508+
/* direct calls using gcc-style compiler built-ins */
509+
typedef volatile int wolfSSL_Atomic_Int;
510+
typedef volatile unsigned int wolfSSL_Atomic_Uint;
511+
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
512+
#define WOLFSSL_ATOMIC_LOAD(x) __atomic_load_n(&(x), \
513+
__ATOMIC_CONSUME)
514+
#define WOLFSSL_ATOMIC_STORE(x, val) __atomic_store_n(&(x), \
515+
val, __ATOMIC_RELEASE)
516+
#define WOLFSSL_ATOMIC_OPS
517517
#elif defined(_MSC_VER) && !defined(WOLFSSL_NOT_WINDOWS_API)
518518
/* Use MSVC compiler intrinsics for atomic ops */
519519
#ifdef _WIN32_WCE

0 commit comments

Comments
 (0)