Removes extraneous x86 net files
[akaros.git] / kern / arch / riscv / atomic.c
1 #include <atomic.h>
2
3 // This emulates compare and swap by hashing the address into one of
4 // K buckets, acquiring the lock for that bucket, then performing the
5 // operation during the critical section.  :-(
6 bool atomic_cas(atomic_t *addr, long exp_val, long new_val)
7 {
8         if ((long)*addr != exp_val)
9                 return 0;
10         
11   #define K 17
12         /* TODO: not sure if this initialization works. */
13         static spinlock_t cas_locks[K*ARCH_CL_SIZE/sizeof(spinlock_t)] =
14                           {SPINLOCK_INITIALIZER_IRQSAVE};
15
16   uintptr_t bucket = (uintptr_t)addr / sizeof(uintptr_t) % K;
17         spinlock_t* lock = &cas_locks[bucket*ARCH_CL_SIZE/sizeof(spinlock_t)];
18         
19         bool retval = 0;
20         spin_lock_irqsave(lock);
21         if ((long)*addr == exp_val) {
22                 atomic_swap(addr, new_val);
23                 retval = 1;
24         }
25         spin_unlock_irqsave(lock);
26         return retval;
27 }
28
29 bool atomic_cas_ptr(void **addr, void *exp_val, void *new_val)
30 {
31         return atomic_cas((atomic_t*)addr, (long)exp_val, (long)new_val);
32 }
33
34 /* Ghetto, copied the regular CAS code... */
35 bool atomic_cas_u32(uint32_t *addr, uint32_t exp_val, uint32_t new_val)
36 {
37         if (*addr != exp_val)
38                 return 0;
39         
40   #define K 17
41         /* TODO: not sure if this initialization works. */
42         static spinlock_t cas_locks[K*ARCH_CL_SIZE/sizeof(spinlock_t)] =
43                           {SPINLOCK_INITIALIZER_IRQSAVE};
44
45   uintptr_t bucket = (uintptr_t)addr / sizeof(uintptr_t) % K;
46         spinlock_t* lock = &cas_locks[bucket*ARCH_CL_SIZE/sizeof(spinlock_t)];
47         
48         bool retval = 0;
49         spin_lock_irqsave(lock);
50         if (*addr == exp_val) {
51                 atomic_swap_u32(addr, new_val);
52                 retval = 1;
53         }
54         spin_unlock_irqsave(lock);
55         return retval;
56 }