Implementation of nginx spinlock

Source: Internet
Author: User
Tags usleep
Two questions: 1) What is the implementation principle of the spinlock? A. try to compete for a shared resource in user mode. if the competition fails, the competition will continue. however, we do not use the mutex and other variable mechanisms provided by the kernel. the kernel is involved, which means the efficiency is low. b. to compete for a shared resource in the user mode, you must use the atomic operation commands provided by the CPU. if SMP has multiple CPUs, you also need to lock the command to lock the bus. c. in order to avoid wasting the CPU by constantly trying because the resources are not available for a long period of competition, the interval between two attempts is a period of time. the interval also increases as the number of attempts increases. during the interval, you can take a short rest of the CPU (note that it is not necessary to let out the CPU), which depends on the CPU to provide the pausse command. (Of course, it doesn't matter if the CPU does not provide pause, but it will consume a lot of power resources.) The pause command improves the performance of the spin-Wait loop. Http://blog.csdn.net/misterliwei/article/details/3951103 D. Waiting for a long time or not get the lock, had to let out the CPU. But must let out a very small for a while. Otherwise it will not be called spin lock. How to let out the CPU, but there can be quickly back? The kernel provides the sched_yield () function sched_yield (). The main function is to run a thread with another level equal to or higher than the current thread. If there is no qualified thread, the function will immediately return and continue executing the program of the current thread reference: sched_yield () function Advanced Process Management http://blog.csdn.net/magod/article/details/7265555 of course, if the system does not support sched_yield, nginx is forced to use usleep () to rest for 1u seconds. 2) What is the use case of the spinlock? Nginx uses the spinlock technology to implement mutex between user-State processes. Since the spinlock is blocked, # define ngx_shmtx_lock (CTX) ngx_spinlock (CTX)-> lock, ngx_pid, 1024)
PS: I found in the nginx1.0.2 code that the implementation of this function of ngx_shmtx_lock does not directly call ngx_spinlock, but the implementation is similar to ngx_spinlock. 3) how to analyze an open-source implementation of a spinlock? // Input parameter // lock: pointer to an integer variable // value: Set the lock to a new value // spin: Number of spin times. the larger the value, the more attempts are made to obtain the lock. then it will be transferred to allow the kernel scheduling thread to temporarily let out the CPU. void
Ngx_spinlock (ngx_atomic_t * Lock, ngx_atomic_int_t value, ngx_uint_t spin)
{
# If (ngx_have_atomic_ops)
Ngx_uint_t I, N;
For (;;){
If (* Lock = 0 & ngx_atomic_cmp_set (lock, 0, value )){
Return;} // Why do I try spin multiple times only when multiple CPUs exist? // Haha, it is very simple. If it is a single core, since you have not obtained the lock, it means that other threads/processes are using the lock, so the CPU will not occupy the spin, otherwise, no one else can get the CPU, and the lock will not be released.

If (ngx_ncpu> 1) {for (n = 1; n <spin; n <= 1) {// The idling time increases with N

For (I = 0; I <n; I ++ ){
Ngx_cpu_pause (); // reduces CPU power consumption and improves efficiency while idling
}
If (* Lock = 0 & ngx_atomic_cmp_set (lock, 0, value )){
Return;
}
} // The lock has not been obtained after trying for so long. Let the CPU be busy with other people's affairs. Let the CPU wait.

Ngx_sched_yield ();
}
# Else
# If (ngx_threads)
# Error ngx_spinlock () or ngx_atomic_cmp_set () are not defined!
# Endif
# Endif
}

# If (ngx_have_sched_yield)
# Define ngx_sched_yield () sched_yield ()
# Else
# Define ngx_sched_yield () usleep (1)
# Endif

An Analysis of the spin lock Article :( turn) spin lock (spin lock) interpretation of the classic, thorough http://hi.baidu.com/p_157s/item/cf1aa182f3669ed35e0ec1a8

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.