Atomic operations on the x86 processors
On the Intel type of x86 processors including AMD, increasingly there are more CPU cores or processors running in parallel.
In the old days when there was a single processor, the operation:
++i;
Would be thread safe because it was one machine instruction on a single processor. These days laptops have numerous CPU cores so that even single instruction operations aren't safe. What do you do? Do you need to wrap all operations in a mutex or semaphore? Well, maybe you don't need too.
Fortunately, the x86 has an instruction prefix that allows a few memory referencing instruction to execute on specific memory locations exclusively.
There are a few basic structures that can use this:
(for the GNU Compiler)
void atom_inc(volatile int *num)
{
__asm__ __volatile__ ( "lock incl %0" : "=m" (*num));
}
void atom_dec(volatile int *num)
{
__asm__ __volatile__ ( "lock decl %0" : "=m" (*num));
}
int atom_xchg(volatile int *m, int inval)
{
register int val = inval;
__asm__ __volatile__ ( "lock xchg %1,%0" : "=m" (*m), "=r" (val) : "1" (inval));
return val;
}
void atom_add(volatile int *m, int inval)
{
register int val = inval;
__asm__ __volatile__ ( "lock add %1,%0" : "=m" (*m), "=r" (val) : "1" (inval));
}
void atom_sub(volatile int *m, int inval)
{
register int val = inval;
__asm__ __volatile__ ( "lock sub %1,%0" : "=m" (*m), "=r" (val) : "1" (inval));
}
For the Microsoft Compiler:
void atom_inc(volatile int *num)
{
_asm
{
mov esi, num
lock inc DWORD PTR [esi]
};
}
void atom_dec(volatile int *num)
{
_asm
{ mov esi, num
lock dec DWORD PTR [esi]
};
}
int atom_xchg(volatile int *m, int inval)
{
_asm
{
mov eax, inval
mov esi, m
lock xchg eax, DWORD PTR [esi]
mov inval, eax
}
return inval;
}
void atom_add(volatile int *num, int val)
{
_asm
{ mov esi, num
mov eax, val
lock add DWORD PTR [esi], eax
};
}
void atom_sub(volatile int *num, int val)
{
_asm
{ mov esi, num
mov eax, val
lock sub DWORD PTR [esi], eax
};
}
The lock prefix is not universally applied. It only works if all accesses to the locations also use lock. So, even though you use "lock" in one section of code, another section of code that just sets the value will not be locked out. Think of it as just a mutex.
Basic usage:
class poll
{
int m_pollCount;
....
....
void pollAdd()
{
atom_inc(&m_pollCount);
}
};
The above example increments a poll object count by one.
SRC=http://www.mohawksoft.org/?q=node/78
Atomic operations on the x86 processors的更多相关文章
- Voting and Shuffling to Optimize Atomic Operations
2iSome years ago I started work on my first CUDA implementation of the Multiparticle Collision Dynam ...
- 什么是Java中的原子操作( atomic operations)
1.啥是java的原子性 原子性:即一个操作或者多个操作 要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行. 一个很经典的例子就是银行账户转账问题: 比如从账户A向账户B转1000元,那么 ...
- 【转】ARM vs X86 – Key differences explained!
原文:http://www.androidauthority.com/arm-vs-x86-key-differences-explained-568718/ Android supports 3 d ...
- 原子操作(atomic operation)
深入分析Volatile的实现原理 引言 在多线程并发编程中synchronized和Volatile都扮演着重要的角色,Volatile是轻量级的synchronized,它在多处理器开发中保证了共 ...
- A multiprocessing system including an apparatus for optimizing spin-lock operations
A multiprocessing system having a plurality of processing nodes interconnected by an interconnect ne ...
- Adaptively handling remote atomic execution based upon contention prediction
In one embodiment, a method includes receiving an instruction for decoding in a processor core and d ...
- Method and apparatus for an atomic operation in a parallel computing environment
A method and apparatus for a atomic operation is described. A method comprises receiving a first pro ...
- Moving x86 assembly to 64-bit (x86-64)
While 64-bit x86 processors have now been on the market for more than 5 years, software support is o ...
- A trip through the Graphics Pipeline 2011_13 Compute Shaders, UAV, atomic, structured buffer
Welcome back to what’s going to be the last “official” part of this series – I’ll do more GPU-relate ...
随机推荐
- n阶导函数存在与n阶可导的区别
1.f(x)n阶导函数存在 <=======> f(n)(x)存在 指的是在某个区间内有定义 2.f(x)n阶可导根据题意可以有两种不同的解释: ①.题目中说的是在某点即在x=x0处n ...
- public static float CompareExchange(ref float location1,float value,float comparand)
https://msdn.microsoft.com/en-us/library/k9hz8w9t(v=vs.110).aspx Compares two single-precision float ...
- Android+Jquery Mobile学习系列(8)-保单/生日提醒功能
其实这个App基本功能早已做完,并且交给老婆试用去了.但由于最近项目要保证稳定,所以持续加班,没有时间写最后一点内容,本节也就简单截图做个说明,不详细叙述实现方式.我会把代码上传到最后一章中,有兴趣的 ...
- 电脑升级win10后visio的问题
上周由于电脑意外蓝屏,系统从win7升级到了win10,昨天工作写文档时才发现缺少画图的工具,于是按照了visio2013,在编辑设计图时发现,一旦用visio打开或编辑图后再到word里设计图的内容 ...
- Docker 探索安装WordPress+Mysql8.0
拉取MYSQL,注意默认是8.0版本,连接加密方式有变化 docker pull mysql 运行MYSQL docker run --name wordpress-mysql -p 3306:330 ...
- 关于sklearn中的导包交叉验证问题
机器学习sklearn中的检查验证模块: 原版本导包: from sklearn.cross_validation import cross_val_score 导包报错: 模块继承在cross_va ...
- php 图片生成器
一.需求 最近公司由于有大量的海报要做,而且海报的布局规模都是一样的,只是内容不同,所以老板想我开发一个图片的生成器.可以根据你输入的内容生成海报图片. 具体有需求有以下的需求 1.可以根据将每条数据 ...
- python 实现线程之间的通信
前言:因为GIL的限制,python的线程是无法真正意义上并行的.相对于异步编程,其性能可以说不是一个等量级的.为什么我们还要学习多线程编程呢,虽然说异步编程好处多,但编程也较为复杂,逻辑不容易理解, ...
- HDU 1166 线段树模板&树状数组模板
HDU1166 上好的线段树模板&&树状数组模板 自己写的第一棵线段树&第一棵树状数组 莫名的兴奋 线段树: #include <cstdio> using nam ...
- C#方法的练习
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo ...