Commit b2901a61 authored by Dave Watson's avatar Dave Watson Committed by afrind

stop in same thread

Summary: In a couple places in ServerBootstrap there is code that needs to run in the EB, but if we are already in the EB, it's fine to run it inline.  Maybe this should be a method on EB directly?  There is runInEventBaseThreadAndWait(), but it explicitly disallows this usage.

Test Plan: used in D1942242, deadlocks without this, since stop() was called in the same eb

Reviewed By: yfeldblum@fb.com

Subscribers: chalfant, doug, fugalh, folly-diffs@, jsedgwick, yfeldblum

FB internal diff: D1947581

Signature: t1:1947581:1427831021:d1d08ff9a4a00501d6be60670709fcb17af04134
parent 3fc41801
...@@ -26,7 +26,8 @@ void ServerWorkerPool::threadStarted( ...@@ -26,7 +26,8 @@ void ServerWorkerPool::threadStarted(
workers_.insert({h, worker}); workers_.insert({h, worker});
for(auto socket : *sockets_) { for(auto socket : *sockets_) {
socket->getEventBase()->runInEventBaseThreadAndWait([this, worker, socket](){ socket->getEventBase()->runImmediatelyOrRunInEventBaseThreadAndWait(
[this, worker, socket](){
socketFactory_->addAcceptCB( socketFactory_->addAcceptCB(
socket, worker.get(), worker->getEventBase()); socket, worker.get(), worker->getEventBase());
}); });
...@@ -39,17 +40,16 @@ void ServerWorkerPool::threadStopped( ...@@ -39,17 +40,16 @@ void ServerWorkerPool::threadStopped(
CHECK(worker != workers_.end()); CHECK(worker != workers_.end());
for (auto& socket : *sockets_) { for (auto& socket : *sockets_) {
folly::Baton<> barrier; socket->getEventBase()->runImmediatelyOrRunInEventBaseThreadAndWait(
socket->getEventBase()->runInEventBaseThreadAndWait([&]() { [&]() {
socketFactory_->removeAcceptCB( socketFactory_->removeAcceptCB(
socket, worker->second.get(), nullptr); socket, worker->second.get(), nullptr);
barrier.post();
}); });
barrier.wait();
} }
if (!worker->second->getEventBase()->isInEventBaseThread()) { if (!worker->second->getEventBase()->isInEventBaseThread()) {
worker->second->getEventBase()->runInEventBaseThreadAndWait([=]() { worker->second->getEventBase()->runImmediatelyOrRunInEventBaseThreadAndWait(
[=]() {
worker->second->dropAllConnections(); worker->second->dropAllConnections();
}); });
} else { } else {
......
...@@ -177,7 +177,7 @@ class ServerBootstrap { ...@@ -177,7 +177,7 @@ class ServerBootstrap {
// Startup all the threads // Startup all the threads
workerFactory_->forEachWorker([this, socket](Acceptor* worker){ workerFactory_->forEachWorker([this, socket](Acceptor* worker){
socket->getEventBase()->runInEventBaseThreadAndWait( socket->getEventBase()->runImmediatelyOrRunInEventBaseThreadAndWait(
[this, worker, socket](){ [this, worker, socket](){
socketFactory_->addAcceptCB(socket, worker, worker->getEventBase()); socketFactory_->addAcceptCB(socket, worker, worker->getEventBase());
}); });
...@@ -262,8 +262,9 @@ class ServerBootstrap { ...@@ -262,8 +262,9 @@ class ServerBootstrap {
for (auto& socket : new_sockets) { for (auto& socket : new_sockets) {
// Startup all the threads // Startup all the threads
workerFactory_->forEachWorker([this, socket](Acceptor* worker){ workerFactory_->forEachWorker([this, socket](Acceptor* worker){
socket->getEventBase()->runInEventBaseThreadAndWait([this, worker, socket](){ socket->getEventBase()->runImmediatelyOrRunInEventBaseThreadAndWait(
socketFactory_->addAcceptCB(socket, worker, worker->getEventBase()); [this, worker, socket](){
socketFactory_->addAcceptCB(socket, worker, worker->getEventBase());
}); });
}); });
...@@ -276,12 +277,10 @@ class ServerBootstrap { ...@@ -276,12 +277,10 @@ class ServerBootstrap {
*/ */
void stop() { void stop() {
for (auto socket : sockets_) { for (auto socket : sockets_) {
folly::Baton<> barrier; socket->getEventBase()->runImmediatelyOrRunInEventBaseThreadAndWait(
socket->getEventBase()->runInEventBaseThread([&]() mutable { [&]() mutable {
socketFactory_->stopSocket(socket); socketFactory_->stopSocket(socket);
barrier.post();
}); });
barrier.wait();
} }
sockets_.clear(); sockets_.clear();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment