]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1147] Reviewed last changes
authorFrancis Dupont <fdupont@isc.org>
Thu, 28 May 2020 11:55:57 +0000 (13:55 +0200)
committerFrancis Dupont <fdupont@isc.org>
Thu, 28 May 2020 11:55:57 +0000 (13:55 +0200)
src/bin/dhcp4/client_handler.cc
src/bin/dhcp4/client_handler.h

index 6aabcf5c6226e686f63cfafef9340fd4bf4ba100..fbab5d4959075108c4dba3580b7052611d9ae869 100644 (file)
@@ -136,9 +136,26 @@ ClientHandler::ClientHandler()
 }
 
 ClientHandler::~ClientHandler() {
-    if (locked_client_id_ || locked_hwaddr_) {
-        lock_guard<mutex> lk(mutex_);
-        unLock();
+    bool unlocked = false;
+    lock_guard<mutex> lk(mutex_);
+    if (locked_client_id_) {
+        unlocked = true;
+        unLockById();
+    }
+    if (locked_hwaddr_) {
+        unlocked = true;
+        unLockByHWAddr();
+    }
+    if (!unlocked || !client_ || !client_->cont_) {
+        return;
+    }
+    // Try to process next query. As the caller holds the mutex of
+    // the handler class the continuation will be resumed after.
+    MultiThreadingMgr& mt_mgr = MultiThreadingMgr::instance();
+    if (mt_mgr.getMode()) {
+        if (!mt_mgr.getThreadPool().addFront(client_->cont_)) {
+            LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_PACKET_QUEUE_FULL);
+        }
     }
 }
 
@@ -178,38 +195,37 @@ ClientHandler::tryLock(Pkt4Ptr query, ContinuationPtr cont) {
     Pkt4Ptr next_query_hw;
     client_.reset(new Client(query, duid, hwaddr));
 
-    {
+    // Try first duid.
+    if (duid) {
+        // Try to acquire the by-client-id lock and return the holder
+        // when it failed.
         lock_guard<mutex> lk(mutex_);
-        // Try first duid.
-        if (duid) {
-            // Try to acquire the by-client-id lock and return the holder
-            // when it failed.
-            holder_id = lookup(duid);
-            if (!holder_id) {
-                locked_client_id_ = duid;
-                lockById();
-            } else if (cont) {
-                next_query_id = holder_id->next_query_;
-                holder_id->next_query_ = query;
-                holder_id->cont_ = cont;
-            }
-        }
+        holder_id = lookup(duid);
         if (!holder_id) {
-            if (!hwaddr) {
-                return (true);
-            }
-            // Try to acquire the by-hw-addr lock and return the holder
-            // when it failed.
-            holder_hw = lookup(hwaddr);
-            if (!holder_hw) {
-                locked_hwaddr_ = hwaddr;
-                lockByHWAddr();
-                return (true);
-            } else if (cont) {
-                next_query_hw = holder_hw->next_query_;
-                holder_hw->next_query_ = query;
-                holder_hw->cont_ = cont;
-            }
+            locked_client_id_ = duid;
+            lockById();
+        } else if (cont) {
+            next_query_id = holder_id->next_query_;
+            holder_id->next_query_ = query;
+            holder_id->cont_ = cont;
+        }
+    }
+    if (!holder_id) {
+        if (!hwaddr) {
+            return (true);
+        }
+        // Try to acquire the by-hw-addr lock and return the holder
+        // when it failed.
+        lock_guard<mutex> lk(mutex_);
+        holder_hw = lookup(hwaddr);
+        if (!holder_hw) {
+            locked_hwaddr_ = hwaddr;
+            lockByHWAddr();
+            return (true);
+        } else if (cont) {
+            next_query_hw = holder_hw->next_query_;
+            holder_hw->next_query_ = query;
+            holder_hw->cont_ = cont;
         }
     }
 
@@ -288,29 +304,6 @@ ClientHandler::lockByHWAddr() {
     addByHWAddr(client_);
 }
 
-void
-ClientHandler::unLock() {
-    if (locked_client_id_) {
-        unLockById();
-    }
-    if (locked_hwaddr_) {
-        unLockByHWAddr();
-    }
-
-    if (!client_ || !client_->cont_) {
-        return;
-    }
-
-    // Try to process next query. As the caller holds the mutex of
-    // the handler class the continuation will be resumed after.
-    MultiThreadingMgr& mt_mgr = MultiThreadingMgr::instance();
-    if (mt_mgr.getMode()) {
-        if (!mt_mgr.getThreadPool().addFront(client_->cont_)) {
-            LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_PACKET_QUEUE_FULL);
-        }
-    }
-}
-
 void
 ClientHandler::unLockById() {
     // Sanity check.
index 8ae393ea897855fa5bcf1713f1f4b853bb29f915..9b2b0c0e2d42291c48b6779d62143df33ad86e1d 100644 (file)
@@ -222,14 +222,6 @@ private:
     /// The mutex must be held by the caller.
     void lockByHWAddr();
 
-    /// @brief Release a client.
-    ///
-    /// If the client has a continuation, push it at front of the thread
-    /// packet queue.
-    ///
-    /// The mutex must be held by the only caller: the destructor.
-    void unLock();
-
     /// @brief Release a client by client ID option.
     ///
     /// The mutex must be held by the caller.