Add MSVC atomics

This commit is contained in:
Juliusz Sosinowicz
2023-05-18 12:06:54 +02:00
parent dd9edfee24
commit 466636214f
2 changed files with 29 additions and 4 deletions

View File

@@ -1177,23 +1177,41 @@ int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
} }
#else #else
/* Default C Implementation */ /* 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); 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); 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); return atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
} }
#endif /* __cplusplus */ #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 */ #endif /* WOLFSSL_ATOMIC_OPS */

View File

@@ -298,6 +298,7 @@
typedef wolfSSL_Mutex wolfSSL_RwLock; typedef wolfSSL_Mutex wolfSSL_RwLock;
#endif #endif
#ifndef WOLFSSL_NO_ATOMICS
#ifdef HAVE_C___ATOMIC #ifdef HAVE_C___ATOMIC
#ifdef __cplusplus #ifdef __cplusplus
#if defined(__GNUC__) && defined(__ATOMIC_RELAXED) #if defined(__GNUC__) && defined(__ATOMIC_RELAXED)
@@ -311,7 +312,13 @@
typedef atomic_int wolfSSL_Atomic_Int; typedef atomic_int wolfSSL_Atomic_Int;
#define WOLFSSL_ATOMIC_OPS #define WOLFSSL_ATOMIC_OPS
#endif #endif
#elif defined(_MSC_VER)
/* Use MSVC compiler intrinsics for atomic ops */
#include <intrin.h>
typedef volatile long wolfSSL_Atomic_Int;
#define WOLFSSL_ATOMIC_OPS
#endif #endif
#endif /* WOLFSSL_NO_ATOMICS */
#ifdef WOLFSSL_ATOMIC_OPS #ifdef WOLFSSL_ATOMIC_OPS
WOLFSSL_LOCAL void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i); WOLFSSL_LOCAL void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i);