Atomic operation atomic_t

Source: Internet
Author: User
An atomic operation is called an atomic operation, that is, it will never be interrupted by any other task or event before execution is completed. That is to say, its smallest execution unit, it is impossible to have a smaller execution unit than it, so the Atom here actually uses the concept of material particles in physics. Atomic operations require hardware support, so they are architecture-related. Their APIs and atomic types are defined in the kernel source code tree include/ASM/atomic. in the H file, they are all implemented in assembly language, because the C language cannot implement such operations. Atomic operations are mainly used to count resources. Many reference counting (refcnt) is implemented through atomic operations. The atomic type is defined as follows: typedef struct {volatile int counter;} atomic_t;

The volatile modifier field tells GCC not to optimize the data of this type, and its access is to the memory instead of the register. Atomic operation APIs include: This function performs atomic read operations on atomic variables. It returns the value of atomic variable v. Atomic_read (atomic_t * V );

 
This function sets the V value of the atomic type to I. Atomic_set (atomic_t * V, int I );

This function is used to add value to variable V of the atomic type. Void atomic_add (int I, atomic_t * V );

 
This function deducts I from variable V of the atomic type. Atomic_sub (int I, atomic_t * V); this function deducts I from the variable V of the atomic type and determines whether the result is 0. If it is 0, true is returned. Otherwise, false is returned. Int atomic_sub_and_test (int I, atomic_t * V); this function increases the value of V to 1. Void atomic_inc (atomic_t * V );

This function is used to subtract 1 from the V atom variable of the atomic type.

Void atomic_dec (atomic_t * V );

This function subtract 1 from the V atom variable of the atomic type and determines whether the result is 0. If it is 0, true is returned. Otherwise, false is returned.

Int atomic_dec_and_test (atomic_t * V); this function increases the atomic value of V by 1 and determines whether the result is 0. If it is 0, true is returned. Otherwise, false is returned. Int atomic_inc_and_test (atomic_t * V );

This function increases I on the V atom of the atomic type variable and determines whether the result is negative. If yes, it returns true; otherwise, it returns false. Int atomic_add_negative (int I, atomic_t * V); this function increases I atomically for an atomic variable V and returns a pointer to v. Int atomic_add_return (int I, atomic_t * V); this function deducts I from the variable V of the atomic type and returns a pointer to v. Int atomic_sub_return (int I, atomic_t * V );

 
This function increases the value of V to 1 and returns a pointer to v. Int atomic_inc_return (atomic_t * V );

This function reduces the value of V atom by 1 and returns a pointer to v. Int atomic_dec_return (atomic_t * V );

Atomic operations are usually used to implement reference counting of resources. In the IP Fragment processing of the TCP/IP protocol stack, reference counting is used, and the fragmentation queue structure struct
Ipq describes an IP Fragment. The refcnt field is the reference counter. Its type is atomic_t. When an IP fragment is created (in the ip_frag_create function ),
Use the atomic_set function to set it to 1. When the IP fragment is referenced, use the atomic_inc function to add 1 to the reference count. When you do not need to reference the IP fragment, use the ipq_put function to release the IP Fragment. ipq_put uses the atomic_dec_and_test function
Reduce the reference count by 1 and determine whether the reference count is 0. If yes, the IP fragmentation is released. The ipq_kill function deletes IP fragments from the ipq queue and removes the reference count of the deleted IP fragments.
1 (implemented by using the atomic_dec function ). Address: http://blog.chinaunix.net/u3/108239/showart_2221305.html

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.