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

STEllAR-GROUP / hpx / #862

10 Jan 2023 05:30PM UTC coverage: 86.582% (-0.05%) from 86.634%
#862

push

StellarBot
Merge #6130

6130: Remove the mutex lock in the critical path of get_partitioner. r=hkaiser a=JiakunYan

Remove the mutex lock in the critical path of hpx::resource::detail::get_partitioner.

The protected variable `partitioner_ref` is only set once during initialization.

Co-authored-by: Jiakun Yan <jiakunyan1998@gmail.com>

6 of 6 new or added lines in 1 file covered. (100.0%)

174767 of 201851 relevant lines covered (86.58%)

2069816.07 hits per line

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

68.57
/libs/core/runtime_local/src/runtime_local.cpp
1
//  Copyright (c) 2007-2022 Hartmut Kaiser
2
//  Copyright (c)      2011 Bryce Lelbach
3
//
4
//  SPDX-License-Identifier: BSL-1.0
5
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
6
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7

8
#include <hpx/config.hpp>
9
#include <hpx/assert.hpp>
10
#include <hpx/command_line_handling_local/late_command_line_handling_local.hpp>
11
#include <hpx/command_line_handling_local/parse_command_line_local.hpp>
12
#include <hpx/coroutines/coroutine.hpp>
13
#include <hpx/coroutines/signal_handler_debugging.hpp>
14
#include <hpx/debugging/backtrace.hpp>
15
#include <hpx/execution_base/this_thread.hpp>
16
#include <hpx/functional/bind.hpp>
17
#include <hpx/functional/function.hpp>
18
#include <hpx/itt_notify/thread_name.hpp>
19
#include <hpx/modules/errors.hpp>
20
#include <hpx/modules/logging.hpp>
21
#include <hpx/modules/threadmanager.hpp>
22
#include <hpx/runtime_local/config_entry.hpp>
23
#include <hpx/runtime_local/custom_exception_info.hpp>
24
#include <hpx/runtime_local/debugging.hpp>
25
#include <hpx/runtime_local/os_thread_type.hpp>
26
#include <hpx/runtime_local/runtime_local.hpp>
27
#include <hpx/runtime_local/runtime_local_fwd.hpp>
28
#include <hpx/runtime_local/shutdown_function.hpp>
29
#include <hpx/runtime_local/startup_function.hpp>
30
#include <hpx/runtime_local/state.hpp>
31
#include <hpx/runtime_local/thread_hooks.hpp>
32
#include <hpx/runtime_local/thread_mapper.hpp>
33
#include <hpx/static_reinit/static_reinit.hpp>
34
#include <hpx/thread_support/set_thread_name.hpp>
35
#include <hpx/threading_base/external_timer.hpp>
36
#include <hpx/threading_base/scheduler_mode.hpp>
37
#include <hpx/timing/high_resolution_clock.hpp>
38
#include <hpx/topology/topology.hpp>
39
#include <hpx/util/from_string.hpp>
40
#include <hpx/version.hpp>
41

42
#include <atomic>
43
#include <condition_variable>
44
#include <cstddef>
45
#include <cstdint>
46
#include <cstring>
47
#include <exception>
48
#include <functional>
49
#include <iomanip>
50
#include <iostream>
51
#include <list>
52
#include <memory>
53
#include <mutex>
54
#include <sstream>
55
#include <string>
56
#include <thread>
57
#include <utility>
58

59
///////////////////////////////////////////////////////////////////////////////
60
// Make sure the system gets properly shut down while handling Ctrl-C and other
61
// system signals
62
#if defined(HPX_WINDOWS)
63

64
namespace hpx {
65
    ///////////////////////////////////////////////////////////////////////////
66
    void handle_termination(char const* reason)
67
    {
68
        if (hpx::threads::coroutines::attach_debugger_on_sigv)
69
        {
70
            util::attach_debugger();
71
        }
72

73
        if (hpx::threads::coroutines::diagnostics_on_terminate)
74
        {
75
            int const verbosity = hpx::threads::coroutines::exception_verbosity;
76

77
            if (verbosity >= 2)
78
            {
79
                std::cerr << full_build_string() << "\n";
80
            }
81

82
#if defined(HPX_HAVE_STACKTRACES)
83
            if (verbosity >= 1)
84
            {
85
                std::size_t const trace_depth =
86
                    util::from_string<std::size_t>(get_config_entry(
87
                        "hpx.trace_depth", HPX_HAVE_THREAD_BACKTRACE_DEPTH));
88
                std::cerr << "{stack-trace}: " << hpx::util::trace(trace_depth)
89
                          << "\n";
90
            }
91
#endif
92

93
            std::cerr << "{what}: " << (reason ? reason : "Unknown reason")
94
                      << "\n";
95
        }
96
    }
97

98
    HPX_CORE_EXPORT BOOL WINAPI termination_handler(DWORD ctrl_type)
99
    {
100
        switch (ctrl_type)
101
        {
102
        case CTRL_C_EVENT:
103
            handle_termination("Ctrl-C");
104
            break;
105

106
        case CTRL_BREAK_EVENT:
107
            handle_termination("Ctrl-Break");
108
            break;
109

110
        case CTRL_CLOSE_EVENT:
111
            handle_termination("Ctrl-Close");
112
            break;
113

114
        case CTRL_LOGOFF_EVENT:
115
            handle_termination("Logoff");
116
            break;
117

118
        case CTRL_SHUTDOWN_EVENT:
119
            handle_termination("Shutdown");
120
            break;
121

122
        default:
123
            break;
124
        }
125
        return FALSE;
126
    }
127
}    // namespace hpx
128

129
#else
130

131
#include <signal.h>
132
#include <stdlib.h>
133
#include <string.h>
134

135
namespace hpx {
136
    ///////////////////////////////////////////////////////////////////////////
137
    [[noreturn]] HPX_CORE_EXPORT void termination_handler(int signum)
×
138
    {
139
        if (signum != SIGINT &&
×
140
            hpx::threads::coroutines::attach_debugger_on_sigv)
×
141
        {
142
            util::attach_debugger();
×
143
        }
×
144

145
        if (hpx::threads::coroutines::diagnostics_on_terminate)
×
146
        {
147
            int const verbosity = hpx::threads::coroutines::exception_verbosity;
×
148
            char* reason = strsignal(signum);
×
149

150
            if (verbosity >= 2)
×
151
            {
152
                std::cerr << full_build_string() << "\n";
×
153
            }
×
154

155
#if defined(HPX_HAVE_STACKTRACES)
156
            if (verbosity >= 1)
×
157
            {
158
                std::size_t const trace_depth =
×
159
                    util::from_string<std::size_t>(get_config_entry(
×
160
                        "hpx.trace_depth", HPX_HAVE_THREAD_BACKTRACE_DEPTH));
×
161
                std::cerr << "{stack-trace}: " << hpx::util::trace(trace_depth)
×
162
                          << "\n";
×
163
            }
×
164
#endif
165

166
            std::cerr << "{what}: " << (reason ? reason : "Unknown reason")
×
167
                      << "\n";
×
168
        }
×
169
        std::abort();
×
170
    }
×
171
}    // namespace hpx
172

173
#endif
174

