df_atomic.h 9.4 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
////====================================================================
//
// df_atomic.h - Pyramid / DFS / df-lib
//
// Copyright (C) 2008 Baidu.com, Inc.
//
// Created on 2008-02-02 by Li Wen (liwen@baidu.com)
//
// -------------------------------------------------------------------
//
// Description
//
//    declaration of atomic operation
//    macro __x84_64__ should be defined for 64-bit platform
//
// -------------------------------------------------------------------
//
// Change Log
//
////====================================================================

#ifndef __DF_DF_ATOMIC_INCLUDE__
#define __DF_DF_ATOMIC_INCLUDE__

// Proclaim we are using SMP
#ifndef CONFIG_SMP
# define CONFIG_SMP
#endif

#include "df_def.h"

#if defined(__i386__) || defined(__x86_64__)

// Atomic operations that C can't guarantee us.  Useful for
// resource counting etc..
// SMP lock prefix
#ifdef CONFIG_SMP
#define LOCK_PREFIX "lock ; "
#else
#define LOCK_PREFIX ""
#endif

//return: the incremented value;
/// 原子地做 8位,16位,32位,64位的++i的操作
/// 该操作虽然参数和返回值都是无符号型整数,但是一样可以
/// 对有符号型整数做操作,只需要做适当的参数转换即可
/// @param pv 指向操作数的指针
/// @return 操作数加1以后的数值
#ifdef __x86_64__
static __inline__ uint64_t df_atomic_inc(volatile uint64_t * pv) {
    register unsigned long __res;
    __asm__ __volatile__ (
        "movq $1,%0;"
        LOCK_PREFIX "xaddq %0,(%1);"
        "incq %0"
        :"=a" (__res), "=q" (pv): "1" (pv));
    return __res;
}
#endif

static __inline__ uint32_t df_atomic_inc(volatile uint32_t * pv) {
    register unsigned int __res;
    __asm__ __volatile__ (
        "movl $1,%0;"
        LOCK_PREFIX "xaddl %0,(%1);"
        "incl %0"
        :"=a" (__res), "=q" (pv): "1" (pv));
    return __res;
}

static __inline__ uint16_t df_atomic_inc(volatile uint16_t * pv) {
    register unsigned short __res;
    __asm__ __volatile__ (
        "movw $1,%0;"
        LOCK_PREFIX "xaddw %0,(%1);"
        "incw %0"
        :"=a" (__res), "=q" (pv): "1" (pv));
    return __res;

}

static __inline__ uint8_t  df_atomic_inc(volatile uint8_t * pv) {
    register unsigned char __res;
    __asm__ __volatile__ (
        "movb $1,%0;"
        LOCK_PREFIX "xaddb %0,(%1);"
        "incb %0"
        :"=a" (__res), "=q" (pv): "1" (pv));
    return __res;
}

//return: the decremented value;
/// 原子地做 8位,16位,32位,64位的--i的操作
/// 该操作虽然参数和返回值都是无符号型整数,但是一样可以
/// 对有符号型整数做操作,只需要做适当的参数转换即可
/// @param pv 指向操作数的指针
/// @return 操作数减1后的数值
#ifdef __x86_64__
static __inline__ uint64_t df_atomic_dec(volatile uint64_t * pv) {
    register unsigned long __res;
    __asm__ __volatile__ (
        "movq $0xffffffffffffffff,%0;"
        LOCK_PREFIX "xaddq %0,(%1);"
        "decq %0"
        : "=a" (__res), "=q" (pv): "1" (pv));
    return __res;
}
#endif
static __inline__ uint32_t df_atomic_dec(volatile uint32_t * pv) {
    register unsigned int __res;
    __asm__ __volatile__ (
        "movl $0xffffffff,%0;"
        LOCK_PREFIX "xaddl %0,(%1);"
        "decl %0"
        : "=a" (__res), "=q" (pv): "1" (pv));
    return __res;

}
static __inline__ uint16_t df_atomic_dec(volatile uint16_t * pv) {
    register unsigned short __res;
    __asm__ __volatile__ (
        "movw $0xffff,%0;"
        LOCK_PREFIX "xaddw %0,(%1);"
        "decw %0"
        : "=a" (__res), "=q" (pv): "1" (pv));
    return __res;
}

