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

STEllAR-GROUP / hpx / #857

28 Dec 2022 11:12PM UTC coverage: 86.543% (-0.06%) from 86.602%
#857

push

StellarBot
Merge #6118

6118: Modernize modules from level 17, 18, 19, and 20 r=hkaiser a=hkaiser

working towards https://github.com/STEllAR-GROUP/hpx/issues/5497

Modules:
- core/threading_base
- full/command_line_handling
- core/io_service
- core/schedulers
- core/synchronization
- core/futures
- core/thread_pools
- core/lcos_local
- core/pack_traversal
- core/resource_partitioner
- core/threading
- full/naming_base


Co-authored-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>

849 of 849 new or added lines in 98 files covered. (100.0%)

174389 of 201505 relevant lines covered (86.54%)

1916353.25 hits per line

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

85.57
/libs/core/execution_base/tests/unit/execution_context.cpp
1
//  Copyright (c) 2019 Thomas Heller
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
#include <hpx/modules/execution_base.hpp>
8
#include <hpx/modules/testing.hpp>
9

10
#include <atomic>
11
#include <chrono>
12
#include <cstddef>
13
#include <mutex>
14
#include <string>
15
#include <thread>
16
#include <vector>
17

18
std::size_t dummy_called = 0;
19

20
struct dummy_context : hpx::execution_base::context_base
2✔
21
{
22
    hpx::execution_base::resource_base const& resource() const noexcept override
×
23
    {
24
        return resource_;
×
25
    }
26

27
    hpx::execution_base::resource_base resource_;
28
};
29

30
struct dummy_agent : hpx::execution_base::agent_base
2✔
31
{
32
    std::string description() const override
×
33
    {
34
        return "";
×
35
    }
×
36
    dummy_context const& context() const noexcept override
×
37
    {
38
        return context_;
×
39
    }
40

41
    void yield(char const*) override
1✔
42
    {
43
        ++dummy_called;
1✔
44
    }
1✔
45
    void yield_k(std::size_t, char const*) override {}
×
46
    void suspend(char const*) override {}
×
47
    void resume(char const*) override {}
×
48
    void abort(char const*) override {}
×
49
    void sleep_for(hpx::chrono::steady_duration const&, char const*) override {}
×
50
    void sleep_until(
×
51
        hpx::chrono::steady_time_point const&, char const*) override
52
    {
53
    }
×
54

55
    dummy_context context_;
56
};
57

58
void test_basic_functionality()
1✔
59
{
60
    // Test that execution context forwards properly and resetting works
61
    {
62
        HPX_TEST_EQ(dummy_called, 0u);
1✔
63
        {
64
            dummy_agent dummy;
1✔
65
            hpx::execution_base::this_thread::reset_agent ctx(dummy);
1✔
66
            hpx::execution_base::this_thread::yield();
1✔
67
        }
1✔
68

69
        HPX_TEST_EQ(dummy_called, 1u);
1✔
70

71
        hpx::execution_base::this_thread::yield();
1✔
72

73
        HPX_TEST_EQ(dummy_called, 1u);
1✔
74
    }
75

76
    // Test that we get different contexts in different threads...
77
    {
78
        auto context = hpx::execution_base::this_thread::agent();
1✔
79
        std::thread t([&context]() {
2✔
80
            HPX_TEST_NEQ(context, hpx::execution_base::this_thread::agent());
1✔
81
        });
1✔
82
        t.join();
1✔
83
    }
1✔
84
}
1✔
85

86
struct simple_spinlock
87
{
88
    simple_spinlock() = default;
1✔
89

90
    void lock()
719,014✔
91
    {
92
        while (locked_.test_and_set())
897,598✔
93
        {
94
            hpx::execution_base::this_thread::yield();
201,702✔
95
        }
96
    }
720,000✔
97

98
    void unlock()
720,000✔
99
    {
100
        locked_.clear();
720,000✔
101
    }
720,000✔
102

103
#if defined(HPX_HAVE_CXX11_ATOMIC_FLAG_INIT)
104
    std::atomic_flag locked_ = ATOMIC_FLAG_INIT;
1✔
105
#else
106
    std::atomic_flag locked_;
107
#endif
108
};
109

110
void test_yield()
1✔
111
{
112
    std::vector<std::thread> ts;
1✔
113
    simple_spinlock mutex;
1✔
114
    std::size_t counter = 0;
1✔
115
    std::size_t repetitions = 1000;
1✔
116
    for (std::size_t i = 0; i != std::thread::hardware_concurrency() * 10; ++i)
721✔
117
    {
118
        ts.emplace_back([&mutex, &counter, repetitions]() {
1,439✔
119
            for (std::size_t repeat = 0; repeat != repetitions; ++repeat)
720,378✔
120
            {
121
                std::unique_lock<simple_spinlock> l(mutex);
719,659✔
122
                ++counter;
719,659✔
123
            }
719,659✔
124
        });
720✔
125
    }
720✔
126

127
    for (auto& t : ts)
721✔
128
        t.join();
720✔
129

130
    HPX_TEST_EQ(
1✔
131
        counter, std::thread::hardware_concurrency() * repetitions * 10);
132
}
1✔
133

134
void test_suspend_resume()
1✔
135
{
136
    std::mutex mtx;
1✔
137
    hpx::execution_base::agent_ref suspended;
1✔
138

139
    bool resumed = false;
1✔
140

141
    std::thread t1([&mtx, &suspended, &resumed]() {
2✔
142
        auto context = hpx::execution_base::this_thread::agent();
1✔
143
        {
144
            std::unique_lock<std::mutex> l(mtx);
1✔
145
            suspended = context;
1✔
146
        }
1✔
147
        context.suspend();
1✔
148
        resumed = true;
1✔
149
    });
1✔
150

151
    while (true)
375✔
152
    {
153
        std::unique_lock<std::mutex> l(mtx);
375✔
154
        if (suspended)
375✔
155
            break;
1✔
156
    }
375✔
157

158
    suspended.resume();
1✔
159

160
    t1.join();
1✔
161
    HPX_TEST(resumed);
1✔
162
}
1✔
163

164
void test_sleep()
1✔
165
{
166
    auto now = std::chrono::steady_clock::now();
1✔
167
    auto sleep_duration = std::chrono::milliseconds(100);
1✔
168
    hpx::execution_base::this_thread::sleep_for(sleep_duration);
1✔
169
    HPX_TEST(now + sleep_duration <= std::chrono::steady_clock::now());
1✔
170

171
    auto sleep_time = sleep_duration * 2 + std::chrono::steady_clock::now();
1✔
172
    hpx::execution_base::this_thread::sleep_until(sleep_time);
1✔
173
    HPX_TEST(now + sleep_duration * 2 <= std::chrono::steady_clock::now());
1✔
174
}
1✔
175

176
int main()
1✔
177
{
178
    test_basic_functionality();
1✔
179
    test_yield();
1✔
180
    test_suspend_resume();
1✔
181
    test_sleep();
1✔
182

183
    return hpx::util::report_errors();
1✔
184
}
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