Bump version to 52:0
[folly.git] / folly / wangle / bootstrap / ServerBootstrap.h
index 3de9c85c9f84425f3c809d9716ebaf35ad37be48..2c2a6b47dcafd5b0f2ad13f3a046c8789b173587 100644 (file)
@@ -45,7 +45,7 @@ class ServerBootstrap {
   ServerBootstrap(const ServerBootstrap& that) = delete;
   ServerBootstrap(ServerBootstrap&& that) = default;
 
-  ServerBootstrap() {}
+  ServerBootstrap() = default;
 
   ~ServerBootstrap() {
     stop();
@@ -120,8 +120,13 @@ class ServerBootstrap {
         1, std::make_shared<wangle::NamedThreadFactory>("Acceptor Thread"));
     }
     if (!io_group) {
+      auto threads = std::thread::hardware_concurrency();
+      if (threads <= 0) {
+        // Reasonable mid-point for concurrency when actual value unknown
+        threads = 8;
+      }
       io_group = std::make_shared<folly::wangle::IOThreadPoolExecutor>(
-        32, std::make_shared<wangle::NamedThreadFactory>("IO Thread"));
+        threads, std::make_shared<wangle::NamedThreadFactory>("IO Thread"));
     }
 
     // TODO better config checking
@@ -284,6 +289,13 @@ class ServerBootstrap {
       }
       sockets_->clear();
     }
+    if (!stopped_) {
+      stopped_ = true;
+      // stopBaton_ may be null if ServerBootstrap has been std::move'd
+      if (stopBaton_) {
+        stopBaton_->post();
+      }
+    }
   }
 
   void join() {
@@ -295,6 +307,13 @@ class ServerBootstrap {
     }
   }
 
+  void waitForStop() {
+    if (!stopped_) {
+      CHECK(stopBaton_);
+      stopBaton_->wait();
+    }
+  }
+
   /*
    * Get the list of listening sockets
    */
@@ -328,6 +347,10 @@ class ServerBootstrap {
     std::make_shared<DefaultAcceptPipelineFactory>()};
   std::shared_ptr<ServerSocketFactory> socketFactory_{
     std::make_shared<AsyncServerSocketFactory>()};
+
+  std::unique_ptr<folly::Baton<>> stopBaton_{
+    folly::make_unique<folly::Baton<>>()};
+  bool stopped_{false};
 };
 
 } // namespace