-
-
Notifications
You must be signed in to change notification settings - Fork 423
/
thread.cpp
519 lines (446 loc) · 15.7 KB
/
thread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Copyright (c) 2007-2022 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <hpx/assert.hpp>
#include <hpx/functional/bind.hpp>
#include <hpx/functional/bind_front.hpp>
#include <hpx/functional/move_only_function.hpp>
#include <hpx/futures/detail/future_data.hpp>
#include <hpx/futures/future.hpp>
#include <hpx/lock_registration/detail/register_locks.hpp>
#include <hpx/modules/errors.hpp>
#include <hpx/modules/memory.hpp>
#include <hpx/modules/threading.hpp>
#include <hpx/thread_support/unlock_guard.hpp>
#include <hpx/threading_base/thread_helpers.hpp>
#include <hpx/threading_base/thread_init_data.hpp>
#include <hpx/threading_base/thread_pool_base.hpp>
#include <hpx/timing/steady_clock.hpp>
#include <cstddef>
#include <exception>
#include <functional>
#include <mutex>
#include <utility>
#if defined(__ANDROID__) || defined(ANDROID)
#include <cpu-features.h>
#endif
namespace hpx {
namespace detail {
static thread_termination_handler_type thread_termination_handler;
}
void set_thread_termination_handler(thread_termination_handler_type f)
{
detail::thread_termination_handler = HPX_MOVE(f);
}
thread::thread() noexcept
: id_(hpx::threads::invalid_thread_id)
{
}
thread::thread(thread&& rhs) noexcept
{
std::lock_guard l(rhs.mtx_);
id_ = rhs.id_;
rhs.id_ = threads::invalid_thread_id;
}
thread& thread::operator=(thread&& rhs) noexcept
{
std::unique_lock l(mtx_);
std::unique_lock l2(rhs.mtx_);
if (joinable_locked())
{
l2.unlock();
l.unlock();
HPX_THROW_EXCEPTION(hpx::error::invalid_status,
"thread::operator=", "destroying running thread");
}
id_ = rhs.id_;
rhs.id_ = threads::invalid_thread_id;
return *this;
}
thread::~thread()
{
if (joinable())
{
if (detail::thread_termination_handler)
{
try
{
HPX_THROW_EXCEPTION(hpx::error::invalid_status,
"thread::~thread", "destroying running thread");
}
catch (...)
{
detail::thread_termination_handler(
std::current_exception());
}
}
else
{
std::terminate();
}
}
HPX_ASSERT(id_ == threads::invalid_thread_id);
}
void thread::swap(thread& rhs) noexcept
{
std::lock_guard l(mtx_);
std::lock_guard l2(rhs.mtx_);
std::swap(id_, rhs.id_);
}
static void run_thread_exit_callbacks()
{
threads::thread_id_type id = threads::get_self_id();
if (id == threads::invalid_thread_id)
{
HPX_THROW_EXCEPTION(hpx::error::null_thread_id,
"run_thread_exit_callbacks", "null thread id encountered");
}
threads::run_thread_exit_callbacks(id);
threads::free_thread_exit_callbacks(id);
}
threads::thread_result_type thread::thread_function_nullary(
hpx::move_only_function<void()> const& func)
{
try
{
// Now notify our calling thread that we started execution.
func();
}
catch (hpx::thread_interrupted const&)
{ //-V565
/* swallow this exception */
}
catch (hpx::exception const&)
{
// Verify that there are no more registered locks for this
// OS-thread. This will throw if there are still any locks held.
util::force_error_on_lock();
// run all callbacks attached to the exit event for this thread
run_thread_exit_callbacks();
throw; // rethrow any exception except 'thread_interrupted'
}
// Verify that there are no more registered locks for this OS-thread.
// This will throw if there are still any locks held.
util::force_error_on_lock();
// run all callbacks attached to the exit event for this thread
run_thread_exit_callbacks();
return threads::thread_result_type(
threads::thread_schedule_state::terminated,
threads::invalid_thread_id);
}
thread::id thread::get_id() const noexcept
{
return id(native_handle());
}
unsigned int thread::hardware_concurrency() noexcept
{
return hpx::threads::hardware_concurrency();
}
void thread::start_thread(
threads::thread_pool_base* pool, hpx::move_only_function<void()>&& func)
{
HPX_ASSERT(pool);
threads::thread_init_data data(
util::one_shot(
hpx::bind(&thread::thread_function_nullary, HPX_MOVE(func))),
"thread::thread_function_nullary",
threads::thread_priority::default_, threads::thread_schedule_hint(),
threads::thread_stacksize::default_,
threads::thread_schedule_state::pending, true);
// create the new thread, note that id_ is guaranteed to be valid before
// the thread function is executed
error_code ec(throwmode::lightweight);
pool->create_thread(data, id_, ec);
if (ec)
{
HPX_THROW_EXCEPTION(hpx::error::thread_resource_error,
"thread::start_thread", "Could not create thread");
return;
}
}
static void resume_thread(threads::thread_id_ref_type const& id)
{
threads::set_thread_state(
id.noref(), threads::thread_schedule_state::pending);
}
void thread::join()
{
std::unique_lock l(mtx_);
if (!joinable_locked())
{
l.unlock();
HPX_THROW_EXCEPTION(hpx::error::invalid_status, "thread::join",
"trying to join a non joinable thread");
}
// keep ourselves alive while being suspended
threads::thread_id_ref_type this_id = threads::get_self_id();
if (this_id == id_)
{
l.unlock();
HPX_THROW_EXCEPTION(hpx::error::thread_resource_error,
"thread::join", "hpx::thread: trying joining itself");
return;
}
this_thread::interruption_point();
// register callback function to be called when thread exits
if (threads::add_thread_exit_callback(id_.noref(),
hpx::bind_front(&resume_thread, HPX_MOVE(this_id))))
{
// wait for thread to be terminated
unlock_guard ul(l);
this_thread::suspend(
threads::thread_schedule_state::suspended, "thread::join");
}
detach_locked(); // invalidate this object
}
// extensions
void thread::interrupt(bool flag)
{
threads::interrupt_thread(native_handle(), flag);
}
bool thread::interruption_requested() const
{
return threads::get_thread_interruption_requested(native_handle());
}
void thread::interrupt(thread::id id, bool flag)
{
threads::interrupt_thread(id.id_, flag);
}
std::size_t thread::get_thread_data() const
{
return threads::get_thread_data(native_handle());
}
std::size_t thread::set_thread_data(std::size_t data)
{
return threads::set_thread_data(native_handle(), data);
}
#if defined(HPX_HAVE_LIBCDS)
std::size_t thread::get_libcds_data() const
{
return threads::get_libcds_data(native_handle());
}
std::size_t thread::set_libcds_data(std::size_t data)
{
return threads::set_libcds_data(native_handle(), data);
}
std::size_t thread::get_libcds_hazard_pointer_data() const
{
return threads::get_libcds_hazard_pointer_data(native_handle());
}
std::size_t thread::set_libcds_hazard_pointer_data(std::size_t data)
{
return threads::set_libcds_hazard_pointer_data(native_handle(), data);
}
std::size_t thread::get_libcds_dynamic_hazard_pointer_data() const
{
return threads::get_libcds_dynamic_hazard_pointer_data(native_handle());
}
std::size_t thread::set_libcds_dynamic_hazard_pointer_data(std::size_t data)
{
return threads::set_libcds_dynamic_hazard_pointer_data(
native_handle(), data);
}
#endif
///////////////////////////////////////////////////////////////////////////
namespace detail {
struct thread_task_base : lcos::detail::future_data<void>
{
private:
using future_base_type = hpx::intrusive_ptr<thread_task_base>;
protected:
using base_type = lcos::detail::future_data<void>;
using result_type = base_type::result_type;
using base_type::mtx_;
public:
thread_task_base(threads::thread_id_ref_type const& id)
{
if (threads::add_thread_exit_callback(id.noref(),
hpx::bind_front(&thread_task_base::thread_exit_function,
future_base_type(this))))
{
id_ = id;
}
}
bool valid() const noexcept
{
return id_ != threads::invalid_thread_id;
}
// cancellation support
bool cancelable() const noexcept override
{
return true;
}
void cancel() override
{
std::lock_guard l(mtx_);
if (!this->is_ready())
{
threads::interrupt_thread(id_.noref());
this->set_error(hpx::error::thread_cancelled,
"thread_task_base::cancel", "future has been canceled");
id_ = threads::invalid_thread_id;
}
}
protected:
void thread_exit_function()
{
// might have been finished or canceled
std::lock_guard l(mtx_);
if (!this->is_ready())
this->set_data(result_type());
id_ = threads::invalid_thread_id;
}
private:
threads::thread_id_ref_type id_;
};
} // namespace detail
hpx::future<void> thread::get_future(error_code& ec)
{
if (id_ == threads::invalid_thread_id)
{
HPX_THROWS_IF(ec, hpx::error::null_thread_id, "thread::get_future",
"null thread id encountered");
return hpx::future<void>();
}
detail::thread_task_base* p = new detail::thread_task_base(id_);
hpx::intrusive_ptr<lcos::detail::future_data<void>> base(p);
if (!p->valid())
{
HPX_THROWS_IF(ec, hpx::error::thread_resource_error,
"thread::get_future",
"Could not create future as thread has been terminated.");
return hpx::future<void>();
}
using traits::future_access;
return future_access<hpx::future<void>>::create(HPX_MOVE(base));
}
///////////////////////////////////////////////////////////////////////////
namespace this_thread {
void yield_to(thread::id id) noexcept
{
this_thread::suspend(threads::thread_schedule_state::pending,
id.native_handle(), "this_thread::yield_to");
}
void yield() noexcept
{
this_thread::suspend(
threads::thread_schedule_state::pending, "this_thread::yield");
}
thread::id get_id() noexcept
{
return thread::id(threads::get_self_id());
}
// extensions
threads::thread_priority get_priority() noexcept
{
return threads::get_thread_priority(threads::get_self_id());
}
std::ptrdiff_t get_stack_size() noexcept
{
return threads::get_stack_size(threads::get_self_id());
}
void interruption_point()
{
threads::interruption_point(threads::get_self_id());
}
bool interruption_enabled()
{
return threads::get_thread_interruption_enabled(
threads::get_self_id());
}
bool interruption_requested()
{
return threads::get_thread_interruption_requested(
threads::get_self_id());
}
void interrupt()
{
threads::interrupt_thread(threads::get_self_id());
threads::interruption_point(threads::get_self_id());
}
void sleep_until(hpx::chrono::steady_time_point const& abs_time)
{
this_thread::suspend(abs_time, "this_thread::sleep_until");
}
std::size_t get_thread_data()
{
return threads::get_thread_data(threads::get_self_id());
}
std::size_t set_thread_data(std::size_t data)
{
return threads::set_thread_data(threads::get_self_id(), data);
}
#if defined(HPX_HAVE_LIBCDS)
std::size_t get_libcds_data()
{
return threads::get_libcds_data(threads::get_self_id());
}
std::size_t set_libcds_data(std::size_t data)
{
return threads::set_libcds_data(threads::get_self_id(), data);
}
std::size_t get_libcds_hazard_pointer_data()
{
return threads::get_libcds_hazard_pointer_data(
threads::get_self_id());
}
std::size_t set_libcds_hazard_pointer_data(std::size_t data)
{
return threads::set_libcds_hazard_pointer_data(
threads::get_self_id(), data);
}
std::size_t get_libcds_dynamic_hazard_pointer_data()
{
return threads::get_libcds_dynamic_hazard_pointer_data(
threads::get_self_id());
}
std::size_t set_libcds_dynamic_hazard_pointer_data(std::size_t data)
{
return threads::set_libcds_dynamic_hazard_pointer_data(
threads::get_self_id(), data);
}
#endif
///////////////////////////////////////////////////////////////////////
disable_interruption::disable_interruption()
: interruption_was_enabled_(interruption_enabled())
{
if (interruption_was_enabled_)
{
interruption_was_enabled_ =
threads::set_thread_interruption_enabled(
threads::get_self_id(), false);
}
}
disable_interruption::~disable_interruption()
{
threads::thread_self* p = threads::get_self_ptr();
if (p)
{
threads::set_thread_interruption_enabled(
threads::get_self_id(), interruption_was_enabled_);
}
}
///////////////////////////////////////////////////////////////////////
restore_interruption::restore_interruption(disable_interruption& d)
: interruption_was_enabled_(d.interruption_was_enabled_)
{
if (!interruption_was_enabled_)
{
interruption_was_enabled_ =
threads::set_thread_interruption_enabled(
threads::get_self_id(), true);
}
}
restore_interruption::~restore_interruption()
{
threads::thread_self* p = threads::get_self_ptr();
if (p)
{
threads::set_thread_interruption_enabled(
threads::get_self_id(), interruption_was_enabled_);
}
}
} // namespace this_thread
} // namespace hpx