2
0
mirror of https://github.com/boostorg/fiber.git synced 2026-02-19 02:12:24 +00:00

fix implementation of packaged_task::reset()

This commit is contained in:
Oliver Kowalke
2015-12-07 18:47:11 +01:00
parent 398cb6fd48
commit 228a676875
4 changed files with 40 additions and 7 deletions

View File

@@ -133,10 +133,6 @@ public:
return wait_until_( lk, timeout_time);
}
void reset() noexcept {
ready_ = false;
}
friend inline
void intrusive_ptr_add_ref( shared_state_base * p) noexcept {
++p->use_count_;

View File

@@ -28,6 +28,8 @@ struct task_base : public shared_state< R > {
}
virtual void run( Args && ... args) = 0;
virtual ptr_t reset() = 0;
};
}}}

View File

@@ -27,13 +27,16 @@ namespace detail {
template< typename Fn, typename Allocator, typename R, typename ... Args >
class task_object : public task_base< R, Args ... > {
private:
typedef task_base< R, Args ... > base_t;
public:
typedef typename std::allocator_traits< Allocator >::template rebind_alloc<
task_object
> allocator_t;
task_object( allocator_t const& alloc, Fn && fn) :
task_base< R, Args ... >(),
base_t(),
fn_( std::move( fn) ),
alloc_( alloc) {
}
@@ -48,6 +51,19 @@ public:
}
}
typename base_t::ptr_t reset() override final {
typedef std::allocator_traits< allocator_t > traits_t;
typename traits_t::pointer ptr{ traits_t::allocate( alloc_, 1) };
try {
traits_t::construct( alloc_, ptr, alloc_, std::move( fn_) );
} catch (...) {
traits_t::deallocate( alloc_, ptr, 1);
throw;
}
return { convert( ptr) };
}
protected:
void deallocate_future() noexcept override final {
destroy_( alloc_, this);
@@ -65,13 +81,16 @@ private:
template< typename Fn, typename Allocator, typename ... Args >
class task_object< Fn, Allocator, void, Args ... > : public task_base< void, Args ... > {
private:
typedef task_base< void, Args ... > base_t;
public:
typedef typename Allocator::template rebind<
task_object< Fn, Allocator, void, Args ... >
>::other allocator_t;
task_object( allocator_t const& alloc, Fn && fn) :
task_base< void, Args ... >(),
base_t(),
fn_( std::move( fn) ),
alloc_( alloc) {
}
@@ -86,6 +105,19 @@ public:
}
}
typename base_t::ptr_t reset() override final {
typedef std::allocator_traits< allocator_t > traits_t;
typename traits_t::pointer ptr{ traits_t::allocate( alloc_, 1) };
try {
traits_t::construct( alloc_, ptr, alloc_, std::move( fn_) );
} catch (...) {
traits_t::deallocate( alloc_, ptr, 1);
throw;
}
return { convert( ptr) };
}
protected:
void deallocate_future() noexcept override final {
destroy_( alloc_, this);

View File

@@ -118,7 +118,10 @@ public:
throw packaged_task_uninitialized{};
}
obtained_ = false;
task_->reset();
packaged_task tmp;
tmp.task_ = task_;
obtained_ = false;
task_ = tmp.task_->reset();
}
};