Helper to backtrace a user context
[akaros.git] / kern / arch / riscv / atomic.h
index 0d35bc8..7ba7f2c 100644 (file)
@@ -4,9 +4,28 @@
 #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 +65,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,9 +129,9 @@ 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)
@@ -118,7 +140,7 @@ static inline void __spin_lock(spinlock_t *lock)
        {
                while (lock->rlock)
                        ;
-       } while (spin_trylock(lock));
+       } while (!__spin_trylock(lock));
        mb();
 }
 
@@ -133,8 +155,4 @@ 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 */