Nginx learning 4-nginx Process Synchronization Method-spin lock)

Source: Internet
Author: User
Spin lock Introduction The nginx framework uses three message transmission methods: Shared Memory, socket, and signal. Nginx mainly uses three Synchronization Methods: Atomic operations, semaphores, and file locks. Based on atomic operations, nginx implements a spin lock. Spin locks are non-sleep locks. If a process view obtains a spin lock and finds that the lock has been obtained by other processes, the current process will not be sleep, but will always remain executable, each time the kernel schedules the execution of this process, it continuously checks whether the lock can be obtained. In application scenarios of spin locks, spin locks are mainly set for multi-processor operating systems, the shared resource protection scenario is that the process uses the lock for a very short period of time (if the lock is used for a long time, the spin lock is not suitable because it will occupy a large amount of CPU resources ). In most cases, it is best not to go to sleep for the nginx worker process, because it is very busy. In this process, epoll may have 100,000 or even millions of TCP connections waiting for processing, once a process is sleep, it must wait for other events to wake up. The load consumption caused by intermediate and frequent inter-process switching may not be acceptable to users. The following describes the implementation of the spin lock through the source code (nginx_spinlock.c, nginx1.4.1 ):
/** Copyright (c) Igor Sysoev * copyright (c) nginx, Inc. */# include <ngx_config.h> # include <ngx_core.h> // function: Implementation of the spin lock method ngx_spinlock Based on atomic operations // parameter explanation: // lock: lock expressed by atomic variables // value: Flag, whether the lock is occupied by a process // spin: In a multi-processor system, when the ngx_spinlock method does not get the lock, the time when the current process waits for other processors to release the lock in a kernel scheduling voidngx_spinlock (ngx_atomic_t * Lock, ngx_atomic_int_t value, ngx_uint_t spin) {# If (ngx_have_atomic_ops) // supports atomic operations ngx_uint_t I, n; // it remains in the loop until the lock (; ;) {// If lock is 0, no other process holds the lock, set the lock value to the value parameter, indicating that the current process holds the lock if (* Lock = 0 & ngx_atomic_cmp_set (lock, 0, value) {return ;} // if it is a multi-processor system if (ngx_ncpu> 1) {for (n = 1; n <spin; n <= 1) {// as the number of waits increases, the actual interval between checking locks is getting larger and larger. For (I = 0; I <n; I ++) {ngx_cpu_pause (); // tell the CPU to be in the spin lock Wait Status} // check whether the lock is released if (* Lock = 0 & ngx_atomic_cmp_set (lock, 0, value )) {return ;}}// the current process gets the processor, but it is still executable ngx_sched_yield () ;}# Else # If (ngx_threads) # error ngx_spinlock () or ngx_atomic_cmp_set () are not defined! # Endif}

The above code should note that, in a multi-processor, when the lock is found to be occupied by other processes, the current process does not immediately give up the CPU processor in use, but waits for a while, check whether the locks are released for processes on other processors, which reduces the number of inter-process switches. Function ngx_cpu_pause (): It is a virtual instruction provided by the architecture system for the spin lock. It will tell the CPU to be in the spin lock wait state, a cpu usually saves energy and reduces power consumption. However, the current process does not give up the processor in use. Function ngx_sched_yield (): the current process is still executable, but the current processor is given priority to schedule other executable processes. In this way, when the process is re-scheduled by the kernel, in the for loop code, you can expect other processes to release the lock.

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.