• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.46
/libs/core/threading_base/include/hpx/threading_base/thread_helpers.hpp
1
//  Copyright (c) 2007-2025 Hartmut Kaiser
2
//  Copyright (c)      2018 Thomas Heller
3
//  Copyright (c)      2011 Bryce Lelbach
4
//  Copyright (c) 2008-2009 Chirag Dekate, Anshul Tandon
5
//
6
//  SPDX-License-Identifier: BSL-1.0
7
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
8
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9

10
#pragma once
11

12
#include <hpx/config.hpp>
13
#include <hpx/modules/coroutines.hpp>
14
#include <hpx/modules/errors.hpp>
15
#include <hpx/modules/lock_registration.hpp>
16
#include <hpx/modules/timing.hpp>
17
#include <hpx/threading_base/register_thread.hpp>
18
#include <hpx/threading_base/thread_description.hpp>
19
#include <hpx/threading_base/thread_pool_base.hpp>
20
#if !defined(HPX_HAVE_THREAD_FULLBACKTRACE_ON_SUSPENSION)
21
#include <hpx/modules/debugging.hpp>
22
#endif
23

24
#include <atomic>
25
#include <chrono>
26
#include <cstddef>
27
#include <cstdint>
28

29
#include <hpx/config/warnings_prefix.hpp>
30

