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 ...
随机推荐
- Android+Jquery Mobile学习系列(2)-HTML5/Jquery Mobile基础
本章介绍两个关键字[HTML5]和[Jquery Mobile],简单说这两者的关系是:HTML5作为主体,Jquery Mobile在HTML5的基础上对其进行了优化.装饰. HTML5 HTML5 ...
- ASP.NET通用权限系统快速开发框架
系统在线演示地址: http://120.90.2.126:8051 登录账户:system,密码:system### DEMO下载地址: http://download.csdn.net/detai ...
- P4135 作诗——分块
题目:https://www.luogu.org/problemnew/show/P4135 分块大法: 块之间记录答案,每一块记录次数前缀和: 注意每次把桶中需要用到位置赋值就好了: 为什么加了特判 ...
- CodeForces A. Meeting of Old Friends
2019-05-30 20:19:57 加油!!! sort(a + 1, a + 5); 卡了一会儿 #include <bits/stdc++.h> using namespace s ...
- 用xftp从win7系统传输一些必要的文件到Linux
新建会话,主机名为Linux系统的ip地址,选用SFTP协议,选用UTF-8编码格式 1.安装JDK 切换到java路径下 卸载openJDK: 用rpm -qa |grep java指令查看 用rp ...
- java 关键字与保留字
Java 关键字列表 (依字母排序 共51组),所有的关键字都是小写,在MyEclipse中都会显示不同的颜色: abstract, assert,boolean, break, byte, case ...
- B - Cows and Poker Game
Problem description There are n cows playing poker at a table. For the current betting phase, each p ...
- ViewPager PagerAdapter 的使用
1: 目的,实现全屏滑动的效果 2:类似于BaseAdapter public class MyPagerAdapter extends PagerAdapter { private Context ...
- [PHP][学习笔记][CURL]监测设备运行情况小demo
1.curl获取的web content 不能直接echo到页面,会造成js各种错误 2.想办法处理字符串的截取.拼接 2.1.裁剪html返回的字符串 function cutStringFrom( ...
- php数据库分页
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...