From c63f340d90800895f007de64b7d2d14624263331 Mon Sep 17 00:00:00 2001 From: nthnluu Date: Sun, 28 Jan 2024 21:20:27 -0500 Subject: Created student weenix repository --- kernel/include/util/atomic.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 kernel/include/util/atomic.h (limited to 'kernel/include/util/atomic.h') diff --git a/kernel/include/util/atomic.h b/kernel/include/util/atomic.h new file mode 100644 index 0000000..2c67e38 --- /dev/null +++ b/kernel/include/util/atomic.h @@ -0,0 +1,31 @@ +#ifndef ATOMIC_H +#define ATOMIC_H + +typedef int atomic_t; + +#define ATOMIC_INIT(i) (i) + +static inline int __atomic_add_unless(atomic_t *a, int v, int u) +{ + int c, old; + c = __sync_fetch_and_add(a, 0); + while (c != u && (old = __sync_val_compare_and_swap(a, c, c + v)) != c) + c = old; + return c; +} + +static inline void atomic_set(atomic_t *a, int i) { *a = i; } + +static inline void atomic_inc(atomic_t *a) { __sync_add_and_fetch(a, 1); } + +static inline int atomic_dec_and_test(atomic_t *a) +{ + return __sync_sub_and_fetch(a, 1) == 0; +} + +static inline int atomic_inc_not_zero(atomic_t *a) +{ + return __atomic_add_unless(a, 1, 0); +} + +#endif \ No newline at end of file -- cgit v1.2.3-70-g09d2