175
///////////////////////////////////////////////////////////////////////////////
176
namespace hpx {
177

178
    ///////////////////////////////////////////////////////////////////////////
179
    HPX_CORE_EXPORT void HPX_CDECL new_handler()
×
180
    {
181
        HPX_THROW_EXCEPTION(hpx::error::out_of_memory, "new_handler",
×
182
            "new allocator failed to allocate memory");
183
    }
×
184

185
    ///////////////////////////////////////////////////////////////////////////
186
    namespace detail {
187
        // Sometimes the HPX library gets simply unloaded as a result of some
188
        // extreme error handling. Avoid hangs in the end by setting a flag.
189
        static bool exit_called = false;
190

191
        void on_exit() noexcept
1,223✔
192
        {
193
            exit_called = true;
1,223✔
194
        }
1,223✔
195

196
        void on_abort(int) noexcept
1✔
197
        {
198
            exit_called = true;
×
199
            std::exit(-1);
×
200
        }
201
    }    // namespace detail
202

203
    ///////////////////////////////////////////////////////////////////////////
204
    void set_error_handlers()
1,223✔
205
    {
206
        // initialize global variables
207
        hpx::threads::coroutines::attach_debugger_on_sigv =
1,223✔
208
            get_config_entry("hpx.attach_debugger", "") == "exception";
1,223✔
209
        hpx::threads::coroutines::diagnostics_on_terminate =
1,223✔
210
            get_config_entry("hpx.diagnostics_on_terminate", "1") == "1";
1,223✔
211
        hpx::threads::coroutines::exception_verbosity = util::from_string<int>(
1,223✔
212
            get_config_entry("hpx.exception_verbosity", "2"));
1,223✔
213
        hpx::threads::coroutines::exception_verbosity = 0;
1,223✔
214
#if defined(HPX_HAVE_STACKTRACES) && defined(HPX_HAVE_THREAD_BACKTRACE_DEPTH)
215
        hpx::threads::coroutines::exception_verbosity =
1,223✔
216
            util::from_string<int>(get_config_entry(
1,223✔
217
                "hpx.trace_depth", HPX_HAVE_THREAD_BACKTRACE_DEPTH));
1,223✔
218
#endif
219

220
#if defined(HPX_WINDOWS)
221
        // Set console control handler to allow server to be stopped.
222
        SetConsoleCtrlHandler(hpx::termination_handler, TRUE);
223
#else
224
        if (util::from_string<int>(get_config_entry("hpx.handle_signals", 1)))
1,223✔
225
        {
226
            struct sigaction new_action;
227
            new_action.sa_handler = hpx::termination_handler;
1,223✔
228
            sigemptyset(&new_action.sa_mask);
1,223✔
229
            new_action.sa_flags = 0;
1,223✔
230

231
            sigaction(SIGINT, &new_action, nullptr);    // Interrupted
1,223✔
232
            sigaction(SIGBUS, &new_action, nullptr);    // Bus error
1,223✔
233
            sigaction(
1,223✔
234
                SIGFPE, &new_action, nullptr);    // Floating point exception
235
            sigaction(SIGILL, &new_action, nullptr);     // Illegal instruction
1,223✔
236
            sigaction(SIGPIPE, &new_action, nullptr);    // Bad pipe
1,223✔
237
            sigaction(SIGSEGV, &new_action, nullptr);    // Segmentation fault
1,223✔
238
            sigaction(SIGSYS, &new_action, nullptr);     // Bad syscall
1,223✔
239

240
            hpx::threads::coroutines::register_signal_handler = false;
1,223✔
241
        }
1,223✔
242
        else
243
        {
244
            hpx::threads::coroutines::register_signal_handler = true;
×
245
        }
246
#endif
247

248
        std::set_new_handler(hpx::new_handler);
1,223✔
249
    }
1,223✔
250

251
    ///////////////////////////////////////////////////////////////////////////
252
    namespace strings {
253
        inline constexpr char const* const runtime_state_names[] = {
254
            "state::invalid",         // -1
255
            "state::initialized",     // 0
256
            "state::pre_startup",     // 1
257
            "state::startup",         // 2
258
            "state::pre_main",        // 3
259
            "state::starting",        // 4
260
            "state::running",         // 5
261
            "state::suspended",       // 6
262
            "state::pre_sleep",       // 7
263
            "state::sleeping",        // 8
264
            "state::pre_shutdown",    // 9
265
            "state::shutdown",        // 10
266
            "state::stopping",        // 11
267
            "state::terminating",     // 12
268
            "state::stopped"          // 13
269
        };
270
    }
271

272
    char const* get_runtime_state_name(state s)
2,534✔
273
    {
274
        if (s < state::invalid || s >= state::last_valid_runtime_state)
2,534✔
275
        {
276
            return "invalid (value out of bounds)";
×
277
        }
278
        return strings::runtime_state_names[static_cast<int>(s) + 1];
2,534✔
279
    }
2,534✔
280

281
    ///////////////////////////////////////////////////////////////////////////
282
    threads::policies::callback_notifier::on_startstop_type
283
        global_on_start_func;
1,253✔
284
    threads::policies::callback_notifier::on_startstop_type global_on_stop_func;
1,253✔
285
    threads::policies::callback_notifier::on_error_type global_on_error_func;
1,253✔
286

287
    ///////////////////////////////////////////////////////////////////////////
288
    runtime::runtime(hpx::util::runtime_configuration& rtcfg, bool initialize)
2,504✔
289
      : rtcfg_(rtcfg)
626✔
290
      , instance_number_(++instance_number_counter_)
626✔
291
      , thread_support_(new util::thread_mapper)
626✔
292
      , topology_(resource::get_partitioner().get_topology())
626✔
293
      , state_(state::invalid)
626✔
294
      , on_start_func_(global_on_start_func)
626✔
295
      , on_stop_func_(global_on_stop_func)
626✔
296
      , on_error_func_(global_on_error_func)
626✔
297
      , result_(0)
626✔
298
      , main_pool_notifier_()
626✔
299
      , main_pool_(main_pool_notifier_, "main_pool")
626✔
300
#ifdef HPX_HAVE_IO_POOL
301
      , io_pool_notifier_()
626✔
302
      , io_pool_(io_pool_notifier_, "io_pool")
626✔
303
#endif
304
#ifdef HPX_HAVE_TIMER_POOL
305
      , timer_pool_notifier_()
626✔
306
      , timer_pool_(timer_pool_notifier_, "timer_pool")
626✔
307
#endif
308
      , notifier_()
626✔
309
      , thread_manager_()
626✔
310
      , stop_called_(false)
626✔
311
      , stop_done_(false)
626✔
312
    {
626✔
313
        LPROGRESS_;
626✔
314

315
        // set notification policies only after the object was completely
316
        // initialized
317
        runtime::set_notification_policies(
626✔
318
            runtime::get_notification_policy(
626✔
319
                "worker-thread", runtime_local::os_thread_type::worker_thread),
320
#ifdef HPX_HAVE_IO_POOL
321
            runtime::get_notification_policy(
626✔
322
                "io-thread", runtime_local::os_thread_type::io_thread),
323
#endif
324
#ifdef HPX_HAVE_TIMER_POOL
325
            runtime::get_notification_policy(
626✔
326
                "timer-thread", runtime_local::os_thread_type::timer_thread),
327
#endif
328
            threads::detail::network_background_callback_type{});
626✔
329

330
        init_global_data();
626✔
331
        util::reinit_construct();
626✔
332

333
        if (initialize)
626✔
334
        {
335
            runtime::init();
626✔
336
        }
626✔
337
    }
626✔
338

339
    // this constructor is called by the distributed runtime only
340
    runtime::runtime(hpx::util::runtime_configuration& rtcfg)
2,376✔
341
      : rtcfg_(rtcfg)
594✔
342
      , instance_number_(++instance_number_counter_)
594✔
343
      , thread_support_(new util::thread_mapper)
594✔
344
      , topology_(resource::get_partitioner().get_topology())
594✔
345
      , state_(state::invalid)
594✔
346
      , on_start_func_(global_on_start_func)
594✔
347
      , on_stop_func_(global_on_stop_func)
594✔
348
      , on_error_func_(global_on_error_func)
594✔
349
      , result_(0)
594✔
350
      , main_pool_notifier_()
594✔
351
      , main_pool_(main_pool_notifier_, "main_pool")
594✔
352
#ifdef HPX_HAVE_IO_POOL
353
      , io_pool_notifier_()
594✔
354
      , io_pool_(io_pool_notifier_, "io_pool")
594✔
355
#endif
356
#ifdef HPX_HAVE_TIMER_POOL
357
      , timer_pool_notifier_()
594✔
358
      , timer_pool_(timer_pool_notifier_, "timer_pool")
594✔
359
#endif
360
      , notifier_()
594✔
361
      , thread_manager_()
594✔
362
      , stop_called_(false)
594✔
363
      , stop_done_(false)
594✔
364
    {
594✔
365
        init_global_data();
594✔
366
        util::reinit_construct();
594✔
367

368
        LPROGRESS_;
594✔
369
    }
594✔
370

371
    void runtime::set_notification_policies(notification_policy_type&& notifier,
1,220✔
372
#ifdef HPX_HAVE_IO_POOL
373
        notification_policy_type&& io_pool_notifier,
374
#endif
375
#ifdef HPX_HAVE_TIMER_POOL
376
        notification_policy_type&& timer_pool_notifier,
377
#endif
378
        threads::detail::network_background_callback_type
379
            network_background_callback)
380
    {
381
        notifier_ = HPX_MOVE(notifier);
1,220✔
382

383
        main_pool_.init(1);
1,220✔
384
#ifdef HPX_HAVE_IO_POOL
385
        io_pool_notifier_ = HPX_MOVE(io_pool_notifier);
1,220✔
386
        io_pool_.init(rtcfg_.get_thread_pool_size("io_pool"));
1,220✔
387
#endif
388
#ifdef HPX_HAVE_TIMER_POOL
389
        timer_pool_notifier_ = HPX_MOVE(timer_pool_notifier);
1,220✔
390
        timer_pool_.init(rtcfg_.get_thread_pool_size("timer_pool"));
1,220✔
391
#endif
392

393
        thread_manager_.reset(new hpx::threads::threadmanager(rtcfg_,
1,220✔
394
#ifdef HPX_HAVE_TIMER_POOL
395
            timer_pool_,
1,220✔
396
#endif
397
            notifier_, network_background_callback));
1,220✔
398
    }
1,220✔
399

400
    void runtime::init()
1,220✔
401
    {
402
        LPROGRESS_;
1,220✔
403

404
        try
405
        {
406
            // now create all threadmanager pools
407
            thread_manager_->create_pools();
1,220✔
408

409
            // this initializes the used_processing_units_ mask
410
            thread_manager_->init();
1,220✔
411

412
            // copy over all startup functions registered so far
413
            for (startup_function_type& f :
1,220✔
414
                detail::global_pre_startup_functions())
1,220✔
415
            {
416
                add_pre_startup_function(HPX_MOVE(f));
×
417
            }
418
            detail::global_pre_startup_functions().clear();
1,220✔
419

420
            for (startup_function_type& f : detail::global_startup_functions())
1,810✔
421
            {
422
                add_startup_function(HPX_MOVE(f));
590✔
423
            }
424
            detail::global_startup_functions().clear();
1,220✔
425

426
            for (shutdown_function_type& f :
1,220✔
427
                detail::global_pre_shutdown_functions())
1,220✔
428
            {
429
                add_pre_shutdown_function(HPX_MOVE(f));
×
430
            }
431
            detail::global_pre_shutdown_functions().clear();
1,220✔
432

433
            for (shutdown_function_type& f :
1,220✔
434
                detail::global_shutdown_functions())
1,220✔
435
            {
436
                add_shutdown_function(HPX_MOVE(f));
×
437
            }
438
            detail::global_shutdown_functions().clear();
1,220✔
439
        }
1,220✔
440
        catch (std::exception const& e)
441
        {
442
            // errors at this point need to be reported directly
443
            detail::report_exception_and_terminate(e);
×
444
        }
×
445
        catch (...)
446
        {
447
            // errors at this point need to be reported directly
448
            detail::report_exception_and_terminate(std::current_exception());
×
449
        }
×
450

451
        // set state to initialized
452
        set_state(state::initialized);
1,220✔
453
    }
1,220✔
454

455
    runtime::~runtime()
1,841✔
456
    {
1,841✔
457
        LRT_(debug).format("~runtime_local(entering)");
1,217✔
458

459
        // stop all services
460
        thread_manager_->stop();    // stops timer_pool_ as well
1,217✔
461
#ifdef HPX_HAVE_IO_POOL
462
        io_pool_.stop();
1,217✔
463
#endif
464
        LRT_(debug).format("~runtime_local(finished)");
1,217✔
465

466
        LPROGRESS_;
1,217✔
467

468
        // allow to reuse instance number if this was the only instance
469
        if (0 == instance_number_counter_)
1,217✔
470
            --instance_number_counter_;
1,217✔
471

472
        util::reinit_destruct();
1,217✔
473
        resource::detail::delete_partitioner();
1,217✔
474
    }
1,841✔
475

476
    void runtime::on_exit(hpx::function<void()> const& f)
×
477
    {
478
        std::lock_guard<std::mutex> l(mtx_);
×
479
        on_exit_functions_.push_back(f);
×
480
    }
×
481

482
    void runtime::starting()
1,220✔
483
    {
484
        state_.store(hpx::state::pre_main);
1,220✔
485
    }
1,220✔
486

487
    void runtime::stopping()
1,218✔
488
    {
489
        state_.store(hpx::state::stopped);
1,218✔
490

491
        std::lock_guard<std::mutex> l(mtx_);
1,218✔
492
        for (auto const& f : on_exit_functions_)
1,218✔
493
            f();
×
494
    }
1,218✔
495

496
    bool runtime::stopped() const
×
497
    {
498
        return state_.load() == hpx::state::stopped;
×
499
    }
500

501
    hpx::util::runtime_configuration& runtime::get_config()
1,654,946✔
502
    {
503
        return rtcfg_;
1,654,946✔
504
    }
505

506
    hpx::util::runtime_configuration const& runtime::get_config() const
67,873✔
507
    {
508
        return rtcfg_;
67,873✔
509
    }
510

511
    std::size_t runtime::get_instance_number() const
2✔
512
    {
513
        return static_cast<std::size_t>(instance_number_);
2✔
514
    }
515

516
    state runtime::get_state() const
95,079,554✔
517
    {
518
        return state_.load();
95,073,486✔
519
    }
520

521
    threads::topology const& runtime::get_topology() const
6✔
522
    {
523
        return topology_;
6✔
524
    }
525

526
    void runtime::set_state(state s)
6,898✔
527
    {
528
        LPROGRESS_ << get_runtime_state_name(s);
6,898✔
529
        state_.store(s);
6,898✔
530
    }
6,898✔
531

532
    ///////////////////////////////////////////////////////////////////////////
533
    std::atomic<int> runtime::instance_number_counter_(-1);
534

535
    ///////////////////////////////////////////////////////////////////////////
536
    namespace {
537
        std::uint64_t& runtime_uptime()
2,528✔
538
        {
539
            static std::uint64_t uptime = 0;
540
            return uptime;
2,528✔
541
        }
542
    }    // namespace
543

544
    void runtime::init_global_data()
1,220✔
545
    {
546
        runtime*& runtime_ = get_runtime_ptr();
1,220✔
547
        HPX_ASSERT(!runtime_);
1,220✔
548
        HPX_ASSERT(nullptr == threads::thread_self::get_self());
1,220✔
549

550
        runtime_ = this;
1,220✔
551
        runtime_uptime() = hpx::chrono::high_resolution_clock::now();
1,220✔
552
    }
1,220✔
553

554
    void runtime::deinit_global_data()
1,218✔
555
    {
556
        runtime*& runtime_ = get_runtime_ptr();
1,218✔
557
        HPX_ASSERT(runtime_);
1,218✔
558
        runtime_uptime() = 0;
1,218✔
559
        runtime_ = nullptr;
1,218✔
560
    }
1,218✔
561

562
    std::uint64_t runtime::get_system_uptime()
90✔
563
    {
564
        std::int64_t diff =
90✔
565
            hpx::chrono::high_resolution_clock::now() - runtime_uptime();
90✔
566
        return diff < 0LL ? 0ULL : static_cast<std::uint64_t>(diff);
90✔
567
    }
568

569
    threads::policies::callback_notifier::on_startstop_type
570
    runtime::on_start_func() const
×
571
    {
572
        return on_start_func_;
×
573
    }
574

575
    threads::policies::callback_notifier::on_startstop_type
576
    runtime::on_stop_func() const
×
577
    {
578
        return on_stop_func_;
×
579
    }
580

581
    threads::policies::callback_notifier::on_error_type runtime::on_error_func()
×
582
        const
583
    {
584
        return on_error_func_;
×
585
    }
586

587
    threads::policies::callback_notifier::on_startstop_type
588
    runtime::on_start_func(
×
589
        threads::policies::callback_notifier::on_startstop_type&& f)
590
    {
591
        threads::policies::callback_notifier::on_startstop_type newf =
×
592
            HPX_MOVE(f);
×
593
        std::swap(on_start_func_, newf);
×
594
        return newf;
×
595
    }
×
596

597
    threads::policies::callback_notifier::on_startstop_type
598
    runtime::on_stop_func(
×
599
        threads::policies::callback_notifier::on_startstop_type&& f)
600
    {
601
        threads::policies::callback_notifier::on_startstop_type newf =
×
602
            HPX_MOVE(f);
×
603
        std::swap(on_stop_func_, newf);
×
604
        return newf;
×
605
    }
×
606

607
    threads::policies::callback_notifier::on_error_type runtime::on_error_func(
×
608
        threads::policies::callback_notifier::on_error_type&& f)
609
    {
610
        threads::policies::callback_notifier::on_error_type newf = HPX_MOVE(f);
×
611
        std::swap(on_error_func_, newf);
×
612
        return newf;
×
613
    }
×
614

615
    std::uint32_t runtime::get_locality_id(error_code& /* ec */) const
25,176,381✔
616
    {
617
        return 0;
25,177,513✔
618
    }
619

620
    std::size_t runtime::get_num_worker_threads() const
1,129✔
621
    {
622
        HPX_ASSERT(thread_manager_);
1,129✔
623
        return thread_manager_->get_os_thread_count();
1,129✔
624
    }
625

626
    std::uint32_t runtime::get_num_localities(
1✔
627
        hpx::launch::sync_policy, error_code& /* ec */) const
628
    {
629
        return 1;
1✔
630
    }
631

632
    std::uint32_t runtime::get_initial_num_localities() const
×
633
    {
634
        return 1;
×
635
    }
636

637
    hpx::future<std::uint32_t> runtime::get_num_localities() const
×
638
    {
639
        return make_ready_future(std::uint32_t(1));
×
640
    }
641

642
    std::string runtime::get_locality_name() const
×
643
    {
644
        return "console";
×
645
    }
×
646

647
    ///////////////////////////////////////////////////////////////////////////
648
    threads::policies::callback_notifier::on_startstop_type
649
    get_thread_on_start_func()
×
650
    {
651
        runtime* rt = get_runtime_ptr();
×
652
        if (nullptr != rt)
×
653
        {
654
            return rt->on_start_func();
×
655
        }
656
        else
657
        {
658
            return global_on_start_func;
×
659
        }
660
    }
×
661

662
    threads::policies::callback_notifier::on_startstop_type
663
    get_thread_on_stop_func()
×
664
    {
665
        runtime* rt = get_runtime_ptr();
×
666
        if (nullptr != rt)
×
667
        {
668
            return rt->on_stop_func();
×
669
        }
670
        else
671
        {
672
            return global_on_stop_func;
×
673
        }
674
    }
×
675

676
    threads::policies::callback_notifier::on_error_type
677
    get_thread_on_error_func()
×
678
    {
679
        runtime* rt = get_runtime_ptr();
×
680
        if (nullptr != rt)
×
681
        {
682
            return rt->on_error_func();
×
683
        }
684
        else
685
        {
686
            return global_on_error_func;
×
687
        }
688
    }
×
689

690
    threads::policies::callback_notifier::on_startstop_type
691
    register_thread_on_start_func(
1✔
692
        threads::policies::callback_notifier::on_startstop_type&& f)
693
    {
694
        runtime* rt = get_runtime_ptr();
1✔
695
        if (nullptr != rt)
1✔
696
        {
697
            return rt->on_start_func(HPX_MOVE(f));
×
698
        }
699

700
        threads::policies::callback_notifier::on_startstop_type newf =
701
            HPX_MOVE(f);
1✔
702
        std::swap(global_on_start_func, newf);
1✔
703
        return newf;
1✔
704
    }
1✔
705

706
    threads::policies::callback_notifier::on_startstop_type
707
    register_thread_on_stop_func(
1✔
708
        threads::policies::callback_notifier::on_startstop_type&& f)
709
    {
710
        runtime* rt = get_runtime_ptr();
1✔
711
        if (nullptr != rt)
1✔
712
        {
713
            return rt->on_stop_func(HPX_MOVE(f));
×
714
        }
715

716
        threads::policies::callback_notifier::on_startstop_type newf =
717
            HPX_MOVE(f);
1✔
718
        std::swap(global_on_stop_func, newf);
1✔
719
        return newf;
1✔
720
    }
1✔
721

722
    threads::policies::callback_notifier::on_error_type
723
    register_thread_on_error_func(
1✔
724
        threads::policies::callback_notifier::on_error_type&& f)
725
    {
726
        runtime* rt = get_runtime_ptr();
1✔
727
        if (nullptr != rt)
1✔
728
        {
729
            return rt->on_error_func(HPX_MOVE(f));
×
730
        }
731

732
        threads::policies::callback_notifier::on_error_type newf = HPX_MOVE(f);
1✔
733
        std::swap(global_on_error_func, newf);
1✔
734
        return newf;
1✔
735
    }
1✔
736

737
    ///////////////////////////////////////////////////////////////////////////
738
    runtime& get_runtime()
1,798,009✔
739
    {
740
        HPX_ASSERT(get_runtime_ptr() != nullptr);
1,798,009✔
741
        return *get_runtime_ptr();
1,798,008✔
742
    }
743

744
    runtime*& get_runtime_ptr()
101,493,669✔
745
    {
746
        static runtime* runtime_ = nullptr;
747
        return runtime_;
101,493,669✔
748
    }
749

750
    std::string get_thread_name()
5✔
751
    {
752
        std::string& thread_name = detail::thread_name();
5✔
753
        if (thread_name.empty())
5✔
754
            return "<unknown>";
×
755
        return thread_name;
5✔
756
    }
5✔
757

758
    // Register the current kernel thread with HPX, this should be done once
759
    // for each external OS-thread intended to invoke HPX functionality.
760
    // Calling this function more than once will silently fail
761
    // (will return false).
762
    bool register_thread(runtime* rt, char const* name, error_code& ec)
4✔
763
    {
764
        HPX_ASSERT(rt);
4✔
765
        return rt->register_thread(name, 0, true, ec);
4✔
766
    }
767

768
    // Unregister the thread from HPX, this should be done once in
769
    // the end before the external thread exists.
770
    void unregister_thread(runtime* rt)
4✔
771
    {
772
        HPX_ASSERT(rt);
4✔
773
        rt->unregister_thread();
4✔
774
    }
4✔
775

776
    // Access data for a given OS thread that was previously registered by
777
    // \a register_thread. This function must be called from a thread that was
778
    // previously registered with the runtime.
779
    runtime_local::os_thread_data get_os_thread_data(std::string const& label)
×
780
    {
781
        return get_runtime().get_os_thread_data(label);
×
782
    }
783

784
    /// Enumerate all OS threads that have registered with the runtime.
785
    bool enumerate_os_threads(
4✔
786
        hpx::function<bool(os_thread_data const&)> const& f)
787
    {
788
        return get_runtime().enumerate_os_threads(f);
4✔
789
    }
790

791
    ///////////////////////////////////////////////////////////////////////////
792
    void report_error(std::size_t num_thread, std::exception_ptr const& e)
×
793
    {
794
        // Early and late exceptions
795
        if (!threads::threadmanager_is(hpx::state::running))
×
796
        {
797
            hpx::runtime* rt = hpx::get_runtime_ptr();
×
798
            if (rt)
×
799
                rt->report_error(num_thread, e);
×
800
            else
801
                detail::report_exception_and_terminate(e);
×
802
            return;
×
803
        }
804

805
        get_runtime().get_thread_manager().report_error(num_thread, e);
×
806
    }
×
807

808
    void report_error(std::exception_ptr const& e)
×
809
    {
810
        // Early and late exceptions
811
        if (!threads::threadmanager_is(hpx::state::running))
×
812
        {
813
            hpx::runtime* rt = hpx::get_runtime_ptr();
×
814
            if (rt)
×
815
                rt->report_error(std::size_t(-1), e);
×
816
            else
817
                detail::report_exception_and_terminate(e);
×
818
            return;
×
819
        }
820

821
        std::size_t num_thread = hpx::get_worker_thread_num();
×
822
        get_runtime().get_thread_manager().report_error(num_thread, e);
×
823
    }
×
824

825
    bool register_on_exit(hpx::function<void()> const& f)
×
826
    {
827
        runtime* rt = get_runtime_ptr();
×
828
        if (nullptr == rt)
×
829
            return false;
×
830

831
        rt->on_exit(f);
×
832
        return true;
×
833
    }
×
834

835
    std::size_t get_runtime_instance_number()
×
836
    {
837
        runtime* rt = get_runtime_ptr();
×
838
        return (nullptr == rt) ? 0 : rt->get_instance_number();
×
839
    }
840

841
    ///////////////////////////////////////////////////////////////////////////
842
    std::string get_config_entry(
9,686✔
843
        std::string const& key, std::string const& dflt)
844
    {
845
        if (get_runtime_ptr() != nullptr)
9,686✔
846
        {
847
            return get_runtime().get_config().get_entry(key, dflt);
6,017✔
848
        }
849

850
        return dflt;
3,669✔
851
    }
9,686✔
852

853
    std::string get_config_entry(std::string const& key, std::size_t dflt)
23,781✔
854
    {
855
        if (get_runtime_ptr() != nullptr)
23,781✔
856
        {
857
            return get_runtime().get_config().get_entry(key, dflt);
21,335✔
858
        }
859

860
        return std::to_string(dflt);
2,446✔
861
    }
23,781✔
862

863
    // set entries
864
    void set_config_entry(std::string const& key, std::string const& value)
4✔
865
    {
866
        if (get_runtime_ptr() != nullptr)
4✔
867
        {
868
            get_runtime_ptr()->get_config().add_entry(key, value);
4✔
869
            return;
4✔
870
        }
871
    }
4✔
872

873
    void set_config_entry(std::string const& key, std::size_t value)
×
874
    {
875
        set_config_entry(key, std::to_string(value));
×
876
    }
×
877

878
    void set_config_entry_callback(std::string const& key,
574✔
879
        hpx::function<void(std::string const&, std::string const&)> const&
880
            callback)
881
    {
882
        if (get_runtime_ptr() != nullptr)
574✔
883
        {
884
            get_runtime_ptr()->get_config().add_notification_callback(
1,148✔
885
                key, callback);
574✔
886
            return;
574✔
887
        }
888
    }
574✔
889

890
    namespace util {
891

892
        ///////////////////////////////////////////////////////////////////////////
893
        // retrieve the command line arguments for the current locality
894
        bool retrieve_commandline_arguments(
1✔
895
            hpx::program_options::options_description const& app_options,
896
            hpx::program_options::variables_map& vm)
897
        {
898
            // The command line for this application instance is available from
899
            // this configuration section:
900
            //
901
            //     [hpx]
902
            //     cmd_line=....
903
            //
904
            std::string cmdline;
1✔
905

906
            hpx::util::section& cfg = hpx::get_runtime().get_config();
1✔
907
            if (cfg.has_entry("hpx.cmd_line"))
1✔
908
                cmdline = cfg.get_entry("hpx.cmd_line");
1✔
909

910
            return hpx::local::detail::parse_commandline(cfg, app_options,
2✔
911
                cmdline, vm, util::commandline_error_mode::allow_unregistered);
1✔
912
        }
1✔
913

914
        ///////////////////////////////////////////////////////////////////////////
915
        // retrieve the command line arguments for the current locality
916
        bool retrieve_commandline_arguments(
×
917
            std::string const& appname, hpx::program_options::variables_map& vm)
918
        {
919
            using hpx::program_options::options_description;
920

921
            options_description desc_commandline(
×
922
                "Usage: " + appname + " [options]");
×
923

924
            return retrieve_commandline_arguments(desc_commandline, vm);
×
925
        }
×
926
    }    // namespace util
927

928
    ///////////////////////////////////////////////////////////////////////////
929
    std::size_t get_os_thread_count()
1,453✔
930
    {
931
        runtime* rt = get_runtime_ptr();
1,453✔
932
        if (nullptr == rt)
1,453✔
933
        {
934
            HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
935
                "hpx::get_os_thread_count()",
936
                "the runtime system has not been initialized yet");
937
            return std::size_t(0);
938
        }
939
        return rt->get_config().get_os_thread_count();
1,453✔
940
    }
