Skip to content

Commit 77b52a2

Browse files
committed
Add some more documentation to new classes
1 parent 2d0a155 commit 77b52a2

File tree

1 file changed

+119
-16
lines changed

1 file changed

+119
-16
lines changed

tiledb/common/thread_pool/thread_pool.h

+119-16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ namespace tiledb::common {
4747

4848
class ThreadPool {
4949
public:
50+
/**
51+
* @brief Abstract base class for tasks that can run in this threadpool.
52+
*/
5053
class ThreadPoolTask {
5154
public:
5255
ThreadPoolTask() = default;
@@ -58,115 +61,215 @@ class ThreadPool {
5861
friend class ThreadPool;
5962

6063
ThreadPool* tp_;
64+
65+
/**
66+
* Pure virtual functions that tasks need to implement so that they can be
67+
* run in the threadpool wait loop
68+
*/
6169
virtual std::future_status wait_for(
6270
const std::chrono::milliseconds timeout_duration) const = 0;
6371
virtual bool valid() const noexcept = 0;
6472
virtual Status get() = 0;
6573
};
6674

75+
/**
76+
* @brief Task class encapsulating std::future. Like std::future it's shared
77+
* state can only be get once and thus only one thread. It can only be moved
78+
* and not copied.
79+
*/
6780
class Task : public ThreadPoolTask {
6881
public:
82+
/**
83+
* Default constructor
84+
* @brief Default constructed SharedTask is possible but not valid().
85+
*/
6986
Task() = default;
7087

71-
// Constructor, std::future can only be moved
88+
/**
89+
* Constructor from std::future
90+
*/
7291
Task(std::future<Status>&& f, ThreadPool* tp)
7392
: ThreadPoolTask(tp)
7493
, f_(std::move(f)){};
7594

76-
// Move constructor
95+
/**
96+
* Move constructor
97+
*/
7798
Task(Task&& t) noexcept
7899
: ThreadPoolTask(t.tp_)
79100
, f_(std::move(t.f_)){};
80101

81-
// Disable copying
102+
/**
103+
* Move assignenent from Task
104+
*/
105+
Task& operator=(Task&& t) noexcept {
106+
tp_ = t.tp_;
107+
f_ = std::move(t.f_);
108+
return *this;
109+
}
110+
111+
/**
112+
* Disable copy
113+
*/
82114
Task(const Task&) = delete;
115+
116+
/**
117+
* Disable copy assignment
118+
*/
83119
Task& operator=(const Task&) = delete;
84120

121+
/**
122+
* Wait in the threadpool for this task to be ready. Checks internally if a
123+
* task is valid.
124+
*/
85125
Status wait() {
86126
return tp_->wait(this);
87127
}
88128

129+
/**
130+
* Is this task valid. Wait can only be called on vaid tasks.
131+
*/
89132
bool valid() const noexcept {
90133
return f_.valid();
91134
}
92135

93136
private:
94137
friend class ThreadPool;
95138

139+
/**
140+
* Wait for input milliseconds for this task to be ready.
141+
*/
96142
std::future_status wait_for(
97143
const std::chrono::milliseconds timeout_duration) const {
98144
return f_.wait_for(timeout_duration);
99145
}
100146

147+
/**
148+
* Get the result of that task. Can only be used once. Only accessible from
149+
* within the threadpool `wait` loop.
150+
*/
101151
Status get() {
102152
return f_.get();
103153
}
104154

155+
/**
156+
* The encapsulates std::shared_future
157+
*/
105158
std::future<Status> f_;
106159
};
107160

161+
/**
162+
* @brief SharedTask class encapsulating std::shared_future. Like
163+
* std::shared_future multiple threads can wait/get on the shared state
164+
* multiple times. It can be both moved and copied.
165+
*/
108166
class SharedTask : public ThreadPoolTask {
109167
public:
168+
/**
169+
* Default constructor
170+
* @brief Default constructed SharedTask is possible but not valid().
171+
*/
110172
SharedTask() = default;
111173

174+
/**
175+
* Constructor from std::shared_future
176+
*/
112177
SharedTask(std::shared_future<Status>&& f, ThreadPool* tp)
113178
: ThreadPoolTask(tp)
114179
, f_(std::move(f)){};
115180

181+
/**
182+
* Constructor from std::future
183+
*/
116184
SharedTask(std::future<Status>&& f, ThreadPool* tp) noexcept
117185
: ThreadPoolTask(tp)
118186
, f_(std::move(f)){};
119187

120-
SharedTask(Task&& t) noexcept
121-
: ThreadPoolTask(t.tp_)
122-
, f_(std::move(t.f_)){};
123-
188+
/**
189+
* Move constructor from a SharedTask
190+
*/
124191
SharedTask(SharedTask&& t) noexcept
125192
: ThreadPoolTask(t.tp_)
126193
, f_(std::move(t.f_)){};
127194

128-
SharedTask(const SharedTask& t) noexcept
129-
: ThreadPoolTask(t.tp_)
130-
, f_(t.f_){};
131-
132-
SharedTask& operator=(const SharedTask& t) noexcept {
195+
/**
196+
* Move assignenent from SharedTask
197+
*/
198+
SharedTask& operator=(SharedTask&& t) noexcept {
133199
tp_ = t.tp_;
134-
f_ = t.f_;
200+
f_ = std::move(t.f_);
135201
return *this;
136202
}
137203

138-
SharedTask& operator=(SharedTask&& t) noexcept {
204+
/**
205+
* Move constructor from a Task
206+
*/
207+
SharedTask(Task&& t) noexcept
208+
: ThreadPoolTask(t.tp_)
209+
, f_(std::move(t.f_)){};
210+
211+
/**
212+
* Move assignenent from Task
213+
*/
214+
SharedTask& operator=(Task&& t) noexcept {
139215
tp_ = t.tp_;
140216
f_ = std::move(t.f_);
141217
return *this;
142218
}
143219

144-
SharedTask& operator=(Task&& t) noexcept {
220+
/**
221+
* Copy constructor
222+
*/
223+
SharedTask(const SharedTask& t) noexcept
224+
: ThreadPoolTask(t.tp_)
225+
, f_(t.f_){};
226+
227+
/**
228+
* Copy assignenent
229+
*/
230+
SharedTask& operator=(const SharedTask& t) noexcept {
145231
tp_ = t.tp_;
146-
f_ = std::move(t.f_);
232+
f_ = t.f_;
147233
return *this;
148234
}
149235

236+
/**
237+
* Wait in the threadpool for this task to be ready. Checks internally if a
238+
* task is valid.
239+
*/
150240
Status wait() {
151241
return tp_->wait(this);
152242
}
153243

244+
/**
245+
* Is this task valid. Wait can only be called on vaid tasks.
246+
*/
154247
bool valid() const noexcept {
155248
return f_.valid();
156249
}
157250

158251
private:
159252
friend class ThreadPool;
160253

254+
/**
255+
* Wait for input milliseconds for this task to be ready.
256+
*/
161257
std::future_status wait_for(
162258
const std::chrono::milliseconds timeout_duration) const {
163259
return f_.wait_for(timeout_duration);
164260
}
165261

262+
/**
263+
* Get the result of that task. Can be called multiple times from multiple
264+
* threads. Only accessible from within the threadpool `wait` loop.
265+
*/
166266
Status get() {
167267
return f_.get();
168268
}
169269

270+
/**
271+
* The encapsulates std::shared_future
272+
*/
170273
std::shared_future<Status> f_;
171274
};
172275

0 commit comments

Comments
 (0)