windows_thread.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #pragma once
  2. #include <intrin.h>
  3. typedef struct {
  4. void *handle;
  5. void *param;
  6. void (*func)(void *param);
  7. } iron_thread_impl_t;
  8. typedef struct {
  9. void *DebugInfo;
  10. long LockCount;
  11. long RecursionCount;
  12. void *OwningThread;
  13. void *LockSemaphore;
  14. unsigned long __w64 SpinCount;
  15. } iron_microsoft_critical_section_t;
  16. typedef struct {
  17. iron_microsoft_critical_section_t criticalSection;
  18. } iron_mutex_impl_t;
  19. static inline bool iron_atomic_compare_exchange(volatile int32_t *pointer, int32_t old_value, int32_t new_value) {
  20. return _InterlockedCompareExchange((volatile long *)pointer, new_value, old_value) == old_value;
  21. }
  22. #define IRON_ATOMIC_COMPARE_EXCHANGE(pointer, oldValue, newValue) (iron_atomic_compare_exchange(pointer, oldValue, newValue))
  23. static inline bool iron_atomic_compare_exchange_pointer(void *volatile *pointer, void *old_value, void *new_value) {
  24. return _InterlockedCompareExchangePointer(pointer, new_value, old_value) == old_value;
  25. }
  26. #define IRON_ATOMIC_COMPARE_EXCHANGE_POINTER(pointer, oldValue, newValue) (iron_atomic_compare_exchange_pointer(pointer, oldValue, newValue))
  27. static inline int32_t iron_atomic_increment(volatile int32_t *pointer) {
  28. return _InterlockedIncrement((volatile long *)pointer) - 1;
  29. }
  30. #define IRON_ATOMIC_INCREMENT(pointer) (iron_atomic_increment(pointer))
  31. static inline int32_t iron_atomic_decrement(volatile int32_t *pointer) {
  32. return _InterlockedDecrement((volatile long *)pointer) + 1;
  33. }
  34. #define IRON_ATOMIC_DECREMENT(pointer) (iron_atomic_decrement(pointer))
  35. static inline void iron_atomic_exchange(volatile int32_t *pointer, int32_t value) {
  36. _InterlockedExchange((volatile long *)pointer, value);
  37. }
  38. #define IRON_ATOMIC_EXCHANGE_32(pointer, value) (iron_atomic_exchange(pointer, value))
  39. static inline void iron_atomic_exchange_float(volatile float *pointer, float value) {
  40. _InterlockedExchange((volatile long *)pointer, *(long *)&value);
  41. }
  42. #define IRON_ATOMIC_EXCHANGE_FLOAT(pointer, value) (iron_atomic_exchange_float(pointer, value))
  43. static inline void iron_atomic_exchange_double(volatile double *pointer, double value) {
  44. _InterlockedExchange64((volatile __int64 *)pointer, *(__int64 *)&value);
  45. }
  46. #define IRON_ATOMIC_EXCHANGE_DOUBLE(pointer, value) (iron_atomic_exchange_double(pointer, value))