运维最新linux同步之原子操作API的使用_原子服务api(1),2024年最新Linux运维应用性能优化

最全的Linux教程,Linux从入门到精通

======================

  1. linux从入门到精通(第2版)

  2. Linux系统移植

  3. Linux驱动开发入门与实战

  4. LINUX 系统移植 第2版

  5. Linux开源网络全栈详解 从DPDK到OpenFlow

华为18级工程师呕心沥血撰写3000页Linux学习笔记教程

第一份《Linux从入门到精通》466页

====================

内容简介

====

本书是获得了很多读者好评的Linux经典畅销书**《Linux从入门到精通》的第2版**。本书第1版出版后曾经多次印刷,并被51CTO读书频道评为“最受读者喜爱的原创IT技术图书奖”。本书第﹖版以最新的Ubuntu 12.04为版本,循序渐进地向读者介绍了Linux 的基础应用、系统管理、网络应用、娱乐和办公、程序开发、服务器配置、系统安全等。本书附带1张光盘,内容为本书配套多媒体教学视频。另外,本书还为读者提供了大量的Linux学习资料和Ubuntu安装镜像文件,供读者免费下载。

华为18级工程师呕心沥血撰写3000页Linux学习笔记教程

本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。

需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

atomic\_t value = 0 ;
(int)value;  //失败,不允许这样强制转换

// include/linux/types.h

typedef struct {
	int counter;
} atomic\_t;

#ifdef CONFIG\_64BIT
typedef struct {
	long counter;
} atomic64\_t;
#endif

atomic_t是不透明类型数据类型,隐藏了其内部格式或者结构。为了确保这些数据只在特殊的有关原子操作的函数中才会使用。不要将该类型转化为其对应的C标准类型,并且不要假设该类型的长度。

2.2 atomic 初始化

// arch/x86/include/asm/atomic.h 
// 原子操作一系列函数的实现与体系架构有关 ,很多通过内联汇编指令实现
#define ATOMIC\_INIT(i) { (i) }

//定义value为原子变量,并初始化为0
atomic\_t value = ATOMIC\_INIT(0);

2.3 atomic read and set

/\*\*
 \* atomic\_read - read atomic variable
 \* @v: pointer of type atomic\_t
 \*
 \* Atomically reads the value of @v.
 \*/
static inline int atomic\_read(const atomic\_t \*v)
{
	return (\*(volatile int \*)&(v)->counter);
}

/\*\*
 \* atomic\_set - set atomic variable
 \* @v: pointer of type atomic\_t
 \* @i: required value
 \*
 \* Atomically sets the value of @v to @i.
 \*/
static inline void atomic\_set(atomic\_t \*v, int i)
{
	v->counter = i;
}

比如:

atomic\_read(&v) //原子的读取整数变量v
atomic\_set(&v, 1) //原子地设置v为1

备注:对于读原子变量,读取地时候加了volatile修饰,保证每次都是去从变量的内存地址中读取原子变量,而不是从缓存中读取变量,保证读操作不会被编译器优化,从而每次读取到正确的值。
volatile不能保证原子操作(不具有保护临界资源的作用),只是避免编译器优化而已。

2.4 atomic add and subtract

/\*\*
 \* atomic\_add - add integer to atomic variable
 \* @i: integer value to add
 \* @v: pointer of type atomic\_t
 \*
 \* Atomically adds @i to @v.
 \*/
static inline void atomic\_add(int i, atomic\_t \*v)
{
	asm volatile(LOCK_PREFIX "addl %1,%0"
		     : "+m" (v->counter)
		     : "ir" (i));
}

/\*\*
 \* atomic\_sub - subtract integer from atomic variable
 \* @i: integer value to subtract
 \* @v: pointer of type atomic\_t
 \*
 \* Atomically subtracts @i from @v.
 \*/
static inline void atomic\_sub(int i, atomic\_t \*v)
{
	asm volatile(LOCK_PREFIX "subl %1,%0"
		     : "+m" (v->counter)
		     : "ir" (i));
}

比如:

atomic\_add(2, &v); //原子地给v加2
atomic\_sub(3, &v); //原子地给v减3

2.5 atomic_sub_and_test

/\*\*
 \* atomic\_sub\_and\_test - subtract value from variable and test result
 \* @i: integer value to subtract
 \* @v: pointer of type atomic\_t
 \*
 \* Atomically subtracts @i from @v and returns
 \* true if the result is zero, or false for all
 \* other cases.
 \*/
static inline int atomic\_sub\_and\_test(int i, atomic\_t \*v)
{
	unsigned char c;

	asm volatile(LOCK_PREFIX "subl %2,%0; sete %1"
		     : "+m" (v->counter), "=qm" (c)
		     : "ir" (i) : "memory");
	return c;
}

比如:
将原子变量减2,如果等于0,返回真;否则返回假。

