Skip to content

Commit

Permalink
Merge pull request #2902 from STEllAR-GROUP/fix_service_executor
Browse files Browse the repository at this point in the history
Fixing service_executor
  • Loading branch information
sithhell committed Sep 15, 2017
2 parents 9501811 + 057e97d commit 9c81e74
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
6 changes: 4 additions & 2 deletions hpx/runtime/threads/executors/service_executors.hpp
Expand Up @@ -8,7 +8,7 @@

#include <hpx/config.hpp>
#include <hpx/exception_fwd.hpp>
#include <hpx/lcos/local/counting_semaphore.hpp>
#include <hpx/lcos/local/condition_variable.hpp>
#include <hpx/runtime/threads/thread_enums.hpp>
#include <hpx/runtime/threads/thread_executor.hpp>
#include <hpx/throw_exception.hpp>
Expand Down Expand Up @@ -73,8 +73,10 @@ namespace hpx { namespace threads { namespace executors

private:
util::io_service_pool* pool_;
typedef hpx::lcos::local::spinlock mutex_type;
mutex_type mtx_;
std::atomic<std::uint64_t> task_count_;
lcos::local::counting_semaphore shutdown_sem_;
lcos::local::condition_variable_any shutdown_cv_;
};
}

Expand Down
20 changes: 15 additions & 5 deletions src/runtime/threads/executors/service_executor.cpp
Expand Up @@ -10,6 +10,7 @@
#include <hpx/throw_exception.hpp>
#include <hpx/runtime_fwd.hpp>
#include <hpx/runtime/threads/thread_enums.hpp>
#include <hpx/util/assert.hpp>
#include <hpx/util/bind.hpp>
#include <hpx/util/io_service_pool.hpp>
#include <hpx/util/steady_clock.hpp>
Expand All @@ -31,7 +32,7 @@ namespace hpx { namespace threads { namespace executors { namespace detail
service_executor::service_executor(
char const* pool_name, char const* pool_name_suffix)
: pool_(get_thread_pool(pool_name, pool_name_suffix)),
task_count_(0), shutdown_sem_(0)
task_count_(0)
{
if (!pool_) {
HPX_THROW_EXCEPTION(bad_parameter,
Expand All @@ -42,16 +43,25 @@ namespace hpx { namespace threads { namespace executors { namespace detail

service_executor::~service_executor()
{
if (task_count_ != 0)
shutdown_sem_.wait();
std::unique_lock<mutex_type> l(mtx_);
while (task_count_ != 0)
{
shutdown_cv_.wait(l);
}
}

void service_executor::thread_wrapper(closure_type&& f) //-V669
{
f(); // execute the actual thread function

if (--task_count_ == 0)
shutdown_sem_.signal();
// By hanging on to the lock during notify_all, we ensure that the
// destructor is only completed after this function returned
std::unique_lock<mutex_type> l(mtx_);
{
HPX_ASSERT(task_count_ > 0);
if (--task_count_ == 0)
shutdown_cv_.notify_all();
}
}

struct thread_wrapper_helper
Expand Down

0 comments on commit 9c81e74

Please sign in to comment.