]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Convert struct dns_view->attributes to atomic_uint to prevent some locking
authorOndřej Surý <ondrej@sury.org>
Thu, 18 Jul 2019 12:22:31 +0000 (14:22 +0200)
committerOndřej Surý <ondrej@sury.org>
Wed, 9 Oct 2019 06:09:44 +0000 (08:09 +0200)
lib/dns/include/dns/view.h
lib/dns/view.c

index 626d776dd5a85a86b69bbcc0986649166e13d988..85642b72a02e082d8ea5284f6ba88d0c79f28786 100644 (file)
@@ -196,9 +196,8 @@ struct dns_view {
        /* Locked by themselves. */
        isc_refcount_t                  references;
        isc_refcount_t                  weakrefs;
+       atomic_uint_fast32_t            attributes;
 
-       /* Locked by lock. */
-       unsigned int                    attributes;
        /* Under owner's locking control. */
        ISC_LINK(struct dns_view)       link;
        dns_viewlist_t *                viewlist;
index b37b7e598de84f211a9f1538c0573ab0e9968ec2..23767d72f8ba9924e78a797e4e18a37c416c9b56 100644 (file)
@@ -19,6 +19,7 @@
  #include <lmdb.h>
 #endif
 
+#include <isc/atomic.h>
 #include <isc/file.h>
 #include <isc/hash.h>
 #include <isc/lex.h>
@@ -64,9 +65,9 @@
               if (result != ISC_R_SUCCESS) goto cleanup;        \
        } while (0)
 
-#define RESSHUTDOWN(v) (((v)->attributes & DNS_VIEWATTR_RESSHUTDOWN) != 0)
-#define ADBSHUTDOWN(v) (((v)->attributes & DNS_VIEWATTR_ADBSHUTDOWN) != 0)
-#define REQSHUTDOWN(v) (((v)->attributes & DNS_VIEWATTR_REQSHUTDOWN) != 0)
+#define RESSHUTDOWN(v) ((atomic_load(&(v)->attributes) & DNS_VIEWATTR_RESSHUTDOWN) != 0)
+#define ADBSHUTDOWN(v) ((atomic_load(&(v)->attributes) & DNS_VIEWATTR_ADBSHUTDOWN) != 0)
+#define REQSHUTDOWN(v) ((atomic_load(&(v)->attributes) & DNS_VIEWATTR_REQSHUTDOWN) != 0)
 
 #define DNS_VIEW_DELONLYHASH 111
 #define DNS_VIEW_FAILCACHESIZE 1021
@@ -140,8 +141,9 @@ dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
        view->task = NULL;
        isc_refcount_init(&view->references, 1);
        isc_refcount_init(&view->weakrefs, 1);
-       view->attributes = (DNS_VIEWATTR_RESSHUTDOWN|DNS_VIEWATTR_ADBSHUTDOWN|
-                           DNS_VIEWATTR_REQSHUTDOWN);
+       atomic_init(&view->attributes, (DNS_VIEWATTR_RESSHUTDOWN |
+                                       DNS_VIEWATTR_ADBSHUTDOWN |
+                                       DNS_VIEWATTR_REQSHUTDOWN));
        view->statickeys = NULL;
        view->dynamickeys = NULL;
        view->matchclients = NULL;
@@ -685,10 +687,7 @@ resolver_shutdown(isc_task_t *task, isc_event_t *event) {
 
        isc_event_free(&event);
 
-       LOCK(&view->lock);
-       view->attributes |= DNS_VIEWATTR_RESSHUTDOWN;
-       UNLOCK(&view->lock);
-
+       atomic_fetch_or(&view->attributes, DNS_VIEWATTR_RESSHUTDOWN);
        dns_view_weakdetach(&view);
 }
 
@@ -704,9 +703,7 @@ adb_shutdown(isc_task_t *task, isc_event_t *event) {
 
        isc_event_free(&event);
 
-       LOCK(&view->lock);
-       view->attributes |= DNS_VIEWATTR_ADBSHUTDOWN;
-       UNLOCK(&view->lock);
+       atomic_fetch_or(&view->attributes, DNS_VIEWATTR_ADBSHUTDOWN);
 
        dns_view_weakdetach(&view);
 }
@@ -723,9 +720,7 @@ req_shutdown(isc_task_t *task, isc_event_t *event) {
 
        isc_event_free(&event);
 
-       LOCK(&view->lock);
-       view->attributes |= DNS_VIEWATTR_REQSHUTDOWN;
-       UNLOCK(&view->lock);
+       atomic_fetch_or(&view->attributes, DNS_VIEWATTR_REQSHUTDOWN);
 
        dns_view_weakdetach(&view);
 }
@@ -775,7 +770,7 @@ dns_view_createresolver(dns_view_t *view,
        }
        event = &view->resevent;
        dns_resolver_whenshutdown(view->resolver, view->task, &event);
-       view->attributes &= ~DNS_VIEWATTR_RESSHUTDOWN;
+       atomic_fetch_and(&view->attributes, ~DNS_VIEWATTR_RESSHUTDOWN);
        isc_refcount_increment(&view->weakrefs);
 
        isc_mem_create(&mctx);
@@ -789,7 +784,7 @@ dns_view_createresolver(dns_view_t *view,
        }
        event = &view->adbevent;
        dns_adb_whenshutdown(view->adb, view->task, &event);
-       view->attributes &= ~DNS_VIEWATTR_ADBSHUTDOWN;
+       atomic_fetch_and(&view->attributes, ~DNS_VIEWATTR_ADBSHUTDOWN);
        isc_refcount_increment(&view->weakrefs);
 
        result = dns_requestmgr_create(view->mctx, timermgr, socketmgr,
@@ -804,7 +799,7 @@ dns_view_createresolver(dns_view_t *view,
        }
        event = &view->reqevent;
        dns_requestmgr_whenshutdown(view->requestmgr, view->task, &event);
-       view->attributes &= ~DNS_VIEWATTR_REQSHUTDOWN;
+       atomic_fetch_and(&view->attributes, ~DNS_VIEWATTR_REQSHUTDOWN);
        isc_refcount_increment(&view->weakrefs);
 
        return (ISC_R_SUCCESS);