modularize core prof correctly, starting mem pool strat for task records
[IRC.git] / Robust / src / Runtime / mlp_lock.h
1 #ifndef ____MLP_LOCK_H__
2 #define ____MLP_LOCK_H__
3
4
5 #include "runtime.h"
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <assert.h>
10
11 #define __xg(x) ((volatile INTPTR *)(x))
12
13 #define CFENCE   asm volatile("":::"memory");
14
15 #define LOCK_PREFIX \
16   ".section .smp_locks,\"a\"\n"   \
17   "  .align 4\n"                  \
18   "  .long 661f\n"             /* address */\
19   ".previous\n"                   \
20   "661:\n\tlock; "
21
22
23 static inline void atomic_dec(volatile int *v) {
24   __asm__ __volatile__ (LOCK_PREFIX "decl %0"
25                         : "+m" (*v));
26 }
27
28 static inline void atomic_inc(volatile int *v) {
29   __asm__ __volatile__ (LOCK_PREFIX "incl %0"
30                         : "+m" (*v));
31 }
32
33 static inline int atomic_sub_and_test(int i, volatile int *v) {
34   unsigned char c;
35
36   __asm__ __volatile__ (LOCK_PREFIX "subl %2,%0; sete %1"
37                         : "+m" (*v), "=qm" (c)
38                         : "ir" (i) : "memory");
39   return c;
40 }
41
42 #ifdef BIT64
43 static inline INTPTR LOCKXCHG(volatile INTPTR * ptr, INTPTR val){
44   INTPTR retval;
45   //note: xchgl always implies lock 
46   __asm__ __volatile__("xchgq %0,%1"
47                        : "=r"(retval)
48                        : "m"(*ptr), "0"(val)
49                        : "memory");
50   return retval;
51  
52 }
53 #else
54 static inline int LOCKXCHG(volatile int* ptr, int val){
55   int retval;
56   //note: xchgl always implies lock 
57   __asm__ __volatile__("xchgl %0,%1"
58                        : "=r"(retval)
59                        : "m"(*ptr), "0"(val)
60                        : "memory");
61   return retval;
62  
63 }
64 #endif
65
66 /*
67 static inline int write_trylock(volatile int *lock) {
68   int retval=0;
69   __asm__ __volatile__("xchgl %0,%1"
70                        : "=r"(retval)
71                        : "m"(*lock), "0"(retval)
72                        : "memory");
73   return retval;
74 }
75 */
76
77 #ifdef BIT64
78 static inline INTPTR CAS(volatile void *ptr, unsigned INTPTR old, unsigned INTPTR new){
79   unsigned INTPTR prev;
80   __asm__ __volatile__("lock; cmpxchgq %1,%2"
81                        : "=a"(prev)
82                        : "r"(new), "m"(*__xg(ptr)), "0"(old)
83                        : "memory");
84   return prev;
85 }
86 #else
87 static inline long CAS(volatile void *ptr, unsigned long old, unsigned long new){
88   unsigned long prev;
89   __asm__ __volatile__("lock; cmpxchgl %k1,%2"
90                        : "=a"(prev)
91                        : "r"(new), "m"(*__xg(ptr)), "0"(old)
92                        : "memory");
93   return prev;
94 }
95 #endif
96
97 static inline int BARRIER(){
98   CFENCE;
99   return 1;
100 }
101
102
103 #endif // ____MLP_LOCK_H__