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

STEllAR-GROUP / hpx / #856

28 Dec 2022 02:00AM UTC coverage: 86.602% (+0.05%) from 86.55%
#856

push

StellarBot
Merge #6119

6119: Update CMakeLists.txt r=hkaiser a=khuck

updating the default APEX version


Co-authored-by: Kevin Huck <khuck@cs.uoregon.edu>

174566 of 201573 relevant lines covered (86.6%)

1876093.78 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,221✔
192
        {
193
            exit_called = true;
1,221✔
194
        }
1,221✔
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,221✔
205
    {
206
        // initialize global variables
207
        hpx::threads::coroutines::attach_debugger_on_sigv =
1,221✔
208
            get_config_entry("hpx.attach_debugger", "") == "exception";
1,221✔
209
        hpx::threads::coroutines::diagnostics_on_terminate =
1,221✔
210
            get_config_entry("hpx.diagnostics_on_terminate", "1") == "1";
1,221✔
211
        hpx::threads::coroutines::exception_verbosity = util::from_string<int>(
1,221✔
212
            get_config_entry("hpx.exception_verbosity", "2"));
1,221✔
213
        hpx::threads::coroutines::exception_verbosity = 0;
1,221✔
214
#if defined(HPX_HAVE_STACKTRACES) && defined(HPX_HAVE_THREAD_BACKTRACE_DEPTH)
215
        hpx::threads::coroutines::exception_verbosity =
1,221✔
216
            util::from_string<int>(get_config_entry(
1,221✔
217
                "hpx.trace_depth", HPX_HAVE_THREAD_BACKTRACE_DEPTH));
1,221✔
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,221✔
225
        {
226
            struct sigaction new_action;
227
            new_action.sa_handler = hpx::termination_handler;
1,221✔
228
            sigemptyset(&new_action.sa_mask);
1,221✔
229
            new_action.sa_flags = 0;
1,221✔
230

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

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

248
        std::set_new_handler(hpx::new_handler);
1,221✔
249
    }
1,221✔
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,536✔
273
    {
274
        if (s < state::invalid || s >= state::last_valid_runtime_state)
2,536✔
275
        {
276
            return "invalid (value out of bounds)";
×
277
        }
278
        return strings::runtime_state_names[static_cast<int>(s) + 1];
2,536✔
279
    }
2,536✔
280

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

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

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

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

333
        if (initialize)
624✔
334
        {
335
            init();
624✔
336
        }
624✔
337
    }
624✔
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,218✔
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,218✔
382

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

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

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

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

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

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

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

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

433
            for (shutdown_function_type& f :
1,218✔
434
                detail::global_shutdown_functions())
1,218✔
435
            {
436
                add_shutdown_function(HPX_MOVE(f));
×
437
            }
438
            detail::global_shutdown_functions().clear();
1,218✔
439
        }
1,218✔
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,218✔
453
    }
1,218✔
454

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

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

466
        LPROGRESS_;
1,215✔
467

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

472
        util::reinit_destruct();
1,215✔
473
        resource::detail::delete_partitioner();
1,215✔
474
    }
1,837✔
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,218✔
483
    {
484
        state_.store(hpx::state::pre_main);
1,218✔
485
    }
1,218✔
486

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

491
        using value_type = hpx::function<void()>;
492

493
        std::lock_guard<std::mutex> l(mtx_);
1,216✔
494
        for (value_type const& f : on_exit_functions_)
1,216✔
495
            f();
×
496
    }
1,216✔
497

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

503
    hpx::util::runtime_configuration& runtime::get_config()
1,654,903✔
504
    {
505
        return rtcfg_;
1,654,903✔
506
    }
507

508
    hpx::util::runtime_configuration const& runtime::get_config() const
67,872✔
509
    {
510
        return rtcfg_;
67,872✔
511
    }
512

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

518
    state runtime::get_state() const
94,291,662✔
519
    {
520
        return state_.load();
94,284,325✔
521
    }
522

523
    threads::topology const& runtime::get_topology() const
4✔
524
    {
525
        return topology_;
4✔
526
    }
527

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

534
    ///////////////////////////////////////////////////////////////////////////
535
    std::atomic<int> runtime::instance_number_counter_(-1);
536

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

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

552
        runtime_ = this;
1,218✔
553
        runtime_uptime() = hpx::chrono::high_resolution_clock::now();
1,218✔
554
    }
1,218✔
555

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

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

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

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

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

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

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

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

617
    std::uint32_t runtime::get_locality_id(error_code& /* ec */) const
25,474,456✔
618
    {
619
        return 0;
25,475,267✔
620
    }
621

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

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

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

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

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

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

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

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

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

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

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

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

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

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

739
    ///////////////////////////////////////////////////////////////////////////
740
    runtime& get_runtime()
1,798,859✔
741
    {
742
        HPX_ASSERT(get_runtime_ptr() != nullptr);
1,798,859✔
743
        return *get_runtime_ptr();
1,798,857✔
744
    }
745

746
    runtime*& get_runtime_ptr()