static __inline__ uint8_t  df_atomic_dec(volatile uint8_t * pv) {
    register unsigned char __res;
    __asm__ __volatile__ (
        "movb $0xff,%0;"
        LOCK_PREFIX "xaddb %0,(%1);"
        "decb %0"
        : "=a" (__res), "=q" (pv): "1" (pv));
    return __res;
}

//return: the initial value of *pv
/// 原子地做 8位,16位,32位,64位的加法的操作
/// 该操作虽然参数和返回值都是无符号型整数,但是一样可以
/// 对有符号型整数做操作,只需要做适当的参数转换即可
/// @param pv 指向操作数的指针
/// @return 操作数加法之前的数值
#ifdef __x86_64__
static __inline__ uint64_t df_atomic_add(volatile uint64_t * pv, const uint64_t av) {
    //:"=a" (__res), "=q" (pv): "m"(av), "1" (pv));
    register unsigned long __res;
    __asm__ __volatile__ (
        "movq %2,%0;"
        LOCK_PREFIX "xaddq %0,(%1);"
        :"=a" (__res), "=q" (pv): "mr"(av), "1" (pv));
    return __res;
}
#endif
static __inline__ uint32_t df_atomic_add(volatile uint32_t * pv, const uint32_t av) {
    //:"=a" (__res), "=q" (pv): "m"(av), "1" (pv));
    register unsigned int __res;
    __asm__ __volatile__ (
        "movl %2,%0;"
        LOCK_PREFIX "xaddl %0,(%1);"
        :"=a" (__res), "=q" (pv): "mr"(av), "1" (pv));
    return __res;
}

static __inline__ uint16_t df_atomic_add(volatile uint16_t * pv, const uint16_t av) {
    //:"=a" (__res), "=q" (pv): "m"(av), "1" (pv));
    register unsigned short __res;
    __asm__ __volatile__ (
        "movw %2,%0;"
        LOCK_PREFIX "xaddw %0,(%1);"
        :"=a" (__res), "=q" (pv): "mr"(av), "1" (pv));
    return __res;
}

static __inline__ uint8_t  df_atomic_add(volatile uint8_t * pv, const uint8_t av) {
    //:"=a" (__res), "=q" (pv): "m"(av), "1" (pv));
    register unsigned char __res;
    __asm__ __volatile__ (
        "movb %2,%0;"
        LOCK_PREFIX "xaddb %0,(%1);"
        :"=a" (__res), "=q" (pv): "mr"(av), "1" (pv));
    return __res;
}

//function: set *pv to nv
//return: the initial value of *pv
/// 原子地把nv赋值给pv指向的整数,支持8位,16位,32位,84位操作
/// @param pv 待赋值的整数(目的操作数)
/// @param nv 向pv赋的整数
/// @return pv指向的赋值前的数值
#ifdef __x86_64__
static __inline__ uint64_t df_atomic_exchange(volatile uint64_t * pv, const uint64_t nv) {
    register unsigned long __res;
    __asm__ __volatile__ (
        "1:"
        LOCK_PREFIX "cmpxchgq %3,(%1);"                \
        "jne 1b":
        "=a" (__res), "=q" (pv): "1" (pv), "q" (nv), "0" (*pv));
    return __res;
}
#endif
static __inline__ uint32_t df_atomic_exchange(volatile uint32_t * pv, const uint32_t nv) {
    register unsigned int __res;
    __asm__ __volatile__ (
        "1:"
        LOCK_PREFIX "cmpxchgl %3,(%1);"                \
        "jne 1b":
        "=a" (__res), "=q" (pv): "1" (pv), "q" (nv), "0" (*pv));
    return __res;
}

static __inline__ uint16_t df_atomic_exchange(volatile uint16_t * pv, const uint16_t nv) {
    register unsigned short __res;
    __asm__ __volatile__ (
        "1:"
        LOCK_PREFIX "cmpxchgw %3,(%1);"                \
        "jne 1b":
        "=a" (__res), "=q" (pv): "1" (pv), "q" (nv), "0" (*pv));
    return __res;
}

