]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
msg/async/ProtocolV[12]: unlock the `write_lock` before doing I/O
authorMax Kellermann <max.kellermann@ionos.com>
Mon, 7 Oct 2024 16:57:59 +0000 (18:57 +0200)
committerMax Kellermann <max.kellermann@ionos.com>
Mon, 7 Oct 2024 21:21:40 +0000 (23:21 +0200)
Reduce lock time/contention.

Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
src/msg/async/ProtocolV1.cc
src/msg/async/ProtocolV2.cc

index b14de7b1e5697d1ff2a8caeab80a393f0ff47b49..996f5a9465d6db00c86fb2d5ded1842b30a9a747 100644 (file)
@@ -223,7 +223,7 @@ void ProtocolV1::send_message(Message *m) {
     is_prepared = true;
   }
 
-  std::lock_guard<std::mutex> l(connection->write_lock);
+  std::unique_lock l{connection->write_lock};
   // "features" changes will change the payload encoding
   if (can_fast_prepare &&
       (can_write == WriteStatus::NOWRITE || connection->get_features() != f)) {
@@ -246,6 +246,11 @@ void ProtocolV1::send_message(Message *m) {
                    << dendl;
     if (can_write != WriteStatus::REPLACING && !write_in_progress) {
       write_in_progress = true;
+
+      /* unlock the mutex now because dispatch_event_external() may
+         block waiting for another mutex */
+      l.unlock();
+
       connection->center->dispatch_event_external(connection->write_handler);
     }
   }
@@ -271,9 +276,14 @@ void ProtocolV1::prepare_send_message(uint64_t features, Message *m,
 
 void ProtocolV1::send_keepalive() {
   ldout(cct, 10) << __func__ << dendl;
-  std::lock_guard<std::mutex> l(connection->write_lock);
+  std::unique_lock l{connection->write_lock};
   if (can_write != WriteStatus::CLOSED) {
     keepalive = true;
+
+    /* unlock the mutex now because dispatch_event_external() may
+       block waiting for another mutex */
+    l.unlock();
+
     connection->center->dispatch_event_external(connection->write_handler);
   }
 }
index 6d44d6c783f44adadae56527c4fc0d2a67ba4977..0ca8afcad25b2ff4b29db6ad258e819d4dd4b6ee 100644 (file)
@@ -439,7 +439,7 @@ void ProtocolV2::send_message(Message *m) {
     is_prepared = false;
   }
 
-  std::lock_guard<std::mutex> l(connection->write_lock);
+  std::unique_lock l{connection->write_lock};
   // "features" changes will change the payload encoding
   if (can_fast_prepare && (!can_write || connection->get_features() != f)) {
     // ensure the correctness of message encoding
@@ -463,6 +463,11 @@ void ProtocolV2::send_message(Message *m) {
                    << dendl;
     if (((!replacing && can_write) || state == STANDBY) && !write_in_progress) {
       write_in_progress = true;
+
+      /* unlock the mutex now because dispatch_event_external() may
+         block waiting for another mutex */
+      l.unlock();
+
       connection->center->dispatch_event_external(connection->write_handler);
     }
   }
@@ -470,9 +475,14 @@ void ProtocolV2::send_message(Message *m) {
 
 void ProtocolV2::send_keepalive() {
   ldout(cct, 10) << __func__ << dendl;
-  std::lock_guard<std::mutex> l(connection->write_lock);
+  std::unique_lock l{connection->write_lock};
   if (state != CLOSED) {
     keepalive = true;
+
+    /* unlock the mutex now because dispatch_event_external() may
+       block waiting for another mutex */
+    l.unlock();
+
     connection->center->dispatch_event_external(connection->write_handler);
   }
 }