101,544,272✔
747
    {
748
        static runtime* runtime_ = nullptr;
749
        return runtime_;
101,544,272✔
750
    }
751

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

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

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

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

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

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

807
        get_runtime().get_thread_manager().report_error(num_thread, e);
×
808
    }
×
809

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

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

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

833
        rt->on_exit(f);
×
834
        return true;
×
835
    }
×
836

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

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

852
        return dflt;
3,663✔
853
    }
9,682✔
854

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

862
        return std::to_string(dflt);
2,442✔
863
    }
23,779✔
864

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

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

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

892
    namespace util {
893

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

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

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

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

923
            options_description desc_commandline(
×
924
                "Usage: " + appname + " [options]");
×
925

926
            return retrieve_commandline_arguments(desc_commandline, vm);
×
927
        }
×
928
    }    // namespace util
929

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

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

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

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

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

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

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

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

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

1013
///////////////////////////////////////////////////////////////////////////////
1014
namespace hpx::util {
1015

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

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

1027
///////////////////////////////////////////////////////////////////////////////
1028
namespace hpx::threads {
1029

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

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

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

1047
        return get_runtime().get_config().get_stack_size(stacksize);
1,595,177✔
1048
    }
1,595,177✔
1049

1050
    void reset_thread_distribution()
×
1051
    {
1052
        get_runtime().get_thread_manager().reset_thread_distribution();
×
1053
    }
×
1054

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

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

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

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

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

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

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

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

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

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

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

1141
        std::list<startup_function_type>& global_startup_functions()
3,026✔
1142
        {
1143
            static std::list<startup_function_type> global_startup_functions_;
3,026✔
1144
            return global_startup_functions_;
3,026✔
1145
        }
1146

1147
        std::list<shutdown_function_type>& global_pre_shutdown_functions()
2,436✔
1148
        {
1149
            static std::list<shutdown_function_type>
2,436✔
1150
                global_pre_shutdown_functions_;
1,052✔
1151
            return global_pre_shutdown_functions_;
2,436✔
1152
        }
1153

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1338
                    set_state(hpx::state::running);
1✔
1339
                    finalize(-1.0);
1✔
1340

1341
                    return threads::thread_result_type(
1✔
1342
                        threads::thread_schedule_state::terminated,
1✔
1343
                        threads::invalid_thread_id);
1344
                }
1345
            }
1,218✔
1346

1347
            if (call_startup)
1,217✔
1348
            {
1349
                call_startup_functions(true);
623✔
1350
                lbt_ << "(3rd stage) run_helper: ran pre-startup functions";
623✔
1351

1352
                call_startup_functions(false);
623✔
1353
                lbt_ << "(4th stage) run_helper: ran startup functions";
623✔
1354
            }
623✔
1355

1356
            lbt_ << "(4th stage) runtime::run_helper: bootstrap complete";
1,217✔
1357
            set_state(hpx::state::running);
1,217✔
1358

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

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

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

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

1392
        return threads::thread_result_type(
1,215✔
1393
            threads::thread_schedule_state::terminated,
1,215✔
1394
            threads::invalid_thread_id);
1395
    }
1,223✔
1396

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

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

1413
        LRT_(info).format("cmd_line: {}", get_config().get_cmd_line());
624✔
1414

1415
        lbt_ << "(1st stage) runtime::start: booting locality " << here();
624✔
1416

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

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

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

1439
        threads::thread_init_data data(hpx::bind(&runtime::run_helper, this,
1,872✔
1440
                                           func, std::ref(result_), true),
624✔
1441
            "run_helper", threads::thread_priority::normal,
624✔
1442
            threads::thread_schedule_hint(0), threads::thread_stacksize::large);
624✔
1443

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

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

1462
        return 0;    // return zero as we don't know the outcome of hpx_main yet
624✔
1463
    }
624✔
1464

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

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

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

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

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

1508
        // set thread name as shown in Visual Studio
1509
        util::set_thread_name(thread_name.c_str());
622✔
1510

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

1516
        wait_finalize();
622✔
1517

1518
        // stop main thread pool
1519
        main_pool_.stop();
622✔
1520
    }
622✔
1521

1522
    int runtime::wait()
624✔
1523
    {
1524
        LRT_(info).format("runtime_local: about to enter wait state");
624✔
1525

1526
        // start the wait_helper in a separate thread
1527
        std::mutex mtx;
624✔
1528
        std::condition_variable cond;
624✔
1529
        bool running = false;
624✔
1530

1531
        std::thread t(hpx::bind(&runtime::wait_helper, this, std::ref(mtx),
1,248✔
1532
            std::ref(cond), std::ref(running)));
624✔
1533

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

1542
        // use main thread to drive main thread pool
1543
        main_pool_.thread_run(0);
622✔
1544

1545
        // block main thread
1546
        t.join();
622✔
1547

1548
        thread_manager_->wait();
622✔
1549

1550
        LRT_(info).format("runtime_local: exiting wait state");
622✔
1551
        return result_;
622✔
1552
    }
