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

STEllAR-GROUP / hpx / #867

15 Jan 2023 08:00PM UTC coverage: 86.487% (+0.5%) from 85.951%
#867

push

StellarBot
Merge #6135

6135: Fixing warnings reported by MSVC analysis r=hkaiser a=hkaiser

- adding MSVC specific #pragma's to suppress the benign warnings


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

120 of 120 new or added lines in 33 files covered. (100.0%)

174599 of 201880 relevant lines covered (86.49%)

1945607.64 hits per line

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

73.33
/libs/core/runtime_local/src/pool_timer.cpp
1
//  Copyright (c) 2016 Bibek Wagle
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/config.hpp>
8
#include <hpx/assert.hpp>
9
#include <hpx/functional/bind_front.hpp>
10
#include <hpx/functional/deferred_call.hpp>
11
#include <hpx/functional/function.hpp>
12
#include <hpx/io_service/io_service_pool.hpp>
13
#include <hpx/modules/errors.hpp>
14
#include <hpx/runtime_local/pool_timer.hpp>
15
#include <hpx/runtime_local/runtime_local.hpp>
16
#include <hpx/runtime_local/shutdown_function.hpp>
17
#include <hpx/synchronization/spinlock.hpp>
18
#include <hpx/thread_support/unlock_guard.hpp>
19

20
#include <asio/basic_waitable_timer.hpp>
21

22
#include <chrono>
23
#include <memory>
24
#include <mutex>
25
#include <string>
26
#include <system_error>
27

