Skip to content

Commit

Permalink
Removing dynamic_cast from thread_data deallocation implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonBikineev committed Sep 28, 2015
1 parent 973de3f commit f9c47b7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
6 changes: 3 additions & 3 deletions hpx/runtime/threads/policies/thread_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ namespace hpx { namespace threads { namespace policies

// this thread has to be in the map now
HPX_ASSERT(thread_map_.find(thrd.get()) != thread_map_.end());
HPX_ASSERT(thrd->is_created_from(&memory_pool_));
HPX_ASSERT(thrd->get_pool() == &memory_pool_);
}

if (added) {
Expand Down Expand Up @@ -727,7 +727,7 @@ namespace hpx { namespace threads { namespace policies

// this thread has to be in the map now
HPX_ASSERT(thread_map_.find(thrd.get()) != thread_map_.end());
HPX_ASSERT(thrd->is_created_from(&memory_pool_));
HPX_ASSERT(thrd->get_pool() == &memory_pool_);

// push the new thread in the pending queue thread
if (initial_state == pending)
Expand Down Expand Up @@ -858,7 +858,7 @@ namespace hpx { namespace threads { namespace policies
/// Destroy the passed thread as it has been terminated
bool destroy_thread(threads::thread_data_base* thrd, boost::int64_t& busy_count)
{
if (thrd->is_created_from(&memory_pool_))
if (thrd->get_pool() == &memory_pool_)
{
terminated_items_.push(thrd);

Expand Down
33 changes: 29 additions & 4 deletions hpx/runtime/threads/thread_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ namespace hpx { namespace threads
struct tag {};
typedef util::spinlock_pool<tag> mutex_type;

struct pool_base
{
virtual ~pool_base() {}
virtual thread_data_base* allocate() = 0;
virtual void deallocate(thread_data_base*) = 0;
};

/// Construct a new \a thread
thread_data_base(thread_init_data& init_data, thread_state_enum newstate)
: current_state_(thread_state(newstate)),
Expand Down Expand Up @@ -584,7 +591,7 @@ namespace hpx { namespace threads
return stacksize_;
}

virtual bool is_created_from(void* pool) const = 0;
virtual pool_base* get_pool() = 0;
virtual thread_state_enum operator()() = 0;
virtual thread_id_type get_thread_id() const = 0;
virtual std::size_t get_thread_phase() const = 0;
Expand Down Expand Up @@ -662,7 +669,25 @@ namespace hpx { namespace threads
thread_data_base* this_() { return this; }

public:
typedef boost::lockfree::caching_freelist<thread_data> pool_type;
struct pool_type: thread_data_base::pool_base
{
pool_type(std::size_t size)
: pool_(size)
{}

virtual thread_data* allocate()
{
return pool_.allocate();
}

virtual void deallocate(thread_data_base* p)
{
pool_.deallocate(static_cast<thread_data*>(p));
}

private:
boost::lockfree::caching_freelist<thread_data> pool_;
};

static boost::intrusive_ptr<thread_data> create(
thread_init_data& init_data, pool_type& pool,
Expand Down Expand Up @@ -704,9 +729,9 @@ namespace hpx { namespace threads
HPX_ASSERT(coroutine_.is_ready());
}

bool is_created_from(void* pool) const
virtual pool_base* get_pool()
{
return pool_ == pool;
return pool_;
}

/// \brief Execute the thread function
Expand Down
13 changes: 3 additions & 10 deletions src/runtime/threads/thread_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,9 @@ namespace hpx { namespace threads
{
if (0 == --p->count_)
{
if (thread_data* td = dynamic_cast<thread_data*>(p))
{
thread_pool* pool = td->pool_;
td->~thread_data();
pool->deallocate(td);
}
else
{
delete p;
}
thread_data_base::pool_base* pool = p->get_pool();
p->~thread_data_base();
pool->deallocate(p);
}
}

Expand Down

0 comments on commit f9c47b7

Please sign in to comment.