Skip to content

Commit f81c5d1

Browse files
committed
Fix support for exception when creating future from work
1 parent 89e3fc9 commit f81c5d1

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

include/task_future.hpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ class task_future_base {
8282
: state(state)
8383
{
8484
}
85+
task_future_base(private_construct, std::exception_ptr exception)
86+
: state(task_state::exception)
87+
, exception(exception)
88+
{
89+
}
8590

8691
task_future_base(const task_future_base&) = delete;
8792
task_future_base& operator=(const task_future_base&) = delete;
@@ -93,8 +98,9 @@ class task_future : public task_future_base, public std::enable_shared_from_this
9398
public:
9499
using value_type = T;
95100

96-
task_future(private_construct, task_state state)
97-
: task_future_base(private_construct{}, state)
101+
template<typename... Args>
102+
task_future(private_construct, Args&&... args)
103+
: task_future_base(private_construct{}, std::forward<Args>(args)...)
98104
, empty()
99105
{
100106
}
@@ -115,7 +121,12 @@ class task_future : public task_future_base, public std::enable_shared_from_this
115121
}
116122
template<typename F>
117123
static std::shared_ptr<task_future> create(F&& work) {
118-
return std::make_shared<task_future>(private_construct{}, std::move(work()));
124+
DISPATCH_QUEUE_TRY {
125+
return std::make_shared<task_future>(private_construct{}, std::move(work()));
126+
}
127+
DISPATCH_QUEUE_CATCH(...) {
128+
return std::make_shared<task_future>(private_construct{}, std::current_exception());
129+
}
119130
}
120131

121132
template<typename F>
@@ -189,8 +200,9 @@ class task_future<void> : public task_future_base, public std::enable_shared_fro
189200
public:
190201
using value_type = void;
191202

192-
task_future(private_construct, task_state state)
193-
: task_future_base(private_construct{}, state)
203+
template<typename... Args>
204+
task_future(private_construct, Args&&... args)
205+
: task_future_base(private_construct{}, std::forward<Args>(args)...)
194206
{
195207
}
196208

@@ -199,8 +211,13 @@ class task_future<void> : public task_future_base, public std::enable_shared_fro
199211
}
200212
template<typename F>
201213
static std::shared_ptr<task_future> create(F&& work) {
202-
work();
203-
return std::make_shared<task_future>(private_construct{}, task_state::ready);
214+
DISPATCH_QUEUE_TRY {
215+
work();
216+
return std::make_shared<task_future>(private_construct{}, task_state::ready);
217+
}
218+
DISPATCH_QUEUE_CATCH(...) {
219+
return std::make_shared<task_future>(private_construct{}, std::current_exception());
220+
}
204221
}
205222

206223
template<typename F>

0 commit comments

Comments
 (0)