LCOV - code coverage report
Current view: top level - core - cfd_threading_internal.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 11 11
Test Date: 2026-10-05 04:47:18 Functions: - 0 0

            Line data    Source code
       1              : #ifndef CFD_THREADING_INTERNAL_H
       2              : #define CFD_THREADING_INTERNAL_H
       3              : 
       4              : #ifdef _WIN32
       5              : #include <windows.h>
       6              : 
       7              : typedef volatile LONG cfd_atomic_int;
       8              : 
       9              : static inline int cfd_atomic_load(const cfd_atomic_int* ptr) {
      10              :     return *ptr;
      11              : }
      12              : 
      13              : /**
      14              :  * @brief Compare and Swap
      15              :  * @return 1 if swap occurred (success), 0 otherwise
      16              :  */
      17              : static inline int cfd_atomic_cas(cfd_atomic_int* ptr, int expected, int desired) {
      18              :     // InterlockedCompareExchange(dest, exchange, comparand)
      19              :     // Returns the initial value of dest.
      20              :     // If return value == comparand (expected), then success.
      21              :     return (InterlockedCompareExchange(ptr, desired, expected) == expected);
      22              : }
      23              : 
      24              : static inline void cfd_atomic_store(cfd_atomic_int* ptr, int value) {
      25              :     InterlockedExchange(ptr, value);
      26              : }
      27              : 
      28              : /* Atomic pointer operations (for function pointer globals) */
      29              : typedef void* volatile cfd_atomic_ptr;
      30              : 
      31              : static inline void* cfd_atomic_ptr_load(const cfd_atomic_ptr* ptr) {
      32              :     return InterlockedCompareExchangePointer((volatile PVOID*)ptr, NULL, NULL);
      33              : }
      34              : 
      35              : static inline void cfd_atomic_ptr_store(cfd_atomic_ptr* ptr, void* value) {
      36              :     InterlockedExchangePointer(ptr, value);
      37              : }
      38              : 
      39              : /** Store value and return the previous pointer, as one atomic step */
      40              : static inline void* cfd_atomic_ptr_exchange(cfd_atomic_ptr* ptr, void* value) {
      41              :     return InterlockedExchangePointer(ptr, value);
      42              : }
      43              : 
      44              : #else
      45              : #include <stdatomic.h>
      46              : 
      47              : typedef atomic_int cfd_atomic_int;
      48              : 
      49          640 : static inline int cfd_atomic_load(cfd_atomic_int* ptr) {
      50          534 :     return atomic_load(ptr);
      51              : }
      52              : 
      53          888 : static inline int cfd_atomic_cas(cfd_atomic_int* ptr, int expected, int desired) {
      54              :     // atomic_compare_exchange_strong takes &expected
      55          888 :     int exp = expected;
      56          888 :     return atomic_compare_exchange_strong(ptr, &exp, desired);
      57              : }
      58              : 
      59           36 : static inline void cfd_atomic_store(cfd_atomic_int* ptr, int value) {
      60           36 :     atomic_store(ptr, value);
      61              : }
      62              : 
      63              : /* Atomic pointer operations (for function pointer globals).
      64              :  * Uses uintptr_t to avoid _Atomic(void*) which can cause TSan issues. */
      65              : #include <stdint.h>
      66              : 
      67              : typedef _Atomic(uintptr_t) cfd_atomic_ptr;
      68              : 
      69            5 : static inline void* cfd_atomic_ptr_load(cfd_atomic_ptr* ptr) {
      70            5 :     return (void*)atomic_load(ptr);
      71              : }
      72              : 
      73           34 : static inline void cfd_atomic_ptr_store(cfd_atomic_ptr* ptr, void* value) {
      74           34 :     atomic_store(ptr, (uintptr_t)value);
      75              : }
      76              : 
      77              : static inline void* cfd_atomic_ptr_exchange(cfd_atomic_ptr* ptr, void* value) {
      78              :     return (void*)atomic_exchange(ptr, (uintptr_t)value);
      79              : }
      80              : 
      81              : #endif
      82              : 
      83              : #endif  // CFD_THREADING_INTERNAL_H
        

Generated by: LCOV version 2.0-1