Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 
0011 
0012 void
0013 ngx_spinlock(ngx_atomic_t *lock, ngx_atomic_int_t value, ngx_uint_t spin)
0014 {
0015 
0016 #if (NGX_HAVE_ATOMIC_OPS)
0017 
0018     ngx_uint_t  i, n;
0019 
0020     for ( ;; ) {
0021 
0022         if (*lock == 0 && ngx_atomic_cmp_set(lock, 0, value)) {
0023             return;
0024         }
0025 
0026         if (ngx_ncpu > 1) {
0027 
0028             for (n = 1; n < spin; n <<= 1) {
0029 
0030                 for (i = 0; i < n; i++) {
0031                     ngx_cpu_pause();
0032                 }
0033 
0034                 if (*lock == 0 && ngx_atomic_cmp_set(lock, 0, value)) {
0035                     return;
0036                 }
0037             }
0038         }
0039 
0040         ngx_sched_yield();
0041     }
0042 
0043 #else
0044 
0045 #if (NGX_THREADS)
0046 
0047 #error ngx_spinlock() or ngx_atomic_cmp_set() are not defined !
0048 
0049 #endif
0050 
0051 #endif
0052 
0053 }