Flow-IPC 1.0.2
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
21#include <flow/util/util.hpp>
22
24{
25
26// Types.
27
28/**
29 * RAII-style class operating a stack-like notion of a the given *thread*'s currently active SHM-aware
30 * `Arena`, so that Stateless_allocator knows which `Arena` is currently active w/r/t a given code context
31 * operating on a SHM-stored container instance.
32 *
33 * In its ctor, it activates the given `Arena*`. In it dtor it re-activates the `Arena*` active at ctor time
34 * (or null if none was active). Behavior is undefined if ctor and dtor are called from different threads.
35 *
36 * @see shm::stl namespace doc header for background.
37 * @see Stateless_allocator, the main class template that Arena_activator helps one use.
38 */
39template<typename Arena>
41{
42public:
43 // Types.
44
45 /// Short-hand for the `Arena` type this activates/deactivates in the current thread of ctor/dtor.
46 using Arena_obj = Arena;
47
48 // Constructors/destructor.
49
50 /**
51 * Activates the given #Arena_obj. `Stateless_allocator<T, Arena>` ops, as invoked by STL-compliant container
52 * types parameterized on `Stateless_allocator<T, Arena>`, shall use that given arena for alloc/dealloc in the
53 * calling thread until `*this` dtor is invoked. The dtor shall restore the current `Arena*` (possibly null)
54 * at time of entry to this ctor.
55 *
56 * Another `Arena_activator<Arena_obj>` can be used to activate/deactivate another #Arena_obj in stack-like
57 * fashion.
58 *
59 * @param new_arena_not_null
60 * The #Arena_obj to activate. Must not be null (else behavior undefined/assertion may trip).
61 */
62 explicit Arena_activator(Arena_obj* new_arena_not_null);
63
64 /// See ctor. This undoes what that did.
66
67 // Methods.
68
69 /**
70 * Returns the active arena, as understood by `Stateless_allocator<Arena_obj>` at this point in the calling
71 * thread. This is generally used by Stateless_allocator itself; but it is publicly exposed also, as it may
72 * be of interest.
73 *
74 * @return See above. Note this may be null.
75 */
77
78private:
79 // Data.
80
81 /**
82 * See this_thread_active_arena. The key point is it is thread-local and a separate object per distinct `Arena`
83 * type.
84 */
86}; // class Arena_activator
87
88// Template static initializers.
89
90template<typename Arena>
92
93// Template implementations.
94
95template<typename Arena>
97 flow::util::Scoped_setter<Arena_obj*>(&s_this_thread_active_arena, std::move(new_arena_not_null))
98{
100 && "Per contract do not activate null. Current arena is null only in the outside scope.");
101}
102
103template<typename Arena>
104Arena_activator<Arena>::~Arena_activator() = default; // Only declared to officially document it.
105
106template<typename Arena>
108{
109 return s_this_thread_active_arena;
110}
111
112} // namespace ipc::shm::stl
RAII-style class operating a stack-like notion of a the given thread's currently active SHM-aware Are...
Arena_activator(Arena_obj *new_arena_not_null)
Activates the given Arena_obj.
~Arena_activator()
See ctor. This undoes what that did.
static thread_local Arena_obj * s_this_thread_active_arena
See this_thread_active_arena.
Arena Arena_obj
Short-hand for the Arena type this activates/deactivates in the current thread of ctor/dtor.
static Arena_obj * this_thread_active_arena()
Returns the active arena, as understood by Stateless_allocator<Arena_obj> at this point in the callin...
ipc::shm sub-module providing integration between STL-compliant components (including containers) and...