当前位置:网站首页>I understand atomic operations under linux
I understand atomic operations under linux
2022-08-06 03:10:00 【Croxd】
What is an atomic operation:
Atomic operation is either not executed, once executed, it will be executed and completed. It is an uninterrupted one, or a series of actions, that is, it will not be interrupted by other events before completing the task, just like atoms cannot be interrupted.Divide into particles.In uniprocessing, an instruction that can be completed with a single instruction can be considered an atomic operation.Atomic operations in software depend on the support of hardware atomic operations.Of course, atomic operations can also be used as reference counting.
In fact, atomic operations and locks achieve the same function in essence, both to protect shared objects, which are atomic and sequential.Atomicity ensures that instructions are not interrupted during execution, either all or not at all.Sequentiality ensures that even if two or more instructions appear in separate threads of execution, or even on separate processors, the order in which they should be executed is maintained.
Atomic operations will be interrupted, but will not affect the result:
In the process of performing an atomic operation, it may be interrupted by other execution flows, such as interruption, process switching, but this does not affect the result.If process 1 is interrupted after ldrex, process 2 will clear Exclusive after executing strex.After process 2 is executed, go to process 1. When process 1 executes strex, because Exclusive is cleared, the content of strex will not be executed, directly set temp to 1, compare temp is not 0, jump to 1 to continueExecute, so that the results of the operation are guaranteed.
1:ldrex [result], [v->counter] //Give the value of counter to result and set the global flag "Exclusive"add [result], [result], [i] //result+1 and pass the value to resultstrex [temp], [result], [v->counter] //Give the value of result to counter, if successful temp=0, clear "Exclusive"teq [temp], #0 //compare temp with 0bne 1b //temp is not equal to 0, then jump to the first 1 to continue the operation
In linux, the kernel provides two sets of atomic operation interfaces: one is to operate on integers; the other is to operate on individual bits.
Atomic operations are defined for integer types:
- atomic_t v = ATOMIC_INIT(0); //Define atomic variable v and initialize it to 0
- atomic_read(atomic_t *v); //Returns the value of the atomic variable
- atomic_set(atomic_t *v); //Set the value of the atomic variable
- atomic_add(int i, atomic_t *v) //Add i to an atomic variable
- atomic_add_return(int i, atomic_t *v) //Add i to an atomic variable and return the latest value of variable v
- atomic_sub(int i, atomic_t *v) //Subtract i from an atomic variable
- atomic_sub_return(int i, atomic_t *v) //Subtract i from an atomic variable and return the latest value of variable v
- void atomic_inc(atomic_t *v); //Increase the atomic variable by 1
- void atomic_dec(atomic_t *v); //Atomic variable is reduced by 1
- int atomic_dec_and_test(atomic_t *v); //Test whether it is 0 after the decrement operation, if it is 0, return true, otherwise return false.
Atomic operations are defined for bit types:
- void set_bit(nr, void *addr); //Set the bit, set the nrth bit of the addr address, the so-called set bit is to write the bit as 1
- void clear_bit(nr, void *addr); //Clear the bit, clear the nrth bit of the addr address, the so-called clear bit is to write the bit to 0
- void change_bit(nr, void *addr);//Change the bit, invert the nrth bit of the addr address.
- test_bit(nr, void *addr);//Test bit, the above operation returns the nrth bit of the addr address.
- int test_and_set_bit(nr, void *addr);//Execute test_bit, then execute set_bit
- int test_and_clear_bit(nr, void *addr);//Execute test_bit, then execute clear_bit
- int test_and_change_bit(nr, void *addr);//Execute test_bit, then execute change_bit
边栏推荐
猜你喜欢
随机推荐
我所理解liunx下的原子操作
全同态加密知识体系整理
C 学生管理系统 删除指定学生节点(一般情况)
MONAI_Label 安装试用
用低代码如何搭建一套进销存管理系统(采购、销售、库存一体化管理)?
LeetCode Daily 2 Questions 02: Number of Palindromes (1200 each)
谷歌账户暂停三个月重新启用,谷歌账户暂停三个月重新启用,转化目标是否有效?
C Student management system Delete the specified student node (special case)
什么是固体充气轮胎(solid pneumatic tyres)?
ALV details are reorganized2022.8.5
A question about pm2 automation deployment project
查找链表第N个节点
Removal control of WPF screenshot control (9) "Imitation WeChat"
greenDAO of Android database framework
LeetCode Daily 2 Questions 01: Flip word prefixes (both 1200 questions)
After 6 years of testing, from the monthly salary of 6k to the current annual salary of 40w, I have summarized these experiences...
深度学习Course2第二周Hyperparameter tuning, Batch Normalization, Programming Frameworks习题整理
2022/08/05 学习笔记 (day24)集合
谷歌分析中的转化目标设置后,大概多久能显示在Google adwords后台?
相机标定 >> 坐标系转换@内参、外参