×
941

942
    bool is_scheduler_numa_sensitive()
×
943
    {
944
        runtime* rt = get_runtime_ptr();
×
945
        if (nullptr == rt)
×
946
        {
947
            HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
948
                "hpx::is_scheduler_numa_sensitive",
949
                "the runtime system has not been initialized yet");
950
            return false;
951
        }
952

953
        if (std::size_t(-1) != get_worker_thread_num())
×
954
            return false;
×
955
        return false;
×
956
    }
×
957

958
    ///////////////////////////////////////////////////////////////////////////
959
    bool is_running()
1,096✔
960
    {
961
        runtime* rt = get_runtime_ptr();
1,096✔
962
        if (nullptr != rt)
1,096✔
963
            return rt->get_state() == hpx::state::running;
1,096✔
964
        return false;
×
965
    }
1,096✔
966

967
    bool is_stopped()
2,747✔
968
    {
969
        if (!detail::exit_called)
2,747✔
970
        {
971
            runtime* rt = get_runtime_ptr();
2,747✔
972
            if (nullptr != rt)
2,747✔
973
                return rt->get_state() == hpx::state::stopped;
2,747✔
974
        }
×
975
        return true;    // assume stopped
×
976
    }
2,747✔
977

978
    bool is_stopped_or_shutting_down()
