Skip to content

Commit 4666362

Browse files
committed
Add MSVC atomics
1 parent dd9edfe commit 4666362

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

wolfcrypt/src/wc_port.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,23 +1177,41 @@ int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
11771177
}
11781178
#else
11791179
/* Default C Implementation */
1180-
WC_INLINE void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
1180+
void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
11811181
{
11821182
atomic_init(c, i);
11831183
}
11841184

1185-
WC_INLINE int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
1185+
int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
11861186
{
11871187
return atomic_fetch_add_explicit(c, i, memory_order_relaxed);
11881188
}
11891189

1190-
WC_INLINE int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
1190+
int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
11911191
{
11921192
return atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
11931193
}
11941194
#endif /* __cplusplus */
11951195

1196-
#endif /* HAVE_C___ATOMIC */
1196+
#elif defined(_MSC_VER)
1197+
1198+
/* Default C Implementation */
1199+
void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
1200+
{
1201+
*c = i;
1202+
}
1203+
1204+
int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
1205+
{
1206+
return (int)_InterlockedExchangeAdd(c, (long)i);
1207+
}
1208+
1209+
int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
1210+
{
1211+
return (int)_InterlockedExchangeAdd(c, (long)-i);
1212+
}
1213+
1214+
#endif
11971215

11981216
#endif /* WOLFSSL_ATOMIC_OPS */
11991217

wolfssl/wolfcrypt/wc_port.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@
298298
typedef wolfSSL_Mutex wolfSSL_RwLock;
299299
#endif
300300

301+
#ifndef WOLFSSL_NO_ATOMICS
301302
#ifdef HAVE_C___ATOMIC
302303
#ifdef __cplusplus
303304
#if defined(__GNUC__) && defined(__ATOMIC_RELAXED)
@@ -311,7 +312,13 @@
311312
typedef atomic_int wolfSSL_Atomic_Int;
312313
#define WOLFSSL_ATOMIC_OPS
313314
#endif
315+
#elif defined(_MSC_VER)
316+
/* Use MSVC compiler intrinsics for atomic ops */
317+
#include <intrin.h>
318+
typedef volatile long wolfSSL_Atomic_Int;
319+
#define WOLFSSL_ATOMIC_OPS
314320
#endif
321+
#endif /* WOLFSSL_NO_ATOMICS */
315322

316323
#ifdef WOLFSSL_ATOMIC_OPS
317324
WOLFSSL_LOCAL void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i);

0 commit comments

Comments
 (0)