在多线程环境中,对共享的变量的访问,可以使用基于Compare And Swap这种lock free的技术进行实现,这种实现的好处是效率高。下面是代码片段来自Android的system/core/libcutils /atomic.c(针对X86):
#elif defined(__i386__) || defined(__x86_64__)

void android_atomic_write(int32_t value, volatile int32_t* addr) {
int32_t oldValue;
do {
oldValue = *addr;
} while (android_atomic_cmpxchg(oldValue, value, addr));
}

int32_t android_atomic_inc(volatile int32_t* addr) {
int32_t oldValue;
do {
oldValue = *addr;
} while (android_atomic_cmpxchg(oldValue, oldValue+1, addr));
return oldValue;
}

int32_t android_atomic_dec(volatile int32_t* addr) {
int32_t oldValue;
do {
oldValue = *addr;
} while (android_atomic_cmpxchg(oldValue, oldValue-1, addr));
return oldValue;
}

int32_t android_atomic_add(int32_t value, volatile int32_t* addr) {
int32_t oldValue;
do {
oldValue = *addr;
} while (android_atomic_cmpxchg(oldValue, oldValue+value, addr));
return oldValue;
}

int android_atomic_cmpxchg(int32_t oldvalue, int32_t newvalue, volatile int32_t* addr) {
int xchg;
asm volatile
(
" lock; cmpxchg %%ecx, (%%edx);"
" setne %%al;"
" andl $1, %%eax"
: "=a" (xchg)
: "a" (oldvalue), "c" (newvalue), "d" (addr)
);
return xchg;
}

android_atomic_cmpxchg是使用GNU C嵌入汇编实现,使用X86提供的对CAS的原子支持的指令。
oldvalue放在eax寄存器中,newvalue放在ecx中,addr(pointer)放在edx中。cmpxchg指令首先比较addr指向的内存与oldvalue(eax),如果二者相等,将newvalue(ecx)放到addr所指向的内存中,同时设置Z标志1。setne与andl 指令的操作的结果很简单:如果Z标志被设置,则eax为0,否则为1。程序执行最终eax放到xchg变量里。下面对cmpxchg命令的解释。
[lock] (注:支持smp) cmpxchg reg(注:source operand), reg/mem(注:destination operand)
If (accumulator(eax) == destination)
{ ZF <-1; destination <- source; }
If (accumulator (eax)!= destination)

{ ZF <-0; accumulator <- destination; }


更多相关文章

  1. ubuntu genymotion 启动adb 被占用5037端口
  2. Android之Gradle基础
  3. 无线调试指令
  4. 关于android使用Xutil保存cookie
  5. 设置ubuntu Android(安卓)sdk JDK环境变量
  6. 使用 gradle 在编译时动态设置 Android(安卓)resValue / BuildCo
  7. android使用jenkins进行自动化打包并且上传到fir或者蒲公英
  8. Android修改XML文件
  9. 配置Android交叉编译工具链环境变量

随机推荐

  1. EventBus的使用详解
  2. android Studio与数据库SQLserver连接实
  3. 实现android轮播图的开源组件--Android-C
  4. Android(安卓)反编译:加固前后对比
  5. android EditText 下划线样式
  6. Android(安卓)9.0 的 recent 键/事件的拦
  7. FileProvider的使用
  8. cocos2dx3.0打包注意事项
  9. 内存泄漏—Android(安卓)Studio 3.0 Prof
  10. Android-2D绘图基础-更新中