Skip to content

Commit

Permalink
Merge pull request #1657 from STEllAR-GROUP/io_service_pool_unique_ptr
Browse files Browse the repository at this point in the history
Use of shared_ptr in io_service_pool changed to unique_ptr
  • Loading branch information
hkaiser committed Jul 30, 2015
2 parents 7a6e03e + d305747 commit 2801b8d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
25 changes: 20 additions & 5 deletions hpx/util/io_service_pool.hpp
Expand Up @@ -14,12 +14,12 @@
#include <hpx/config/warnings_prefix.hpp>

#include <vector>
#include <memory>

#include <boost/asio/io_service.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>

///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace util
Expand Down Expand Up @@ -89,17 +89,32 @@ namespace hpx { namespace util
void clear_locked();

private:
typedef boost::shared_ptr<boost::asio::io_service> io_service_ptr;
typedef boost::shared_ptr<boost::asio::io_service::work> work_ptr;
typedef std::unique_ptr<boost::asio::io_service> io_service_ptr;
#if (defined(HPX_GCC_VERSION) && HPX_GCC_VERSION < 40700)
typedef std::unique_ptr<boost::asio::io_service::work> work_type;
#else
typedef boost::asio::io_service::work work_type;
#endif

BOOST_FORCEINLINE work_type initialize_work(boost::asio::io_service& io_service)
{
return work_type(
#if (defined(HPX_GCC_VERSION) && HPX_GCC_VERSION < 40700)
new boost::asio::io_service::work(io_service)
#else
io_service
#endif
);
}

boost::mutex mtx_;

/// The pool of io_services.
std::vector<io_service_ptr> io_services_;
std::vector<boost::shared_ptr<boost::thread> > threads_;
std::vector<boost::thread> threads_;

/// The work that keeps the io_services running.
std::vector<work_ptr> work_;
std::vector<work_type> work_;

/// The next io_service to use for a connection.
std::size_t next_io_service_;
Expand Down
28 changes: 10 additions & 18 deletions src/util/io_service_pool.cpp
Expand Up @@ -44,10 +44,8 @@ namespace hpx { namespace util
// will not exit until they are explicitly stopped.
for (std::size_t i = 0; i < pool_size; ++i)
{
io_service_ptr io_service(new boost::asio::io_service);
work_ptr work(new boost::asio::io_service::work(*io_service));
io_services_.push_back(io_service);
work_.push_back(work);
io_services_.emplace_back(new boost::asio::io_service);
work_.emplace_back(initialize_work(*io_services_[i]));
}
}

Expand All @@ -62,10 +60,8 @@ namespace hpx { namespace util
{
for (std::size_t i = 0; i < pool_size_; ++i)
{
io_service_ptr io_service(new boost::asio::io_service);
work_ptr work(new boost::asio::io_service::work(*io_service));
io_services_.push_back(io_service);
work_.push_back(work);
io_services_.emplace_back(new boost::asio::io_service);
work_.emplace_back(initialize_work(*io_services_[i]));
}
}

Expand Down Expand Up @@ -115,18 +111,16 @@ namespace hpx { namespace util
{
for (std::size_t i = 0; i < pool_size_; ++i)
{
io_service_ptr io_service(new boost::asio::io_service);
work_ptr work(new boost::asio::io_service::work(*io_service));
io_services_.push_back(io_service);
work_.push_back(work);
io_services_.emplace_back(new boost::asio::io_service);
work_.emplace_back(initialize_work(*io_services_[i]));
}
}

for (std::size_t i = 0; i < pool_size_; ++i)
{
boost::shared_ptr<boost::thread> thread(new boost::thread(
boost::bind(&io_service_pool::thread_run, this, i)));
threads_.push_back(thread);
boost::thread thread(boost::bind(
&io_service_pool::thread_run, this, i));
threads_.emplace_back(std::move(thread));
}

next_io_service_ = 0;
Expand All @@ -152,7 +146,7 @@ namespace hpx { namespace util
{
// Wait for all threads in the pool to exit.
for (std::size_t i = 0; i < threads_.size(); ++i)
threads_[i]->join();
threads_[i].join();
threads_.clear();
}

Expand All @@ -166,8 +160,6 @@ namespace hpx { namespace util
{
if (!stopped_) {
// Explicitly inform all work to exit.
for (std::size_t i = 0; i < work_.size(); ++i)
work_[i].reset();
work_.clear();

// Explicitly stop all io_services.
Expand Down

0 comments on commit 2801b8d

Please sign in to comment.