move refcount to header
This commit is contained in:
@@ -6,7 +6,6 @@ kernel_sources += files(
|
|||||||
'panic.c',
|
'panic.c',
|
||||||
'percpu.c',
|
'percpu.c',
|
||||||
'print.c',
|
'print.c',
|
||||||
'refcount.c',
|
|
||||||
'spin.c',
|
'spin.c',
|
||||||
'wait.c',
|
'wait.c',
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
#include <zinnia/refcount.h>
|
|
||||||
#include <stdatomic.h>
|
|
||||||
|
|
||||||
void refcount_init(struct refcount* rc) {
|
|
||||||
atomic_store_explicit(&rc->count, 1, memory_order_relaxed);
|
|
||||||
}
|
|
||||||
|
|
||||||
void refcount_inc(struct refcount* rc) {
|
|
||||||
atomic_fetch_add_explicit(&rc->count, 1, memory_order_relaxed);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool refcount_dec(struct refcount* rc) {
|
|
||||||
if (atomic_fetch_sub_explicit(&rc->count, 1, memory_order_release) != 1)
|
|
||||||
return false;
|
|
||||||
atomic_thread_fence(memory_order_acquire);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
@@ -8,6 +8,17 @@ struct refcount {
|
|||||||
_Atomic(size_t) count;
|
_Atomic(size_t) count;
|
||||||
};
|
};
|
||||||
|
|
||||||
void refcount_init(struct refcount* rc);
|
static inline void refcount_init(struct refcount* rc) {
|
||||||
void refcount_inc(struct refcount* rc);
|
atomic_store_explicit(&rc->count, 1, memory_order_relaxed);
|
||||||
bool refcount_dec(struct refcount* rc);
|
}
|
||||||
|
|
||||||
|
static inline void refcount_inc(struct refcount* rc) {
|
||||||
|
atomic_fetch_add_explicit(&rc->count, 1, memory_order_relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool refcount_dec(struct refcount* rc) {
|
||||||
|
if (atomic_fetch_sub_explicit(&rc->count, 1, memory_order_release) != 1)
|
||||||
|
return false;
|
||||||
|
atomic_thread_fence(memory_order_acquire);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user