475,297✔
979
    {
980
        runtime* rt = get_runtime_ptr();
475,297✔
981
        if (!detail::exit_called && nullptr != rt)
475,297✔
982
        {
983
            state st = rt->get_state();
475,297✔
984
            return st >= hpx::state::shutdown;
475,297✔
985
        }
986
        return true;    // assume stopped
×
987
    }
475,297✔
988

989
    bool tolerate_node_faults()
×
990
    {
991
#ifdef HPX_HAVE_FAULT_TOLERANCE
992
        return true;
993
#else
994
        return false;
×
995
#endif
996
    }
997

998
    bool is_starting()
185,003✔
999
    {
1000
        runtime* rt = get_runtime_ptr();
185,003✔
1001
        return nullptr != rt ? rt->get_state() <= hpx::state::startup : true;
185,003✔
1002
    }
1003

1004
    bool is_pre_startup()
×
1005
    {
1006
        runtime* rt = get_runtime_ptr();
×
1007
        return nullptr != rt ? rt->get_state() < hpx::state::startup : true;
×
1008
    }
1009
}    // namespace hpx
1010

1011
///////////////////////////////////////////////////////////////////////////////
1012
namespace hpx::util {
1013

1014
    std::string expand(std::string const& in)
×
1015
    {
1016
        return get_runtime().get_config().expand(in);
×
1017
    }
1018

1019
    void expand(std::string& in)
1✔
1020
    {
1021
        get_runtime().get_config().expand(in, std::string::size_type(-1));
1✔
1022
    }
1✔
1023
}    // namespace hpx::util
1024

1025
///////////////////////////////////////////////////////////////////////////////
1026
namespace hpx::threads {
1027

1028
    threadmanager& get_thread_manager()
50,608✔
1029
    {
1030
        return get_runtime().get_thread_manager();
50,608✔
1031
    }
1032

1033
    // shortcut for runtime_configuration::get_default_stack_size
1034
    std::ptrdiff_t get_default_stack_size()
×
1035
    {
1036
        return get_runtime().get_config().get_default_stack_size();
×
1037
    }
1038

1039
    // shortcut for runtime_configuration::get_stack_size
1040
    std::ptrdiff_t get_stack_size(threads::thread_stacksize stacksize)
1,595,226✔
1041
    {
1042
        if (stacksize == threads::thread_stacksize::current)
1,595,226✔
1043
            return threads::get_self_stacksize();
×
1044

1045
        return get_runtime().get_config().get_stack_size(stacksize);
1,595,221✔
1046
    }
1,595,221✔
1047

1048
    void reset_thread_distribution()
×
1049
    {
1050
        get_runtime().get_thread_manager().reset_thread_distribution();
×
1051
    }
×
1052

1053
    void set_scheduler_mode(threads::policies::scheduler_mode m)
×
1054
    {
1055
        get_runtime().get_thread_manager().set_scheduler_mode(m);
×
1056
    }
×
1057

1058
    void add_scheduler_mode(threads::policies::scheduler_mode m)
1✔
1059
    {
1060
        get_runtime().get_thread_manager().add_scheduler_mode(m);
1✔
1061
    }
1✔
1062

1063
    void add_remove_scheduler_mode(
×
1064
        threads::policies::scheduler_mode to_add_mode,
1065
        threads::policies::scheduler_mode to_remove_mode)
1066
    {
1067
        get_runtime().get_thread_manager().add_remove_scheduler_mode(
×
1068
            to_add_mode, to_remove_mode);
×
1069
    }
×
1070

1071
    void remove_scheduler_mode(threads::policies::scheduler_mode m)
1✔
1072
    {
1073
        get_runtime().get_thread_manager().remove_scheduler_mode(m);
1✔
1074
    }
1✔
1075

1076
    topology const& get_topology()
2✔
1077
    {
1078
        hpx::runtime* rt = hpx::get_runtime_ptr();
2✔
1079
        if (rt == nullptr)
2✔
1080
        {
1081
            HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
1082
                "hpx::threads::get_topology",
1083
                "the hpx runtime system has not been initialized yet");
1084
        }
1085
        return rt->get_topology();
2✔
1086
    }
