The ROme OpTimistic Simulator  2.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
x86.c
Go to the documentation of this file.
1 
32 // Do not compile anything here if we're not on an x86 machine!
33 
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdbool.h>
37 #include <arch/atomic.h>
38 #include <mm/dymelor.h>
39 
49 inline bool iCAS(volatile uint32_t *ptr, uint32_t oldVal, uint32_t newVal)
50 {
51  unsigned long res = 0;
52 
53  __asm__ __volatile__("lock cmpxchgl %1, %2;" // ZF = 1 if succeeded
54  "lahf;" // to get the correct result even if oldVal == 0
55  "bt $14, %%ax;" // is ZF set? (ZF is the 6'th bit in %ah, so it's the 14'th in ax)
56  "adc %0, %0" // get the result
57  :"=r"(res)
58  :"r"(newVal), "m"(*ptr), "a"(oldVal), "0"(res)
59  :"memory");
60 
61  return (bool)res;
62 }
63 
71 inline int atomic_test_and_set(int *b)
72 {
73  int result = 0;
74 
75  __asm__ __volatile__("lock bts $0, %1;\n\t"
76  "adc %0, %0"
77  :"=r"(result)
78  :"m"(*b), "0"(result)
79  :"memory");
80 
81  return !result;
82 }
83 
90 inline void atomic_inc(atomic_t * v)
91 {
92  __asm__ __volatile__("lock incl %0"
93  :"=m"(v->count)
94  :"m"(v->count));
95 }
96 
103 inline void atomic_dec(atomic_t * v)
104 {
105  __asm__ __volatile__("lock decl %0"
106  :"=m"(v->count)
107  :"m"(v->count));
108 }
109 
119 {
120  unsigned char c = 0;
121 
122  __asm__ __volatile__("lock incl %0\n\t"
123  "sete %1"
124  :"=m"(v->count), "=qm"(c)
125  :"m"(v->count)
126  :"memory");
127  return c != 0;
128 }
129 
135 inline void spin_lock(spinlock_t * s)
136 {
137  __asm__ __volatile__("1:\n\t" "movl $1,%%eax\n\t"
138  "lock xchgl %%eax, %0\n\t"
139  "testl %%eax, %%eax\n\t"
140  "jnz 1b"
141  : /* no output */
142  :"m"(s->lock)
143  :"eax", "memory");
144 }
145 
151 inline bool spin_trylock(spinlock_t * s)
152 {
153  return atomic_test_and_set((int *)&s->lock);
154 }
155 
161 inline void spin_unlock(spinlock_t * s)
162 {
163  __asm__ __volatile__("mov $0, %%eax\n\t"
164  "lock xchgl %%eax, %0"
165  : /* no output */
166  :"m"(s->lock)
167  :"eax", "memory");
168 }
volatile int count
Atomic counter. Use the provided API to ensure atomicity.
Definition: atomic.h:43
Dynamic Memory Logger and Restorer (DyMeLoR)
int atomic_test_and_set(int *b)
Definition: x86.c:71
void atomic_dec(atomic_t *v)
Definition: x86.c:103
void spin_unlock(spinlock_t *s)
Definition: x86.c:161
Atomic operations.
void spin_lock(spinlock_t *s)
Definition: x86.c:135
bool iCAS(volatile uint32_t *ptr, uint32_t oldVal, uint32_t newVal)
Definition: x86.c:49
bool spin_trylock(spinlock_t *s)
Definition: x86.c:151
int atomic_inc_and_test(atomic_t *v)
Definition: x86.c:118
volatile unsigned int lock
The lock guard.
Definition: atomic.h:52
void atomic_inc(atomic_t *v)
Definition: x86.c:90