diff --git a/hpx/runtime/threads/policies/thread_queue.hpp b/hpx/runtime/threads/policies/thread_queue.hpp index 4f3e390a0905..b6f177c9d30e 100644 --- a/hpx/runtime/threads/policies/thread_queue.hpp +++ b/hpx/runtime/threads/policies/thread_queue.hpp @@ -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) { @@ -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) @@ -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); diff --git a/hpx/runtime/threads/thread_data.hpp b/hpx/runtime/threads/thread_data.hpp index 7898edd958aa..1f957efc3068 100644 --- a/hpx/runtime/threads/thread_data.hpp +++ b/hpx/runtime/threads/thread_data.hpp @@ -121,6 +121,13 @@ namespace hpx { namespace threads struct tag {}; typedef util::spinlock_pool 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)), @@ -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; @@ -662,7 +669,25 @@ namespace hpx { namespace threads thread_data_base* this_() { return this; } public: - typedef boost::lockfree::caching_freelist 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(p)); + } + + private: + boost::lockfree::caching_freelist pool_; + }; static boost::intrusive_ptr create( thread_init_data& init_data, pool_type& pool, @@ -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 diff --git a/src/runtime/threads/thread_data.cpp b/src/runtime/threads/thread_data.cpp index a21e248050ce..f758b1a35b46 100644 --- a/src/runtime/threads/thread_data.cpp +++ b/src/runtime/threads/thread_data.cpp @@ -36,16 +36,9 @@ namespace hpx { namespace threads { if (0 == --p->count_) { - if (thread_data* td = dynamic_cast(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); } }