Spin_lock macro and spin_unlock macro

Source: Internet
Author: User

Today, I read chapter 1 "interrupt and interrupt handling procedures" and saw the spin_lock and spin_unlock, so I went into some in-depth research.

  • The spin_lock macro is defined as follows:
  1. # Define spin_lock (LOCK) _ spin_lock (LOCK)
  2. # DEFINE _ spin_lock (LOCK) _ Lock (Lock)
  3. # DEFINE _ Lock (Lock )/
  4. Do {preempt_disable (); _ acquire (Lock); (void) (Lock);} while (0)
  5. # Define preempt_disable () do {} while (0)
  6. # DEFINE _ acquire (x) _ context _ (x, 1)

  • The spin_unlock macro is defined as follows:
  1. # If defined (config_debug_spinlock) | defined (config_preempt) |/
  2. ! Defined (config_smp)
  3. # Define spin_unlock (LOCK) _ spin_unlock (LOCK)
  4. # Else
  5. # Define spin_unlock (LOCK )/
  6. Do {__ raw_spin_unlock (& (LOCK)-> raw_lock); _ release (Lock);} while (0)
  7. # DEFINE _ spin_unlock (LOCK) _ unlock (LOCK)
  8. # DEFINE _ unlock (LOCK )/
  9. Do {preempt_enable (); _ release (Lock); (void) (Lock);} while (0)
  10. Static inline void _ raw_spin_unlock (raw_spinlock_t * Lock)
  11. {
  12. MB ();
  13. Lock-> lock = 0;
  14. }
  15. # Define MB ()/
  16. _ ASM _ volatile _ ("MB": "Memory ")
  17. # Ifdef _ checker __
  18. # DEFINE _ release (x) _ context _ (x,-1)
  19. # Else
  20. # DEFINE _ Releases (X)

The semantics of MB () is discussed at the following URL:
Http://www.linuxforum.net/forum/showflat.php? Cat = & board = linuxk & number = 688286

For more information, see documentation/memory-barrier.txt

_ ASM _ volatile _ ("MB": "Memory") This line of code is the memory barrier.

1) _ ASM _ indicates that the compiler inserts Assembly statements here

2) _ volatile _ is used to tell the compiler that it is strictly prohibited to re-combine the Assembly statements here with other statements for optimization. That is: the original version processes the compilation here according to the original format.

3) memory forces the GCC compiler to assume that all memory units in RAM are modified by Assembly commands, so that the data in the CPU's registers and cached memory units will be voided. The CPU will have to re-read the data in the memory as needed. This prevents the CPU from using the data in the registers and cache to optimize the command and avoid accessing the memory.

4) "": indicates that this is an empty command. Barrier () does not need to insert a serialized Assembly command here.

For detailed analysis, see:
Http://blog.csdn.net/qinzhonghello/archive/2008/11/25/3372478.aspx

  • There is a lock parameter in the spin_lock and spin_unlock macros. Its type is defined as follows:
  1. Typedef struct {
  2. Volatile unsigned int lock _ attribute _ (aligned (4 )));
  3. # Ifdef config_preempt
  4. Unsigned int break_lock;
  5. # Endif
  6. } Spinlock_t;

Take a closer look at the Code:
Volatile unsigned int lock _ attribute _ (aligned (4 )));

First, the volatile keyword is a type modifier. The type variables it declares can be changed by unknown factors in some compilers, such as operating systems, hardware, or other threads. When the variable declared by this keyword is encountered, the compiler will not optimize the code that accesses the variable, so as to provide stable access to the special address.
For detailed usage of volatile, see:
Http://princetonboy.ycool.com/post.2979571.html


Second, the keyword _ attribute _ can set attributes for the variable or structure field. Aligned (alignment) This attribute sets an alignment format of the specified size (in bytes), __attribute _ (aligned (4 ))) specifies the 4-byte alignment format.
For detailed usage of the keyword _ attribute _, see:
Http://hi.baidu.com/cygnusnow/blog/item/8b82000f871fcf2f6159f3de.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.