Android atomic operations (x86)

Source: Internet
Author: User

 

  • In android, cmpxchg performs atomic operations, such as auto-addition and auto-subtraction.
  • Cmpxchg exists in 486 and more advanced CPUs. Other types of CPUs, such as Arm, do not have this command.

Cmpxchg commands are encapsulated in Functionsandroid_atomic_cmpxchgThe function is located in atomic. c and is preprocessed.

#elif defined(__i386__) || defined(__x86_64__)

Protected.

1. source code analysis:


 

The reference link of the annotation is:

  • Gnu asm template syntax
  • Lock
  • Cmpxchg instruction
  • Setne

Therefore, the function is used:If oldvalue = * addr, The newvalue is assigned to * addr, and 1 is returned. Otherwise, 0 is returned..

(1 indicates that the new value has been assigned to the variable/register, and 0 indicates that no value is assigned)

Example 2: 2.1 oldval = newval
int32_t a = 5;int b = android_atomic_cmpxchg( a, a+1, &a );printf("%d, %d\n", a, b);

The result is:

6, 1
2.2 oldval! = Newval
int32_t a = 5;int b= android_atomic_cmpxchg( a, a, &a );printf("%d, %d\n", a, b);

The result is:

5, 0
3 Reference

Other functions suchandroid_atomic_inc,android_atomic_decAnd so on.android_atomic_cmpxchgTo implement these functions.atomic.cMedium

Related Article

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.