Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sharing EventLoopThreadPool between TcpServers (#411) #525

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions muduo/net/EventLoopThreadPool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ EventLoopThreadPool::EventLoopThreadPool(EventLoop* baseLoop, const string& name
: baseLoop_(baseLoop),
name_(nameArg),
started_(false),
numThreads_(0),
next_(0)
numThreads_(0)
{
}

Expand Down Expand Up @@ -60,12 +59,8 @@ EventLoop* EventLoopThreadPool::getNextLoop()
if (!loops_.empty())
{
// round-robin
loop = loops_[next_];
++next_;
if (implicit_cast<size_t>(next_) >= loops_.size())
{
next_ = 0;
}
int64_t next = next_.getAndAdd(1);
loop = loops_[implicit_cast<size_t>(next) % loops_.size()];
}
return loop;
}
Expand Down
3 changes: 2 additions & 1 deletion muduo/net/EventLoopThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "muduo/base/noncopyable.h"
#include "muduo/base/Types.h"
#include "muduo/base/Atomic.h"

#include <functional>
#include <memory>
Expand Down Expand Up @@ -58,7 +59,7 @@ class EventLoopThreadPool : noncopyable
string name_;
bool started_;
int numThreads_;
int next_;
AtomicInt64 next_;
std::vector<std::unique_ptr<EventLoopThread>> threads_;
std::vector<EventLoop*> loops_;
};
Expand Down
23 changes: 21 additions & 2 deletions muduo/net/TcpServer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TcpServer::TcpServer(EventLoop* loop,
name_(nameArg),
acceptor_(new Acceptor(loop, listenAddr, option == kReusePort)),
threadPool_(new EventLoopThreadPool(loop, name_)),
sharedThreadPool_(NULL),
connectionCallback_(defaultConnectionCallback),
messageCallback_(defaultMessageCallback),
nextConnId_(1)
Expand All @@ -36,6 +37,16 @@ TcpServer::TcpServer(EventLoop* loop,
std::bind(&TcpServer::newConnection, this, _1, _2));
}

TcpServer::TcpServer(EventLoop* loop,
const InetAddress& listenAddr,
const string& nameArg,
EventLoopThreadPool* sharedThreadPool,
Option option)
: TcpServer(loop, listenAddr, nameArg, option)
{
sharedThreadPool_ = CHECK_NOTNULL(sharedThreadPool);
}

TcpServer::~TcpServer()
{
loop_->assertInLoopThread();
Expand All @@ -60,7 +71,15 @@ void TcpServer::start()
{
if (started_.getAndSet(1) == 0)
{
threadPool_->start(threadInitCallback_);
if (sharedThreadPool_ != NULL)
{
// the owner must start this ThreadPool before starting TcpServer
assert(sharedThreadPool_->started());
}
else
{
threadPool_->start(threadInitCallback_);
}

assert(!acceptor_->listening());
loop_->runInLoop(
Expand All @@ -71,7 +90,7 @@ void TcpServer::start()
void TcpServer::newConnection(int sockfd, const InetAddress& peerAddr)
{
loop_->assertInLoopThread();
EventLoop* ioLoop = threadPool_->getNextLoop();
EventLoop* ioLoop = sharedThreadPool_ != NULL ? sharedThreadPool_->getNextLoop() : threadPool_->getNextLoop();
char buf[64];
snprintf(buf, sizeof buf, "-%s#%d", ipPort_.c_str(), nextConnId_);
++nextConnId_;
Expand Down
6 changes: 6 additions & 0 deletions muduo/net/TcpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class TcpServer : noncopyable
const InetAddress& listenAddr,
const string& nameArg,
Option option = kNoReusePort);
TcpServer(EventLoop* loop,
const InetAddress& listenAddr,
const string& nameArg,
EventLoopThreadPool* sharedThreadPool,
Option option = kNoReusePort);
~TcpServer(); // force out-line dtor, for std::unique_ptr members.

const string& ipPort() const { return ipPort_; }
Expand Down Expand Up @@ -104,6 +109,7 @@ class TcpServer : noncopyable
const string name_;
std::unique_ptr<Acceptor> acceptor_; // avoid revealing Acceptor
std::shared_ptr<EventLoopThreadPool> threadPool_;
EventLoopThreadPool* sharedThreadPool_;
ConnectionCallback connectionCallback_;
MessageCallback messageCallback_;
WriteCompleteCallback writeCompleteCallback_;
Expand Down