7061461d33affe6f344b05ac158a87954fd3318c
[coreboot.git] / src / arch / i386 / include / arch / smp / atomic.h
1 #ifndef ARCH_SMP_ATOMIC_H
2 #define ARCH_SMP_ATOMIC_H
3
4 /*
5  * Make sure gcc doesn't try to be clever and move things around
6  * on us. We need to use _exactly_ the address the user gave us,
7  * not some alias that contains the same information.
8  */
9 typedef struct { volatile int counter; } atomic_t;
10
11 #define ATOMIC_INIT(i)  { (i) }
12
13 /*
14  * Atomic operations that C can't guarantee us.  Useful for
15  * resource counting etc..
16  */
17
18 /**
19  * atomic_read - read atomic variable
20  * @v: pointer of type atomic_t
21  * 
22  * Atomically reads the value of @v.  Note that the guaranteed
23  * useful range of an atomic_t is only 24 bits.
24  */ 
25 #define atomic_read(v)          ((v)->counter)
26
27 /**
28  * atomic_set - set atomic variable
29  * @v: pointer of type atomic_t
30  * @i: required value
31  * 
32  * Atomically sets the value of @v to @i.  Note that the guaranteed
33  * useful range of an atomic_t is only 24 bits.
34  */ 
35 #define atomic_set(v,i)         (((v)->counter) = (i))
36
37 /**
38  * atomic_inc - increment atomic variable
39  * @v: pointer of type atomic_t
40  * 
41  * Atomically increments @v by 1.  Note that the guaranteed
42  * useful range of an atomic_t is only 24 bits.
43  */ 
44 static __inline__ __attribute__((always_inline)) void atomic_inc(atomic_t *v)
45 {
46         __asm__ __volatile__(
47                 "lock ; incl %0"
48                 :"=m" (v->counter)
49                 :"m" (v->counter));
50 }
51
52 /**
53  * atomic_dec - decrement atomic variable
54  * @v: pointer of type atomic_t
55  * 
56  * Atomically decrements @v by 1.  Note that the guaranteed
57  * useful range of an atomic_t is only 24 bits.
58  */ 
59 static __inline__ __attribute__((always_inline)) void atomic_dec(atomic_t *v)
60 {
61         __asm__ __volatile__(
62                 "lock ; decl %0"
63                 :"=m" (v->counter)
64                 :"m" (v->counter));
65 }
66
67
68
69 #endif /* ARCH_SMP_ATOMIC_H */