[PATCH 1 of 2] Core: use ngx_trylock() where possible

Piotr Sikora piotrsikora at google.com
Thu Aug 18 00:29:31 UTC 2016


# HG changeset patch
# User Piotr Sikora <piotrsikora at google.com>
# Date 1471265530 25200
#      Mon Aug 15 05:52:10 2016 -0700
# Node ID 2f2ec92c3af93c11e195fb6d805df57518fede7c
# Parent  c131f20c9562387f94a268440594c288725d3ba8
Core: use ngx_trylock() where possible.

It makes code much more readable.

No binary changes.

Signed-off-by: Piotr Sikora <piotrsikora at google.com>

diff -r c131f20c9562 -r 2f2ec92c3af9 src/core/ngx_rwlock.c
--- a/src/core/ngx_rwlock.c
+++ b/src/core/ngx_rwlock.c
@@ -23,7 +23,7 @@ ngx_rwlock_wlock(ngx_atomic_t *lock)
 
     for ( ;; ) {
 
-        if (*lock == 0 && ngx_atomic_cmp_set(lock, 0, NGX_RWLOCK_WLOCK)) {
+        if (ngx_trylock(lock, NGX_RWLOCK_WLOCK)) {
             return;
         }
 
@@ -35,9 +35,7 @@ ngx_rwlock_wlock(ngx_atomic_t *lock)
                     ngx_cpu_pause();
                 }
 
-                if (*lock == 0
-                    && ngx_atomic_cmp_set(lock, 0, NGX_RWLOCK_WLOCK))
-                {
+                if (ngx_trylock(lock, NGX_RWLOCK_WLOCK)) {
                     return;
                 }
             }
diff -r c131f20c9562 -r 2f2ec92c3af9 src/core/ngx_shmtx.c
--- a/src/core/ngx_shmtx.c
+++ b/src/core/ngx_shmtx.c
@@ -62,7 +62,7 @@ ngx_shmtx_destroy(ngx_shmtx_t *mtx)
 ngx_uint_t
 ngx_shmtx_trylock(ngx_shmtx_t *mtx)
 {
-    return (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid));
+    return ngx_trylock(mtx->lock, ngx_pid);
 }
 
 
@@ -75,7 +75,7 @@ ngx_shmtx_lock(ngx_shmtx_t *mtx)
 
     for ( ;; ) {
 
-        if (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid)) {
+        if (ngx_trylock(mtx->lock, ngx_pid)) {
             return;
         }
 
@@ -87,9 +87,7 @@ ngx_shmtx_lock(ngx_shmtx_t *mtx)
                     ngx_cpu_pause();
                 }
 
-                if (*mtx->lock == 0
-                    && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid))
-                {
+                if (ngx_trylock(mtx->lock, ngx_pid)) {
                     return;
                 }
             }
@@ -100,7 +98,7 @@ ngx_shmtx_lock(ngx_shmtx_t *mtx)
         if (mtx->semaphore) {
             (void) ngx_atomic_fetch_add(mtx->wait, 1);
 
-            if (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid)) {
+            if (ngx_trylock(mtx->lock, ngx_pid)) {
                 (void) ngx_atomic_fetch_add(mtx->wait, -1);
                 return;
             }
diff -r c131f20c9562 -r 2f2ec92c3af9 src/core/ngx_spinlock.c
--- a/src/core/ngx_spinlock.c
+++ b/src/core/ngx_spinlock.c
@@ -19,7 +19,7 @@ ngx_spinlock(ngx_atomic_t *lock, ngx_ato
 
     for ( ;; ) {
 
-        if (*lock == 0 && ngx_atomic_cmp_set(lock, 0, value)) {
+        if (ngx_trylock(lock, value)) {
             return;
         }
 
@@ -31,7 +31,7 @@ ngx_spinlock(ngx_atomic_t *lock, ngx_ato
                     ngx_cpu_pause();
                 }
 
-                if (*lock == 0 && ngx_atomic_cmp_set(lock, 0, value)) {
+                if (ngx_trylock(lock, value)) {
                     return;
                 }
             }
diff -r c131f20c9562 -r 2f2ec92c3af9 src/core/ngx_times.c
--- a/src/core/ngx_times.c
+++ b/src/core/ngx_times.c
@@ -84,7 +84,7 @@ ngx_time_update(void)
     ngx_time_t      *tp;
     struct timeval   tv;
 
-    if (!ngx_trylock(&ngx_time_lock)) {
+    if (!ngx_trylock(&ngx_time_lock, 1)) {
         return;
     }
 
@@ -200,7 +200,7 @@ ngx_time_sigsafe_update(void)
     ngx_time_t      *tp;
     struct timeval   tv;
 
-    if (!ngx_trylock(&ngx_time_lock)) {
+    if (!ngx_trylock(&ngx_time_lock, 1)) {
         return;
     }
 
diff -r c131f20c9562 -r 2f2ec92c3af9 src/os/unix/ngx_atomic.h
--- a/src/os/unix/ngx_atomic.h
+++ b/src/os/unix/ngx_atomic.h
@@ -306,7 +306,9 @@ ngx_atomic_fetch_add(ngx_atomic_t *value
 
 void ngx_spinlock(ngx_atomic_t *lock, ngx_atomic_int_t value, ngx_uint_t spin);
 
-#define ngx_trylock(lock)  (*(lock) == 0 && ngx_atomic_cmp_set(lock, 0, 1))
+#define ngx_trylock(lock, value)                                              \
+    (*(lock) == 0 && ngx_atomic_cmp_set(lock, 0, value))
+
 #define ngx_unlock(lock)    *(lock) = 0
 
 
diff -r c131f20c9562 -r 2f2ec92c3af9 src/os/win32/ngx_atomic.h
--- a/src/os/win32/ngx_atomic.h
+++ b/src/os/win32/ngx_atomic.h
@@ -62,7 +62,9 @@ typedef volatile ngx_atomic_uint_t  ngx_
 
 void ngx_spinlock(ngx_atomic_t *lock, ngx_atomic_int_t value, ngx_uint_t spin);
 
-#define ngx_trylock(lock)  (*(lock) == 0 && ngx_atomic_cmp_set(lock, 0, 1))
+#define ngx_trylock(lock, value)                                              \
+    (*(lock) == 0 && ngx_atomic_cmp_set(lock, 0, value))
+
 #define ngx_unlock(lock)    *(lock) = 0
 
 



More information about the nginx-devel mailing list