replace stdatomic.h with zinnia/atomic.h
This commit is contained in:
@@ -1,163 +0,0 @@
|
||||
// SPDX-License-Identifier: ISC
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum memory_order {
|
||||
memory_order_relaxed = __ATOMIC_RELAXED,
|
||||
memory_order_consume = __ATOMIC_CONSUME,
|
||||
memory_order_acquire = __ATOMIC_ACQUIRE,
|
||||
memory_order_release = __ATOMIC_RELEASE,
|
||||
memory_order_acq_rel = __ATOMIC_ACQ_REL,
|
||||
memory_order_seq_cst = __ATOMIC_SEQ_CST,
|
||||
} memory_order;
|
||||
|
||||
#define atomic_thread_fence(order) __atomic_thread_fence(order)
|
||||
#define atomic_signal_fence(order) __atomic_signal_fence(order)
|
||||
|
||||
#ifdef __clang__
|
||||
|
||||
#define atomic_init(obj, value) __c11_atomic_init(obj, value)
|
||||
|
||||
#define atomic_store_explicit(obj, val, order) __c11_atomic_store(obj, val, order)
|
||||
#define atomic_load_explicit(obj, order) __c11_atomic_load(obj, order)
|
||||
|
||||
#define atomic_exchange_explicit(obj, val, order) __c11_atomic_exchange(obj, val, order)
|
||||
|
||||
#define atomic_compare_exchange_strong_explicit(obj, expected, desired, succ, fail) \
|
||||
__c11_atomic_compare_exchange_strong(obj, expected, desired, succ, fail)
|
||||
#define atomic_compare_exchange_weak_explicit(obj, expected, desired, succ, fail) \
|
||||
__c11_atomic_compare_exchange_weak(obj, expected, desired, succ, fail)
|
||||
|
||||
#define atomic_fetch_add_explicit(obj, arg, order) __c11_atomic_fetch_add(obj, arg, order)
|
||||
#define atomic_fetch_sub_explicit(obj, arg, order) __c11_atomic_fetch_sub(obj, arg, order)
|
||||
#define atomic_fetch_or_explicit(obj, arg, order) __c11_atomic_fetch_or(obj, arg, order)
|
||||
#define atomic_fetch_xor_explicit(obj, arg, order) __c11_atomic_fetch_xor(obj, arg, order)
|
||||
#define atomic_fetch_and_explicit(obj, arg, order) __c11_atomic_fetch_and(obj, arg, order)
|
||||
|
||||
#define atomic_is_lock_free(obj) __c11_atomic_is_lock_free(sizeof(*(obj)))
|
||||
|
||||
#else /* GCC */
|
||||
|
||||
#define atomic_init(obj, value) \
|
||||
do { \
|
||||
*(obj) = (value); \
|
||||
} while (0)
|
||||
|
||||
#define atomic_store_explicit(obj, val, order) \
|
||||
({ \
|
||||
auto __ao = (obj); \
|
||||
typeof(*__ao) __av = (val); \
|
||||
__atomic_store(__ao, &__av, order); \
|
||||
})
|
||||
|
||||
#define atomic_load_explicit(obj, order) \
|
||||
({ \
|
||||
auto __ao = (obj); \
|
||||
typeof(*__ao) __ar; \
|
||||
__atomic_load(__ao, &__ar, order); \
|
||||
__ar; \
|
||||
})
|
||||
|
||||
#define atomic_exchange_explicit(obj, val, order) \
|
||||
({ \
|
||||
auto __ao = (obj); \
|
||||
typeof(*__ao) __av = (val); \
|
||||
typeof(*__ao) __ar; \
|
||||
__atomic_exchange(__ao, &__av, &__ar, order); \
|
||||
__ar; \
|
||||
})
|
||||
|
||||
#define atomic_compare_exchange_strong_explicit(obj, expected, desired, succ, fail) \
|
||||
({ \
|
||||
auto __ao = (obj); \
|
||||
auto __ae = (expected); \
|
||||
typeof(*__ao) __ad = (desired); \
|
||||
__atomic_compare_exchange(__ao, __ae, &__ad, 0, succ, fail); \
|
||||
})
|
||||
|
||||
#define atomic_compare_exchange_weak_explicit(obj, expected, desired, succ, fail) \
|
||||
({ \
|
||||
auto __ao = (obj); \
|
||||
auto __ae = (expected); \
|
||||
typeof(*__ao) __ad = (desired); \
|
||||
__atomic_compare_exchange(__ao, __ae, &__ad, 1, succ, fail); \
|
||||
})
|
||||
|
||||
#define atomic_fetch_add_explicit(obj, arg, order) __atomic_fetch_add(obj, arg, order)
|
||||
#define atomic_fetch_sub_explicit(obj, arg, order) __atomic_fetch_sub(obj, arg, order)
|
||||
#define atomic_fetch_or_explicit(obj, arg, order) __atomic_fetch_or(obj, arg, order)
|
||||
#define atomic_fetch_xor_explicit(obj, arg, order) __atomic_fetch_xor(obj, arg, order)
|
||||
#define atomic_fetch_and_explicit(obj, arg, order) __atomic_fetch_and(obj, arg, order)
|
||||
|
||||
#define atomic_is_lock_free(obj) __atomic_is_lock_free(sizeof(*(obj)), (obj))
|
||||
|
||||
#endif
|
||||
|
||||
#define atomic_store(obj, val) atomic_store_explicit(obj, val, memory_order_seq_cst)
|
||||
#define atomic_load(obj) atomic_load_explicit(obj, memory_order_seq_cst)
|
||||
|
||||
#define atomic_exchange(obj, val) atomic_exchange_explicit(obj, val, memory_order_seq_cst)
|
||||
|
||||
#define atomic_compare_exchange_strong(obj, expected, desired) \
|
||||
atomic_compare_exchange_strong_explicit(obj, expected, desired, memory_order_seq_cst, memory_order_seq_cst)
|
||||
#define atomic_compare_exchange_weak(obj, expected, desired) \
|
||||
atomic_compare_exchange_weak_explicit(obj, expected, desired, memory_order_seq_cst, memory_order_seq_cst)
|
||||
|
||||
#define atomic_fetch_add(obj, arg) atomic_fetch_add_explicit(obj, arg, memory_order_seq_cst)
|
||||
#define atomic_fetch_sub(obj, arg) atomic_fetch_sub_explicit(obj, arg, memory_order_seq_cst)
|
||||
#define atomic_fetch_or(obj, arg) atomic_fetch_or_explicit(obj, arg, memory_order_seq_cst)
|
||||
#define atomic_fetch_xor(obj, arg) atomic_fetch_xor_explicit(obj, arg, memory_order_seq_cst)
|
||||
#define atomic_fetch_and(obj, arg) atomic_fetch_and_explicit(obj, arg, memory_order_seq_cst)
|
||||
|
||||
// atomic_flag
|
||||
typedef struct atomic_flag {
|
||||
_Atomic(bool) __val;
|
||||
} atomic_flag;
|
||||
|
||||
#define ATOMIC_FLAG_INIT {0}
|
||||
|
||||
#define atomic_flag_test_and_set_explicit(obj, order) atomic_exchange_explicit(&(obj)->__val, 1, order)
|
||||
#define atomic_flag_test_and_set(obj) atomic_flag_test_and_set_explicit(obj, memory_order_seq_cst)
|
||||
#define atomic_flag_clear_explicit(obj, order) atomic_store_explicit(&(obj)->__val, 0, order)
|
||||
#define atomic_flag_clear(obj) atomic_flag_clear_explicit(obj, memory_order_seq_cst)
|
||||
|
||||
typedef _Atomic(bool) atomic_bool;
|
||||
typedef _Atomic(char) atomic_char;
|
||||
typedef _Atomic(signed char) atomic_schar;
|
||||
typedef _Atomic(unsigned char) atomic_uchar;
|
||||
typedef _Atomic(short) atomic_short;
|
||||
typedef _Atomic(unsigned short) atomic_ushort;
|
||||
typedef _Atomic(int) atomic_int;
|
||||
typedef _Atomic(unsigned int) atomic_uint;
|
||||
typedef _Atomic(long) atomic_long;
|
||||
typedef _Atomic(unsigned long) atomic_ulong;
|
||||
typedef _Atomic(long long) atomic_llong;
|
||||
typedef _Atomic(unsigned long long) atomic_ullong;
|
||||
typedef _Atomic(__CHAR16_TYPE__) atomic_char16_t;
|
||||
typedef _Atomic(__CHAR32_TYPE__) atomic_char32_t;
|
||||
typedef _Atomic(__WCHAR_TYPE__) atomic_wchar_t;
|
||||
|
||||
typedef _Atomic(int_least8_t) atomic_int_least8_t;
|
||||
typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
|
||||
typedef _Atomic(int_least16_t) atomic_int_least16_t;
|
||||
typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
|
||||
typedef _Atomic(int_least32_t) atomic_int_least32_t;
|
||||
typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
|
||||
typedef _Atomic(int_least64_t) atomic_int_least64_t;
|
||||
typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
|
||||
typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
|
||||
typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
|
||||
typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
|
||||
typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
|
||||
typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
|
||||
typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
|
||||
typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
|
||||
typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
|
||||
typedef _Atomic(intptr_t) atomic_intptr_t;
|
||||
typedef _Atomic(uintptr_t) atomic_uintptr_t;
|
||||
typedef _Atomic(size_t) atomic_size_t;
|
||||
typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
|
||||
typedef _Atomic(intmax_t) atomic_intmax_t;
|
||||
typedef _Atomic(uintmax_t) atomic_uintmax_t;
|
||||
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
enum atomic_order {
|
||||
ATOMIC_RELAXED = __ATOMIC_RELAXED,
|
||||
ATOMIC_ACQUIRE = __ATOMIC_ACQUIRE,
|
||||
ATOMIC_RELEASE = __ATOMIC_RELEASE,
|
||||
ATOMIC_ACQ_REL = __ATOMIC_ACQ_REL,
|
||||
ATOMIC_SEQ_CST = __ATOMIC_SEQ_CST,
|
||||
};
|
||||
|
||||
typedef _Atomic(bool) atomic_bool;
|
||||
typedef _Atomic(int) atomic_int;
|
||||
typedef _Atomic(unsigned int) atomic_uint;
|
||||
typedef _Atomic(long) atomic_long;
|
||||
typedef _Atomic(unsigned long) atomic_ulong;
|
||||
typedef _Atomic(intptr_t) atomic_intptr_t;
|
||||
typedef _Atomic(uintptr_t) atomic_uintptr_t;
|
||||
typedef _Atomic(size_t) atomic_size_t;
|
||||
|
||||
#ifdef __clang__
|
||||
|
||||
#define atomic_init(obj, value) __c11_atomic_init(obj, value)
|
||||
|
||||
#define atomic_load(obj, order) __c11_atomic_load(obj, order)
|
||||
#define atomic_store(obj, val, order) __c11_atomic_store(obj, val, order)
|
||||
#define atomic_exchange(obj, val, order) __c11_atomic_exchange(obj, val, order)
|
||||
|
||||
#define atomic_compare_exchange(obj, expected, desired, success, failure) \
|
||||
__c11_atomic_compare_exchange_strong(obj, expected, desired, success, failure)
|
||||
#define atomic_compare_exchange_weak(obj, expected, desired, success, failure) \
|
||||
__c11_atomic_compare_exchange_weak(obj, expected, desired, success, failure)
|
||||
|
||||
#define atomic_fetch_add(obj, arg, order) __c11_atomic_fetch_add(obj, arg, order)
|
||||
#define atomic_fetch_sub(obj, arg, order) __c11_atomic_fetch_sub(obj, arg, order)
|
||||
#define atomic_fetch_and(obj, arg, order) __c11_atomic_fetch_and(obj, arg, order)
|
||||
#define atomic_fetch_or(obj, arg, order) __c11_atomic_fetch_or(obj, arg, order)
|
||||
#define atomic_fetch_xor(obj, arg, order) __c11_atomic_fetch_xor(obj, arg, order)
|
||||
|
||||
#else
|
||||
|
||||
#define atomic_init(obj, value) \
|
||||
do { \
|
||||
*(obj) = (value); \
|
||||
} while (0)
|
||||
|
||||
#define atomic_load(obj, order) __atomic_load_n(obj, order)
|
||||
#define atomic_store(obj, val, order) __atomic_store_n(obj, val, order)
|
||||
#define atomic_exchange(obj, val, order) __atomic_exchange_n(obj, val, order)
|
||||
|
||||
#define atomic_compare_exchange(obj, expected, desired, success, failure) \
|
||||
__atomic_compare_exchange_n(obj, expected, desired, false, success, failure)
|
||||
#define atomic_compare_exchange_weak(obj, expected, desired, success, failure) \
|
||||
__atomic_compare_exchange_n(obj, expected, desired, true, success, failure)
|
||||
|
||||
#define atomic_fetch_add(obj, arg, order) __atomic_fetch_add(obj, arg, order)
|
||||
#define atomic_fetch_sub(obj, arg, order) __atomic_fetch_sub(obj, arg, order)
|
||||
#define atomic_fetch_and(obj, arg, order) __atomic_fetch_and(obj, arg, order)
|
||||
#define atomic_fetch_or(obj, arg, order) __atomic_fetch_or(obj, arg, order)
|
||||
#define atomic_fetch_xor(obj, arg, order) __atomic_fetch_xor(obj, arg, order)
|
||||
|
||||
#endif
|
||||
|
||||
#define atomic_fence(order) __atomic_thread_fence(order)
|
||||
#define atomic_compiler_fence(order) __atomic_signal_fence(order)
|
||||
@@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <zinnia/atomic.h>
|
||||
#include <zinnia/list.h>
|
||||
#include <zinnia/percpu.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
struct dpc {
|
||||
void (*callback)(struct dpc* dpc, void* data);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <zinnia/atomic.h>
|
||||
#include <zinnia/utils.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stddef.h>
|
||||
|
||||
struct refcount {
|
||||
@@ -9,16 +9,16 @@ struct refcount {
|
||||
};
|
||||
|
||||
static inline void refcount_init(struct refcount* rc) {
|
||||
atomic_store_explicit(&rc->count, 1, memory_order_relaxed);
|
||||
atomic_store(&rc->count, 1, ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
static inline void refcount_inc(struct refcount* rc) {
|
||||
atomic_fetch_add_explicit(&rc->count, 1, memory_order_relaxed);
|
||||
atomic_fetch_add(&rc->count, 1, ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
static inline bool refcount_dec(struct refcount* rc) {
|
||||
if (atomic_fetch_sub_explicit(&rc->count, 1, memory_order_release) != 1)
|
||||
if (atomic_fetch_sub(&rc->count, 1, ATOMIC_RELEASE) != 1)
|
||||
return false;
|
||||
atomic_thread_fence(memory_order_acquire);
|
||||
atomic_fence(ATOMIC_ACQUIRE);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <zinnia/atomic.h>
|
||||
#include <zinnia/errno.h>
|
||||
#include <zinnia/init.h>
|
||||
#include <zinnia/initgraph.h>
|
||||
@@ -7,7 +8,6 @@
|
||||
#include <zinnia/tailq.h>
|
||||
#include <zinnia/types.h>
|
||||
#include <bits/sched.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <zinnia/atomic.h>
|
||||
#include <zinnia/sched.h>
|
||||
#include <zinnia/spin.h>
|
||||
#include <zinnia/tailq.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
// A queue of tasks waiting for an event.
|
||||
struct waitq {
|
||||
|
||||
Reference in New Issue
Block a user