vmap: Use {map,unmap}_segment() helpers
[akaros.git] / kern / arch / riscv / atomic.h
index fd94e9e..e57815d 100644 (file)
@@ -1,12 +1,30 @@
-#ifndef ROS_KERN_ARCH_ATOMIC_H
-#define ROS_KERN_ARCH_ATOMIC_H
+#pragma once
 
 #include <ros/common.h>
 #include <arch/arch.h>
 
-bool atomic_cas(atomic_t *addr, long exp_val, long new_val);
-bool atomic_cas_ptr(void **addr, void *exp_val, void *new_val);
-bool atomic_cas_u32(uint32_t *addr, uint32_t exp_val, uint32_t new_val);
+#ifdef __riscv64
+# define LR_P "lr.d"
+# define SC_P "sc.d"
+#else
+# define LR_P "lr.w"
+# define SC_P "sc.w"
+#endif
+
+static bool atomic_cas(atomic_t *addr, long exp_val, long new_val)
+{
+  return __sync_bool_compare_and_swap(addr, exp_val, new_val);
+}
+
+static bool atomic_cas_ptr(void** addr, void* exp_val, void* new_val)
+{
+  return __sync_bool_compare_and_swap(addr, exp_val, new_val);
+}
+
+static bool atomic_cas_u32(uint32_t *addr, uint32_t exp_val, uint32_t new_val)
+{
+  return __sync_bool_compare_and_swap(addr, exp_val, new_val);
+}
 
 static inline void atomic_init(atomic_t *number, long val)
 {
@@ -46,16 +64,19 @@ static inline void atomic_dec(atomic_t *number)
 
 /* Adds val to number, so long as number was not zero. Returns TRUE if the
  * operation succeeded (added, not zero), returns FALSE if number is zero. */
-static inline bool atomic_add_not_zero(atomic_t *number, long val)
+static inline bool atomic_add_not_zero(atomic_t *num, long inc)
 {
-       long old_num, new_num;
-       do {
-               old_num = atomic_read(number);
-               if (!old_num)
-                       return FALSE;
-               new_num = old_num + val;
-       } while (!atomic_cas(number, old_num, new_num));
-       return TRUE;
+       long res, tmp;
+       asm volatile ("1:\n"
+                     LR_P " %0, 0(%2)\n"     // tmp = *num; lock line
+                     "li    %1, 1\n"         // res = 1
+                     "beqz  %0, 2f\n"        // if (val == 0) goto fail
+                     "add   %0, %0, %3\n"    // tmp += inc
+                     SC_P " %1, %0, 0(%2)\n" // if (locked) *num = tmp
+                     "bnez  %1, 1b\n"        // else goto retry
+                                 "2:\n"
+                     : "=&r"(tmp), "=&r"(res) : "r"(num), "r"(inc) : "memory");
+       return res == 0;
 }
 
 /* Subtraces val from number, returning True if the new value is 0. */
@@ -107,31 +128,28 @@ static inline bool spin_locked(spinlock_t* lock)
        return lock->rlock;
 }
 
-static inline uint32_t spin_trylock(spinlock_t* lock)
+static inline bool __spin_trylock(spinlock_t *lock)
 {
-       return __sync_fetch_and_or(&lock->rlock, 1);
+       return !__sync_fetch_and_or(&lock->rlock, 1);
 }
 
-static inline void spin_lock(spinlock_t *lock)
+static inline void __spin_lock(spinlock_t *lock)
 {
-       while(spin_trylock(lock))
-               while(lock->rlock);
+       do
+       {
+               while (lock->rlock)
+                       ;
+       } while (!__spin_trylock(lock));
        mb();
 }
 
-static inline void spin_unlock(spinlock_t *lock)
+static inline void __spin_unlock(spinlock_t *lock)
 {
        mb();
        lock->rlock = 0;
 }
 
-static inline void spinlock_init(spinlock_t *lock)
+static inline void __spinlock_init(spinlock_t *lock)
 {
        lock->rlock = 0;
 }
-
-static inline void spinlock_debug(spinlock_t* lock)
-{
-}
-
-#endif /* ROS_KERN_ARCH_ATOMIC_H */