31
///////////////////////////////////////////////////////////////////////////////
32
namespace hpx::threads {
33

34
    ///////////////////////////////////////////////////////////////////////////
35
    /// \brief  Set the thread state of the \a thread referenced by the
36
    ///         thread_id \a id.
37
    ///
38
    /// \param id         [in] The thread id of the thread the state should
39
    ///                   be modified for.
40
    /// \param state      [in] The new state to be set for the thread
41
    ///                   referenced by the \a id parameter.
42
    /// \param stateex    [in] The new extended state to be set for the
43
    ///                   thread referenced by the \a id parameter.
44
    /// \param priority   [in]
45
    /// \param retry_on_active [in]
46
    ///
47
    /// \param ec         [in,out] this represents the error status on exit,
48
    ///                   if this is pre-initialized to \a hpx#throws
49
    ///                   the function will throw on error instead.
50
    ///
51
    /// \note             If the thread referenced by the parameter \a id
52
    ///                   is in \a thread_state#active state this function
53
    ///                   schedules a new thread which will set the state of
54
    ///                   the thread as soon as it is not active anymore. The
55
    ///                   function returns \a thread_state#active in this case.
56
    ///
57
    /// \returns          This function returns the previous state of the
58
    ///                   thread referenced by the \a id parameter. It will
59
    ///                   return one of the values as defined by the
60
    ///                   \a thread_state enumeration. If the
61
    ///                   thread is not known to the thread-manager the
62
    ///                   return value will be \a thread_state#unknown.
63
    ///
64
    /// \note             As long as \a ec is not pre-initialized to
65
    ///                   \a hpx#throws this function doesn't
66
    ///                   throw but returns the result code using the
67
    ///                   parameter \a ec. Otherwise, it throws an instance
68
    ///                   of hpx#exception.
69
    HPX_CXX_EXPORT HPX_CORE_EXPORT thread_state set_thread_state(
70
        thread_id_type const& id,
71
        thread_schedule_state state = thread_schedule_state::pending,
72
        thread_restart_state stateex = thread_restart_state::signaled,
73
        thread_priority priority = thread_priority::normal,
74
        bool retry_on_active = true, hpx::error_code& ec = hpx::throws);
75

76
    ///////////////////////////////////////////////////////////////////////
77
    /// \brief  Set the thread state of the \a thread referenced by the
78
    ///         thread_id \a id.
79
    ///
80
    /// Set a timer to set the state of the given \a thread to the given
81
    /// new value after it expired (at the given time)
82
    ///
83
    /// \param id         [in] The thread id of the thread the state should
84
    ///                   be modified for.
85
    /// \param abs_time   [in] Absolute point in time for the new thread to be
86
    ///                   run
87
    /// \param started    [in,out] A helper variable allowing to track the
88
    ///                   state of the timer helper thread
89
    /// \param state      [in] The new state to be set for the thread
90
    ///                   referenced by the \a id parameter.
91
    /// \param stateex    [in] The new extended state to be set for the
92
    ///                   thread referenced by the \a id parameter.
93
    /// \param priority        [in]
94
    /// \param retry_on_active [in]
95
    /// \param ec         [in,out] this represents the error status on exit,
96
    ///                   if this is pre-initialized to \a hpx#throws
97
    ///                   the function will throw on error instead.
98
    ///
99
    /// \returns
100
    ///
101
    /// \note             As long as \a ec is not pre-initialized to
102
    ///                   \a hpx#throws this function doesn't
103
    ///                   throw but returns the result code using the
104
    ///                   parameter \a ec. Otherwise, it throws an instance
105
    ///                   of hpx#exception.
106
    HPX_CXX_EXPORT HPX_CORE_EXPORT thread_id_ref_type set_thread_state(
107
        thread_id_type const& id,
108
        hpx::chrono::steady_time_point const& abs_time,
109
        std::atomic<bool>* started,
110
        thread_schedule_state state = thread_schedule_state::pending,
111
        thread_restart_state stateex = thread_restart_state::timeout,
112
        thread_priority priority = thread_priority::normal,
113
        bool retry_on_active = true, hpx::error_code& ec = hpx::throws);
114

115
    HPX_CXX_EXPORT inline thread_id_ref_type set_thread_state(
116
        thread_id_type const& id,
117
        hpx::chrono::steady_time_point const& abs_time,
118
        thread_schedule_state state = thread_schedule_state::pending,
119
        thread_restart_state stateex = thread_restart_state::timeout,
120
        thread_priority priority = thread_priority::normal,
12✔
121
        bool retry_on_active = true, hpx::error_code& /*ec*/ = throws)
122
    {
123
        return set_thread_state(id, abs_time, nullptr, state, stateex, priority,
124
            retry_on_active, throws);
125
    }
126

127
    ///////////////////////////////////////////////////////////////////////////
128
    /// \brief  Set the thread state of the \a thread referenced by the
129
    ///         thread_id \a id.
130
    ///
131
    /// Set a timer to set the state of the given \a thread to the given
132
    /// new value after it expired (after the given duration)
133
    ///
134
    /// \param id         [in] The thread id of the thread the state should
135
    ///                   be modified for.
136
    /// \param rel_time   [in] Time duration after which the new thread should
137
    ///                   be run
138
    /// \param state      [in] The new state to be set for the thread
139
    ///                   referenced by the \a id parameter.
140
    /// \param stateex    [in] The new extended state to be set for the
141
    ///                   thread referenced by the \a id parameter.
142
    /// \param priority        [in]
143
    /// \param retry_on_active [in]
144
    /// \param ec         [in,out] this represents the error status on exit,
145
    ///                   if this is pre-initialized to \a hpx#throws
146
    ///                   the function will throw on error instead.
147
    ///
148
    /// \returns
149
    ///
150
    /// \note             As long as \a ec is not pre-initialized to
151
    ///                   \a hpx#throws this function doesn't
10✔
152
    ///                   throw but returns the result code using the
153
    ///                   parameter \a ec. Otherwise, it throws an instance
154
    ///                   of hpx#exception.
155
    HPX_CXX_EXPORT inline thread_id_ref_type set_thread_state(
156
        thread_id_type const& id, hpx::chrono::steady_duration const& rel_time,
157
        thread_schedule_state state = thread_schedule_state::pending,
158
        thread_restart_state stateex = thread_restart_state::timeout,
159
        thread_priority priority = thread_priority::normal,
10✔
160
        bool retry_on_active = true, hpx::error_code& ec = hpx::throws)
161
    {
162
        return set_thread_state(id, rel_time.from_now(), state, stateex,
163
            priority, retry_on_active, ec);
164
    }
165

166
    ///////////////////////////////////////////////////////////////////////////
167
    /// The function get_thread_backtrace is part of the thread related API
168
    /// allows to query the currently stored thread back trace (which is
169
    /// captured during thread suspension).
170
    ///
171
    /// \param id         [in] The thread id of the thread being queried.
172
    /// \param ec         [in,out] this represents the error status on exit,
173
    ///                   if this is pre-initialized to \a hpx#throws
174
    ///                   the function will throw on error instead.
175
    ///
176
    /// \returns          This function returns the currently captured stack
177
    ///                   back trace of the thread referenced by the \a id
178
    ///                   parameter. If the thread is not known to the
179
    ///                   thread-manager the return value will be the zero.
180
    ///
181
    /// \note             As long as \a ec is not pre-initialized to
182
    ///                   \a hpx#throws this function doesn't
183
    ///                   throw but returns the result code using the
184
    ///                   parameter \a ec. Otherwise, it throws an instance
185
    ///                   of hpx#exception.
186
#ifdef HPX_HAVE_THREAD_FULLBACKTRACE_ON_SUSPENSION
187
    HPX_CXX_EXPORT HPX_CORE_EXPORT char const* get_thread_backtrace(
188
        thread_id_type const& id, hpx::hpx::error_code& ec = hpx::throws);
189
    HPX_CXX_EXPORT HPX_CORE_EXPORT char const* set_thread_backtrace(
190
        thread_id_type const& id, char const* bt = nullptr,
191
        hpx::error_code& ec = hpx::throws);
192
#else
193
#if !defined(DOXYGEN)
194
    HPX_CXX_EXPORT HPX_CORE_EXPORT util::backtrace const* get_thread_backtrace(
195
        thread_id_type const& id, hpx::error_code& ec = hpx::throws);
196
    HPX_CXX_EXPORT HPX_CORE_EXPORT util::backtrace const* set_thread_backtrace(
197
        thread_id_type const& id, util::backtrace const* bt = nullptr,
198
        hpx::error_code& ec = hpx::throws);
199
#endif
200
#endif
201

202
    ///////////////////////////////////////////////////////////////////////////
203
    /// The function get_thread_state is part of the thread related API. It
204
    /// queries the state of one of the threads known to the thread-manager.
205
    ///
206
    /// \param id         [in] The thread id of the thread the state should
207
    ///                   be modified for.
208
    /// \param ec         [in,out] this represents the error status on exit,
209
    ///                   if this is pre-initialized to \a hpx#throws
210
    ///                   the function will throw on error instead.
211
    ///
212
    /// \returns          This function returns the thread state of the
213
    ///                   thread referenced by the \a id parameter. If the
214
    ///                   thread is not known to the thread-manager the return
215
    ///                   value will be \a terminated.
216
    ///
217
    /// \note             As long as \a ec is not pre-initialized to
218
    ///                   \a hpx#throws this function doesn't
219
    ///                   throw but returns the result code using the
220
    ///                   parameter \a ec. Otherwise, it throws an instance
221
    ///                   of hpx#exception.
222
    HPX_CXX_EXPORT HPX_CORE_EXPORT thread_state get_thread_state(
223
        thread_id_type const& id, hpx::error_code& ec = hpx::throws) noexcept;
224

225
    ///////////////////////////////////////////////////////////////////////////
226
    /// The function get_thread_phase is part of the thread related API.
227
    /// It queries the phase of one of the threads known to the thread-manager.
228
    ///
229
    /// \param id         [in] The thread id of the thread the phase should
230
    ///                   be modified for.
231
    /// \param ec         [in,out] this represents the error status on exit,
232
    ///                   if this is pre-initialized to \a hpx#throws
233
    ///                   the function will throw on error instead.
234
    ///
235
    /// \returns          This function returns the thread phase of the
236
    ///                   thread referenced by the \a id parameter. If the
237
    ///                   thread is not known to the thread-manager the return
238
    ///                   value will be ~0.
239
    ///
240
    /// \note             As long as \a ec is not pre-initialized to
241
    ///                   \a hpx#throws this function doesn't
242
    ///                   throw but returns the result code using the
243
    ///                   parameter \a ec. Otherwise, it throws an instance
244
    ///                   of hpx#exception.
245
    HPX_CXX_EXPORT HPX_CORE_EXPORT std::size_t get_thread_phase(
246
        thread_id_type const& id, hpx::error_code& ec = hpx::throws) noexcept;
247

248
    ///////////////////////////////////////////////////////////////////////////
249
    /// Returns whether the given thread can be interrupted at this point.
250
    ///
251
    /// \param id         [in] The thread id of the thread which should be
252
    ///                   queried.
253
    /// \param ec         [in,out] this represents the error status on exit,
254
    ///                   if this is pre-initialized to \a hpx#throws
255
    ///                   the function will throw on error instead.
256
    ///
257
    /// \returns          This function returns \a true if the given thread
258
    ///                   can be interrupted at this point in time. It will
259
    ///                   return \a false otherwise.
260
    ///
261
    /// \note             As long as \a ec is not pre-initialized to
262
    ///                   \a hpx#throws this function doesn't
263
    ///                   throw but returns the result code using the
264
    ///                   parameter \a ec. Otherwise, it throws an instance
265
    ///                   of hpx#exception.
266
    HPX_CXX_EXPORT HPX_CORE_EXPORT bool get_thread_interruption_enabled(
267
        thread_id_type const& id, hpx::error_code& ec = hpx::throws);
268

269
    /// Set whether the given thread can be interrupted at this point.
270
    ///
271
    /// \param id         [in] The thread id of the thread which should
272
    ///                   receive the new value.
273
    /// \param enable     [in] This value will determine the new interruption
274
    ///                   enabled status for the given thread.
275
    /// \param ec         [in,out] this represents the error status on exit,
276
    ///                   if this is pre-initialized to \a hpx#throws
277
    ///                   the function will throw on error instead.
278
    ///
279
    /// \returns          This function returns the previous value of
280
    ///                   whether the given thread could have been interrupted.
281
    ///
282
    /// \note             As long as \a ec is not pre-initialized to
283
    ///                   \a hpx#throws this function doesn't
284
    ///                   throw but returns the result code using the
285
    ///                   parameter \a ec. Otherwise, it throws an instance
286
    ///                   of hpx#exception.
287
    HPX_CXX_EXPORT HPX_CORE_EXPORT bool set_thread_interruption_enabled(
288
        thread_id_type const& id, bool enable,
289
        hpx::error_code& ec = hpx::throws);
290

291
    /// Returns whether the given thread has been flagged for interruption.
292
    ///
293
    /// \param id         [in] The thread id of the thread which should be
294
    ///                   queried.
295
    /// \param ec         [in,out] this represents the error status on exit,
296
    ///                   if this is pre-initialized to \a hpx#throws
297
    ///                   the function will throw on error instead.
298
    ///
299
    /// \returns          This function returns \a true if the given thread
300
    ///                   was flagged for interruption. It will return
301
    ///                   \a false otherwise.
302
    ///
303
    /// \note             As long as \a ec is not pre-initialized to
304
    ///                   \a hpx#throws this function doesn't
305
    ///                   throw but returns the result code using the
306
    ///                   parameter \a ec. Otherwise, it throws an instance
307
    ///                   of hpx#exception.
308
    HPX_CXX_EXPORT HPX_CORE_EXPORT bool get_thread_interruption_requested(
309
        thread_id_type const& id, hpx::error_code& ec = hpx::throws);
310

311
    /// Flag the given thread for interruption.
312
    ///
313
    /// \param id         [in] The thread id of the thread which should be
314
    ///                   interrupted.
315
    /// \param flag       [in] The flag encodes whether the thread should be
316
    ///                   interrupted (if it is \a true), or 'uninterrupted'
317
    ///                   (if it is \a false).
318
    /// \param ec         [in,out] this represents the error status on exit,
319
    ///                   if this is pre-initialized to \a hpx#throws
320
    ///                   the function will throw on error instead.
321
    ///
322
    /// \note             As long as \a ec is not pre-initialized to
323
    ///                   \a hpx#throws this function doesn't
324
    ///                   throw but returns the result code using the
325
    ///                   parameter \a ec. Otherwise, it throws an instance
326
    ///                   of hpx#exception.
327
    HPX_CORE_EXPORT void interrupt_thread(
×
328
        thread_id_type const& id, bool flag, hpx::error_code& ec = hpx::throws);
×
329

330
    HPX_CXX_EXPORT inline void interrupt_thread(
331
        thread_id_type const& id, hpx::error_code& ec = hpx::throws)
332
    {
333
        interrupt_thread(id, true, ec);
334
    }
335

336
    ///////////////////////////////////////////////////////////////////////////
337
    /// Interrupt the current thread at this point if it was canceled. This
338
    /// will throw a thread_interrupted exception, which will cancel the thread.
339
    ///
340
    /// \param id         [in] The thread id of the thread which should be
341
    ///                   interrupted.
342
    /// \param ec         [in,out] this represents the error status on exit,
343
    ///                   if this is pre-initialized to \a hpx#throws
344
    ///                   the function will throw on error instead.
345
    ///
346
    /// \note             As long as \a ec is not pre-initialized to
347
    ///                   \a hpx#throws this function doesn't
348
    ///                   throw but returns the result code using the
349
    ///                   parameter \a ec. Otherwise, it throws an instance
350
    ///                   of hpx#exception.
351
    HPX_CXX_EXPORT HPX_CORE_EXPORT void interruption_point(
352
        thread_id_type const& id, hpx::error_code& ec = hpx::throws);
353

354
    ///////////////////////////////////////////////////////////////////////////
355
    /// Return priority of the given thread
356
    ///
357
    /// \param id         [in] The thread id of the thread whose priority
358
    ///                   is queried.
359
    /// \param ec         [in,out] this represents the error status on exit,
360
    ///                   if this is pre-initialized to \a hpx#throws
361
    ///                   the function will throw on error instead.
362
    ///
363
    /// \note             As long as \a ec is not pre-initialized to
364
    ///                   \a hpx#throws this function doesn't
365
    ///                   throw but returns the result code using the
366
    ///                   parameter \a ec. Otherwise, it throws an instance
367
    ///                   of hpx#exception.
368
    HPX_CORE_EXPORT threads::thread_priority get_thread_priority(
369
        thread_id_type const& id, hpx::error_code& ec = hpx::throws) noexcept;
370

371
    ///////////////////////////////////////////////////////////////////////////
372
    /// Return stack size of the given thread
373
    ///
374
    /// \param id         [in] The thread id of the thread whose priority
375
    ///                   is queried.
376
    /// \param ec         [in,out] this represents the error status on exit,
377
    ///                   if this is pre-initialized to \a hpx#throws
378
    ///                   the function will throw on error instead.
379
    ///
380
    /// \note             As long as \a ec is not pre-initialized to
381
    ///                   \a hpx#throws this function doesn't
382
    ///                   throw but returns the result code using the
383
    ///                   parameter \a ec. Otherwise, it throws an instance
384
    ///                   of hpx#exception.
385
    HPX_CXX_EXPORT HPX_CORE_EXPORT std::ptrdiff_t get_stack_size(
386
        thread_id_type const& id, hpx::error_code& ec = hpx::throws) noexcept;
387

388
    ///////////////////////////////////////////////////////////////////////////
389
    /// \cond NOINTERNAL
390
    HPX_CXX_EXPORT HPX_CORE_EXPORT void run_thread_exit_callbacks(
391
        thread_id_type const& id, hpx::error_code& ec = hpx::throws);
392

393
    HPX_CXX_EXPORT HPX_CORE_EXPORT bool add_thread_exit_callback(
394
        thread_id_type const& id, hpx::function<void()> const& f,
395
        hpx::error_code& ec = hpx::throws);
396

397
    HPX_CXX_EXPORT HPX_CORE_EXPORT void free_thread_exit_callbacks(
398
        thread_id_type const& id, hpx::error_code& ec = hpx::throws);
399

400
    ///////////////////////////////////////////////////////////////////////////
401
    HPX_CXX_EXPORT HPX_CORE_EXPORT std::size_t get_thread_data(
402
        thread_id_type const& id, hpx::error_code& ec = hpx::throws);
403

404
    HPX_CXX_EXPORT HPX_CORE_EXPORT std::size_t set_thread_data(
405
        thread_id_type const& id, std::size_t data,
406
        hpx::error_code& ec = hpx::throws);
407

408
#if defined(HPX_HAVE_LIBCDS)
409
    HPX_CXX_EXPORT HPX_CORE_EXPORT std::size_t get_libcds_data(
410
        thread_id_type const& id, hpx::error_code& ec = hpx::throws);
411

412
    HPX_CXX_EXPORT HPX_CORE_EXPORT std::size_t set_libcds_data(
413
        thread_id_type const& id, std::size_t data,
414
        hpx::error_code& ec = hpx::throws);
415

416
    HPX_CXX_EXPORT HPX_CORE_EXPORT std::size_t get_libcds_hazard_pointer_data(
417
        thread_id_type const& id, hpx::error_code& ec = hpx::throws);
418

419
    HPX_CXX_EXPORT HPX_CORE_EXPORT std::size_t set_libcds_hazard_pointer_data(
420
        thread_id_type const& id, std::size_t data,
421
        hpx::error_code& ec = hpx::throws);
422

423
    HPX_CXX_EXPORT HPX_CORE_EXPORT std::size_t
424
    get_libcds_dynamic_hazard_pointer_data(
425
        thread_id_type const& id, hpx::error_code& ec = hpx::throws);
426

427
    HPX_CXX_EXPORT HPX_CORE_EXPORT std::size_t
428
    set_libcds_dynamic_hazard_pointer_data(thread_id_type const& id,
429
        std::size_t data, hpx::error_code& ec = hpx::throws);
430
#endif
431

432
    HPX_CXX_EXPORT HPX_CORE_EXPORT std::size_t&
433
    get_continuation_recursion_count() noexcept;
434
    HPX_CXX_EXPORT HPX_CORE_EXPORT void
435
    reset_continuation_recursion_count() noexcept;
436
    /// \endcond
437

438
    /// Returns a pointer to the pool that was used to run the current thread
439
    ///
440
    /// \throws If <code>&ec != &throws</code>, never throws, but will set \a ec
441
    ///         to an appropriate value when an error occurs. Otherwise, this
442
    ///         function will throw an \a hpx#exception with an error code of
443
    ///         \a hpx#error#yield_aborted if it is signaled with \a
444
    ///            wait_aborted.
445
    ///         If called outside  a HPX-thread, this function will throw an
446
    ///         \a hpx#exception with an error code of \a
447
    ///            hpx#error#null_thread_id.
448
    ///         If this function is called while the thread-manager is not
449
    ///         running, it will throw an \a hpx#exception with an error code of
450
    ///         \a hpx#error#invalid_status.
451
    HPX_CXX_EXPORT HPX_CORE_EXPORT threads::thread_pool_base* get_pool(
452
        thread_id_type const& id, hpx::error_code& ec = hpx::throws);
453
}    // namespace hpx::threads
454

