@@ -47,6 +47,9 @@ namespace tiledb::common {
47
47
48
48
class ThreadPool {
49
49
public:
50
+ /* *
51
+ * @brief Abstract base class for tasks that can run in this threadpool.
52
+ */
50
53
class ThreadPoolTask {
51
54
public:
52
55
ThreadPoolTask () = default ;
@@ -58,115 +61,215 @@ class ThreadPool {
58
61
friend class ThreadPool ;
59
62
60
63
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
+ */
61
69
virtual std::future_status wait_for (
62
70
const std::chrono::milliseconds timeout_duration) const = 0;
63
71
virtual bool valid () const noexcept = 0;
64
72
virtual Status get () = 0;
65
73
};
66
74
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
+ */
67
80
class Task : public ThreadPoolTask {
68
81
public:
82
+ /* *
83
+ * Default constructor
84
+ * @brief Default constructed SharedTask is possible but not valid().
85
+ */
69
86
Task () = default ;
70
87
71
- // Constructor, std::future can only be moved
88
+ /* *
89
+ * Constructor from std::future
90
+ */
72
91
Task (std::future<Status>&& f, ThreadPool* tp)
73
92
: ThreadPoolTask(tp)
74
93
, f_(std::move(f)){};
75
94
76
- // Move constructor
95
+ /* *
96
+ * Move constructor
97
+ */
77
98
Task (Task&& t) noexcept
78
99
: ThreadPoolTask(t.tp_)
79
100
, f_(std::move(t.f_)){};
80
101
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
+ */
82
114
Task (const Task&) = delete ;
115
+
116
+ /* *
117
+ * Disable copy assignment
118
+ */
83
119
Task& operator =(const Task&) = delete ;
84
120
121
+ /* *
122
+ * Wait in the threadpool for this task to be ready. Checks internally if a
123
+ * task is valid.
124
+ */
85
125
Status wait () {
86
126
return tp_->wait (this );
87
127
}
88
128
129
+ /* *
130
+ * Is this task valid. Wait can only be called on vaid tasks.
131
+ */
89
132
bool valid () const noexcept {
90
133
return f_.valid ();
91
134
}
92
135
93
136
private:
94
137
friend class ThreadPool ;
95
138
139
+ /* *
140
+ * Wait for input milliseconds for this task to be ready.
141
+ */
96
142
std::future_status wait_for (
97
143
const std::chrono::milliseconds timeout_duration) const {
98
144
return f_.wait_for (timeout_duration);
99
145
}
100
146
147
+ /* *
148
+ * Get the result of that task. Can only be used once. Only accessible from
149
+ * within the threadpool `wait` loop.
150
+ */
101
151
Status get () {
102
152
return f_.get ();
103
153
}
104
154
155
+ /* *
156
+ * The encapsulates std::shared_future
157
+ */
105
158
std::future<Status> f_;
106
159
};
107
160
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
+ */
108
166
class SharedTask : public ThreadPoolTask {
109
167
public:
168
+ /* *
169
+ * Default constructor
170
+ * @brief Default constructed SharedTask is possible but not valid().
171
+ */
110
172
SharedTask () = default ;
111
173
174
+ /* *
175
+ * Constructor from std::shared_future
176
+ */
112
177
SharedTask (std::shared_future<Status>&& f, ThreadPool* tp)
113
178
: ThreadPoolTask(tp)
114
179
, f_(std::move(f)){};
115
180
181
+ /* *
182
+ * Constructor from std::future
183
+ */
116
184
SharedTask (std::future<Status>&& f, ThreadPool* tp) noexcept
117
185
: ThreadPoolTask(tp)
118
186
, f_(std::move(f)){};
119
187
120
- SharedTask (Task&& t) noexcept
121
- : ThreadPoolTask(t.tp_)
122
- , f_(std::move(t.f_)){};
123
-
188
+ /* *
189
+ * Move constructor from a SharedTask
190
+ */
124
191
SharedTask (SharedTask&& t) noexcept
125
192
: ThreadPoolTask(t.tp_)
126
193
, f_(std::move(t.f_)){};
127
194
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 {
133
199
tp_ = t.tp_ ;
134
- f_ = t.f_ ;
200
+ f_ = std::move ( t.f_ ) ;
135
201
return *this ;
136
202
}
137
203
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 {
139
215
tp_ = t.tp_ ;
140
216
f_ = std::move (t.f_ );
141
217
return *this ;
142
218
}
143
219
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 {
145
231
tp_ = t.tp_ ;
146
- f_ = std::move ( t.f_ ) ;
232
+ f_ = t.f_ ;
147
233
return *this ;
148
234
}
149
235
236
+ /* *
237
+ * Wait in the threadpool for this task to be ready. Checks internally if a
238
+ * task is valid.
239
+ */
150
240
Status wait () {
151
241
return tp_->wait (this );
152
242
}
153
243
244
+ /* *
245
+ * Is this task valid. Wait can only be called on vaid tasks.
246
+ */
154
247
bool valid () const noexcept {
155
248
return f_.valid ();
156
249
}
157
250
158
251
private:
159
252
friend class ThreadPool ;
160
253
254
+ /* *
255
+ * Wait for input milliseconds for this task to be ready.
256
+ */
161
257
std::future_status wait_for (
162
258
const std::chrono::milliseconds timeout_duration) const {
163
259
return f_.wait_for (timeout_duration);
164
260
}
165
261
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
+ */
166
266
Status get () {
167
267
return f_.get ();
168
268
}
169
269
270
+ /* *
271
+ * The encapsulates std::shared_future
272
+ */
170
273
std::shared_future<Status> f_;
171
274
};
172
275
0 commit comments