// include/linux/stddef.h
enum {
	false	= 0,
	true	= 1
};

//等价于:
if(atomic\_sub\_and\_test(2, &v))
	return true;   //-->v == 2
else
	return false;  // -->v != 2

2.6 atomic increment and decrement

/\*\*
 \* atomic\_inc - increment atomic variable
 \* @v: pointer of type atomic\_t
 \*
 \* Atomically increments @v by 1.
 \*/
static inline void atomic\_inc(atomic\_t \*v)
{
	asm volatile(LOCK_PREFIX "incl %0"
		     : "+m" (v->counter));
}

/\*\*
 \* atomic\_dec - decrement atomic variable
 \* @v: pointer of type atomic\_t
 \*
 \* Atomically decrements @v by 1.
 \*/
static inline void atomic\_dec(atomic\_t \*v)
{
	asm volatile(LOCK_PREFIX "decl %0"
		     : "+m" (v->counter));
}


比如:

atomic\_inc(&v)  //原子地给v加1
atomic\_dec(&v)  //原子地给v减1

2.7 atomic inc or dec and test

/\*\*
 \* atomic\_dec\_and\_test - decrement and test
 \* @v: pointer of type atomic\_t
 \*
 \* Atomically decrements @v by 1 and
 \* returns true if the result is 0, or false for all other
 \* cases.
 \*/
static inline int atomic\_dec\_and\_test(atomic\_t \*v)
{
	unsigned char c;

	asm volatile(LOCK_PREFIX "decl %0; sete %1"
		     : "+m" (v->counter), "=qm" (c)
		     : : "memory");
	return c != 0;
}

/\*\*
 \* atomic\_inc\_and\_test - increment and test
 \* @v: pointer of type atomic\_t
 \*
 \* Atomically increments @v by 1
 \* and returns true if the result is zero, or false for all
 \* other cases.
 \*/
static inline int atomic\_inc\_and\_test(atomic\_t \*v)
{
	unsigned char c;

	asm volatile(LOCK_PREFIX "incl %0; sete %1"
		     : "+m" (v->counter), "=qm" (c)
		     : : "memory");
	return c != 0;
}


//等价于:
if(atomic\_dec\_and\_test(&v))
	return true;   //-->v == 1
else
	return false;  // -->v != 1
	
if( atomic\_inc\_and\_test(&v))
	return true;   //-->v == -1
else
	return false;  // -->v != -1

2.8 atomic add or sub and return

/\*\*
 \* atomic\_add\_return - add integer and return
 \* @i: integer value to add
 \* @v: pointer of type atomic\_t
 \*
 \* Atomically adds @i to @v and returns @i + @v
 \*/
static inline int atomic\_add\_return(int i, atomic\_t \*v)
{
	return i + xadd(&v->counter, i);
}

/\*\*
 \* atomic\_sub\_return - subtract integer and return
 \* @v: pointer of type atomic\_t
 \* @i: integer value to subtract
 \*
 \* Atomically subtracts @i from @v and returns @v - @i
 \*/
static inline int atomic\_sub\_return(int i, atomic\_t \*v)
{
	return atomic\_add\_return(-i, v);
}

#define atomic\_inc\_return(v) (atomic\_add\_return(1, v))


![](https://img-blog.csdnimg.cn/img_convert/9a8cb5f8c0ec69e6499adead0da6e95b.png)


最全的Linux教程,Linux从入门到精通

======================

1.  **linux从入门到精通(第2版)**

2.  **Linux系统移植**

3.  **Linux驱动开发入门与实战**

4.  **LINUX 系统移植 第2版**

5.  **Linux开源网络全栈详解 从DPDK到OpenFlow**



![华为18级工程师呕心沥血撰写3000页Linux学习笔记教程](https://img-blog.csdnimg.cn/img_convert/59742364bb1338737fe2d315a9e2ec54.png)



第一份《Linux从入门到精通》466页

====================

内容简介

====

本书是获得了很多读者好评的Linux经典畅销书**《Linux从入门到精通》的第2版**。本书第1版出版后曾经多次印刷,并被51CTO读书频道评为“最受读者喜爱的原创IT技术图书奖”。本书第﹖版以最新的Ubuntu 12.04为版本,循序渐进地向读者介绍了Linux 的基础应用、系统管理、网络应用、娱乐和办公、程序开发、服务器配置、系统安全等。本书附带1张光盘,内容为本书配套多媒体教学视频。另外,本书还为读者提供了大量的Linux学习资料和Ubuntu安装镜像文件,供读者免费下载。



![华为18级工程师呕心沥血撰写3000页Linux学习笔记教程](https://img-blog.csdnimg.cn/img_convert/9d4aefb6a92edea27b825e59aa1f2c54.png)



**本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。**

> 需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 23
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值