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

Fixing service_executor #2902

Merged
merged 2 commits into from Sep 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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