Atomic operations under Linux

Source: Internet
Author: User

What operations does Linux support have atomic characteristics? Knowing these things is the basis for understanding and designing a non-locking programming algorithm.

__sync_fetch_and_add series of commands, found this series of commands to say the best of an article, English good students can go directly to the original text. Multithreaded simple data type access and atomic variables
__sync_fetch_and_add Series A total of 12 functions, with plus/minus/and/or/XOR//and/or the atomic operation function of the function, __sync_fetch_and_add, as the name implies, fetch first, and then add, return is the previous value of the self-added. Take count = 4 As an example, call __sync_fetch_and_add (&count,1), after which the return value is 4, and then count becomes 5.
There are __sync_fetch_and_add, nature will have __sync_add_and_fetch, hehe this meaning is very clear, the first self-added, in return. The relationship between their two brothers is the same as that of i++ and ++i. By Tanhao strong his old people have received protection fee will be clear.
With this baby function, we have a new solution. For multi-threaded global variables to self-add, we will never have to thread lock. The following line of code is the same as the line of code protected by Pthread_mutex, and is thread-safe.
__sync_fetch_and_add (&global_int, 1);
The following is a family photo of this group of functions, you can see the name to know what these functions are doing.
When compiling with GCC, add the option-march=i686
Sam: On my server, I found no add-on.

Type __sync_fetch_and_add (type *ptr, type value);
Type __sync_fetch_and_sub (type *ptr, type value);
Type __sync_fetch_and_or (type *ptr, type value);
Type __sync_fetch_and_and (type *ptr, type value);
Type __sync_fetch_and_xor (type *ptr, type value);
Type __sync_fetch_and_nand (type *ptr, type value);
Type __sync_add_and_fetch (type *ptr, type value);
Type __sync_sub_and_fetch (type *ptr, type value);
Type __sync_or_and_fetch (type *ptr, type value);
Type __sync_and_and_fetch (type *ptr, type value);
Type __sync_xor_and_fetch (type *ptr, type value);
Type __sync_nand_and_fetch (type *ptr, type value);

//SAM: I wonder why we have to write the ellipsis behind, is there a parameter that doesn't need our attention? Do not need to pass parameters when using? The following two functions are what elder brother wants, can easily realize the function of mutual exclusion lock.
BOOL __sync_bool_compare_and_swap (type*ptr, type oldval, type newval, ...)
Type __sync_val_compare_and_swap (type *ptr, type Oldval, type newval, ...)
These functions provide an atomic comparison and exchange, and if *ptr = = Oldval, newval is written to *ptr, and
The first function returns true if it is equal and written.
The value of the second function before the return operation.

__sync_synchronize (...)


There are two other functions:
Type __sync_lock_test_and_set (type *ptr, type value, ...)
Set *ptr to value and return the values before the *ptr operation.

void __sync_lock_release (Type *ptr, ...)
Place the *ptr 0

__sync_synchronize (...)


Issue a full barrier.

About the memory barrier,cpu will sort our instructions, generally improving the efficiency of the program, but sometimes it may lead to the results we do not want to get, for example, we have a hardware device, it has 4 registers, when you send an operation command, A register is stored in your operation instructions (such as read), two registers are parameters (such as the address and size), the last register is the control register, after all the parameters are set to send instructions to it, the device begins to read the parameters, execute the command, the program may be as follows:

Write1 (dev.register_size,size);
Write1 (DEV.REGISTER_ADDR,ADDR);
Write1 (Dev.register_cmd,read);
Write1 (Dev.register_control,go);


If the last write1 is replaced by the first few statements, then it is definitely not what we expect, so we can add a memory barrier before the last statement, forcing the CPU to execute the previous write before executing the last one:


Write1 (dev.register_size,size);
Write1 (DEV.REGISTER_ADDR,ADDR);
Write1 (Dev.register_cmd,read);
__sync_synchronize ();
Write1 (Dev.register_control,go);


There are several types of memory barrier:
Acquire barrier: It is not allowed to move memory read instructions after barrier to barrier (kernel () in Linux WMB).
Release Barrier: The memory read instruction before barrier is not allowed to be moved after barrier ($ () in Linux kernel).
Full Barrier: The collection of the two barrier (MB () in Linux kernel).


There are two other functions:

Type __sync_lock_test_and_set (type *ptr, type value, ...)
Set *ptr to value and return the values before the *ptr operation.

void __sync_lock_release (Type *ptr, ...)
Place the *ptr 0


Sample program:


#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

static int count = 0;


void *test_func (void *arg)
{
int i=0;
for (I=0;i<20000;++i) {
__sync_fetch_and_add (&count,1);
}
return NULL;
}

int main (int argc, const char *argv[])
{
pthread_t ID[20];
int i = 0;

for (I=0;i<20;++i) {
Pthread_create (&id[i],null,test_func,null);
}

for (I=0;i<20;++i) {
Pthread_join (Id[i],null);
}

printf ("%d\n", count);
return 0;
}

Refer to the original article:

1, http://blog.csdn.net/hzhsan/article/details/25124901

2, http://www.linuxidc.com/Linux/2011-06/37403.htm

Atomic operations under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.