Files
armorpaint/base/sources/backends/posix_thread.h
T

115 lines
3.7 KiB
C
Raw Normal View History

2024-11-10 22:11:29 +01:00
#pragma once
2025-02-26 08:47:09 +01:00
#include <iron_global.h>
2025-02-26 18:46:36 +01:00
#include <pthread.h>
typedef struct {
pthread_mutex_t mutex;
2025-03-09 17:02:12 +01:00
} iron_mutex_impl_t;
2025-02-26 18:46:36 +01:00
typedef struct {
void *param;
void (*thread)(void *param);
pthread_t pthread;
2025-03-09 17:02:12 +01:00
} iron_thread_impl_t;
2024-11-10 22:11:29 +01:00
2025-03-09 17:02:12 +01:00
#if defined(IRON_MACOS) || defined(IRON_IOS)
2024-11-10 22:11:29 +01:00
#include <libkern/OSAtomic.h>
2025-03-09 17:02:12 +01:00
static inline bool iron_atomic_compare_exchange(volatile int32_t *pointer, int32_t old_value, int32_t new_value) {
2024-11-10 22:11:29 +01:00
return OSAtomicCompareAndSwap32Barrier(old_value, new_value, pointer);
}
2025-03-09 17:02:12 +01:00
static inline bool iron_atomic_compare_exchange_pointer(void *volatile *pointer, void *old_value, void *new_value) {
2024-11-10 22:11:29 +01:00
return OSAtomicCompareAndSwapPtrBarrier(old_value, new_value, pointer);
}
2025-03-09 17:02:12 +01:00
static inline int32_t iron_atomic_increment(volatile int32_t *pointer) {
2024-11-10 22:11:29 +01:00
return OSAtomicIncrement32Barrier(pointer) - 1;
}
2025-03-09 17:02:12 +01:00
static inline int32_t iron_atomic_decrement(volatile int32_t *pointer) {
2024-11-10 22:11:29 +01:00
return OSAtomicDecrement32Barrier(pointer) + 1;
}
2025-03-09 17:02:12 +01:00
static inline void iron_atomic_exchange(volatile int32_t *pointer, int32_t value) {
2024-11-10 22:11:29 +01:00
__sync_swap(pointer, value);
}
2025-03-09 17:02:12 +01:00
static inline void iron_atomic_exchange_float(volatile float *pointer, float value) {
2024-11-10 22:11:29 +01:00
__sync_swap((volatile int32_t *)pointer, *(int32_t *)&value);
}
2025-03-09 17:02:12 +01:00
static inline void iron_atomic_exchange_double(volatile double *pointer, double value) {
2024-11-10 22:11:29 +01:00
__sync_swap((volatile int64_t *)pointer, *(int64_t *)&value);
}
#else
// clang/gcc intrinsics
2025-03-09 17:02:12 +01:00
static inline bool iron_atomic_compare_exchange(volatile int32_t *pointer, int32_t old_value, int32_t new_value) {
2024-11-10 22:11:29 +01:00
return __sync_val_compare_and_swap(pointer, old_value, new_value) == old_value;
}
2025-03-09 17:02:12 +01:00
static inline bool iron_atomic_compare_exchange_pointer(void *volatile *pointer, void *old_value, void *new_value) {
2024-11-10 22:11:29 +01:00
return __sync_val_compare_and_swap(pointer, old_value, new_value) == old_value;
}
2025-03-09 17:02:12 +01:00
static inline int32_t iron_atomic_increment(volatile int32_t *pointer) {
2024-11-10 22:11:29 +01:00
return __sync_fetch_and_add(pointer, 1);
}
2025-03-09 17:02:12 +01:00
static inline int32_t iron_atomic_decrement(volatile int32_t *pointer) {
2024-11-10 22:11:29 +01:00
return __sync_fetch_and_sub(pointer, 1);
}
#ifdef __clang__
2025-03-09 17:02:12 +01:00
static inline void iron_atomic_exchange(volatile int32_t *pointer, int32_t value) {
2024-11-10 22:11:29 +01:00
__sync_swap(pointer, value);
}
2025-03-09 17:02:12 +01:00
static inline void iron_atomic_exchange_float(volatile float *pointer, float value) {
2024-11-10 22:11:29 +01:00
__sync_swap((volatile int32_t *)pointer, *(int32_t *)&value);
}
2025-03-09 17:02:12 +01:00
static inline void iron_atomic_exchange_double(volatile double *pointer, double value) {
2024-11-10 22:11:29 +01:00
__sync_swap((volatile int64_t *)pointer, *(int64_t *)&value);
}
#else
// Beware, __sync_lock_test_and_set is not a full barrier and can have platform-specific weirdness
2025-03-09 17:02:12 +01:00
static inline void iron_atomic_exchange(volatile int32_t *pointer, int32_t value) {
2024-11-10 22:11:29 +01:00
__sync_lock_test_and_set(pointer, value);
}
2025-03-09 17:02:12 +01:00
static inline void iron_atomic_exchange_float(volatile float *pointer, float value) {
2024-11-10 22:11:29 +01:00
__sync_lock_test_and_set((volatile int32_t *)pointer, *(int32_t *)&value);
}
2025-03-09 17:02:12 +01:00
static inline void iron_atomic_exchange_double(volatile double *pointer, double value) {
2024-11-10 22:11:29 +01:00
__sync_lock_test_and_set((volatile int64_t *)pointer, *(int64_t *)&value);
}
#endif
#endif
2025-03-09 17:02:12 +01:00
#define IRON_ATOMIC_COMPARE_EXCHANGE(pointer, oldValue, newValue) (iron_atomic_compare_exchange(pointer, oldValue, newValue))
2024-11-10 22:11:29 +01:00
2025-03-09 17:02:12 +01:00
#define IRON_ATOMIC_COMPARE_EXCHANGE_POINTER(pointer, oldValue, newValue) (iron_atomic_compare_exchange_pointer(pointer, oldValue, newValue))
2024-11-10 22:11:29 +01:00
2025-03-09 17:02:12 +01:00
#define IRON_ATOMIC_INCREMENT(pointer) (iron_atomic_increment(pointer))
2024-11-10 22:11:29 +01:00
2025-03-09 17:02:12 +01:00
#define IRON_ATOMIC_DECREMENT(pointer) (iron_atomic_decrement(pointer))
2024-11-10 22:11:29 +01:00
2025-03-09 17:02:12 +01:00
#define IRON_ATOMIC_EXCHANGE_32(pointer, value) (iron_atomic_exchange(pointer, value))
2024-11-10 22:11:29 +01:00
2025-03-09 17:02:12 +01:00
#define IRON_ATOMIC_EXCHANGE_FLOAT(pointer, value) (iron_atomic_exchange_float(pointer, value))
2024-11-10 22:11:29 +01:00
2025-03-09 17:02:12 +01:00
#define IRON_ATOMIC_EXCHANGE_DOUBLE(pointer, value) (iron_atomic_exchange_double(pointer, value))