| 1 |
// Copyright (c) 2023, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include "internal.h" |
| 5 |
|
| 6 |
#if defined(OPENSSL_WINDOWS_ATOMIC) |
| 7 |
|
| 8 |
#include <windows.h> |
| 9 |
|
| 10 |
|
| 11 |
// See comment above the typedef of CRYPTO_refcount_t about these tests. |
| 12 |
OPENSSL_STATIC_ASSERT(alignof(CRYPTO_refcount_t) == alignof(LONG), |
| 13 |
CRYPTO_refcount_t_does_not_match_LONG_alignment); |
| 14 |
OPENSSL_STATIC_ASSERT(sizeof(CRYPTO_refcount_t) == sizeof(LONG), |
| 15 |
CRYPTO_refcount_t_does_not_match_LONG_size); |
| 16 |
|
| 17 |
OPENSSL_STATIC_ASSERT((CRYPTO_refcount_t)-1 == CRYPTO_REFCOUNT_MAX, |
| 18 |
CRYPTO_REFCOUNT_MAX_is_incorrect); |
| 19 |
|
| 20 |
static uint32_t atomic_load_u32(volatile LONG *ptr) { |
| 21 |
// This is not ideal because it still writes to a cacheline. MSVC is not able |
| 22 |
// to optimize this to a true atomic read, and Windows does not provide an |
| 23 |
// InterlockedLoad function. |
| 24 |
// |
| 25 |
// The Windows documentation [1] does say "Simple reads and writes to |
| 26 |
// properly-aligned 32-bit variables are atomic operations", but this is not |
| 27 |
// phrased in terms of the C11 and C++11 memory models, and indeed a read or |
| 28 |
// write seems to produce slightly different code on MSVC than a sequentially |
| 29 |
// consistent std::atomic::load in C++. Moreover, it is unclear if non-MSVC |
| 30 |
// compilers on Windows provide the same guarantees. Thus we avoid relying on |
| 31 |
// this and instead still use an interlocked function. This is still |
| 32 |
// preferable a global mutex, and eventually this code will be replaced by |
| 33 |
// [2]. Additionally, on clang-cl, we'll use the |OPENSSL_C11_ATOMIC| path. |
| 34 |
// |
| 35 |
// [1] https://learn.microsoft.com/en-us/windows/win32/sync/interlocked-variable-access |
| 36 |
// [2] https://devblogs.microsoft.com/cppblog/c11-atomics-in-visual-studio-2022-version-17-5-preview-2/ |
| 37 |
return (uint32_t)InterlockedCompareExchange(ptr, 0, 0); |
| 38 |
} |
| 39 |
|
| 40 |
static int atomic_compare_exchange_u32(volatile LONG *ptr, uint32_t *expected32, |
| 41 |
uint32_t desired) { |
| 42 |
LONG expected = (LONG)*expected32; |
| 43 |
LONG actual = InterlockedCompareExchange(ptr, (LONG)desired, expected); |
| 44 |
*expected32 = (uint32_t)actual; |
| 45 |
return actual == expected; |
| 46 |
} |
| 47 |
|
| 48 |
void CRYPTO_refcount_inc(CRYPTO_refcount_t *in_count) { |
| 49 |
volatile LONG *count = (volatile LONG *)in_count; |
| 50 |
uint32_t expected = atomic_load_u32(count); |
| 51 |
|
| 52 |
while (expected != CRYPTO_REFCOUNT_MAX) { |
| 53 |
const uint32_t new_value = expected + 1; |
| 54 |
if (atomic_compare_exchange_u32(count, &expected, new_value)) { |
| 55 |
break; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *in_count) { |
| 61 |
volatile LONG *count = (volatile LONG *)in_count; |
| 62 |
uint32_t expected = atomic_load_u32(count); |
| 63 |
|
| 64 |
for (;;) { |
| 65 |
if (expected == 0) { |
| 66 |
abort(); |
| 67 |
} else if (expected == CRYPTO_REFCOUNT_MAX) { |
| 68 |
return 0; |
| 69 |
} else { |
| 70 |
const uint32_t new_value = expected - 1; |
| 71 |
if (atomic_compare_exchange_u32(count, &expected, new_value)) { |
| 72 |
return new_value == 0; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
#endif // OPENSSL_WINDOWS_ATOMIC |