×
1087
}    // namespace hpx::threads
1088

1089
///////////////////////////////////////////////////////////////////////////////
1090
namespace hpx {
1091
    std::uint64_t get_system_uptime()
90✔
1092
    {
1093
        return runtime::get_system_uptime();
90✔
1094
    }
1095

1096
    hpx::util::runtime_configuration const& get_config()
16,879✔
1097
    {
1098
        return get_runtime().get_config();
16,879✔
1099
    }
1100

1101
    hpx::util::io_service_pool* get_thread_pool(
19,048✔
1102
        char const* name, char const* name_suffix)
1103
    {
1104
        std::string full_name(name);
19,048✔
1105
        full_name += name_suffix;
19,048✔
1106
        return get_runtime().get_thread_pool(full_name.c_str());
19,048✔
1107
    }
19,048✔
1108

1109
    ///////////////////////////////////////////////////////////////////////////
1110
    /// Return true if networking is enabled.
1111
    bool is_networking_enabled()
2,247✔
1112
    {
1113
        runtime* rt = get_runtime_ptr();
2,247✔
1114
        if (nullptr != rt)
2,247✔
1115
        {
1116
            return rt->is_networking_enabled();
1,190✔
1117
        }
1118
        return true;    // be on the safe side, enable networking
1,057✔
1119
    }
2,247✔
1120
}    // namespace hpx
1121

1122
#if defined(_WIN64) && defined(HPX_DEBUG) &&                                   \
1123
    !defined(HPX_HAVE_FIBER_BASED_COROUTINES)
1124
#include <io.h>
1125
#endif
1126

1127
namespace hpx {
1128
    namespace detail {
1129
        ///////////////////////////////////////////////////////////////////////
1130
        // There is no need to protect these global from thread concurrent
1131
        // access as they are access during early startup only.
1132
        std::list<startup_function_type>& global_pre_startup_functions()
2,440✔
1133
        {
1134
            static std::list<startup_function_type>
2,440✔
1135
                global_pre_startup_functions_;
1,054✔
1136
            return global_pre_startup_functions_;
2,440✔
1137
        }
1138

1139
        std::list<startup_function_type>& global_startup_functions()
3,030✔
1140
        {
1141
            static std::list<startup_function_type> global_startup_functions_;
3,030✔
1142
            return global_startup_functions_;
3,030✔
1143
        }
1144

1145
        std::list<shutdown_function_type>& global_pre_shutdown_functions()
2,440✔
1146
        {
1147
            static std::list<shutdown_function_type>
2,440✔
1148
                global_pre_shutdown_functions_;
1,054✔
1149
            return global_pre_shutdown_functions_;
2,440✔
1150
        }
1151

1152
        std::list<shutdown_function_type>& global_shutdown_functions()
2,440✔
1153
        {
1154
            static std::list<shutdown_function_type> global_shutdown_functions_;
2,440✔
1155
            return global_shutdown_functions_;
2,440✔
1156
        }
1157
    }    // namespace detail
1158

1159
    ///////////////////////////////////////////////////////////////////////////
1160
    void register_pre_startup_function(startup_function_type f)
×
1161
    {
1162
        runtime* rt = get_runtime_ptr();
×
1163
        if (nullptr != rt)
×
1164
        {
1165
            if (rt->get_state() > hpx::state::pre_startup)
×
1166
            {
1167
                HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
1168
                    "register_pre_startup_function",
1169
                    "Too late to register a new pre-startup function.");
1170
                return;
1171
            }
1172
            rt->add_pre_startup_function(HPX_MOVE(f));
×
1173
        }
×
1174
        else
1175
        {
1176
            detail::global_pre_startup_functions().push_back(HPX_MOVE(f));
×
1177
        }
1178
    }
×
1179

1180
    void register_startup_function(startup_function_type f)
590✔
1181
    {
1182
        runtime* rt = get_runtime_ptr();
590✔
1183
        if (nullptr != rt)
590✔
1184
        {
1185
            if (rt->get_state() > hpx::state::startup)
×
1186
            {
1187
                HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
1188
                    "register_startup_function",
1189
                    "Too late to register a new startup function.");
1190
                return;
1191
            }
1192
            rt->add_startup_function(HPX_MOVE(f));
×
1193
        }
×
1194
        else
1195
        {
1196
            detail::global_startup_functions().push_back(HPX_MOVE(f));
590✔
1197
        }
1198
    }
590✔
1199

1200
    void register_pre_shutdown_function(shutdown_function_type f)
598✔
1201
    {
1202
        runtime* rt = get_runtime_ptr();
598✔
1203
        if (nullptr != rt)
598✔
1204
        {
1205
            if (rt->get_state() > hpx::state::pre_shutdown)
598✔
1206
            {
1207
                HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
1208
                    "register_pre_shutdown_function",
1209
                    "Too late to register a new pre-shutdown function.");
1210
                return;
1211
            }
1212
            rt->add_pre_shutdown_function(HPX_MOVE(f));
598✔
1213
        }
598✔
1214
        else
1215
        {
1216
            detail::global_pre_shutdown_functions().push_back(HPX_MOVE(f));
×
1217
        }
1218
    }
598✔
1219

1220
    void register_shutdown_function(shutdown_function_type f)
597✔
1221
    {
1222
        runtime* rt = get_runtime_ptr();
597✔
1223
        if (nullptr != rt)
597✔
1224
        {
1225
            if (rt->get_state() > hpx::state::shutdown)
597✔
1226
            {
1227
                HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
1228
                    "register_shutdown_function",
1229
                    "Too late to register a new shutdown function.");
1230
                return;
1231
            }
1232
            rt->add_shutdown_function(HPX_MOVE(f));
597✔
1233
        }
597✔
1234
        else
1235
        {
1236
            detail::global_shutdown_functions().push_back(HPX_MOVE(f));
×
1237
        }
1238
    }
597✔
1239

1240
    void runtime::call_startup_functions(bool pre_startup)
1,250✔
1241
    {
1242
        if (pre_startup)
1,250✔
1243
        {
1244
            set_state(hpx::state::pre_startup);
625✔
1245
            for (startup_function_type& f : pre_startup_functions_)
625✔
1246
            {
1247
                f();
×
1248
            }
1249
        }
625✔
1250
        else
1251
        {
1252
            set_state(hpx::state::startup);
625✔
1253
            for (startup_function_type& f : startup_functions_)
629✔
1254
            {
1255
                f();
4✔
1256
            }
1257
        }
1258
    }
1,250✔
1259

1260
    namespace detail {
1261
        void handle_print_bind(std::size_t num_threads)
×
1262
        {
1263
            threads::topology& top = threads::create_topology();
×
1264
            auto const& rp = hpx::resource::get_partitioner();
×
1265
            auto const& tm = get_runtime().get_thread_manager();
×
1266

1267
            {
1268
                // make sure all output is kept together
1269
                std::ostringstream strm;
×
1270

1271
                strm << std::string(79, '*') << '\n';
×
1272
                strm << "locality: " << hpx::get_locality_id() << '\n';
×
1273
                for (std::size_t i = 0; i != num_threads; ++i)
×
1274
                {
1275
                    // print the mask for the current PU
1276
                    threads::mask_cref_type pu_mask = rp.get_pu_mask(i);
×
1277

1278
                    if (!threads::any(pu_mask))
×
1279
                    {
1280
                        strm << std::setw(4) << i    //-V112
×
1281
                             << ": thread binding disabled" << std::endl;
×
1282
                    }
×
1283
                    else
1284
                    {
1285
                        std::string pool_name = tm.get_pool(i).get_pool_name();
×
1286
                        top.print_affinity_mask(strm, i, pu_mask, pool_name);
×
1287
                    }
×
1288

1289
                    // Make sure the mask does not contradict the CPU bindings
1290
                    // returned by the system (see #973: Would like option to
1291
                    // report HWLOC bindings).
1292
                    error_code ec(throwmode::lightweight);
×
1293
                    std::thread& blob = tm.get_os_thread_handle(i);
×
1294
                    threads::mask_type boundcpu =
1295
                        top.get_cpubind_mask(blob, ec);
×
1296

1297
                    // The masks reported by HPX must be the same as the ones
1298
                    // reported from HWLOC.
1299
                    if (!ec && threads::any(boundcpu) &&
×
1300
                        !threads::equal(boundcpu, pu_mask, num_threads))
×
1301
                    {
1302
                        std::string boundcpu_str = threads::to_string(boundcpu);
×
1303
                        std::string pu_mask_str = threads::to_string(pu_mask);
×
1304
                        HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
1305
                            "handle_print_bind",
1306
                            hpx::util::format(
1307
                                "unexpected mismatch between locality {1}: "
1308
                                "binding reported from HWLOC({2}) and HPX({3}) "
1309
                                "on thread {4}",
1310
                                hpx::get_locality_id(), boundcpu_str,
1311
                                pu_mask_str, i));
1312
                    }
×
1313
                }
×
1314

1315
                std::cout << strm.str();
×
1316
            }
×
1317
        }
×
1318
    }    // namespace detail
1319

1320
    threads::thread_result_type runtime::run_helper(
1,220✔
1321
        hpx::function<runtime::hpx_main_function_type> const& func, int& result,
1322
        bool call_startup)
