diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index e9ce63760..c789ae2e4 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -1177,23 +1177,41 @@ int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i) } #else /* Default C Implementation */ -WC_INLINE void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i) +void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i) { atomic_init(c, i); } -WC_INLINE int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i) +int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i) { return atomic_fetch_add_explicit(c, i, memory_order_relaxed); } -WC_INLINE int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i) +int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i) { return atomic_fetch_sub_explicit(c, i, memory_order_relaxed); } #endif /* __cplusplus */ -#endif /* HAVE_C___ATOMIC */ +#elif defined(_MSC_VER) + +/* Default C Implementation */ +void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i) +{ + *c = i; +} + +int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i) +{ + return (int)_InterlockedExchangeAdd(c, (long)i); +} + +int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i) +{ + return (int)_InterlockedExchangeAdd(c, (long)-i); +} + +#endif #endif /* WOLFSSL_ATOMIC_OPS */ diff --git a/wolfssl/wolfcrypt/wc_port.h b/wolfssl/wolfcrypt/wc_port.h index 635ed59fd..1737c3f63 100644 --- a/wolfssl/wolfcrypt/wc_port.h +++ b/wolfssl/wolfcrypt/wc_port.h @@ -298,6 +298,7 @@ typedef wolfSSL_Mutex wolfSSL_RwLock; #endif +#ifndef WOLFSSL_NO_ATOMICS #ifdef HAVE_C___ATOMIC #ifdef __cplusplus #if defined(__GNUC__) && defined(__ATOMIC_RELAXED) @@ -311,7 +312,13 @@ typedef atomic_int wolfSSL_Atomic_Int; #define WOLFSSL_ATOMIC_OPS #endif +#elif defined(_MSC_VER) + /* Use MSVC compiler intrinsics for atomic ops */ + #include + typedef volatile long wolfSSL_Atomic_Int; + #define WOLFSSL_ATOMIC_OPS #endif +#endif /* WOLFSSL_NO_ATOMICS */ #ifdef WOLFSSL_ATOMIC_OPS WOLFSSL_LOCAL void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i);