Skip to content

Commit

Permalink
Don't use boost::intrusive_ptr for thread_id_type
Browse files Browse the repository at this point in the history
This change gets rid of using boost::intrusive_ptr to refcount thread_data. The
lifetime of thread_data is perfectly managed by the scheduling policy. That is,
once a task terminates, it can be deleted/reused.
  • Loading branch information
Thomas Heller committed Jan 22, 2018
1 parent 805c603 commit 79eeeb6
Show file tree
Hide file tree
Showing 38 changed files with 353 additions and 223 deletions.
2 changes: 1 addition & 1 deletion hpx/lcos/detail/future_data.hpp
Expand Up @@ -223,7 +223,7 @@ namespace detail
if (recurse_asynchronously)
{
error_code ec;
threads::thread_id_type id = threads::register_thread_nullary(
threads::register_thread_nullary(
compose_cb_impl(std::move(f1_), std::move(f2_)),
"compose_cb",
threads::pending, true, threads::thread_priority_boost,
Expand Down
6 changes: 3 additions & 3 deletions hpx/lcos/local/detail/condition_variable.hpp
Expand Up @@ -40,11 +40,11 @@ namespace hpx { namespace lcos { namespace local { namespace detail
boost::intrusive::link_mode<boost::intrusive::normal_link>
> hook_type;

queue_entry(threads::thread_id_repr_type const& id, void* q)
queue_entry(threads::thread_id_type const& id, void* q)
: id_(id), q_(q)
{}

threads::thread_id_repr_type id_;
threads::thread_id_type id_;
void* q_;
hook_type slist_hook_;
};
Expand All @@ -68,7 +68,7 @@ namespace hpx { namespace lcos { namespace local { namespace detail

~reset_queue_entry()
{
if (e_.id_ != threads::invalid_thread_id_repr)
if (e_.id_ != threads::invalid_thread_id)
{
queue_type* q = static_cast<queue_type*>(e_.q_);
q->erase(last_); // remove entry from queue
Expand Down
2 changes: 1 addition & 1 deletion hpx/lcos/local/mutex.hpp
Expand Up @@ -48,7 +48,7 @@ namespace hpx { namespace lcos { namespace local

protected:
mutable mutex_type mtx_;
threads::thread_id_repr_type owner_id_;
threads::thread_id_type owner_id_;
detail::condition_variable cond_;
};

Expand Down
8 changes: 4 additions & 4 deletions hpx/lcos/local/recursive_mutex.hpp
Expand Up @@ -50,7 +50,7 @@ namespace hpx { namespace lcos { namespace local
static thread_id_type call()
{
return hpx::threads::get_self_ptr() ?
(thread_id_type)hpx::threads::get_self_id().get() :
reinterpret_cast<thread_id_type>(hpx::threads::get_self_id().get()) :
thread_id_from_mutex<void>::call();
};
};
Expand Down Expand Up @@ -126,8 +126,8 @@ namespace hpx { namespace lcos { namespace local
/// called outside of a HPX-thread.
// bool timed_lock(::boost::system_time const& wait_until);
// {
// threads::thread_id_repr_type const current_thread_id =
// threads::get_self_id().get();
// threads::thread_id_type const current_thread_id =
// threads::get_self_id();
//
// return try_recursive_lock(current_thread_id) ||
// try_timed_lock(current_thread_id, wait_until);
Expand Down Expand Up @@ -197,7 +197,7 @@ namespace hpx { namespace lcos { namespace local
return false;
}

// bool try_timed_lock(threads::thread_id_repr_type current_thread_id,
// bool try_timed_lock(threads::thread_id_type current_thread_id,
// ::boost::system_time const& target)
// {
// if (mtx.timed_lock(target))
Expand Down
6 changes: 3 additions & 3 deletions hpx/runtime/actions/action_support.hpp
Expand Up @@ -42,19 +42,19 @@ namespace hpx { namespace actions { namespace detail
{
action_serialization_data()
: parent_locality_(naming::invalid_locality_id)
, parent_id_(static_cast<std::uint64_t>(-1))
, parent_id_(static_cast<std::uint64_t>(0))
, parent_phase_(0)
, priority_(static_cast<threads::thread_priority>(0))
, stacksize_(static_cast<threads::thread_stacksize>(0))
{}

action_serialization_data(std::uint32_t parent_locality,
std::uint64_t parent_id,
threads::thread_id_type parent_id,
std::uint64_t parent_phase,
threads::thread_priority priority,
threads::thread_stacksize stacksize)
: parent_locality_(parent_locality)
, parent_id_(parent_id)
, parent_id_(reinterpret_cast<std::uint64_t>(parent_id.get()))
, parent_phase_(parent_phase)
, priority_(priority)
, stacksize_(stacksize)
Expand Down
2 changes: 1 addition & 1 deletion hpx/runtime/actions/base_action.hpp
Expand Up @@ -81,7 +81,7 @@ namespace hpx { namespace actions
virtual std::uint32_t get_parent_locality_id() const = 0;

/// Return the thread id of the parent thread
virtual threads::thread_id_repr_type get_parent_thread_id() const = 0;
virtual threads::thread_id_type get_parent_thread_id() const = 0;

/// Return the thread phase of the parent thread
virtual std::uint64_t get_parent_thread_phase() const = 0;
Expand Down
3 changes: 1 addition & 2 deletions hpx/runtime/actions/transfer_action.hpp
Expand Up @@ -168,8 +168,7 @@ namespace hpx { namespace actions

threads::thread_init_data data;
#if defined(HPX_HAVE_THREAD_PARENT_REFERENCE)
data.parent_id =
reinterpret_cast<threads::thread_id_repr_type>(this->parent_id_);
data.parent_id = this->parent_id_;
data.parent_locality_id = this->parent_locality_;
#endif
applier::detail::apply_helper<typename base_type::derived_type>::call(
Expand Down
18 changes: 9 additions & 9 deletions hpx/runtime/actions/transfer_base_action.hpp
Expand Up @@ -176,7 +176,7 @@ namespace hpx { namespace actions
: arguments_(std::forward<Ts>(vs)...),
#if defined(HPX_HAVE_THREAD_PARENT_REFERENCE)
parent_locality_(transfer_base_action::get_locality_id()),
parent_id_(reinterpret_cast<std::uint64_t>(threads::get_parent_id())),
parent_id_(threads::get_parent_id()),
parent_phase_(threads::get_parent_phase()),
#endif
priority_(
Expand All @@ -194,7 +194,7 @@ namespace hpx { namespace actions
: arguments_(std::forward<Ts>(vs)...),
#if defined(HPX_HAVE_THREAD_PARENT_REFERENCE)
parent_locality_(transfer_base_action::get_locality_id()),
parent_id_(reinterpret_cast<std::uint64_t>(threads::get_parent_id())),
parent_id_(threads::get_parent_id()),
parent_phase_(threads::get_parent_phase()),
#endif
priority_(
Expand Down Expand Up @@ -266,9 +266,9 @@ namespace hpx { namespace actions
}

/// Return the thread id of the parent thread
threads::thread_id_repr_type get_parent_thread_id() const
threads::thread_id_type get_parent_thread_id() const
{
return threads::invalid_thread_id_repr;
return threads::invalid_thread_id;
}

/// Return the phase of the parent thread
Expand All @@ -284,9 +284,9 @@ namespace hpx { namespace actions
}

/// Return the thread id of the parent thread
threads::thread_id_repr_type get_parent_thread_id() const
threads::thread_id_type get_parent_thread_id() const
{
return reinterpret_cast<threads::thread_id_repr_type>(parent_id_);
return parent_id_;
}

/// Return the phase of the parent thread
Expand Down Expand Up @@ -368,7 +368,7 @@ namespace hpx { namespace actions

#if defined(HPX_HAVE_THREAD_PARENT_REFERENCE)
parent_locality_ = data.parent_locality_;
parent_id_ = data.parent_id_;
parent_id_ = reinterpret_cast<threads::thread_data*>(data.parent_id_);
parent_phase_ = data.parent_phase_;
#endif
priority_ = data.priority_;
Expand All @@ -385,7 +385,7 @@ namespace hpx { namespace actions

#if !defined(HPX_HAVE_THREAD_PARENT_REFERENCE)
std::uint32_t parent_locality_ = naming::invalid_locality_id;
std::uint64_t parent_id_ = std::uint64_t(-1);
threads::thread_id_type parent_id_;
std::uint64_t parent_phase_ = 0;
#endif
detail::action_serialization_data data(parent_locality_,
Expand All @@ -405,7 +405,7 @@ namespace hpx { namespace actions

#if defined(HPX_HAVE_THREAD_PARENT_REFERENCE)
std::uint32_t parent_locality_;
std::uint64_t parent_id_;
threads::thread_id_type parent_id_;
std::uint64_t parent_phase_;
#endif
threads::thread_priority priority_;
Expand Down
3 changes: 1 addition & 2 deletions hpx/runtime/actions/transfer_continuation_action.hpp
Expand Up @@ -175,8 +175,7 @@ namespace hpx { namespace actions

threads::thread_init_data data;
#if defined(HPX_HAVE_THREAD_PARENT_REFERENCE)
data.parent_id =
reinterpret_cast<threads::thread_id_repr_type>(this->parent_id_);
data.parent_id = this->parent_id_;
data.parent_locality_id = this->parent_locality_;
#endif
applier::detail::apply_helper<typename base_type::derived_type>::call(
Expand Down
9 changes: 5 additions & 4 deletions hpx/runtime/threads/coroutines/coroutine.hpp
Expand Up @@ -37,6 +37,7 @@
#include <hpx/runtime/threads/coroutines/detail/coroutine_impl.hpp>
#include <hpx/runtime/threads/coroutines/detail/coroutine_self.hpp>
#include <hpx/runtime/threads/thread_enums.hpp>
#include <hpx/runtime/threads/thread_id_type.hpp>
#include <hpx/util/assert.hpp>

#include <cstddef>
Expand All @@ -54,7 +55,7 @@ namespace hpx { namespace threads { namespace coroutines

typedef detail::coroutine_impl impl_type;
typedef impl_type::pointer impl_ptr;
typedef impl_type::thread_id_repr_type thread_id_repr_type;
typedef impl_type::thread_id_type thread_id_type;

typedef impl_type::result_type result_type;
typedef impl_type::arg_type arg_type;
Expand All @@ -64,7 +65,7 @@ namespace hpx { namespace threads { namespace coroutines
coroutine() : m_pimpl(nullptr) {}

coroutine(functor_type&& f,
thread_id_repr_type id = nullptr,
thread_id_type id = nullptr,
std::ptrdiff_t stack_size = detail::default_stack_size)
: m_pimpl(impl_type::create(
std::move(f), id, stack_size))
Expand Down Expand Up @@ -95,7 +96,7 @@ namespace hpx { namespace threads { namespace coroutines
lhs.swap(rhs);
}

thread_id_repr_type get_thread_id() const
thread_id_type get_thread_id() const
{
return m_pimpl->get_thread_id();
}
Expand Down Expand Up @@ -124,7 +125,7 @@ namespace hpx { namespace threads { namespace coroutines
}
#endif

void rebind(functor_type&& f, thread_id_repr_type id = nullptr)
void rebind(functor_type&& f, thread_id_type id = nullptr)
{
HPX_ASSERT(exited());
impl_type::rebind(m_pimpl.get(), std::move(f), id);
Expand Down
5 changes: 0 additions & 5 deletions hpx/runtime/threads/coroutines/coroutine_fwd.hpp
Expand Up @@ -34,11 +34,6 @@

namespace hpx { namespace threads
{
class HPX_EXPORT thread_data;

HPX_EXPORT void intrusive_ptr_add_ref(thread_data* p);
HPX_EXPORT void intrusive_ptr_release(thread_data* p);

namespace coroutines
{
namespace detail
Expand Down
11 changes: 6 additions & 5 deletions hpx/runtime/threads/coroutines/detail/context_base.hpp
Expand Up @@ -46,6 +46,7 @@
#include <hpx/runtime/threads/coroutines/detail/swap_context.hpp> //for swap hints
#include <hpx/runtime/threads/coroutines/detail/tss.hpp>
#include <hpx/runtime/threads/coroutines/exception.hpp>
#include <hpx/runtime/threads/thread_id_type.hpp>
#include <hpx/util/assert.hpp>

#include <atomic>
Expand Down Expand Up @@ -92,10 +93,10 @@ namespace hpx { namespace threads { namespace coroutines { namespace detail
{
public:
typedef void deleter_type(context_base const*);
typedef void* thread_id_repr_type;
typedef hpx::threads::thread_id_type thread_id_type;

template <typename Derived>
context_base(Derived& derived, std::ptrdiff_t stack_size, thread_id_repr_type id)
context_base(Derived& derived, std::ptrdiff_t stack_size, thread_id_type id)
: default_context_impl(derived, stack_size),
m_caller(),
#if HPX_COROUTINE_IS_REFERENCE_COUNTED
Expand Down Expand Up @@ -238,7 +239,7 @@ namespace hpx { namespace threads { namespace coroutines { namespace detail
}
#endif

thread_id_repr_type get_thread_id() const
thread_id_type get_thread_id() const
{
return m_thread_id;
}
Expand Down Expand Up @@ -560,7 +561,7 @@ namespace hpx { namespace threads { namespace coroutines { namespace detail
ctx_exited_abnormally // process exited uncleanly.
};

void rebind_base(thread_id_repr_type id)
void rebind_base(thread_id_type id)
{
#if defined(HPX_HAVE_THREAD_OPERATIONS_COUNT)
HPX_ASSERT(exited() && 0 == m_wait_counter && !pending());
Expand Down Expand Up @@ -660,7 +661,7 @@ namespace hpx { namespace threads { namespace coroutines { namespace detail

// This is used to generate a meaningful exception trace.
std::exception_ptr m_type_info;
thread_id_repr_type m_thread_id;
thread_id_type m_thread_id;

std::size_t continuation_recursion_count_;
};
Expand Down
18 changes: 9 additions & 9 deletions hpx/runtime/threads/coroutines/detail/coroutine_impl.hpp
Expand Up @@ -40,10 +40,11 @@
#include <hpx/runtime/threads/coroutines/detail/context_base.hpp>
#include <hpx/runtime/threads/coroutines/detail/coroutine_accessor.hpp>
#include <hpx/runtime/threads/thread_enums.hpp>
#include <hpx/runtime/threads/thread_id_type.hpp>
#include <hpx/util/assert.hpp>
#include <hpx/util/unique_function.hpp>

#include <boost/intrusive_ptr.hpp>
#include <boost/smart_ptr/intrusive_ptr.hpp>

#include <cstddef>
#include <utility>
Expand All @@ -61,8 +62,7 @@ namespace hpx { namespace threads { namespace coroutines { namespace detail

public:
typedef context_base super_type;
typedef context_base::thread_id_repr_type thread_id_repr_type;
typedef boost::intrusive_ptr<threads::thread_data> thread_id_type;
typedef context_base::thread_id_type thread_id_type;

typedef std::pair<thread_state_enum, thread_id_type> result_type;
typedef thread_state_ex_enum arg_type;
Expand All @@ -71,7 +71,7 @@ namespace hpx { namespace threads { namespace coroutines { namespace detail

typedef boost::intrusive_ptr<coroutine_impl> pointer;

coroutine_impl(functor_type&& f, thread_id_repr_type id,
coroutine_impl(functor_type&& f, thread_id_type id,
std::ptrdiff_t stack_size)
: context_base(*this, stack_size, id)
, m_result_last(std::make_pair(thread_state_enum::unknown, nullptr))
Expand All @@ -88,14 +88,14 @@ namespace hpx { namespace threads { namespace coroutines { namespace detail
#endif

static inline coroutine_impl* create(
functor_type&& f, thread_id_repr_type id = nullptr,
functor_type&& f, thread_id_type id = nullptr,
std::ptrdiff_t stack_size = default_stack_size)
{
coroutine_impl* p = allocate(id, stack_size);

if (!p)
{
std::size_t const heap_num = std::size_t(id) / 32; //-V112
std::size_t const heap_num = std::size_t(id.get()) / 32; //-V112

// allocate a new coroutine object, if non is available (or all
// heaps are locked)
Expand All @@ -110,7 +110,7 @@ namespace hpx { namespace threads { namespace coroutines { namespace detail
}

static inline void rebind(
coroutine_impl* p, functor_type&& f, thread_id_repr_type id = nullptr)
coroutine_impl* p, functor_type&& f, thread_id_type id = nullptr)
{
p->rebind(std::move(f), id);
}
Expand Down Expand Up @@ -167,7 +167,7 @@ namespace hpx { namespace threads { namespace coroutines { namespace detail
this->super_type::reset();
}

void rebind(functor_type && f, thread_id_repr_type id)
void rebind(functor_type && f, thread_id_type id)
{
this->rebind_stack(); // count how often a coroutines object was reused
m_fun = std::move(f);
Expand All @@ -176,7 +176,7 @@ namespace hpx { namespace threads { namespace coroutines { namespace detail

private:
static HPX_EXPORT coroutine_impl* allocate(
thread_id_repr_type id, std::ptrdiff_t stacksize);
thread_id_type id, std::ptrdiff_t stacksize);

static HPX_EXPORT void deallocate(coroutine_impl* wrapper);

Expand Down
5 changes: 3 additions & 2 deletions hpx/runtime/threads/coroutines/detail/coroutine_self.hpp
Expand Up @@ -33,6 +33,7 @@
#include <hpx/runtime/threads/coroutines/detail/coroutine_accessor.hpp>
#include <hpx/runtime/threads/coroutines/detail/coroutine_impl.hpp>
#include <hpx/runtime/threads/thread_enums.hpp>
#include <hpx/runtime/threads/thread_id_type.hpp>
#include <hpx/util/assert.hpp>
#include <hpx/util/function.hpp>

Expand Down Expand Up @@ -71,7 +72,7 @@ namespace hpx { namespace threads { namespace coroutines { namespace detail

typedef coroutine_impl impl_type;
typedef impl_type* impl_ptr; // Note, no reference counting here.
typedef impl_type::thread_id_repr_type thread_id_repr_type;
typedef impl_type::thread_id_type thread_id_type;

typedef impl_type::result_type result_type;
typedef impl_type::arg_type arg_type;
Expand Down Expand Up @@ -140,7 +141,7 @@ namespace hpx { namespace threads { namespace coroutines { namespace detail
return m_pimpl->pending() != 0;
}

thread_id_repr_type get_thread_id() const
thread_id_type get_thread_id() const
{
HPX_ASSERT(m_pimpl);
return m_pimpl->get_thread_id();
Expand Down

0 comments on commit 79eeeb6

Please sign in to comment.