static __inline__ uint8_t  df_atomic_exchange(volatile uint8_t * pv, const uint8_t nv) {
    register unsigned char __res;
    __asm__ __volatile__ (
        "1:"
        LOCK_PREFIX "cmpxchgb %3,(%1);"                \
        "jne 1b":
        "=a" (__res), "=q" (pv): "1" (pv), "q" (nv), "0" (*pv));
    return __res;
}

//function: compare *pv to cv, if equal, set *pv to nv, otherwise do nothing.
//return: the initial value of *pv
/// 比较pv和cv,如果两者相等,则返回pv原有数值并且把nv赋值给pv
/// 否则什么也不作,返回pv原有数值
/// @param pv 待赋值的整数(目的操作数)
/// @param nv 向pv赋的整数
/// @param cv 和pv比较的整数
/// @return pv指向的操作前的数值
#ifdef __x86_64__
static __inline__ uint64_t df_atomic_compare_exchange(volatile uint64_t * pv,
        const uint64_t nv, const uint64_t cv) {
    register unsigned long __res;
    __asm__ __volatile__ (
        LOCK_PREFIX "cmpxchgq %3,(%1)"
        : "=a" (__res), "=q" (pv) : "1" (pv), "q" (nv), "0" (cv));
    return __res;
}
#endif
static __inline__ uint32_t df_atomic_compare_exchange(volatile uint32_t * pv,
        const uint32_t nv, const uint32_t cv) {
    register unsigned int __res;
    __asm__ __volatile__ (
        LOCK_PREFIX "cmpxchgl %3,(%1)"
        : "=a" (__res), "=q" (pv) : "1" (pv), "q" (nv), "0" (cv));
    return __res;
}
static __inline__ uint16_t df_atomic_compare_exchange(volatile uint16_t * pv,
        const uint16_t nv, const uint16_t cv) {
    register unsigned short __res;
    __asm__ __volatile__ (
        LOCK_PREFIX "cmpxchgw %3,(%1)"
        : "=a" (__res), "=q" (pv) : "1" (pv), "q" (nv), "0" (cv));
    return __res;
}
static __inline__ uint8_t df_atomic_compare_exchange(volatile uint8_t * pv,
        const uint8_t nv, const uint8_t cv) {
    register unsigned char  __res;
    __asm__ __volatile__ (
        LOCK_PREFIX "cmpxchgb %3,(%1)"
        : "=a" (__res), "=q" (pv) : "1" (pv), "q" (nv), "0" (cv));
    return __res;
}

typedef void * pvoid;

//function: set *pv to nv
//return: the initial value of *pv
/// 把nv原子地赋值给*pv
static __inline__ pvoid df_atomic_exchange_pointer(volatile pvoid * pv, const pvoid nv) {
#ifdef __x86_64__
    return (pvoid) df_atomic_exchange((uint64_t *) pv, (uint64_t) nv);
#else
    return (pvoid) df_atomic_exchange((uint32_t *) pv, (uint32_t) nv);
#endif
}
//function: compare *pv to cv, if equal, set *pv to nv, otherwise do nothing.
//return: the initial value of *pv
/// 比较cv和*pv,如果两者相等则把nv赋值给*pv,并且返回*pv原有数值
/// 否则返回*pv原有数值,不做赋值操作
static __inline__ pvoid df_atomic_compare_exchange_pointer(volatile pvoid * pv,
        const pvoid nv, const pvoid cv) {
#ifdef __x86_64__
    return (pvoid) df_atomic_compare_exchange((uint64_t *) pv, (uint64_t) nv, (uint64_t)cv);
#else
    return (pvoid) df_atomic_compare_exchange((uint32_t *) pv, (uint32_t) nv, (uint32_t)cv);
#endif
}

#undef LOCK_PREFIX

#else // #if defined(__i386__) || defined(__x86_64__)

#error "must compiled on i386 or x86_64"

#endif //

#endif //__DF_DF_ATOMIC_INCLUDE__