1323
    {
1324
        bool caught_exception = false;
1,220✔
1325
        try
1326
        {
1327
            {
1328
                hpx::program_options::options_description options;
1,220✔
1329
                result = hpx::local::detail::handle_late_commandline_options(
1,220✔
1330
                    get_config(), options, &detail::handle_print_bind);
1,220✔
1331
                if (result)
1,220✔
1332
                {
1333
                    lbt_ << "runtime_local::run_helper: bootstrap "
1✔
1334
                            "aborted, bailing out";
1335

1336
                    set_state(hpx::state::running);
1✔
1337
                    finalize(-1.0);
1✔
1338

1339
                    return threads::thread_result_type(
1✔
1340
                        threads::thread_schedule_state::terminated,
1✔
1341
                        threads::invalid_thread_id);
1342
                }
1343
            }
1,220✔
1344

1345
            if (call_startup)
1,219✔
1346
            {
1347
                call_startup_functions(true);
625✔
1348
                lbt_ << "(3rd stage) run_helper: ran pre-startup functions";
625✔
1349

1350
                call_startup_functions(false);
625✔
1351
                lbt_ << "(4th stage) run_helper: ran startup functions";
625✔
1352
            }
625✔
1353

1354
            lbt_ << "(4th stage) runtime::run_helper: bootstrap complete";
1,219✔
1355
            set_state(hpx::state::running);
1,219✔
1356

1357
            // Now, execute the user supplied thread function (hpx_main)
1358
            if (!!func)
1,219✔
1359
            {
1360
                lbt_ << "(last stage) runtime::run_helper: about to "
1,085✔
1361
                        "invoke hpx_main";
1362

1363
                // Change our thread description, as we're about to call hpx_main
1364
                threads::set_thread_description(
1,085✔
1365
                    threads::get_self_id(), "hpx_main");
1,085✔
1366

1367
                // Call hpx_main
1368
                result = func();
1,085✔
1369
            }
1,076✔
1370
        }
1,217✔
1371
        catch (...)
1372
        {
1373
            // make sure exceptions thrown in hpx_main don't escape
1374
            // unnoticed
1375
            {
1376
                std::lock_guard<std::mutex> l(mtx_);
7✔
1377
                exception_ = std::current_exception();
7✔
1378
            }
7✔
1379
            result = -1;
7✔
1380
            caught_exception = true;
7✔
1381
        }
7✔
1382

1383
        if (caught_exception)
1,217✔
1384
        {
1385
            HPX_ASSERT(exception_);
7✔
1386
            report_error(exception_, false);
7✔
1387
            finalize(-1.0);    // make sure the application exits
7✔
1388
        }
7✔
1389

1390
        return threads::thread_result_type(
1,217✔
1391
            threads::thread_schedule_state::terminated,
1,217✔
1392
            threads::invalid_thread_id);
1393
    }
1,225✔
1394

1395
    int runtime::start(
626✔
1396
        hpx::function<hpx_main_function_type> const& func, bool blocking)
1397
    {
1398
#if defined(_WIN64) && defined(HPX_DEBUG) &&                                   \
1399
    !defined(HPX_HAVE_FIBER_BASED_COROUTINES)
1400
        // needs to be called to avoid problems at system startup
1401
        // see: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100319
1402
        _isatty(0);
1403
#endif
1404
        // {{{ early startup code - local
1405

1406
        // initialize instrumentation system
1407
#ifdef HPX_HAVE_APEX
1408
        util::external_timer::init(nullptr, 0, 1);
1409
#endif
1410

1411
        LRT_(info).format("cmd_line: {}", get_config().get_cmd_line());
626✔
1412

1413
        lbt_ << "(1st stage) runtime::start: booting locality " << here();
626✔
1414

1415
        // Register this thread with the runtime system to allow calling
1416
        // certain HPX functionality from the main thread. Also calls
1417
        // registered startup callbacks.
1418
        init_tss_helper("main-thread",
626✔
1419
            runtime_local::os_thread_type::main_thread, 0, 0, "", "", false);
1420

1421
#ifdef HPX_HAVE_IO_POOL
1422
        // start the io pool
1423
        io_pool_.run(false);
626✔
1424
        lbt_ << "(1st stage) runtime::start: started the application "
626✔
1425
                "I/O service pool";
1426
#endif
1427
        // start the thread manager
1428
        thread_manager_->run();
626✔
1429
        lbt_ << "(1st stage) runtime::start: started threadmanager";
626✔
1430
        // }}}
1431

1432
        // {{{ launch main
1433
        // register the given main function with the thread manager
1434
        lbt_ << "(1st stage) runtime::start: launching run_helper "
626✔
1435
                "HPX thread";
1436

1437
        threads::thread_init_data data(hpx::bind(&runtime::run_helper, this,
1,878✔
1438
                                           func, std::ref(result_), true),
626✔
1439
            "run_helper", threads::thread_priority::normal,
626✔
1440
            threads::thread_schedule_hint(0), threads::thread_stacksize::large);
626✔
1441

1442
        this->runtime::starting();
626✔
1443
        threads::thread_id_ref_type id = threads::invalid_thread_id;
626✔
1444
        thread_manager_->register_thread(data, id);
626✔
1445
        // }}}
1446

1447
        // block if required
1448
        if (blocking)
626✔
1449
        {
1450
            return wait();    // wait for the shutdown_action to be executed
×
1451
        }
1452
        else
1453
        {
1454
            // wait for at least hpx::state::running
1455
            util::yield_while(
626✔
1456
                [this]() { return get_state() < hpx::state::running; },
76,782✔
1457
                "runtime::start");
1458
        }
1459

1460
        return 0;    // return zero as we don't know the outcome of hpx_main yet
626✔
1461
    }
626✔
1462

1463
    int runtime::start(bool blocking)
16✔
1464
    {
1465
        hpx::function<hpx_main_function_type> empty_main;
16✔
1466
        return start(empty_main, blocking);
16✔
1467
    }
16✔
1468

1469
    ///////////////////////////////////////////////////////////////////////////
1470
    void runtime::notify_finalize()
624✔
1471
    {
1472
        std::unique_lock<std::mutex> l(mtx_);
624✔
1473
        if (!stop_called_)
624✔
1474
        {
1475
            stop_called_ = true;
624✔
1476
            stop_done_ = true;
624✔
1477
            wait_condition_.notify_all();
624✔
1478
        }
624✔
1479
    }
624✔
1480

1481
    void runtime::wait_finalize()
626✔
1482
    {
1483
        std::unique_lock<std::mutex> l(mtx_);
626✔
1484
        while (!stop_done_)
1,198✔
1485
        {
1486
            LRT_(info).format("runtime: about to enter wait state");
574✔
1487
            wait_condition_.wait(l);    //-V1089
572✔
1488
            LRT_(info).format("runtime: exiting wait state");
574✔
1489
        }
1490
    }
624✔
1491

1492
    void runtime::wait_helper(
626✔
1493
        std::mutex& mtx, std::condition_variable& cond, bool& running)
1494
    {
1495
        // signal successful initialization
1496
        {
1497
            std::lock_guard<std::mutex> lk(mtx);
626✔
1498
            running = true;
626✔
1499
            cond.notify_all();
626✔
1500
        }
626✔
1501

1502
        // register this thread with any possibly active Intel tool
1503
        std::string thread_name("main-thread#wait_helper");
626✔
1504
        HPX_ITT_THREAD_SET_NAME(thread_name.c_str());
624✔
1505

1506
        // set thread name as shown in Visual Studio
1507
        util::set_thread_name(thread_name.c_str());
624✔
1508

1509
#if defined(HPX_HAVE_APEX)
1510
        // not registering helper threads - for now
1511
        //util::external_timer::register_thread(thread_name.c_str());
1512
#endif
1513

1514
        wait_finalize();
624✔
1515

1516
        // stop main thread pool
1517
        main_pool_.stop();
624✔
1518
    }
624✔
1519

1520
    int runtime::wait()
626✔
1521
    {
1522
        LRT_(info).format("runtime_local: about to enter wait state");
626✔
1523

1524
        // start the wait_helper in a separate thread
1525
        std::mutex mtx;
626✔
1526
        std::condition_variable cond;
626✔
1527
        bool running = false;
626✔
1528

1529
        std::thread t(hpx::bind(&runtime::wait_helper, this, std::ref(mtx),
1,252✔
1530
            std::ref(cond), std::ref(running)));
626✔
1531

1532
        // wait for the thread to run
1533
        {
1534
            std::unique_lock<std::mutex> lk(mtx);
626✔
1535
            // NOLINTNEXTLINE(bugprone-infinite-loop)
1536
            while (!running)      //-V776 //-V1044
1,252✔
1537
                cond.wait(lk);    //-V1089
626✔
1538
        }
624✔
1539

1540
        // use main thread to drive main thread pool
1541
        main_pool_.thread_run(0);
624✔
1542

1543
        // block main thread
1544
        t.join();
624✔
1545

1546
        thread_manager_->wait();
624✔
1547

1548
        LRT_(info).format("runtime_local: exiting wait state");
624✔
1549
        return result_;
624✔
1550
    }
624✔
1551

1552
    ///////////////////////////////////////////////////////////////////////////
1553
    // First half of termination process: stop thread manager,
1554
    // schedule a task managed by timer_pool to initiate second part
1555
    void runtime::stop(bool blocking)
624✔
1556
    {
1557
        LRT_(warning).format("runtime_local: about to stop services");
624✔
1558

1559
        // execute all on_exit functions whenever the first thread calls this
1560
        this->runtime::stopping();
624✔
1561

1562
        // stop runtime_local services (threads)
1563
        thread_manager_->stop(false);    // just initiate shutdown
624✔
1564

1565
#ifdef HPX_HAVE_APEX
1566
        util::external_timer::finalize();
1567
#endif
1568

1569
        if (threads::get_self_ptr())
624✔
1570
        {
1571
            // schedule task on separate thread to execute stop_helper() below
1572
            // this is necessary as this function (stop()) might have been called
1573
            // from a HPX thread, so it would deadlock by waiting for the thread
1574
            // manager
1575
            std::mutex mtx;
×
1576
            std::condition_variable cond;
×
1577
            std::unique_lock<std::mutex> l(mtx);
×
1578

1579
            std::thread t(hpx::bind(&runtime::stop_helper, this, blocking,
×
1580
                std::ref(cond), std::ref(mtx)));
×
1581
            cond.wait(l);    //-V1089
×
1582

1583
            t.join();
×
1584
        }
×
1585
        else
1586
        {
1587
            thread_manager_->stop(blocking);    // wait for thread manager
624✔
1588

1589
            deinit_global_data();
624✔
1590

1591
            // this disables all logging from the main thread
1592
            deinit_tss_helper("main-thread", 0);
624✔
1593

1594
            LRT_(info).format("runtime_local: stopped all services");
624✔
1595
        }
1596

1597
#ifdef HPX_HAVE_TIMER_POOL
1598
        LTM_(info).format("stop: stopping timer pool");
624✔
1599
        timer_pool_.stop();
624✔
1600
        if (blocking)
624✔
1601
        {
1602
            timer_pool_.join();
624✔
1603
            timer_pool_.clear();
624✔
1604
        }
624✔
1605
#endif
1606
#ifdef HPX_HAVE_IO_POOL
1607
        LTM_(info).format("stop: stopping io pool");
624✔
1608
        io_pool_.stop();
624✔
1609
        if (blocking)
624✔
1610
        {
1611
            io_pool_.join();
624✔
1612
            io_pool_.clear();
624✔
1613
        }
624✔
1614
#endif
1615
    }
