aboutsummaryrefslogtreecommitdiff
path: root/kernel/include/util/atomic.h
diff options
context:
space:
mode:
authornthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
committernthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
commitc63f340d90800895f007de64b7d2d14624263331 (patch)
tree2c0849fa597dd6da831c8707b6f2603403778d7b /kernel/include/util/atomic.h
Created student weenix repository
Diffstat (limited to 'kernel/include/util/atomic.h')
-rw-r--r--kernel/include/util/atomic.h31
1 files changed, 31 insertions, 0 deletions
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