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

STEllAR-GROUP / hpx / #862

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

push

StellarBot
Merge #6130

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

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

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

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

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

174767 of 201851 relevant lines covered (86.58%)

2069816.07 hits per line

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

64.67
/libs/core/errors/src/exception.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/errors/error.hpp>
11
#include <hpx/errors/error_code.hpp>
12
#include <hpx/errors/exception.hpp>
13
#include <hpx/errors/exception_info.hpp>
14
#include <hpx/errors/exception_list.hpp>
15
#include <hpx/modules/format.hpp>
16
#include <hpx/modules/logging.hpp>
17

18
#if defined(HPX_WINDOWS)
19
#include <process.h>
20
#elif defined(HPX_HAVE_UNISTD_H)
21
#include <unistd.h>
22
#endif
23

24
#include <algorithm>
25
#include <atomic>
26
#include <cstddef>
27
#include <cstdint>
28
#include <exception>
29
#include <memory>
30
#include <stdexcept>
31
#include <string>
32
#include <system_error>
33
#include <utility>
34
#include <vector>
35

36
namespace hpx {
37
    ///////////////////////////////////////////////////////////////////////////
38
    /// Construct a hpx::exception from a \a hpx::error.
39
    ///
40
    /// \param e    The parameter \p e holds the hpx::error code the new
41
    ///             exception should encapsulate.
42
    exception::exception(hpx::error e)
270,306✔
43
      : std::system_error(make_error_code(e, throwmode::plain))
270,306✔
44
    {
270,306✔
45
        HPX_ASSERT((e >= hpx::error::success && e < hpx::error::last_error) ||
270,312✔
46
            (e & hpx::error::system_error_flag));
47
        if (e != hpx::error::success)
270,307✔
48
        {
49
            LERR_(error).format(    //-V1067
736✔
50
                "created exception: {}", this->what());
736✔
51
        }
736✔
52
    }
270,307✔
53

54
    /// Construct a hpx::exception from a boost#system_error.
55
    exception::exception(std::system_error const& e)
×
56
      : std::system_error(e)
×
57
    {
×
58
        LERR_(error).format("created exception: {}", this->what());    //-V1067
×
59
    }
×
60

61
    /// Construct a hpx::exception from a boost#system#error_code (this is
62
    /// new for Boost V1.69).
63
    exception::exception(std::error_code const& e)
×
64
      : std::system_error(e)
×
65
    {
×
66
        LERR_(error).format("created exception: {}", this->what());    //-V1067
×
67
    }
×
68

69
    /// Construct a hpx::exception from a \a hpx::error and an error message.
70
    ///
71
    /// \param e      The parameter \p e holds the hpx::error code the new
72
    ///               exception should encapsulate.
73
    /// \param msg    The parameter \p msg holds the error message the new
74
    ///               exception should encapsulate.
75
    /// \param mode   The parameter \p mode specifies whether the returned
76
    ///               hpx::error_code belongs to the error category
77
    ///               \a hpx_category (if mode is \a throwmode::plain, this is the
78
    ///               default) or to the category \a hpx_category_rethrow
79
    ///               (if mode is \a throwmode::rethrow).
80
    exception::exception(hpx::error e, char const* msg, throwmode mode)
×
81
      : std::system_error(make_system_error_code(e, mode), msg)
×
82
    {
×
83
        HPX_ASSERT((e >= hpx::error::success && e < hpx::error::last_error) ||
×
84
            (e & hpx::error::system_error_flag));
85
        if (e != hpx::error::success)
×
86
        {
87
            LERR_(error).format(    //-V1067
×
88
                "created exception: {}", this->what());
×
89
        }
×
90
    }
×
91

92
    /// Construct a hpx::exception from a \a hpx::error and an error message.
93
    ///
94
    /// \param e      The parameter \p e holds the hpx::error code the new
95
    ///               exception should encapsulate.
96
    /// \param msg    The parameter \p msg holds the error message the new
97
    ///               exception should encapsulate.
98
    /// \param mode   The parameter \p mode specifies whether the returned
99
    ///               hpx::error_code belongs to the error category
100
    ///               \a hpx_category (if mode is \a throwmode::plain, this is the
101
    ///               default) or to the category \a hpx_category_rethrow
102
    ///               (if mode is \a throwmode::rethrow).
103
    exception::exception(hpx::error e, std::string const& msg, throwmode mode)
4,943✔
104
      : std::system_error(make_system_error_code(e, mode), msg)
4,943✔
105
    {
9,886✔
106
        HPX_ASSERT((e >= hpx::error::success && e < hpx::error::last_error) ||
4,943✔
107
            (e & hpx::error::system_error_flag));
108
        if (e != hpx::error::success)
4,943✔
109
        {
110
            LERR_(error).format(    //-V1067
4,943✔
111
                "created exception: {}", this->what());
4,878✔
112
        }
4,943✔
113
    }
4,943✔
114

115
    /// Destruct a hpx::exception
116
    ///
117
    /// \throws nothing
118
    exception::~exception() = default;
283,403✔
119

120
    /// The function \a get_error() returns the hpx::error code stored
121
    /// in the referenced instance of a hpx::exception. It returns
122
    /// the hpx::error code this exception instance was constructed
123
    /// from.
124
    ///
125
    /// \throws nothing
126
    error exception::get_error() const noexcept
3,230✔
127
    {
128
        return static_cast<error>(this->std::system_error::code().value());
3,230✔
129
    }
130

131
    /// The function \a get_error_code() returns a hpx::error_code which
132
    /// represents the same error condition as this hpx::exception instance.
133
    ///
134
    /// \param mode   The parameter \p mode specifies whether the returned
135
    ///               hpx::error_code belongs to the error category
136
    ///               \a hpx_category (if mode is \a throwmode::plain, this is the
137
    ///               default) or to the category \a hpx_category_rethrow
138
    ///               (if mode is \a throwmode::rethrow).
139
    error_code exception::get_error_code(throwmode mode) const noexcept
2,597✔
140
    {
141
        (void) mode;
142
        return error_code(this->std::system_error::code().value(), *this);
2,597✔
143
    }
144

145
    static custom_exception_info_handler_type custom_exception_info_handler;
1,253✔
146

147
    void set_custom_exception_info_handler(custom_exception_info_handler_type f)
1,223✔
148
    {
149
        custom_exception_info_handler = HPX_MOVE(f);
1,223✔
150
    }
1,223✔
151

152
    static pre_exception_handler_type pre_exception_handler;
1,253✔
153

154
    void set_pre_exception_handler(pre_exception_handler_type f)
1,223✔
155
    {
156
        pre_exception_handler = HPX_MOVE(f);
1,223✔
157
    }
1,223✔
158
}    // namespace hpx
159

160
namespace hpx::detail {
161

162
    template <typename Exception>
163
    HPX_CORE_EXPORT std::exception_ptr construct_lightweight_exception(
63✔
164
        Exception const& e, std::string const& func, std::string const& file,
165
        long line)
166
    {
167
        // create a std::exception_ptr object encapsulating the Exception to
168
        // be thrown and annotate it with all the local information we have
169
        try
170
        {
171
            throw_with_info(e,
126✔
172
                std::move(
63✔
173
                    hpx::exception_info().set(hpx::detail::throw_function(func),
126✔
174
                        hpx::detail::throw_file(file),
63✔
175
                        hpx::detail::throw_line(line))));
63✔
176
        }
63✔
177
        catch (...)
178
        {
179
            return std::current_exception();
63✔
180
        }
63✔
181

182
        // need this return to silence a warning with icc
183
        HPX_ASSERT(false);    // -V779
×
184
        return std::exception_ptr();
×
185
    }
126✔
186

187
    template <typename Exception>
188
    HPX_CORE_EXPORT std::exception_ptr construct_lightweight_exception(
342✔
189
        Exception const& e)
190
    {
191
        // create a std::exception_ptr object encapsulating the Exception to
192
        // be thrown and annotate it with all the local information we have
193
        try
194
        {
195
            hpx::throw_with_info(e);
342✔
196
        }
342✔
197
        catch (...)
198
        {
199
            return std::current_exception();
342✔
200
        }
342✔
201

202
        // need this return to silence a warning with icc
203
        HPX_ASSERT(false);    // -V779
×
204
        return std::exception_ptr();
×
205
    }
684✔
206

207
    template HPX_CORE_EXPORT std::exception_ptr construct_lightweight_exception(
208
        hpx::thread_interrupted const&);
209
    template HPX_CORE_EXPORT std::exception_ptr construct_lightweight_exception(
210
        hpx::exception_list const&);
211

212
    template <typename Exception>
213
    HPX_CORE_EXPORT std::exception_ptr construct_custom_exception(
2,597✔
214
        Exception const& e, std::string const& func, std::string const& file,
215
        long line, std::string const& auxinfo)
216
    {
217
        if (!custom_exception_info_handler)
2,597✔
218
        {
219
            return construct_lightweight_exception(e, func, file, line);
63✔
220
        }
221

222
        // create a std::exception_ptr object encapsulating the Exception to
223
        // be thrown and annotate it with information provided by the hook
224
        try
225
        {
226
            throw_with_info(
2,534✔
227
                e, custom_exception_info_handler(func, file, line, auxinfo));
2,534✔
228
        }
2,534✔
229
        catch (...)
230
        {
231
            return std::current_exception();
2,534✔
232
        }
2,534✔
233

234
        // need this return to silence a warning with icc
235
        HPX_ASSERT(false);    // -V779
×
236
        return std::exception_ptr();
×
237
    }
5,131✔
238

239
    ///////////////////////////////////////////////////////////////////////////
240
    template <typename Exception>
241
    inline constexpr bool is_of_lightweight_hpx_category(
×
242
        Exception const&) noexcept
243
    {
244
        return false;
×
245
    }
246

247
    inline bool is_of_lightweight_hpx_category(hpx::exception const& e) noexcept
2,597✔
248
    {
249
        return e.get_error_code().category() == get_lightweight_hpx_category();
2,597✔
250
    }
251

252
    ///////////////////////////////////////////////////////////////////////////
253
    std::exception_ptr access_exception(error_code const& e)
4,021✔
254
    {
255
        return e.exception_;
4,021✔
256
    }
257

258
    template <typename Exception>
259
    HPX_CORE_EXPORT std::exception_ptr get_exception(Exception const& e,
2,597✔
260
        std::string const& func, std::string const& file, long line,
261
        std::string const& auxinfo)
262
    {
263
        if (is_of_lightweight_hpx_category(e))
2,597✔
264
        {
265
            return construct_lightweight_exception(e, func, file, line);
×
266
        }
267

268
        return construct_custom_exception(e, func, file, line, auxinfo);
2,597✔
269
    }
2,597✔
270

271
    template <typename Exception>
272
    HPX_CORE_EXPORT void throw_exception(Exception const& e,
1,769✔
273
        std::string const& func, std::string const& file, long line)
274
    {
275
        if (pre_exception_handler)
1,769✔
276
        {
277
            pre_exception_handler();
1,726✔
278
        }
1,726✔
279

280
        std::rethrow_exception(get_exception(e, func, file, line));
1,769✔
281
    }
1,769✔
282

283
    ///////////////////////////////////////////////////////////////////////////
284
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
285
        hpx::exception const&, std::string const&, std::string const&, long,
286
        std::string const&);
287

288
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
289
        std::system_error const&, std::string const&, std::string const&, long,
290
        std::string const&);
291

292
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
293
        std::exception const&, std::string const&, std::string const&, long,
294
        std::string const&);
295
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
296
        hpx::detail::std_exception const&, std::string const&,
297
        std::string const&, long, std::string const&);
298
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
299
        std::bad_exception const&, std::string const&, std::string const&, long,
300
        std::string const&);
301
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
302
        hpx::detail::bad_exception const&, std::string const&,
303
        std::string const&, long, std::string const&);
304
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
305
        std::bad_typeid const&, std::string const&, std::string const&, long,
306
        std::string const&);
307
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
308
        hpx::detail::bad_typeid const&, std::string const&, std::string const&,
309
        long, std::string const&);
310
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
311
        std::bad_cast const&, std::string const&, std::string const&, long,
312
        std::string const&);
313
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
314
        hpx::detail::bad_cast const&, std::string const&, std::string const&,
315
        long, std::string const&);
316
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
317
        std::bad_alloc const&, std::string const&, std::string const&, long,
318
        std::string const&);
319
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
320
        hpx::detail::bad_alloc const&, std::string const&, std::string const&,
321
        long, std::string const&);
322
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
323
        std::logic_error const&, std::string const&, std::string const&, long,
324
        std::string const&);
325
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
326
        std::runtime_error const&, std::string const&, std::string const&, long,
327
        std::string const&);
328
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
329
        std::out_of_range const&, std::string const&, std::string const&, long,
330
        std::string const&);
331
    template HPX_CORE_EXPORT std::exception_ptr get_exception(
332
        std::invalid_argument const&, std::string const&, std::string const&,
333
        long, std::string const&);
334

335
    ///////////////////////////////////////////////////////////////////////////
336
    template HPX_CORE_EXPORT void throw_exception(
337
        hpx::exception const&, std::string const&, std::string const&, long);
338

339
    template HPX_CORE_EXPORT void throw_exception(
340
        std::system_error const&, std::string const&, std::string const&, long);
341

342
    template HPX_CORE_EXPORT void throw_exception(
343
        std::exception const&, std::string const&, std::string const&, long);
344
    template HPX_CORE_EXPORT void throw_exception(
345
        hpx::detail::std_exception const&, std::string const&,
346
        std::string const&, long);
347
    template HPX_CORE_EXPORT void throw_exception(std::bad_exception const&,
348
        std::string const&, std::string const&, long);
349
    template HPX_CORE_EXPORT void throw_exception(
350
        hpx::detail::bad_exception const&, std::string const&,
351
        std::string const&, long);
352
    template HPX_CORE_EXPORT void throw_exception(
353
        std::bad_typeid const&, std::string const&, std::string const&, long);
354
    template HPX_CORE_EXPORT void throw_exception(
355
        hpx::detail::bad_typeid const&, std::string const&, std::string const&,
356
        long);
357
    template HPX_CORE_EXPORT void throw_exception(
358
        std::bad_cast const&, std::string const&, std::string const&, long);
359
    template HPX_CORE_EXPORT void throw_exception(hpx::detail::bad_cast const&,
360
        std::string const&, std::string const&, long);
361
    template HPX_CORE_EXPORT void throw_exception(
362
        std::bad_alloc const&, std::string const&, std::string const&, long);
363
    template HPX_CORE_EXPORT void throw_exception(hpx::detail::bad_alloc const&,
364
        std::string const&, std::string const&, long);
365
    template HPX_CORE_EXPORT void throw_exception(
366
        std::logic_error const&, std::string const&, std::string const&, long);
367
    template HPX_CORE_EXPORT void throw_exception(std::runtime_error const&,
368
        std::string const&, std::string const&, long);
369
    template HPX_CORE_EXPORT void throw_exception(
370
        std::out_of_range const&, std::string const&, std::string const&, long);
371
    template HPX_CORE_EXPORT void throw_exception(std::invalid_argument const&,
372
        std::string const&, std::string const&, long);
373
}    // namespace hpx::detail
374

375
///////////////////////////////////////////////////////////////////////////////
376
namespace hpx {
377

378
    ///////////////////////////////////////////////////////////////////////////
379
    /// Return the error message.
380
    std::string get_error_what(hpx::exception_info const& xi)
2✔
381
    {
382
        // Try a cast to std::exception - this should handle boost.system
383
        // error codes in addition to the standard library exceptions.
384
        std::exception const* se = dynamic_cast<std::exception const*>(&xi);
2✔
385
        return se ? se->what() : std::string("<unknown>");
2✔
386
    }
×
387

388
    std::string get_error_what(std::exception_ptr const& e)
2,351✔
389
    {
390
        try
391
        {
392
            std::rethrow_exception(e);
2,351✔
393
        }
2,351✔
394
        catch (hpx::thread_interrupted const&)
395
        {
396
            return "thread_interrupted";
×
397
        }
×
398
        catch (std::exception const& e)
399
        {
400
            return get_error_what(e);
2,339✔
401
        }
2,339✔
402
        catch (...)
403
        {
404
            return "<unknown>";
11✔
405
        }
2,351✔
406
    }
4,702✔
407

408
    ///////////////////////////////////////////////////////////////////////////
409
    error get_error(hpx::exception const& e)
×
410
    {
411
        return static_cast<hpx::error>(e.get_error());
×
412
    }
413

414
    error get_error(hpx::error_code const& e)
×
415
    {
416
        return static_cast<hpx::error>(e.value());
×
417
    }
418

419
    error get_error(std::exception_ptr const& e)
8,585✔
420
    {
421
        try
422
        {
423
            std::rethrow_exception(e);
8,585✔
424
        }
8,585✔
425
        catch (hpx::thread_interrupted const&)
426
        {
427
            return hpx::error::thread_cancelled;
1✔
428
        }
1✔
429
        catch (hpx::exception const& he)
430
        {
431
            return he.get_error();
3,159✔
432
        }
3,160✔
433
        catch (std::system_error const& e)
434
        {
435
            int code = e.code().value();
×
436
            if (code < hpx::error::success || code >= hpx::error::last_error)
×
437
            {
438
                code |= hpx::error::system_error_flag;
×
439
            }
×
440
            return static_cast<hpx::error>(code);
×
441
        }
3,159✔
442
        catch (...)
443
        {
444
            return hpx::error::unknown_error;
5,424✔
445
        }
5,425✔
446
    }
17,169✔
447

448
    /// Return the function name from which the exception was thrown.
449
    std::string get_error_function_name(hpx::exception_info const& xi)
7✔
450
    {
451
        std::string const* function = xi.get<hpx::detail::throw_function>();
7✔
452
        if (function)
7✔
453
            return *function;
7✔
454

455
        return std::string();
×
456
    }
7✔
457

458
    /// Return the (source code) file name of the function from which the
459
    /// exception was thrown.
460
    std::string get_error_file_name(hpx::exception_info const& xi)
×
461
    {
462
        std::string const* file = xi.get<hpx::detail::throw_file>();
×
463
        if (file)
×
464
            return *file;
×
465

466
        return "<unknown>";
×
467
    }
×
468

469
    /// Return the line number in the (source code) file of the function from
470
    /// which the exception was thrown.
471
    long get_error_line_number(hpx::exception_info const& xi)
×
472
    {
473
        long const* line = xi.get<hpx::detail::throw_line>();
×
474
        if (line)
×
475
            return *line;
×
476
        return -1;
×
477
    }
×
478

479
}    // 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