624✔
1616

1617
    // Second step in termination: shut down all services.
1618
    // This gets executed as a task in the timer_pool io_service and not as
1619
    // a HPX thread!
1620
    void runtime::stop_helper(
×
1621
        bool blocking, std::condition_variable& cond, std::mutex& mtx)
1622
    {
1623
        // wait for thread manager to exit
1624
        thread_manager_->stop(blocking);    // wait for thread manager
×
1625

1626
        deinit_global_data();
×
1627

1628
        // this disables all logging from the main thread
1629
        deinit_tss_helper("main-thread", 0);
×
1630

1631
        LRT_(info).format("runtime_local: stopped all services");
×
1632

1633
        std::lock_guard<std::mutex> l(mtx);
×
1634
        cond.notify_all();    // we're done now
×
1635
    }
×
1636

1637
    int runtime::suspend()
118✔
1638
    {
1639
        LRT_(info).format("runtime_local: about to suspend runtime");
118✔
1640

1641
        if (state_.load() == hpx::state::sleeping)
118✔
1642
        {
1643
            return 0;
×
1644
        }
1645

1646
        if (state_.load() != hpx::state::running)
118✔
1647
        {
1648
            HPX_THROW_EXCEPTION(hpx::error::invalid_status, "runtime::suspend",
×
1649
                "Can only suspend runtime from running state");
1650
            return -1;
1651
        }
1652

1653
        thread_manager_->suspend();
118✔
1654

1655
#ifdef HPX_HAVE_TIMER_POOL
1656
        timer_pool_.wait();
118✔
1657
#endif
1658
#ifdef HPX_HAVE_IO_POOL
1659
        io_pool_.wait();
118✔
1660
#endif
1661

1662
        set_state(hpx::state::sleeping);
118✔
1663

1664
        return 0;
118✔
1665
    }
118✔
1666

1667
    int runtime::resume()
118✔
1668
    {
1669
        LRT_(info).format("runtime_local: about to resume runtime");
118✔
1670

1671
        if (state_.load() == hpx::state::running)
118✔
1672
        {
1673
            return 0;
×
1674
        }
1675

1676
        if (state_.load() != hpx::state::sleeping)
118✔
1677
        {
1678
            HPX_THROW_EXCEPTION(hpx::error::invalid_status, "runtime::resume",
×
1679
                "Can only resume runtime from suspended state");
1680
            return -1;
1681
        }
1682

1683
        thread_manager_->resume();
118✔
1684

1685
        set_state(hpx::state::running);
118✔
1686

1687
        return 0;
118✔
1688
    }
118✔
1689

1690
    int runtime::finalize(double /*shutdown_timeout*/)
624✔
1691
    {
1692
        notify_finalize();
624✔
1693
        return 0;
624✔
1694
    }
1695

1696
    bool runtime::is_networking_enabled()
×
1697
    {
1698
        return false;
×
1699
    }
1700

1701
    hpx::threads::threadmanager& runtime::get_thread_manager()
371,329✔
1702
    {
1703
        return *thread_manager_;
371,329✔
1704
    }
1705

1706
    std::string runtime::here() const
1,575✔
1707
    {
1708
        return "127.0.0.1";
1,575✔
1709
    }
×
1710

1711
    ///////////////////////////////////////////////////////////////////////////
1712
    bool runtime::report_error(std::size_t num_thread,
5✔
1713
        std::exception_ptr const& e, bool /*terminate_all*/)
1714
    {
1715
        // call thread-specific user-supplied on_error handler
1716
        bool report_exception = true;
5✔
1717
        if (on_error_func_)
5✔
1718
        {
1719
            report_exception = on_error_func_(num_thread, e);
1✔
1720
        }
1✔
1721

1722
        // Early and late exceptions, errors outside of HPX-threads
1723
        if (!threads::get_self_ptr() ||
5✔
1724
            !threads::threadmanager_is(hpx::state::running))
5✔
1725
        {
1726
            // report the error to the local console
1727
            if (report_exception)
×
1728
            {
1729
                detail::report_exception_and_continue(e);
×
1730
            }
×
1731

1732
            // store the exception to be able to rethrow it later
1733
            {
1734
                std::lock_guard<std::mutex> l(mtx_);
×
1735
                exception_ = e;
×
1736
            }
×
1737

1738
            notify_finalize();
×
1739
            stop(false);
×
1740

1741
            return report_exception;
×
1742
        }
1743

1744
        return report_exception;
5✔
1745
    }
5✔
1746

1747
    bool runtime::report_error(std::exception_ptr const& e, bool terminate_all)
5✔
1748
    {
1749
        return report_error(hpx::get_worker_thread_num(), e, terminate_all);
5✔
1750
    }
1751

1752
    void runtime::rethrow_exception()
1,218✔
1753
    {
1754
        if (state_.load() > hpx::state::running)
1,218✔
1755
        {
1756
            std::lock_guard<std::mutex> l(mtx_);
1,218✔
1757
            if (exception_)
1,218✔
1758
            {
1759
                std::exception_ptr e = exception_;
6✔
1760
                exception_ = std::exception_ptr();
6✔
1761
                std::rethrow_exception(e);
7✔
1762
            }
6✔
1763
        }
1,217✔
1764
    }
1,217✔
1765

1766
    ///////////////////////////////////////////////////////////////////////////
1767
    int runtime::run(hpx::function<hpx_main_function_type> const& func)
609✔
1768
    {
1769
        // start the main thread function
1770
        start(func);
609✔
1771

1772
        // now wait for everything to finish
1773
        wait();
609✔
1774
        stop();
609✔
1775

1776
        rethrow_exception();
609✔
1777
        return result_;
609✔
1778
    }
1779

1780
    ///////////////////////////////////////////////////////////////////////////
1781
    int runtime::run()
×
1782
    {
1783
        // start the main thread function
1784
        start();
×
1785

1786
        // now wait for everything to finish
1787
        int result = wait();
×
1788
        stop();
×
1789

1790
        rethrow_exception();
×
1791
        return result;
×
1792
    }
1793

1794
    util::thread_mapper& runtime::get_thread_mapper()
×
1795
    {
1796
        return *thread_support_;
×
1797
    }
1798

1799
    ///////////////////////////////////////////////////////////////////////////
1800
    threads::policies::callback_notifier runtime::get_notification_policy(
1,878✔
1801
        char const* prefix, runtime_local::os_thread_type type)
1802
    {
1803
        typedef bool (runtime::*report_error_t)(
1804
            std::size_t, std::exception_ptr const&, bool);
1805

1806
        using placeholders::_1;
1807
        using placeholders::_2;
1808
        using placeholders::_3;
1809
        using placeholders::_4;
1810

1811
        notification_policy_type notifier;
1,878✔
1812

1813
        notifier.add_on_start_thread_callback(
1,878✔
1814
            hpx::bind(&runtime::init_tss_helper, this, prefix, type, _1, _2, _3,
3,756✔
1815
                _4, false));
1,878✔
1816
        notifier.add_on_stop_thread_callback(
1,878✔
1817
            hpx::bind(&runtime::deinit_tss_helper, this, prefix, _1));
1,878✔
1818
        notifier.set_on_error_callback(
1,878✔
1819
            hpx::bind(static_cast<report_error_t>(&runtime::report_error), this,
3,756✔
1820
                _1, _2, true));
1,878✔
1821

1822
        return notifier;
1,878✔
1823
    }
1,878✔
1824

1825
    void runtime::init_tss_helper(char const* context,
5,800✔
1826
        runtime_local::os_thread_type type, std::size_t local_thread_num,
1827
        std::size_t global_thread_num, char const* pool_name,
1828
        char const* postfix, bool service_thread)
1829
    {
1830
        error_code ec(throwmode::lightweight);
5,838✔
1831
        return init_tss_ex(context, type, local_thread_num, global_thread_num,
11,600✔
1832
            pool_name, postfix, service_thread, ec);
5,838✔
1833
    }
5,838✔
1834

1835
    void runtime::init_tss_ex(char const* context,
5,824✔
1836
        runtime_local::os_thread_type type, std::size_t local_thread_num,
1837
        std::size_t global_thread_num, char const* pool_name,
1838
        char const* postfix, bool service_thread, error_code& ec)
