posix_thread.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #include <iron_global.h>
  3. #include <pthread.h>
  4. typedef struct {
  5. pthread_mutex_t mutex;
  6. } iron_mutex_impl_t;
  7. typedef struct {
  8. void *param;
  9. void (*thread)(void *param);
  10. pthread_t pthread;
  11. } iron_thread_impl_t;
  12. #if defined(IRON_MACOS) || defined(IRON_IOS)
  13. #include <libkern/OSAtomic.h>
  14. static inline bool iron_atomic_compare_exchange(volatile int32_t *pointer, int32_t old_value, int32_t new_value) {
  15. return OSAtomicCompareAndSwap32Barrier(old_value, new_value, pointer);
  16. }
  17. static inline bool iron_atomic_compare_exchange_pointer(void *volatile *pointer, void *old_value, void *new_value) {
  18. return OSAtomicCompareAndSwapPtrBarrier(old_value, new_value, pointer);
  19. }
  20. static inline int32_t iron_atomic_increment(volatile int32_t *pointer) {
  21. return OSAtomicIncrement32Barrier(pointer) - 1;
  22. }
  23. static inline int32_t iron_atomic_decrement(volatile int32_t *pointer) {
  24. return OSAtomicDecrement32Barrier(pointer) + 1;
  25. }
  26. static inline void iron_atomic_exchange(volatile int32_t *pointer, int32_t value) {
  27. __sync_swap(pointer, value);
  28. }
  29. static inline void iron_atomic_exchange_float(volatile float *pointer, float value) {
  30. __sync_swap((volatile int32_t *)pointer, *(int32_t *)&value);
  31. }
  32. static inline void iron_atomic_exchange_double(volatile double *pointer, double value) {
  33. __sync_swap((volatile int64_t *)pointer, *(int64_t *)&value);
  34. }
  35. #else
  36. // clang/gcc intrinsics
  37. static inline bool iron_atomic_compare_exchange(volatile int32_t *pointer, int32_t old_value, int32_t new_value) {
  38. return __sync_val_compare_and_swap(pointer, old_value, new_value) == old_value;
  39. }
  40. static inline bool iron_atomic_compare_exchange_pointer(void *volatile *pointer, void *old_value, void *new_value) {
  41. return __sync_val_compare_and_swap(pointer, old_value, new_value) == old_value;
  42. }
  43. static inline int32_t iron_atomic_increment(volatile int32_t *pointer) {
  44. return __sync_fetch_and_add(pointer, 1);
  45. }
  46. static inline int32_t iron_atomic_decrement(volatile int32_t *pointer) {
  47. return __sync_fetch_and_sub(pointer, 1);
  48. }
  49. #ifdef __clang__
  50. static inline void iron_atomic_exchange(volatile int32_t *pointer, int32_t value) {
  51. __sync_swap(pointer, value);
  52. }
  53. static inline void iron_atomic_exchange_float(volatile float *pointer, float value) {
  54. __sync_swap((volatile int32_t *)pointer, *(int32_t *)&value);
  55. }
  56. static inline void iron_atomic_exchange_double(volatile double *pointer, double value) {
  57. __sync_swap((volatile int64_t *)pointer, *(int64_t *)&value);
  58. }
  59. #else
  60. // Beware, __sync_lock_test_and_set is not a full barrier and can have platform-specific weirdness
  61. static inline void iron_atomic_exchange(volatile int32_t *pointer, int32_t value) {
  62. __sync_lock_test_and_set(pointer, value);
  63. }
  64. static inline void iron_atomic_exchange_float(volatile float *pointer, float value) {
  65. __sync_lock_test_and_set((volatile int32_t *)pointer, *(int32_t *)&value);
  66. }
  67. static inline void iron_atomic_exchange_double(volatile double *pointer, double value) {
  68. __sync_lock_test_and_set((volatile int64_t *)pointer, *(int64_t *)&value);
  69. }
  70. #endif
  71. #endif
  72. #define IRON_ATOMIC_COMPARE_EXCHANGE(pointer, oldValue, newValue) (iron_atomic_compare_exchange(pointer, oldValue, newValue))
  73. #define IRON_ATOMIC_COMPARE_EXCHANGE_POINTER(pointer, oldValue, newValue) (iron_atomic_compare_exchange_pointer(pointer, oldValue, newValue))
  74. #define IRON_ATOMIC_INCREMENT(pointer) (iron_atomic_increment(pointer))
  75. #define IRON_ATOMIC_DECREMENT(pointer) (iron_atomic_decrement(pointer))
  76. #define IRON_ATOMIC_EXCHANGE_32(pointer, value) (iron_atomic_exchange(pointer, value))
  77. #define IRON_ATOMIC_EXCHANGE_FLOAT(pointer, value) (iron_atomic_exchange_float(pointer, value))
  78. #define IRON_ATOMIC_EXCHANGE_DOUBLE(pointer, value) (iron_atomic_exchange_double(pointer, value))