Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

« Newer Snippets
Older Snippets »
1 total  XML / RSS feed 

Usefull atomic i86 operations

Some useful atomic operations in assembly

#ifndef  __ELIB_ATOMICS_HH_INCLUDDED__
#define  __ELIB_ATOMICS_HH_INCLUDDED__

#include <sys/types.h>

#ifndef SMP
#define SMP // just in case
#endif
#ifdef SMP
#define CPU_LOCK "lock; "
#else
#define CPU_LOCK
#endif

namespace E_LIB_NS
{
    __inline bool cmpxchg8(volatile u_int8_t *dest, u_int8_t oldv, u_int8_t newv)
    {
        register bool ret;
        __asm__ __volatile__ (CPU_LOCK "cmpxchgb %2, %1; sete %0"
                              : "=r" (ret), "=m" (*dest)
                              : "r" (newv), "a" (oldv)
                              : "memory");
        return ret;
    }

    __inline bool cmpxchg32(volatile u_int32_t *dest, u_int32_t oldv, u_int32_t newv)
    {
        register bool ret;
        __asm__ __volatile__ (CPU_LOCK "cmpxchgl %2, %1; sete %0"
                              : "=r" (ret), "=m" (*dest)
                              : "r" (newv), "a" (oldv)
                              : "memory");
        return ret;
    }

    __inline void increment32(volatile int32_t *p)
    {
        __asm__ __volatile__ (CPU_LOCK "incl %0"
                              : "=m" (*p)
                              : "m" (*p)
                              : "memory");
    }

    __inline void decrement32(volatile int32_t *p)
    {
        __asm__ __volatile__ (CPU_LOCK "decl %0"
                              : "=m" (*p)
                              : "m" (*p)
                              : "memory");
    }

    __inline void add32(volatile int32_t *p, int32_t incr)
    {
        __asm__ __volatile__ (CPU_LOCK "addl %1, %0"
                              : "=m" (*p)
                              : "r" (incr), "0" (*p)
                              : "memory");
    }

    __inline void move8(volatile u_int8_t *p, u_int8_t val)
    {
        __asm__ __volatile__ (
                              "movb %1, %0"
                              : "=m" (*p)
                              : "r" (val)
                              : "memory");
    }

    __inline void move32(volatile u_int32_t *p, u_int32_t val)
    {
        __asm__ __volatile__ (CPU_LOCK "movl %1, %0"
                              : "=m" (*p)
                              : "r" (val)
                              : "memory");
    }
} // namespace E_LIB_NS

#endif // __ELIB_ATOMICS_HH_INCLUDDED__

« Newer Snippets
Older Snippets »
1 total  XML / RSS feed