1839
    {
1840
        // set the thread's name, if it's not already set
1841
        HPX_ASSERT(detail::thread_name().empty());
5,824✔
1842

1843
        std::string fullname;
5,835✔
1844
        fullname += context;
5,835✔
1845
        if (postfix && *postfix)
5,835✔
1846
            fullname += postfix;
×
1847
        fullname += "#" + std::to_string(global_thread_num);
5,835✔
1848
        detail::thread_name() = HPX_MOVE(fullname);
5,823✔
1849

1850
        char const* name = detail::thread_name().c_str();
5,798✔
1851

1852
        // initialize thread mapping for external libraries (i.e. PAPI)
1853
        thread_support_->register_thread(name, type);
5,839✔
1854

1855
        // register this thread with any possibly active Intel tool
1856
        HPX_ITT_THREAD_SET_NAME(name);
5,839✔
1857

1858
        // set thread name as shown in Visual Studio
1859
        util::set_thread_name(name);
5,839✔
1860

1861
#if defined(HPX_HAVE_APEX)
1862
        if (std::strstr(name, "worker") != nullptr)
1863
            util::external_timer::register_thread(name);
1864
#endif
1865

1866
        // call thread-specific user-supplied on_start handler
1867
        if (on_start_func_)
5,839✔
1868
        {
1869
            on_start_func_(
×
1870
                local_thread_num, global_thread_num, pool_name, context);
×
1871
        }
×
1872

1873
        // if this is a service thread, set its service affinity
1874
        if (service_thread)
5,839✔
1875
        {
1876
            // FIXME: We don't set the affinity of the service threads on BG/Q,
1877
            // as this is causing a hang (needs to be investigated)
1878
#if !defined(__bgq__)
1879
            threads::mask_cref_type used_processing_units =
1✔
1880
                thread_manager_->get_used_processing_units();
1✔
1881

1882
            // --hpx:bind=none  should disable all affinity definitions
1883
            if (threads::any(used_processing_units))
1✔
1884
            {
1885
                this->topology_.set_thread_affinity_mask(
1✔
1886
                    this->topology_.get_service_affinity_mask(
2✔
1887
                        used_processing_units),
1✔
1888
                    ec);
1✔
1889

1890
                // comment this out for now as on CircleCI this is causing
1891
                // unending grief
1892
                //if (ec)
1893
                //{
1894
                //    HPX_THROW_EXCEPTION(hpx::error::kernel_error,
1895
                //        "runtime::init_tss_ex",
1896
                //        "failed to set thread affinity mask ({}) for service "
1897
                //        "thread: {}",
1898
                //        hpx::threads::to_string(used_processing_units),
1899
                //        detail::thread_name());
1900
                //}
1901
            }
1✔
1902
#endif
1903
        }
1✔
1904
    }
5,839✔
1905

1906
    void runtime::deinit_tss_helper(
5,740✔
1907
        char const* context, std::size_t global_thread_num)
1908
    {
1909
        threads::reset_continuation_recursion_count();
5,693✔
1910

1911
        // call thread-specific user-supplied on_stop handler
1912
        if (on_stop_func_)
5,693✔
1913
        {
1914
            on_stop_func_(global_thread_num, global_thread_num, "", context);
×
1915
        }
×
1916

1917
        // reset PAPI support
1918
        thread_support_->unregister_thread();
5,693✔
1919

1920
        // reset thread local storage
1921
        detail::thread_name().clear();
5,693✔
1922
    }
5,693✔
1923

1924
    void runtime::add_pre_startup_function(startup_function_type f)
×
1925
    {
1926
        if (!f.empty())
×
1927
        {
1928
            std::lock_guard<std::mutex> l(mtx_);
×
1929
            pre_startup_functions_.push_back(HPX_MOVE(f));
×
1930
        }
×
1931
    }
×
1932

1933
    void runtime::add_startup_function(startup_function_type f)
4✔
1934
    {
1935
        if (!f.empty())
4✔
1936
        {
1937
            std::lock_guard<std::mutex> l(mtx_);
4✔
1938
            startup_functions_.push_back(HPX_MOVE(f));
4✔
1939
        }
4✔
1940
    }
4✔
1941

1942
    void runtime::add_pre_shutdown_function(shutdown_function_type f)
×
1943
    {
1944
        if (!f.empty())
×
1945
        {
1946
            std::lock_guard<std::mutex> l(mtx_);
×
1947
            pre_shutdown_functions_.push_back(HPX_MOVE(f));
×
1948
        }
×
1949
    }
×
1950

1951
    void runtime::add_shutdown_function(shutdown_function_type f)
1✔
1952
    {
1953
        if (!f.empty())
1✔
1954
        {
1955
            std::lock_guard<std::mutex> l(mtx_);
1✔
1956
            shutdown_functions_.push_back(HPX_MOVE(f));
1✔
1957
        }
1✔
1958
    }
1✔
1959

1960
    hpx::util::io_service_pool* runtime::get_thread_pool(char const* name)
2,659✔
1961
    {
1962
        HPX_ASSERT(name != nullptr);
2,659✔
1963
#ifdef HPX_HAVE_IO_POOL
1964
        if (0 == std::strncmp(name, "io", 2))
2,659✔
1965
            return &io_pool_;
1✔
1966
#endif
1967
#ifdef HPX_HAVE_TIMER_POOL
1968
        if (0 == std::strncmp(name, "timer", 5))
2,658✔
1969
            return &timer_pool_;
2,656✔
1970
#endif
1971
        if (0 == std::strncmp(name, "main", 4))    //-V112
2✔
1972
            return &main_pool_;
2✔
1973

1974
        HPX_THROW_EXCEPTION(hpx::error::bad_parameter,
×
1975
            "runtime::get_thread_pool", "unknown thread pool requested: {}",
1976
            name);
1977
        return nullptr;
1978
    }
2,659✔
1979

1980
    /// Register an external OS-thread with HPX
1981
    bool runtime::register_thread(char const* name,
1✔
1982
        std::size_t global_thread_num, bool service_thread, error_code& ec)
1983
    {
1984
        std::string thread_name(name);
1✔
1985
        thread_name += "-thread";
1✔
1986

1987
        init_tss_ex(thread_name.c_str(),
2✔
1988
            runtime_local::os_thread_type::custom_thread, global_thread_num,
1✔
1989
            global_thread_num, "", nullptr, service_thread, ec);
1✔
1990

1991
        return !ec ? true : false;
1✔
1992
    }
1✔
1993

1994
    /// Unregister an external OS-thread with HPX
1995
    bool runtime::unregister_thread()
4✔
1996
    {
1997
        deinit_tss_helper(
4✔
1998
            detail::thread_name().c_str(), hpx::get_worker_thread_num());
4✔
1999
        return true;
4✔
2000
    }
2001

2002
    // Access data for a given OS thread that was previously registered by
2003
    // \a register_thread. This function must be called from a thread that was
2004
    // previously registered with the runtime.
2005
    runtime_local::os_thread_data runtime::get_os_thread_data(
×
2006
        std::string const& label) const
2007
    {
2008
        return thread_support_->get_os_thread_data(label);
×
2009
    }
2010

2011
    /// Enumerate all OS threads that have registered with the runtime.
2012
    bool runtime::enumerate_os_threads(
4✔
2013
        hpx::function<bool(os_thread_data const&)> const& f) const
2014
    {
2015
        return thread_support_->enumerate_os_threads(f);
4✔
2016
    }
2017

2018
    ///////////////////////////////////////////////////////////////////////////
2019
    threads::policies::callback_notifier get_notification_policy(
×
2020
        char const* prefix)
2021
    {
2022
        return get_runtime().get_notification_policy(
×
2023
            prefix, runtime_local::os_thread_type::worker_thread);
×
2024
    }
2025

2026
    std::uint32_t get_locality_id(error_code& ec)
57,402,762✔
2027
    {
2028
        runtime* rt = get_runtime_ptr();
57,391,578✔
2029
        if (nullptr == rt || rt->get_state() == state::invalid)
57,402,762✔
2030
        {
2031
            // same as naming::invalid_locality_id
2032
            return ~static_cast<std::uint32_t>(0);
600,884✔
2033
        }
2034

2035
        return rt->get_locality_id(ec);
56,707,887✔
2036
    }
57,309,077✔
2037

2038
    std::size_t get_num_worker_threads()
1,140✔
2039
    {
2040
        runtime* rt = get_runtime_ptr();
1,140✔
2041
        if (nullptr == rt)
1,140✔
2042
        {
2043
            HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
2044
                "hpx::get_num_worker_threads",
2045
                "the runtime system has not been initialized yet");
2046
            return std::size_t(0);
2047
        }
2048

2049
        return rt->get_num_worker_threads();
1,140✔
2050
    }
×
2051

2052
    /// \brief Return the number of localities which are currently registered
2053
    ///        for the running application.
2054
    std::uint32_t get_num_localities(hpx::launch::sync_policy, error_code& ec)
311✔
2055
    {
2056
        runtime* rt = get_runtime_ptr();
311✔
2057
        if (nullptr == rt)
311✔
2058
        {
2059
            HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
2060
                "hpx::get_num_localities",
2061
                "the runtime system has not been initialized yet");
2062
            return std::size_t(0);
2063
        }
2064

2065
        return rt->get_num_localities(hpx::launch::sync, ec);
311✔
2066
    }
×
2067

2068
    std::uint32_t get_initial_num_localities()
67,873✔
2069
    {
2070
        runtime* rt = get_runtime_ptr();
67,873✔
2071
        if (nullptr == rt)
67,873✔
2072
        {
2073
            HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
2074
                "hpx::get_initial_num_localities",
2075
                "the runtime system has not been initialized yet");
2076
            return std::size_t(0);
2077
        }
2078

2079
        return rt->get_initial_num_localities();
67,873✔
2080
    }
×
2081

2082
    hpx::future<std::uint32_t> get_num_localities()
9✔
2083
    {
2084
        runtime* rt = get_runtime_ptr();
9✔
2085
        if (nullptr == rt)
9✔
2086
        {
2087
            HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
2088
                "hpx::get_num_localities",
2089
                "the runtime system has not been initialized yet");
2090
            return make_ready_future(std::uint32_t(0));
2091
        }
2092

2093
        return rt->get_num_localities();
9✔
2094
    }
×
2095

2096
    namespace threads {
2097
        char const* get_stack_size_name(std::ptrdiff_t size)
2✔
2098
        {
2099
            thread_stacksize size_enum = thread_stacksize::unknown;
2✔
2100

2101
            hpx::util::runtime_configuration const& rtcfg = hpx::get_config();
2✔
2102
            if (rtcfg.get_stack_size(thread_stacksize::small_) == size)
2✔
2103
                size_enum = thread_stacksize::small_;
×
2104
            else if (rtcfg.get_stack_size(thread_stacksize::medium) == size)
2✔
2105
                size_enum = thread_stacksize::medium;
×
2106
            else if (rtcfg.get_stack_size(thread_stacksize::large) == size)
2✔
2107
                size_enum = thread_stacksize::large;
2✔
2108
            else if (rtcfg.get_stack_size(thread_stacksize::huge) == size)
×
2109
                size_enum = thread_stacksize::huge;
×
2110
            else if (rtcfg.get_stack_size(thread_stacksize::nostack) == size)
×
2111
                size_enum = thread_stacksize::nostack;
×
2112

2113
            return get_stack_size_enum_name(size_enum);
2✔
2114
        }
2115
    }    // namespace threads
2116
}    // namespace hpx
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

© 2025 Coveralls, Inc