455
namespace hpx::this_thread {
456

457
    ///////////////////////////////////////////////////////////////////////////
458
    /// The function \a suspend will return control to the thread manager
459
    /// (suspends the current thread). It sets the new state of this thread to
460
    /// the thread state passed as the parameter.
461
    ///
462
    /// \note Must be called from within a HPX-thread.
463
    ///
464
    /// \throws If <code>&ec != &throws</code>, never throws, but will set \a ec
465
    ///         to an appropriate value when an error occurs. Otherwise, this
466
    ///         function will throw an \a hpx#exception with an error code of
467
    ///         \a hpx#error#yield_aborted if it is signaled with \a
468
    ///            wait_aborted.
469
    ///         If called outside  a HPX-thread, this function will throw an
470
    ///         \a hpx#exception with an error code of \a
471
    ///            hpx#error#null_thread_id.
472
    ///         If this function is called while the thread-manager is not
473
    ///         running, it will throw an \a hpx#exception with an error code of
474
    ///         \a hpx#error#invalid_status.
475
    ///
476
    HPX_CXX_EXPORT HPX_CORE_EXPORT threads::thread_restart_state suspend(
477
        threads::thread_schedule_state state, threads::thread_id_type nextid,
478
        threads::thread_description const& description =
479
            threads::thread_description("this_thread::suspend"),
480
        hpx::error_code& ec = hpx::throws);
481

482
    /// The function \a suspend will return control to the thread manager
483
    /// (suspends the current thread). It sets the new state of this thread to
484
    /// the thread state passed as the parameter.
485
    ///
486
    /// \note Must be called from within a HPX-thread.
487
    ///
488
    /// \throws If <code>&ec != &throws</code>, never throws, but will set \a ec
489
    ///         to an appropriate value when an error occurs. Otherwise, this
490
    ///         function will throw an \a hpx#exception with an error code of
491
    ///         \a hpx#error#yield_aborted if it is signaled with \a
492
    ///            wait_aborted.
493
    ///         If called outside a HPX-thread, this function will throw an
1,072,565✔
494
    ///         \a hpx#exception with an error code of \a
495
    ///            hpx#error#null_thread_id.
496
    ///         If this function is called while the thread-manager is not
497
    ///         running, it will throw an \a hpx#exception with an error code of
498
    ///         \a hpx#error#invalid_status.
499
    ///
500
    HPX_CXX_EXPORT inline threads::thread_restart_state suspend(
501
        threads::thread_schedule_state state =
502
            threads::thread_schedule_state::pending,
503
        threads::thread_description const& description =
504
            threads::thread_description("this_thread::suspend"),
505
        hpx::error_code& ec = hpx::throws)
506
    {
507
        return suspend(state, threads::invalid_thread_id, description, ec);
508
    }
509

510
    /// The function \a suspend will return control to the thread manager
511
    /// (suspends the current thread). It sets the new state of this thread to
512
    /// \a suspended and schedules a wakeup for this threads at the given
513
    /// time.
514
    ///
515
    /// \note Must be called from within a HPX-thread.
516
    ///
517
    /// \throws If <code>&ec != &throws</code>, never throws, but will set \a ec
518
    ///         to an appropriate value when an error occurs. Otherwise, this
519
    ///         function will throw an \a hpx#exception with an error code of
520
    ///         \a hpx#error#yield_aborted if it is signaled with \a
521
    ///            wait_aborted.
522
    ///         If called outside a HPX-thread, this function will throw an
523
    ///         \a hpx#exception with an error code of \a
524
    ///            hpx#error#null_thread_id.
525
    ///         If this function is called while the thread-manager is not
526
    ///         running, it will throw an \a hpx#exception with an error code of
527
    ///         \a hpx#error#invalid_status.
528
    ///
529
    HPX_CXX_EXPORT HPX_CORE_EXPORT threads::thread_restart_state suspend(
530
        hpx::chrono::steady_time_point const& abs_time,
531
        threads::thread_id_type id,
532
        threads::thread_description const& description =
533
            threads::thread_description("this_thread::suspend"),
534
        hpx::error_code& ec = hpx::throws);
535

536
    /// The function \a suspend will return control to the thread manager
537
    /// (suspends the current thread). It sets the new state of this thread to
538
    /// \a suspended and schedules a wakeup for this threads at the given
539
    /// time.
540
    ///
541
    /// \note Must be called from within a HPX-thread.
542
    ///
543
    /// \throws If <code>&ec != &throws</code>, never throws, but will set \a ec
544
    ///         to an appropriate value when an error occurs. Otherwise, this
545
    ///         function will throw an \a hpx#exception with an error code of
546
    ///         \a hpx#error#yield_aborted if it is signaled with \a
547
    ///            wait_aborted.
129✔
548
    ///         If called outside a HPX-thread, this function will throw an
549
    ///         \a hpx#exception with an error code of \a
550
    ///            hpx#error#null_thread_id.
551
    ///         If this function is called while the thread-manager is not
552
    ///         running, it will throw an \a hpx#exception with an error code of
553
    ///         \a hpx#error#invalid_status.
554
    ///
555
    HPX_CXX_EXPORT inline threads::thread_restart_state suspend(
556
        hpx::chrono::steady_time_point const& abs_time,
557
        threads::thread_description const& description =
558
            threads::thread_description("this_thread::suspend"),
559
        hpx::error_code& ec = hpx::throws)
560
    {
561
        return suspend(abs_time, threads::invalid_thread_id, description, ec);
562
    }
563

564
    /// The function \a suspend will return control to the thread manager
565
    /// (suspends the current thread). It sets the new state of this thread to
566
    /// \a suspended and schedules a wakeup for this threads after the given
567
    /// duration.
568
    ///
569
    /// \note Must be called from within a HPX-thread.
×
570
    ///
571
    /// \throws If <code>&ec != &throws</code>, never throws, but will set \a ec
572
    ///         to an appropriate value when an error occurs. Otherwise, this
573
    ///         function will throw an \a hpx#exception with an error code of
574
    ///         \a hpx#error#yield_aborted if it is signaled with \a
575
    ///            wait_aborted.
×
576
    ///         If called outside a HPX-thread, this function will throw an
×
577
    ///         \a hpx#exception with an error code of \a
578
    ///            hpx#error#null_thread_id.
579
    ///         If this function is called while the thread-manager is not
580
    ///         running, it will throw an \a hpx#exception with an error code of
581
    ///         \a hpx#error#invalid_status.
582
    ///
583
    HPX_CXX_EXPORT inline threads::thread_restart_state suspend(
584
        hpx::chrono::steady_duration const& rel_time,
585
        threads::thread_description const& description =
586
            threads::thread_description("this_thread::suspend"),
587
        hpx::error_code& ec = hpx::throws)
588
    {
589
        return suspend(
590
            rel_time.from_now(), threads::invalid_thread_id, description, ec);
591
    }
592

593
    /// The function \a suspend will return control to the thread manager
594
    /// (suspends the current thread). It sets the new state of this thread to
595
    /// \a suspended and schedules a wakeup for this threads after the given
596
    /// duration.
597
    ///
598
    /// \note Must be called from within a HPX-thread.
×
599
    ///
600
    /// \throws If <code>&ec != &throws</code>, never throws, but will set \a ec
601
    ///         to an appropriate value when an error occurs. Otherwise, this
602
    ///         function will throw an \a hpx#exception with an error code of
603
    ///         \a hpx#error#yield_aborted if it is signaled with \a
604
    ///            wait_aborted.
605
    ///         If called outside a HPX-thread, this function will throw an
×
606
    ///         \a hpx#exception with an error code of \a
607
    ///            hpx#error#null_thread_id.
608
    ///         If this function is called while the thread-manager is not
609
    ///         running, it will throw an \a hpx#exception with an error code of
610
    ///         \a hpx#error#invalid_status.
611
    ///
612
    HPX_CXX_EXPORT inline threads::thread_restart_state suspend(
613
        hpx::chrono::steady_duration const& rel_time,
614
        threads::thread_id_type const& id,
615
        threads::thread_description const& description =
616
            threads::thread_description("this_thread::suspend"),
617
        hpx::error_code& ec = hpx::throws)
618
    {
619
        return suspend(rel_time.from_now(), id, description, ec);
620
    }
621

622
    /// The function \a suspend will return control to the thread manager
623
    /// (suspends the current thread). It sets the new state of this thread to
624
    /// \a suspended and schedules a wakeup for this threads after the given
625
    /// time (specified in milliseconds).
626
    ///
627
    /// \note Must be called from within a HPX-thread.
628
    ///
629
    /// \throws If <code>&ec != &throws</code>, never throws, but will set \a ec
630
    ///         to an appropriate value when an error occurs. Otherwise, this
631
    ///         function will throw an \a hpx#exception with an error code of
632
    ///         \a hpx#error#yield_aborted if it is signaled with \a
×
633
    ///            wait_aborted.
634
    ///         If called outside a HPX-thread, this function will throw an
635
    ///         \a hpx#exception with an error code of \a
636
    ///            hpx#error#null_thread_id.
637
    ///         If this function is called while the thread-manager is not
638
    ///         running, it will throw an \a hpx#exception with an error code of
639
    ///         \a hpx#error#invalid_status.
640
    ///
641
    HPX_CXX_EXPORT inline threads::thread_restart_state suspend(
642
        std::uint64_t ms,
643
        threads::thread_description const& description =
644
            threads::thread_description("this_thread::suspend"),
645
        hpx::error_code& ec = hpx::throws)
646
    {
647
        return suspend(std::chrono::milliseconds(ms),
648
            threads::invalid_thread_id, description, ec);
649
    }
650

651
    /// Returns a pointer to the pool that was used to run the current thread
652
    ///
653
    /// \throws If <code>&ec != &throws</code>, never throws, but will set \a ec
654
    ///         to an appropriate value when an error occurs. Otherwise, this
655
    ///         function will throw an \a hpx#exception with an error code of
656
    ///         \a hpx#error#yield_aborted if it is signaled with \a
657
    ///            wait_aborted.
658
    ///         If called outside a HPX-thread, this function will throw an
659
    ///         \a hpx#exception with an error code of \a
660
    ///            hpx#error#null_thread_id.
661
    ///         If this function is called while the thread-manager is not
662
    ///         running, it will throw an \a hpx#exception with an error code of
663
    ///         \a hpx#error#invalid_status.
664
    HPX_CXX_EXPORT HPX_CORE_EXPORT threads::thread_pool_base* get_pool(
665
        hpx::error_code& ec = hpx::throws);
666

667
    /// \cond NOINTERNAL
668
    // returns the remaining available stack space
669
    HPX_CXX_EXPORT HPX_CORE_EXPORT std::ptrdiff_t
670
    get_available_stack_space() noexcept;
671

672
    // returns whether the remaining stack-space is at least as large as
673
    // requested
674
    HPX_CXX_EXPORT HPX_CORE_EXPORT bool has_sufficient_stack_space(
675
        std::size_t space_needed = static_cast<std::size_t>(
676
            8 * HPX_THREADS_STACK_OVERHEAD)) noexcept;
677
    /// \endcond
678
}    // namespace hpx::this_thread
679

680
#include <hpx/config/warnings_suffix.hpp>
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc