Flow-IPC 3.0.0
Flow-IPC project: Full implementation reference.
arena_activator.hpp
Go to the documentation of this file.
1/* Flow-IPC: Shared Memory
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19#pragma once
20
22#include <flow/util/util.hpp>
23
25{
26
27// Types.
28
29/**
30 * RAII-style class operating a stack-like notion of the given *thread*'s currently active
31 * SHM-aware #Arena, so that Stateless_allocator knows which `Arena` is currently active w/r/t a given code context
32 * operating on a SHM-stored container instance.
33 *
34 * In its ctor, it activates the given `Arena*`. In its dtor it re-activates the `Arena*` active at ctor time
35 * (or null if none was active). Behavior is undefined if ctor and dtor are called from different threads.
36 *
37 * @see shm::stl namespace doc header for background.
38 * @see Stateless_allocator, the main class template that Arena_activator helps one use.
39 */
40template<typename Arena_t>
42{
43public:
44 // Types.
45
46 /// Short-hand for the `Arena_t` type this activates/deactivates in the current thread of ctor/dtor.
47 using Arena = Arena_t;
48
49 // Constructors/destructor.
50
51 /**
52 * Activates the given #Arena. `Stateless_allocator<T, Arena>` ops, as invoked by STL-compliant container
53 * types parameterized on `Stateless_allocator<T, Arena>`, shall use that given arena for alloc/dealloc in the
54 * calling thread until `*this` dtor is invoked. The dtor shall restore the current `Arena*` (possibly null)
55 * at time of entry to this ctor.
56 *
57 * Another `Arena_activator<Arena>` can be used to activate/deactivate another #Arena in stack-like
58 * fashion.
59 *
60 * @param new_arena_not_null
61 * The #Arena to activate. Must not be null (else behavior undefined/assertion may trip).
62 */
63 explicit Arena_activator(Arena* new_arena_not_null);
64
65 /// See ctor. This undoes what that did.
67
68 // Methods.
69
70 /**
71 * Returns the active arena, as understood by `Stateless_allocator<T, Arena>` (any `T`) at this point in the calling
72 * thread. This is generally used by Stateless_allocator itself; but it is publicly exposed also, as it may
73 * be of interest.
74 *
75 * @return See above. Note this may be null.
76 */
78
79private:
80 // Data.
81
82 /**
83 * See this_thread_active_arena(). The key point is it is thread-local and a separate object per distinct #Arena
84 * type.
85 */
86 static thread_local Arena* s_this_thread_active_arena;
87}; // class Arena_activator
88
89// Template static initializers.
90
91template<typename Arena_t>
92thread_local Arena_t* Arena_activator<Arena_t>::s_this_thread_active_arena{nullptr};
93
94// Template implementations.
95
96template<typename Arena_t>
98 flow::util::Scoped_setter<Arena*>(&s_this_thread_active_arena, std::move(new_arena_not_null))
99{
101 && "Per contract do not activate null. Current arena is null only in the outside scope.");
102}
103
104template<typename Arena_t>
105Arena_activator<Arena_t>::~Arena_activator() = default; // Only declared to officially document it.
106
107template<typename Arena_t>
109{
110 return s_this_thread_active_arena;
111}
112
113} // namespace ipc::shm::stl
RAII-style class operating a stack-like notion of the given thread's currently active SHM-aware Arena...
~Arena_activator()
See ctor. This undoes what that did.
Arena_activator(Arena *new_arena_not_null)
Activates the given Arena.
Arena_t Arena
Short-hand for the Arena_t type this activates/deactivates in the current thread of ctor/dtor.
static Arena * this_thread_active_arena()
Returns the active arena, as understood by Stateless_allocator<T, Arena> (any T) at this point in the...
static thread_local Arena * s_this_thread_active_arena
See this_thread_active_arena().
ipc::shm sub-module providing integration between STL-compliant components (including containers) and...