Created
October 20, 2012 17:13
-
-
Save tux21b/3924068 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The "atomic.h" file provides low-level atomic memory primitives for the | |
// x86 plattform. All functions in this file provide sequential memory | |
// consistency. | |
// atomic_load atomically loads *addr. | |
static inline int atomic_load(int *addr) | |
{ | |
int val = *(volatile const int*)(addr); | |
__asm__ __volatile__ ("" ::: "memory"); | |
return val; | |
} | |
// atomic_store atomically stores val into *addr. | |
static inline void atomic_store(int *addr, int val) | |
{ | |
__asm__ __volatile__ ( | |
"xchgl %0, %1\n" : | |
"=q" (val) : | |
"m" (*addr), "0" (val) : | |
"memory"); | |
} | |
// atomic_add atomically adds delta to *addr and returns the new value. | |
static inline int atomic_add(int *addr, int delta) | |
{ | |
int t = delta; | |
__asm__ __volatile__ ( | |
"lock; xaddl %0, %1" : | |
"+q" (delta), "+m" (*addr) : | |
: | |
"memory"); | |
return delta + t; | |
} | |
// atomic_cas executes the compare-and-swap operation on *addr. | |
static inline int atomic_cas(int *addr, int expected, int desired) | |
{ | |
__asm__ __volatile__ ("" ::: "memory"); | |
int prev = expected; | |
__asm__ __volatile__ ( | |
"lock; cmpxchgl %1, %2\n" : | |
"=a" (prev) : | |
"q" (desired), "m" (*addr), "a" (expected) : | |
"memory"); | |
int success = (prev == expected); | |
__asm__ __volatile__ ("" ::: "memory"); | |
return success; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment