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

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

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

84.21
/libs/full/components_base/include/hpx/components_base/server/fixed_component_base.hpp
1
//  Copyright (c) 2011      Bryce Lelbach
2
//  Copyright (c) 2007-2025 Hartmut Kaiser
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
#pragma once
9

10
#include <hpx/config.hpp>
11
#include <hpx/assert.hpp>
12
#include <hpx/components_base/agas_interface.hpp>
13
#include <hpx/components_base/component_type.hpp>
14
#include <hpx/components_base/components_base_fwd.hpp>
15
#include <hpx/components_base/traits/is_component.hpp>
16
#include <hpx/modules/errors.hpp>
17
#include <hpx/modules/format.hpp>
18
#include <hpx/naming_base/address.hpp>
19
#include <hpx/naming_base/id_type.hpp>
20

21
#include <cstddef>
22
#include <cstdint>
23
#include <type_traits>
24

25
namespace hpx::components {
26

27
    ///////////////////////////////////////////////////////////////////////////
28
    template <typename Component>
29
    // NOLINTNEXTLINE(bugprone-crtp-constructor-accessibility)
30
    class fixed_component_base : public traits::detail::fixed_component_tag
31
    {
32
        using this_component_type =
33
            std::conditional_t<std::is_void_v<Component>, fixed_component_base,
34
                Component>;
35

36
        constexpr Component& derived() noexcept
37
        {
38
            return static_cast<Component&>(*this);
39
        }
40
        constexpr Component const& derived() const noexcept
41
        {
42
            return static_cast<Component const&>(*this);
43
        }
44

45
    public:
46
        using wrapped_type = this_component_type;
47
        using base_type_holder = this_component_type;
48
        using wrapping_type = fixed_component<this_component_type>;
49

50
        // Construct an empty fixed_component
51
        // NOLINTNEXTLINE(bugprone-crtp-constructor-accessibility)
122✔
52
        constexpr fixed_component_base(
53
            std::uint64_t msb, std::uint64_t lsb) noexcept
122✔
54
          : msb_(msb)
122✔
55
          , lsb_(lsb)
56
        {
57
        }
58

59
        ~fixed_component_base() = default;
60

61
        /// \brief finalize() will be called just before the instance gets
62
        ///        destructed
61✔
63
        void finalize() const
64
        {
65
            /// Unbind the GID if it's not this instantiations fixed gid and is
66
            /// is not invalid.
67
            if (naming::invalid_gid != gid_)
68
            {
69
                error_code ec(throwmode::lightweight);    // ignore errors
61✔
70
                agas::unbind_gid_local(gid_, ec);
71

72
                gid_ = naming::gid_type();    // invalidate GID
73
            }
61✔
74
        }
75

76
        // Return the component's fixed GID.
77
        naming::gid_type get_base_gid(
122✔
78
            naming::gid_type const& assign_gid = naming::invalid_gid) const
79
        {
80
            if (assign_gid)
81
            {
82
                HPX_THROW_EXCEPTION(hpx::error::bad_parameter,
×
83
                    "fixed_component_base::get_base_gid",
84
                    "fixed_components must be assigned new gids on creation");
85
            }
86

87
            if (!gid_)
88
            {
89
                naming::address const addr(
122✔
90
                    naming::get_gid_from_locality_id(agas::get_locality_id()),
91
                    components::get_component_type<wrapped_type>(),
92
                    const_cast<this_component_type*>(
93
                        static_cast<this_component_type const*>(this)));
94

95
                gid_ = naming::gid_type(msb_, lsb_);
122✔
96

97
                // Try to bind the preset GID first
98
                if (!agas::bind_gid_local(gid_, addr))
122✔
99
                {
100
                    naming::gid_type const g = gid_;
101
                    gid_ = naming::gid_type();    // invalidate GID
102

103
                    HPX_THROW_EXCEPTION(hpx::error::duplicate_component_address,
×
104
                        "fixed_component_base<Component>::get_base_gid",
105
                        "could not bind_gid(local): {}", g);
106
                }
107
            }
108
            return gid_;
122✔
109
        }
110

111
        hpx::id_type get_id() const
112
        {
113
            // fixed_address components are created without any credits
114
            naming::gid_type gid = derived().get_base_gid();
115
            HPX_ASSERT(!naming::detail::has_credits(gid));
116

117
            agas::replenish_credits(gid);
118
            return hpx::id_type(gid, hpx::id_type::management_type::managed);
119
        }
120

121
        hpx::id_type get_unmanaged_id() const
122
        {
122✔
123
            return hpx::id_type(derived().get_base_gid(),
124
                hpx::id_type::management_type::unmanaged);
125
        }
122✔
126

127
        void set_locality_id(std::uint32_t locality_id, error_code& ec = throws)
128
        {
64✔
129
            if (gid_)
130
            {
131
                HPX_THROWS_IF(ec, hpx::error::invalid_status,
132
                    "fixed_component_base::set_locality_id",
×
133
                    "can't change locality_id after GID has already been "
134
                    "registered");
135
            }
136
            else
137
            {
138
                // erase current locality_id and replace with given one
139
                msb_ = naming::replace_locality_id(msb_, locality_id);
140
            }
64✔
141
        }
142

64✔
143
        static void mark_as_migrated() noexcept
144
        {
145
            // If this assertion is triggered then this component instance is
146
            // being migrated even if the component type has not been enabled to
147
            // support migration.
148
            HPX_ASSERT(false);
149
        }
150

151
        static void on_migrated() noexcept
152
        {
153
            // If this assertion is triggered then this component instance is
154
            // being migrated even if the component type has not been enabled to
155
            // support migration.
156
            HPX_ASSERT(false);
157
        }
158

159
    private:
160
        mutable naming::gid_type gid_;
161
        std::uint64_t msb_;
162
        std::uint64_t lsb_;
163
    };
164

165
    namespace detail {
166

167
        ///////////////////////////////////////////////////////////////////////
168
        struct fixed_heap
169
        {
170
            static void* alloc(std::size_t) noexcept
171
            {
172
                HPX_ASSERT(false);    // this shouldn't ever be called
173
                return nullptr;
174
            }
175
            static void free(void*, std::size_t) noexcept
176
            {
177
                HPX_ASSERT(false);    // this shouldn't ever be called
178
            }
179
        };
180
    }    // namespace detail
181

182
    ///////////////////////////////////////////////////////////////////////////
183
    template <typename Component>
184
    class fixed_component : public Component
185
    {
186
    public:
187
        using type_holder = Component;
188
        using component_type = fixed_component<Component>;
189
        using derived_type = component_type;
190
        using heap_type = detail::fixed_heap;
191

192
        /// \brief  The function \a create is used for allocation and
193
        ///         initialization of instances of the derived components.
194
        static Component* create(std::size_t /* count */) noexcept
195
        {
196
            HPX_ASSERT(false);    // this shouldn't ever be called
197
            return nullptr;
198
        }
199

200
        /// \brief  The function \a destroy is used for destruction and
201
        ///         de-allocation of instances of the derived components.
202
        static void destroy(
203
            Component* /* p */, std::size_t /* count */ = 1) noexcept
204
        {
205
            HPX_ASSERT(false);    // this shouldn't ever be called
206
        }
207
    };
208
}    // namespace hpx::components
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

© 2026 Coveralls, Inc