[svn] commit: r3517 - in /branches/trac408/src/lib/nsas: nameserver_entry.h zone_entry.cc zone_entry.h

BIND 10 source code commits bind10-changes at lists.isc.org
Sat Nov 13 17:28:17 UTC 2010


Author: vorner
Date: Sat Nov 13 17:28:16 2010
New Revision: 3517

Log:
ZoneEntry::Lock implementation

Modified:
    branches/trac408/src/lib/nsas/nameserver_entry.h
    branches/trac408/src/lib/nsas/zone_entry.cc
    branches/trac408/src/lib/nsas/zone_entry.h

Modified: branches/trac408/src/lib/nsas/nameserver_entry.h
==============================================================================
--- branches/trac408/src/lib/nsas/nameserver_entry.h (original)
+++ branches/trac408/src/lib/nsas/nameserver_entry.h Sat Nov 13 17:28:16 2010
@@ -55,7 +55,7 @@
     {}
 };
 
-
+class ZoneEntry;
 
 /// \brief Nameserver Entry
 ///
@@ -202,6 +202,8 @@
     std::vector<AddressEntry> address_; ///< Set of V4/V6 addresses
     time_t          expiration_;        ///< Summary expiration time
     time_t          last_access_;       ///< Last access time to the structure
+    // We allow ZoneEntry to lock us
+    friend class ZoneEntry;
 };
 
 }   // namespace dns

Modified: branches/trac408/src/lib/nsas/zone_entry.cc
==============================================================================
--- branches/trac408/src/lib/nsas/zone_entry.cc (original)
+++ branches/trac408/src/lib/nsas/zone_entry.cc Sat Nov 13 17:28:16 2010
@@ -16,14 +16,23 @@
 
 #include "zone_entry.h"
 #include "address_request_callback.h"
+#include "nameserver_entry.h"
+
+#include <algorithm>
+#include <boost/foreach.hpp>
+
+using namespace std;
+using namespace boost;
 
 namespace isc {
 namespace nsas {
 
 namespace {
 // Shorter aliases for frequently used types
-typedef boost::mutex::scoped_lock LLock; // Local lock, nameservers not locked
-typedef boost::shared_ptr<AddressRequestCallback> CallbackPtr;
+typedef mutex::scoped_lock LLock; // Local lock, nameservers not locked
+typedef shared_ptr<LLock> LockPtr;
+typedef vector<LockPtr> Locks;
+typedef shared_ptr<AddressRequestCallback> CallbackPtr;
 }
 
 void
@@ -46,5 +55,33 @@
     return (result);
 }
 
+// Struct, we are somewhere inside, no need to play the private & public game
+struct ZoneEntry::Lock::Impl {
+    Locks locks;
+};
+
+ZoneEntry::Lock::Lock(shared_ptr<Impl> impl) :
+    impl_(impl)
+{ }
+
+ZoneEntry::Lock
+ZoneEntry::getLock() {
+    // First, lock the zone so we can get the nameservers
+    LockPtr lock(new LLock(mutex_));
+    // Get a sorted copy of the nameservers
+    // They are sorted to avoid possible race conditions, they will be locked
+    // in increasing order
+    NameserverVector nameserverCopy(nameservers_);
+    sort(nameserverCopy.begin(), nameserverCopy.end());
+    // Construct the list of locks and lock all the nameservers
+    shared_ptr<Lock::Impl> impl(new Lock::Impl);
+    impl->locks.push_back(lock);
+    BOOST_FOREACH(NameserverPtr ns, nameserverCopy) {
+        impl->locks.push_back(LockPtr(new LLock(ns->mutex_)));
+    }
+
+    return (Lock(impl));
+}
+
 }; // namespace nsas
 }; // namespace isc

Modified: branches/trac408/src/lib/nsas/zone_entry.h
==============================================================================
--- branches/trac408/src/lib/nsas/zone_entry.h (original)
+++ branches/trac408/src/lib/nsas/zone_entry.h Sat Nov 13 17:28:16 2010
@@ -133,12 +133,14 @@
      * Copy constructor, assignment operator and destructor are default.
      * The constructor that creates a new lock is private, use getLock()
      * to lock a zone entry.
+     *
+     * It is an error for the lock to survive destruction of its zone entry.
      */
     class Lock {
         private:
             struct Impl;
             boost::shared_ptr<Impl> impl_;
-            Lock(ZoneEntry&);
+            Lock(boost::shared_ptr<Impl>);
             friend class ZoneEntry;
     };
 
@@ -147,7 +149,7 @@
      *
      * \see Lock
      */
-    Lock getLock() { return Lock(*this); }
+    Lock getLock();
 private:
     mutable boost::mutex    mutex_;     ///< Mutex protecting this zone entry
     std::string     name_;      ///< Canonical zone name




More information about the bind10-changes mailing list