@@ -1273,75 +1273,80 @@ char* wc_strdup_ex(const char *src, int memType) {
12731273
12741274#if defined(WOLFSSL_ATOMIC_OPS ) && !defined(SINGLE_THREADED )
12751275
1276- #ifdef HAVE_C___ATOMIC
1277- /* Atomic ops using standard C lib */
1278- #ifdef __cplusplus
1279- /* C++ using direct calls to compiler built-in functions */
1276+ #if defined(WOLFSSL_USER_DEFINED_ATOMICS )
1277+
1278+ #elif defined(HAVE_C___ATOMIC ) && defined(WOLFSSL_HAVE_ATOMIC_H )
1279+
1280+ /* Default C Implementation */
12801281void wolfSSL_Atomic_Int_Init (wolfSSL_Atomic_Int * c , int i )
12811282{
1282- * c = i ;
1283+ atomic_init ( c , i ) ;
12831284}
12841285
12851286void wolfSSL_Atomic_Uint_Init (wolfSSL_Atomic_Uint * c , unsigned int i )
12861287{
1287- * c = i ;
1288+ atomic_init ( c , i ) ;
12881289}
12891290
12901291int wolfSSL_Atomic_Int_FetchAdd (wolfSSL_Atomic_Int * c , int i )
12911292{
1292- return __atomic_fetch_add (c , i , __ATOMIC_RELAXED );
1293+ return atomic_fetch_add_explicit (c , i , memory_order_relaxed );
12931294}
12941295
12951296int wolfSSL_Atomic_Int_FetchSub (wolfSSL_Atomic_Int * c , int i )
12961297{
1297- return __atomic_fetch_sub (c , i , __ATOMIC_RELAXED );
1298+ return atomic_fetch_sub_explicit (c , i , memory_order_relaxed );
12981299}
12991300
13001301int wolfSSL_Atomic_Int_AddFetch (wolfSSL_Atomic_Int * c , int i )
13011302{
1302- return __atomic_add_fetch (c , i , __ATOMIC_RELAXED );
1303+ int ret = atomic_fetch_add_explicit (c , i , memory_order_relaxed );
1304+ return ret + i ;
13031305}
13041306
13051307int wolfSSL_Atomic_Int_SubFetch (wolfSSL_Atomic_Int * c , int i )
13061308{
1307- return __atomic_sub_fetch (c , i , __ATOMIC_RELAXED );
1309+ int ret = atomic_fetch_sub_explicit (c , i , memory_order_relaxed );
1310+ return ret - i ;
13081311}
13091312
1310- int wolfSSL_Atomic_Int_CompareExchange (wolfSSL_Atomic_Int * c , int * expected_i ,
1311- int new_i )
1313+ int wolfSSL_Atomic_Int_CompareExchange (
1314+ wolfSSL_Atomic_Int * c , int * expected_i , int new_i )
13121315{
13131316 /* For the success path, use full synchronization with barriers --
13141317 * "Sequentially-consistent ordering" -- so that all threads see the same
13151318 * "single total modification order of all atomic operations" -- but on
13161319 * failure we just need to be sure we acquire the value that changed out
13171320 * from under us.
13181321 */
1319- return __atomic_compare_exchange_n ( c , expected_i , new_i , 0 /* weak */ ,
1320- __ATOMIC_SEQ_CST , __ATOMIC_ACQUIRE );
1322+ return atomic_compare_exchange_strong_explicit (
1323+ c , expected_i , new_i , memory_order_seq_cst , memory_order_acquire );
13211324}
13221325
13231326unsigned int wolfSSL_Atomic_Uint_FetchAdd (wolfSSL_Atomic_Uint * c ,
13241327 unsigned int i )
13251328{
1326- return __atomic_fetch_add (c , i , __ATOMIC_RELAXED );
1329+ return atomic_fetch_add_explicit (c , i , memory_order_relaxed );
13271330}
13281331
13291332unsigned int wolfSSL_Atomic_Uint_FetchSub (wolfSSL_Atomic_Uint * c ,
13301333 unsigned int i )
13311334{
1332- return __atomic_fetch_sub (c , i , __ATOMIC_RELAXED );
1335+ return atomic_fetch_sub_explicit (c , i , memory_order_relaxed );
13331336}
13341337
13351338unsigned int wolfSSL_Atomic_Uint_AddFetch (wolfSSL_Atomic_Uint * c ,
13361339 unsigned int i )
13371340{
1338- return __atomic_add_fetch (c , i , __ATOMIC_RELAXED );
1341+ unsigned int ret = atomic_fetch_add_explicit (c , i , memory_order_relaxed );
1342+ return ret + i ;
13391343}
13401344
13411345unsigned int wolfSSL_Atomic_Uint_SubFetch (wolfSSL_Atomic_Uint * c ,
13421346 unsigned int i )
13431347{
1344- return __atomic_sub_fetch (c , i , __ATOMIC_RELAXED );
1348+ unsigned int ret = atomic_fetch_sub_explicit (c , i , memory_order_relaxed );
1349+ return ret - i ;
13451350}
13461351
13471352int wolfSSL_Atomic_Uint_CompareExchange (
@@ -1353,89 +1358,90 @@ int wolfSSL_Atomic_Uint_CompareExchange(
13531358 * failure we just need to be sure we acquire the value that changed out
13541359 * from under us.
13551360 */
1356- return __atomic_compare_exchange_n (
1357- c , expected_i , new_i , 0 /* weak */ , __ATOMIC_SEQ_CST , __ATOMIC_ACQUIRE );
1361+ return atomic_compare_exchange_strong_explicit (
1362+ c , expected_i , new_i , memory_order_seq_cst , memory_order_acquire );
13581363}
13591364
13601365int wolfSSL_Atomic_Ptr_CompareExchange (
13611366 void * * c , void * * expected_ptr , void * new_ptr )
13621367{
1368+ /* use gcc-built-in __atomic_compare_exchange_n(), not
1369+ * atomic_compare_exchange_strong_explicit(), to sidestep _Atomic type
1370+ * requirements.
1371+ */
13631372 return __atomic_compare_exchange_n (
1364- c , expected_ptr , new_ptr , 0 /* weak */ , __ATOMIC_SEQ_CST , __ATOMIC_ACQUIRE );
1373+ c , expected_ptr , new_ptr , 0 /* weak */ ,
1374+ __ATOMIC_SEQ_CST , __ATOMIC_ACQUIRE );
13651375}
13661376
1367- #else
1377+ #elif defined(__GNUC__ ) && defined(__ATOMIC_RELAXED )
1378+ /* direct calls using gcc-style compiler built-ins */
13681379
1369- /* Default C Implementation */
13701380void wolfSSL_Atomic_Int_Init (wolfSSL_Atomic_Int * c , int i )
13711381{
1372- atomic_init ( c , i ) ;
1382+ * c = i ;
13731383}
13741384
13751385void wolfSSL_Atomic_Uint_Init (wolfSSL_Atomic_Uint * c , unsigned int i )
13761386{
1377- atomic_init ( c , i ) ;
1387+ * c = i ;
13781388}
13791389
13801390int wolfSSL_Atomic_Int_FetchAdd (wolfSSL_Atomic_Int * c , int i )
13811391{
1382- return atomic_fetch_add_explicit (c , i , memory_order_relaxed );
1392+ return __atomic_fetch_add (c , i , __ATOMIC_RELAXED );
13831393}
13841394
13851395int wolfSSL_Atomic_Int_FetchSub (wolfSSL_Atomic_Int * c , int i )
13861396{
1387- return atomic_fetch_sub_explicit (c , i , memory_order_relaxed );
1397+ return __atomic_fetch_sub (c , i , __ATOMIC_RELAXED );
13881398}
13891399
13901400int wolfSSL_Atomic_Int_AddFetch (wolfSSL_Atomic_Int * c , int i )
13911401{
1392- int ret = atomic_fetch_add_explicit (c , i , memory_order_relaxed );
1393- return ret + i ;
1402+ return __atomic_add_fetch (c , i , __ATOMIC_RELAXED );
13941403}
13951404
13961405int wolfSSL_Atomic_Int_SubFetch (wolfSSL_Atomic_Int * c , int i )
13971406{
1398- int ret = atomic_fetch_sub_explicit (c , i , memory_order_relaxed );
1399- return ret - i ;
1407+ return __atomic_sub_fetch (c , i , __ATOMIC_RELAXED );
14001408}
14011409
1402- int wolfSSL_Atomic_Int_CompareExchange (
1403- wolfSSL_Atomic_Int * c , int * expected_i , int new_i )
1410+ int wolfSSL_Atomic_Int_CompareExchange (wolfSSL_Atomic_Int * c , int * expected_i ,
1411+ int new_i )
14041412{
14051413 /* For the success path, use full synchronization with barriers --
14061414 * "Sequentially-consistent ordering" -- so that all threads see the same
14071415 * "single total modification order of all atomic operations" -- but on
14081416 * failure we just need to be sure we acquire the value that changed out
14091417 * from under us.
14101418 */
1411- return atomic_compare_exchange_strong_explicit (
1412- c , expected_i , new_i , memory_order_seq_cst , memory_order_acquire );
1419+ return __atomic_compare_exchange_n ( c , expected_i , new_i , 0 /* weak */ ,
1420+ __ATOMIC_SEQ_CST , __ATOMIC_ACQUIRE );
14131421}
14141422
14151423unsigned int wolfSSL_Atomic_Uint_FetchAdd (wolfSSL_Atomic_Uint * c ,
14161424 unsigned int i )
14171425{
1418- return atomic_fetch_add_explicit (c , i , memory_order_relaxed );
1426+ return __atomic_fetch_add (c , i , __ATOMIC_RELAXED );
14191427}
14201428
14211429unsigned int wolfSSL_Atomic_Uint_FetchSub (wolfSSL_Atomic_Uint * c ,
14221430 unsigned int i )
14231431{
1424- return atomic_fetch_sub_explicit (c , i , memory_order_relaxed );
1432+ return __atomic_fetch_sub (c , i , __ATOMIC_RELAXED );
14251433}
14261434
14271435unsigned int wolfSSL_Atomic_Uint_AddFetch (wolfSSL_Atomic_Uint * c ,
14281436 unsigned int i )
14291437{
1430- unsigned int ret = atomic_fetch_add_explicit (c , i , memory_order_relaxed );
1431- return ret + i ;
1438+ return __atomic_add_fetch (c , i , __ATOMIC_RELAXED );
14321439}
14331440
14341441unsigned int wolfSSL_Atomic_Uint_SubFetch (wolfSSL_Atomic_Uint * c ,
14351442 unsigned int i )
14361443{
1437- unsigned int ret = atomic_fetch_sub_explicit (c , i , memory_order_relaxed );
1438- return ret - i ;
1444+ return __atomic_sub_fetch (c , i , __ATOMIC_RELAXED );
14391445}
14401446
14411447int wolfSSL_Atomic_Uint_CompareExchange (
@@ -1447,24 +1453,19 @@ int wolfSSL_Atomic_Uint_CompareExchange(
14471453 * failure we just need to be sure we acquire the value that changed out
14481454 * from under us.
14491455 */
1450- return atomic_compare_exchange_strong_explicit (
1451- c , expected_i , new_i , memory_order_seq_cst , memory_order_acquire );
1456+ return __atomic_compare_exchange_n (
1457+ c , expected_i , new_i , 0 /* weak */ , __ATOMIC_SEQ_CST , __ATOMIC_ACQUIRE );
14521458}
14531459
14541460int wolfSSL_Atomic_Ptr_CompareExchange (
14551461 void * * c , void * * expected_ptr , void * new_ptr )
14561462{
1457- /* use gcc-built-in __atomic_compare_exchange_n(), not
1458- * atomic_compare_exchange_strong_explicit(), to sidestep _Atomic type
1459- * requirements.
1460- */
14611463 return __atomic_compare_exchange_n (
1462- c , expected_ptr , new_ptr , 0 /* weak */ , __ATOMIC_SEQ_CST , __ATOMIC_ACQUIRE );
1464+ c , expected_ptr , new_ptr , 0 /* weak */ ,
1465+ __ATOMIC_SEQ_CST , __ATOMIC_ACQUIRE );
14631466}
14641467
1465- #endif /* __cplusplus */
1466-
1467- #elif defined(_MSC_VER )
1468+ #elif defined(_MSC_VER ) && !defined(WOLFSSL_NOT_WINDOWS_API )
14681469
14691470void wolfSSL_Atomic_Int_Init (wolfSSL_Atomic_Int * c , int i )
14701471{
@@ -1545,8 +1546,8 @@ unsigned int wolfSSL_Atomic_Uint_SubFetch(wolfSSL_Atomic_Uint* c,
15451546int wolfSSL_Atomic_Uint_CompareExchange (
15461547 wolfSSL_Atomic_Uint * c , unsigned int * expected_i , unsigned int new_i )
15471548{
1548- long actual_i = InterlockedCompareExchange
1549- (( wolfSSL_Atomic_Int * )c , (long )new_i , (long )* expected_i );
1549+ long actual_i = InterlockedCompareExchange (
1550+ (wolfSSL_Atomic_Int * )c , (long )new_i , (long )* expected_i );
15501551 if (actual_i == (long )* expected_i ) {
15511552 return 1 ;
15521553 }
@@ -1560,23 +1561,23 @@ int wolfSSL_Atomic_Uint_CompareExchange(
15601561 void * * c , void * * expected_ptr , void * new_ptr )
15611562{
15621563#ifdef _WIN64
1563- LONG64 actual_ptr = InterlockedCompareExchange64
1564- (( LONG64 * )c , (LONG64 )new_i , (LONG64 )* expected_i );
1565- if (actual_ptr == (LONG64 )* expected_i ) {
1564+ LONG64 actual_ptr = InterlockedCompareExchange64 (
1565+ ( LONG64 * )c , (LONG64 )new_ptr , (LONG64 )* expected_ptr );
1566+ if (actual_ptr == (LONG64 )* expected_ptr ) {
15661567 return 1 ;
15671568 }
15681569 else {
1569- * expected_i = (void * )actual_ptr ;
1570+ * expected_ptr = (void * )actual_ptr ;
15701571 return 0 ;
15711572 }
15721573#else /* !_WIN64 */
1573- LONG actual_ptr = InterlockedCompareExchange
1574- (( LONG * )c , (LONG )new_i , (LONG )* expected_i );
1575- if (actual_ptr == (LONG )* expected_i ) {
1574+ LONG actual_ptr = InterlockedCompareExchange (
1575+ ( LONG * )c , (LONG )new_ptr , (LONG )* expected_ptr );
1576+ if (actual_ptr == (LONG )* expected_ptr ) {
15761577 return 1 ;
15771578 }
15781579 else {
1579- * expected_i = (void * )actual_ptr ;
1580+ * expected_ptr = (void * )actual_ptr ;
15801581 return 0 ;
15811582 }
15821583#endif /* !_WIN64 */
0 commit comments