622✔
1553

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

1561
        // execute all on_exit functions whenever the first thread calls this
1562
        this->runtime::stopping();
622✔
1563

1564
        // stop runtime_local services (threads)
1565
        thread_manager_->stop(false);    // just initiate shutdown
622✔
1566

1567
#ifdef HPX_HAVE_APEX
1568
        util::external_timer::finalize();
1569
#endif
1570

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

1581
            std::thread t(hpx::bind(&runtime::stop_helper, this, blocking,
×
1582
                std::ref(cond), std::ref(mtx)));
×
1583
            cond.wait(l);
×
1584

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

1591
            deinit_global_data();
622✔
1592

1593
            // this disables all logging from the main thread
1594
            deinit_tss_helper("main-thread", 0);
622✔
1595

1596
            LRT_(info).format("runtime_local: stopped all services");
622✔
1597
        }
1598

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

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

1628
        deinit_global_data();
×
1629

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

1633
        LRT_(info).format("runtime_local: stopped all services");
×
1634

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

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

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

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

1655
        thread_manager_->suspend();
118✔
1656

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

1664
        set_state(hpx::state::sleeping);
118✔
1665

1666
        return 0;
118✔
1667
    }
118✔
1668

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

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

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

1685
        thread_manager_->resume();
118✔
1686

1687
        set_state(hpx::state::running);
118✔
1688

1689
        return 0;
118✔
1690
    }
118✔
1691

1692
    int runtime::finalize(double /*shutdown_timeout*/)
622✔
1693
    {
1694
        notify_finalize();
622✔
1695
        return 0;
622✔
1696
    }
1697

1698
    bool runtime::is_networking_enabled()
×
1699
    {
1700
        return false;
×
1701
    }
1702

1703
    hpx::threads::threadmanager& runtime::get_thread_manager()
371,873✔
1704
    {
1705
        return *thread_manager_;
371,873✔
1706
    }
1707

1708
    std::string runtime::here() const
1,573✔
1709
    {
1710
        return "127.0.0.1";
1,573✔
1711
    }
×
1712

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

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

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

1740
            notify_finalize();
×
1741
            stop(false);
×
1742

1743
            return report_exception;
×
1744
        }
1745

1746
        return report_exception;
5✔
1747
    }
5✔
1748

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

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

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

1774
        // now wait for everything to finish
1775
        wait();
607✔
1776
        stop();
607✔
1777

1778
        rethrow_exception();
607✔
1779
        return result_;
607✔
1780
    }
1781

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

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

1792
        rethrow_exception();
×
1793
        return result;
×
1794
    }
1795

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

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

1808
        using placeholders::_1;
1809
        using placeholders::_2;
1810
        using placeholders::_3;
1811
        using placeholders::_4;
1812

1813
        notification_policy_type notifier;
1,872✔
1814

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

1824
        return notifier;
1,872✔
1825
    }
1,872✔
1826

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

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

1845
        std::string fullname;
5,816✔
1846
        fullname += context;
5,816✔
1847
        if (postfix && *postfix)
5,816✔
1848
            fullname += postfix;
×
1849
        fullname += "#" + std::to_string(global_thread_num);
5,819✔
1850
        detail::thread_name() = HPX_MOVE(fullname);
5,805✔
1851

1852
        char const* name = detail::thread_name().c_str();
5,794✔
1853

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

1857
        // register this thread with any possibly active Intel tool
1858
        HPX_ITT_THREAD_SET_NAME(name);
5,821✔
1859

1860
        // set thread name as shown in Visual Studio
1861
        util::set_thread_name(name);
5,821✔
1862

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

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

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

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

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

1908
    void runtime::deinit_tss_helper(
5,727✔
1909
        char const* context, std::size_t global_thread_num)
1910
    {
1911
        threads::reset_continuation_recursion_count();
5,713✔
1912

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

1919
        // reset PAPI support
1920
        thread_support_->unregister_thread();
5,712✔
1921

1922
        // reset thread local storage
1923
        detail::thread_name().clear();
5,712✔
1924
    }
5,712✔
1925

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

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

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

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

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

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

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

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

1993
        return !ec ? true : false;
1✔
1994
    }
1✔
1995

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

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

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

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

2028
    std::uint32_t get_locality_id(error_code& ec)
57,606,256✔
2029
    {
2030
        runtime* rt = get_runtime_ptr();
57,591,174✔
2031
        if (nullptr == rt || rt->get_state() == state::invalid)
57,606,256✔
2032
        {
2033
            // same as naming::invalid_locality_id
2034
            return ~static_cast<std::uint32_t>(0);
646,429✔
2035
        }
2036

2037
        return rt->get_locality_id(ec);
56,858,731✔
2038
    }
57,505,761✔
2039

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

2051
        return rt->get_num_worker_threads();
1,140✔
2052
    }
×
2053

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

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

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

2081
        return rt->get_initial_num_localities();
67,872✔
2082
    }
×
2083

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

2095
        return rt->get_num_localities();
9✔
2096
    }
×
2097

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

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

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