Skip to content

Commit

Permalink
Tasks: Wait in destructor
Browse files Browse the repository at this point in the history
  • Loading branch information
ypatia committed Dec 18, 2024
1 parent 88c0ecb commit 619e781
Showing 1 changed file with 56 additions and 5 deletions.
61 changes: 56 additions & 5 deletions tiledb/common/thread_pool/thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class ThreadPool {
public:
ThreadPoolTask() = default;
ThreadPoolTask(ThreadPool* tp)
: tp_(tp){};
: tp_(tp) {};

virtual ~ThreadPoolTask(){};
virtual ~ThreadPoolTask() {};

protected:
friend class ThreadPool;
Expand Down Expand Up @@ -98,7 +98,29 @@ class ThreadPool {
*/
Task(std::future<Status>&& f, ThreadPool* tp)
: ThreadPoolTask(tp)
, f_(std::move(f)){};
, f_(std::move(f)) {};

/**
* Move is supported
*/
Task(Task&&) = default;
Task& operator=(Task&&) = default;

/**
* Disable copy and copy assignment
*/
Task(const Task&) = delete;
Task& operator=(const Task&) = delete;

~Task() {
if (f_.valid()) {
try {
f_.wait();
} catch (...) {
return;
}
}
}

/**
* Wait in the threadpool for this task to be ready.
Expand Down Expand Up @@ -158,19 +180,48 @@ class ThreadPool {
*/
SharedTask() = default;

/**
* Move constructor from a SharedTask
*/
SharedTask(SharedTask&& t) = default;
/**
* Move assignenent from SharedTask
*/
SharedTask& operator=(SharedTask&& t) = default;

/**
* Copy constructor
*/
SharedTask(const SharedTask& t) = default;

/**
* Copy assignenent
*/
SharedTask& operator=(const SharedTask& t) = default;

/**
* Constructor from std::future or std::shared_future
*/
SharedTask(auto&& f, ThreadPool* tp)
: ThreadPoolTask(tp)
, f_(std::forward<decltype(f)>(f)){};
, f_(std::forward<decltype(f)>(f)) {};

/**
* Move constructor from a Task
*/
SharedTask(Task&& t) noexcept
: ThreadPoolTask(t.tp_)
, f_(std::move(t.f_)){};
, f_(std::move(t.f_)) {};

~SharedTask() {
if (f_.valid()) {
try {
f_.wait();
} catch (...) {
return;
}
}
}

/**
* Wait in the threadpool for this task to be ready.
Expand Down

0 comments on commit 619e781

Please sign in to comment.