28
namespace hpx ::util::detail {
29

30
    ///////////////////////////////////////////////////////////////////////////
31
    class pool_timer : public std::enable_shared_from_this<pool_timer>
32
    {
33
    private:
34
        friend class util::pool_timer;
35

36
        using mutex_type = hpx::spinlock;
37

38
    public:
39
        pool_timer();
40

41
        pool_timer(hpx::function<bool()> const& f,
42
            hpx::function<void()> const& on_term,
43
            std::string const& description, bool pre_shutdown);
44

45
        ~pool_timer();
46

47
        bool start(
48
            hpx::chrono::steady_duration const& time_duration, bool evaluate);
49
        bool stop();
50

51
        bool is_started() const
×
52
        {
53
            return is_started_;
×
54
        }
55
        bool is_terminated() const
×
56
        {
57
            return is_terminated_;
×
58
        }
59
        void timer_handler(std::error_code const&);
60

61
        void terminate();    // handle system shutdown
62
        bool stop_locked();
63

64
    private:
65
        using deadline_timer =
66
            asio::basic_waitable_timer<std::chrono::steady_clock>;
67

68
        mutable mutex_type mtx_;
69
        hpx::function<bool()> f_;          ///< function to call
70
        hpx::function<void()> on_term_;    ///< function to call on termination
71
        std::string description_;    ///< description of this interval timer
72

73
        bool pre_shutdown_;     ///< execute termination during pre-shutdown
74
        bool is_started_;       ///< timer has been started (is running)
75
        bool first_start_;      ///< flag to distinguish first start invocation
76
        bool is_terminated_;    ///< The timer has been terminated
77
        bool is_stopped_;
78
        deadline_timer* timer_;
79
    };
80

81
    ///////////////////////////////////////////////////////////////////////////
82
    pool_timer::pool_timer()
×
83
      : pre_shutdown_()
×
84
      , is_started_(false)
×
85
      , first_start_(true)
×
86
      , is_terminated_(false)
×
87
      , is_stopped_(false)
×
88
      , timer_(new deadline_timer(
×
89
            hpx::get_runtime().get_thread_pool("timer_pool")->get_io_service()))
×
90
    {
×
91
    }
×
92

93
    pool_timer::pool_timer(hpx::function<bool()> const& f,
286✔
94
        hpx::function<void()> const& on_term, std::string const& description,
95
        bool pre_shutdown)
96
      : f_(f)
286✔
97
      , on_term_(on_term)
286✔
98
      , description_(description)
286✔
99
      , pre_shutdown_(pre_shutdown)
286✔
100
      , is_started_(false)
286✔
101
      , first_start_(true)
286✔
102
      , is_terminated_(false)
286✔
103
      , is_stopped_(false)
286✔
104
      , timer_(new deadline_timer(
572✔
105
            hpx::get_runtime().get_thread_pool("timer_pool")->get_io_service()))
286✔
106
    {
286✔
107
    }
286✔
108

109
    void pool_timer::timer_handler(std::error_code const& err)
1✔
110
    {
111
        if (!is_stopped_ || !is_terminated_)
1✔
112
        {
113
            is_started_ = false;
1✔
114
            if (!err)
1✔
115
                f_();
1✔
116
        }
1✔
117
    }
1✔
118

119
    bool pool_timer::start(
10✔
120
        hpx::chrono::steady_duration const& time_duration, bool /* evaluate_ */)
121
    {
122
        std::unique_lock<mutex_type> l(mtx_);
10✔
123
        if (is_terminated_)
10✔
124
            return false;
×
125

126
        if (!is_started_)
10✔
127
        {
128
            is_stopped_ = false;
1✔
129
            is_started_ = true;
1✔
130

131
            if (first_start_)
1✔
132
            {
133
                first_start_ = false;
1✔
134

135
                hpx::unlock_guard<std::unique_lock<mutex_type>> ul(l);
1✔
136
                if (pre_shutdown_)
1✔
137
                {
138
                    register_pre_shutdown_function(util::deferred_call(
1✔
139
                        &pool_timer::terminate, this->shared_from_this()));
1✔
140
                }
1✔
141
                else
142
                {
143
                    register_shutdown_function(util::deferred_call(
×
144
                        &pool_timer::terminate, this->shared_from_this()));
×
145
                }
146
            }
1✔
147

148
            HPX_ASSERT(timer_ != nullptr);
1✔
149
            timer_->expires_from_now(time_duration.value());
1✔
150
            timer_->async_wait(hpx::bind_front(    //-V779
1✔
151
                &pool_timer::timer_handler, this->shared_from_this()));
1✔
152

153
            return true;
1✔
154
        }
155
        return false;
9✔
156
    }
10✔
157

158
    bool pool_timer::stop()
19,849,997✔
159
    {
160
        std::lock_guard<mutex_type> l(mtx_);
19,849,997✔
161
        return stop_locked();
19,849,997✔
162
    }
19,849,997✔
163

164
    bool pool_timer::stop_locked()
19,850,283✔
165
    {
166
        if (!is_terminated_ && is_started_ && !is_stopped_)
19,850,283✔
167
        {
168
            is_started_ = false;
×
169
            is_stopped_ = true;
×
170

171
            HPX_ASSERT(timer_ != nullptr);
×
172
            timer_->cancel();
×
173
            return true;
×
174
        }
175
        return false;
19,850,283✔
176
    }
19,850,283✔
177

178
    void pool_timer::terminate()
573✔
179
    {
180
        std::unique_lock<mutex_type> l(mtx_);
573✔
181
        if (!is_terminated_)
573✔
182
        {
183
            is_terminated_ = true;
286✔
184
            stop_locked();
286✔
185

186
            if (on_term_)
286✔
187
            {
188
                l.unlock();
286✔
189
                on_term_();
286✔
190
            }
286✔
191
        }
286✔
192
        delete timer_;
573✔
193
        timer_ = nullptr;
573✔
194
    }
573✔
195

196
    pool_timer::~pool_timer()
286✔
197
    {
198
        try
199
        {
200
            terminate();
286✔
201
        }
286✔
202
        catch (...)
203
        {
204
            ;    // there is nothing we can do here
205
        }
×
206
    }
286✔
207
}    // namespace hpx::util::detail
208

209
namespace hpx::util {
210
    pool_timer::pool_timer() = default;
×
211

212
    pool_timer::pool_timer(hpx::function<bool()> const& f,
286✔
213
        hpx::function<void()> const& on_term, std::string const& description,
214
        bool pre_shutdown)
215
      : timer_(std::make_shared<detail::pool_timer>(
572✔
216
            f, on_term, description, pre_shutdown))
286✔
217
    {
218
    }
286✔
219

220
    pool_timer::~pool_timer()
286✔
221
    {
222
        timer_->terminate();
286✔
223
    }
286✔
224

225
    bool pool_timer::start(
10✔
226
        hpx::chrono::steady_duration const& time_duration, bool evaluate)
227
    {
228
        return timer_->start(time_duration, evaluate);
10✔
229
    }
230

231
    bool pool_timer::stop()
19,849,997✔
232
    {
233
        return timer_->stop();
19,849,997✔
234
    }
235

236
    bool pool_timer::is_started() const
×
237
    {
238
        return timer_->is_started();
×
239
    }
240

241
    bool pool_timer::is_terminated() const
×
242
    {
243
        return timer_->is_terminated();
×
244
    }
245
}    // namespace hpx::util
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