@@ -1271,12 +1271,15 @@ char* wc_strdup_ex(const char *src, int memType) {
12711271}
12721272#endif
12731273
1274- #if defined(WOLFSSL_ATOMIC_OPS ) && !defined(SINGLE_THREADED )
1274+ #ifdef WOLFSSL_ATOMIC_OPS
1275+
1276+ #if defined(WOLFSSL_USER_DEFINED_ATOMICS )
1277+
1278+ #elif defined(SINGLE_THREADED )
1279+
1280+ #elif defined(__GNUC__ ) && defined(__ATOMIC_RELAXED )
1281+ /* direct calls using gcc-style compiler built-ins */
12751282
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 */
12801283void wolfSSL_Atomic_Int_Init (wolfSSL_Atomic_Int * c , int i )
12811284{
12821285 * c = i ;
@@ -1357,7 +1360,16 @@ int wolfSSL_Atomic_Uint_CompareExchange(
13571360 c , expected_i , new_i , 0 /* weak */ , __ATOMIC_SEQ_CST , __ATOMIC_ACQUIRE );
13581361}
13591362
1360- #else
1363+ int wolfSSL_Atomic_Ptr_CompareExchange (
1364+ void * * c , void * * expected_ptr , void * new_ptr )
1365+ {
1366+ return __atomic_compare_exchange_n (
1367+ c , expected_ptr , new_ptr , 0 /* weak */ ,
1368+ __ATOMIC_SEQ_CST , __ATOMIC_ACQUIRE );
1369+ }
1370+
1371+ #elif defined(HAVE_C___ATOMIC ) && defined(WOLFSSL_HAVE_ATOMIC_H ) && \
1372+ !defined(__cplusplus )
13611373
13621374/* Default C Implementation */
13631375void wolfSSL_Atomic_Int_Init (wolfSSL_Atomic_Int * c , int i )
@@ -1444,9 +1456,19 @@ int wolfSSL_Atomic_Uint_CompareExchange(
14441456 c , expected_i , new_i , memory_order_seq_cst , memory_order_acquire );
14451457}
14461458
1447- #endif /* __cplusplus */
1459+ int wolfSSL_Atomic_Ptr_CompareExchange (
1460+ void * * c , void * * expected_ptr , void * new_ptr )
1461+ {
1462+ /* use gcc-built-in __atomic_compare_exchange_n(), not
1463+ * atomic_compare_exchange_strong_explicit(), to sidestep _Atomic type
1464+ * requirements.
1465+ */
1466+ return __atomic_compare_exchange_n (
1467+ c , expected_ptr , new_ptr , 0 /* weak */ ,
1468+ __ATOMIC_SEQ_CST , __ATOMIC_ACQUIRE );
1469+ }
14481470
1449- #elif defined(_MSC_VER )
1471+ #elif defined(_MSC_VER ) && !defined( WOLFSSL_NOT_WINDOWS_API )
14501472
14511473void wolfSSL_Atomic_Int_Init (wolfSSL_Atomic_Int * c , int i )
14521474{
@@ -1527,8 +1549,8 @@ unsigned int wolfSSL_Atomic_Uint_SubFetch(wolfSSL_Atomic_Uint* c,
15271549int wolfSSL_Atomic_Uint_CompareExchange (
15281550 wolfSSL_Atomic_Uint * c , unsigned int * expected_i , unsigned int new_i )
15291551{
1530- long actual_i = InterlockedCompareExchange
1531- (( wolfSSL_Atomic_Int * )c , (long )new_i , (long )* expected_i );
1552+ long actual_i = InterlockedCompareExchange (
1553+ (wolfSSL_Atomic_Int * )c , (long )new_i , (long )* expected_i );
15321554 if (actual_i == (long )* expected_i ) {
15331555 return 1 ;
15341556 }
@@ -1538,6 +1560,32 @@ int wolfSSL_Atomic_Uint_CompareExchange(
15381560 }
15391561}
15401562
1563+ int wolfSSL_Atomic_Ptr_CompareExchange (
1564+ void * * c , void * * expected_ptr , void * new_ptr )
1565+ {
1566+ #ifdef _WIN64
1567+ LONG64 actual_ptr = InterlockedCompareExchange64 (
1568+ (LONG64 * )c , (LONG64 )new_ptr , (LONG64 )* expected_ptr );
1569+ if (actual_ptr == (LONG64 )* expected_ptr ) {
1570+ return 1 ;
1571+ }
1572+ else {
1573+ * expected_ptr = (void * )actual_ptr ;
1574+ return 0 ;
1575+ }
1576+ #else /* !_WIN64 */
1577+ LONG actual_ptr = InterlockedCompareExchange (
1578+ (LONG * )c , (LONG )new_ptr , (LONG )* expected_ptr );
1579+ if (actual_ptr == (LONG )* expected_ptr ) {
1580+ return 1 ;
1581+ }
1582+ else {
1583+ * expected_ptr = (void * )actual_ptr ;
1584+ return 0 ;
1585+ }
1586+ #endif /* !_WIN64 */
1587+ }
1588+
15411589#endif
15421590
15431591#endif /* WOLFSSL_ATOMIC_OPS */
0 commit comments