mCRL2
Loading...
Searching...
No Matches
aterm_pool.h
Go to the documentation of this file.
1// Author(s): Maurice Laveaux.
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9
10#ifndef ATERMPP_DETAIL_ATERM_POOL_H
11#define ATERMPP_DETAIL_ATERM_POOL_H
12
15
17
18namespace atermpp
19{
20namespace detail
21{
22
30
31template<std::size_t N>
33
34// There are some annoying circular dependencies between a aterm_pool and the contained thread_aterm_pool_interfaces
35class aterm_pool;
36
40{
41public:
42 thread_aterm_pool_interface(aterm_pool& pool, std::function<void()> mark_function, std::function<void()> print_function, std::function<std::size_t()> protection_set_size_function);
44
46 void mark()
47 {
49 }
50
53 {
55 }
56
58 std::size_t protection_set_size() const
59 {
61 }
62
63 // Prematurely unregister the thread_aterm_pool_interface.
64 void unregister();
65
66private:
68 std::function<void()> m_mark_function;
69 std::function<void()> m_print_function;
70 std::function<std::size_t()> m_protection_set_size_function;
71 bool m_registered = true;
72};
73
75
82{
83public:
84 inline aterm_pool();
85 inline ~aterm_pool();
86
87 friend class thread_aterm_pool;
88
91 inline function_symbol create_function_symbol(const std::string& name, const std::size_t arity, const bool check_for_registered_functions = false);
92
95 inline function_symbol create_function_symbol(std::string&& name, const std::size_t arity, const bool check_for_registered_functions = false);
96
100
104
106 inline std::size_t capacity() const noexcept;
107
109 inline std::size_t size() const;
110
112 inline void print_performance_statistics() const;
113
115 aterm& empty_list() noexcept { return reinterpret_cast<aterm&>(m_empty_list); } // TODO remove this reinterpret cast by letting m_empty_list become an aterm.
116
118 const function_symbol& as_int() noexcept { return m_function_symbol_pool.as_int(); }
119
122
125
127 inline void add_deletion_hook(function_symbol sym, term_callback callback);
128
130 inline void enable_garbage_collection(bool enable) { m_enable_garbage_collection = enable; };
131
133
134 // These functions of the aterm pool should be called through a thread_aterm_pool.
135private:
139
144 inline void created_term(bool allow_collect, mcrl2::utilities::shared_mutex& mutex);
145
149
151 inline bool create_int(aterm& term, std::size_t val);
152
154 inline bool create_term(aterm& term, const function_symbol& sym);
155
157 template<class ...Terms>
158 inline bool create_appl(aterm& term, const function_symbol& sym, const Terms&... arguments);
159
163 template<typename ForwardIterator>
164 bool create_appl_dynamic(aterm& term,
165 const function_symbol& sym,
166 ForwardIterator begin,
167 ForwardIterator end);
168
172 template<typename InputIterator, typename ATermConverter>
173 bool create_appl_dynamic(aterm& term,
174 const function_symbol& sym,
175 ATermConverter convert_to_aterm,
176 InputIterator begin,
177 InputIterator end);
178
182
184
186 inline std::size_t protection_set_size() const;
187
189 std::vector<thread_aterm_pool_interface* > m_thread_pools;
190
193
196
198 std::tuple<
208
211
213 std::atomic<long> m_count_until_collection = 0;
214 std::atomic<long> m_count_until_resize = 0;
215
217
220
223};
224
225inline
226thread_aterm_pool_interface::thread_aterm_pool_interface(aterm_pool& pool, std::function<void()> mark_function, std::function<void()> print_function, std::function<std::size_t()> protection_set_size_function)
227 : m_pool(pool), m_mark_function(mark_function), m_print_function(print_function), m_protection_set_size_function(protection_set_size_function)
228{
230}
231
232inline
234{
235 if (m_registered)
236 {
238 m_registered = false;
239 }
240}
241
242inline
244{
245 unregister();
246}
247
248} // namespace detail
249} // namespace atermpp
250
253
254#endif // ATERMPP_DETAIL_ATERM_POOL_H
The aterm_core base class that provides protection of the underlying shared terms.
Definition aterm_core.h:182
The interface for the term library. Provides the storage of of all classes of terms.
Definition aterm_pool.h:82
const function_symbol & as_int() noexcept
Definition aterm_pool.h:118
void created_term(bool allow_collect, mcrl2::utilities::shared_mutex &mutex)
Triggers garbage collection and resizing when conditions are met.
bool create_appl(aterm &term, const function_symbol &sym, const Terms &... arguments)
Creates a function application with the given function symbol and arguments.
std::atomic< bool > m_enable_garbage_collection
Definition aterm_pool.h:216
std::atomic< long > m_count_until_resize
Definition aterm_pool.h:214
void remove_thread_aterm_pool(thread_aterm_pool_interface &pool)
Remove thread specific aterm pool.
std::vector< thread_aterm_pool_interface * > m_thread_pools
The set of local aterm pools.
Definition aterm_pool.h:189
bool create_int(aterm &term, std::size_t val)
Creates a integral term with the given value.
function_symbol_pool m_function_symbol_pool
Storage for the function symbols.
Definition aterm_pool.h:192
void collect_impl(mcrl2::utilities::shared_mutex &mutex)
Collect garbage on all storages.
arbitrary_function_application_storage m_appl_dynamic_storage
Storage for term_appl with a dynamic number of arguments larger than 7.
Definition aterm_pool.h:210
const function_symbol & as_empty_list() noexcept
Definition aterm_pool.h:124
function_symbol create_function_symbol(const std::string &name, const std::size_t arity, const bool check_for_registered_functions=false)
Creates a function symbol pair (name, arity).
const function_symbol & as_list() noexcept
Definition aterm_pool.h:121
aterm_core m_empty_list
Represents an empty list.
Definition aterm_pool.h:222
mcrl2::utilities::shared_mutex & shared_mutex()
Definition aterm_pool.h:183
void print_performance_statistics() const
Prints various performance statistics for the term pool.
std::atomic< long > m_count_until_collection
Track the number of terms destroyed and reduce the freelist.
Definition aterm_pool.h:213
void register_thread_aterm_pool(thread_aterm_pool_interface &pool)
Register a thread specific aterm pool.
bool create_term(aterm &term, const function_symbol &sym)
Creates a term with the given function symbol.
void collect(mcrl2::utilities::shared_mutex &mutex)
Force garbage collection on all storages.
bool create_appl_dynamic(aterm &term, const function_symbol &sym, ForwardIterator begin, ForwardIterator end)
Creates a function application with the given function symbol and the arguments as provided by the gi...
aterm & empty_list() noexcept
Definition aterm_pool.h:115
mcrl2::utilities::shared_mutex m_shared_mutex
Garbage collection is enabled.
Definition aterm_pool.h:219
void add_deletion_hook(function_symbol sym, term_callback callback)
Add a callback that is triggered whenever a term with the given function symbol is destroyed.
function_symbol_pool & get_symbol_pool()
Definition aterm_pool.h:132
std::tuple< term_storage, function_application_storage< 1 >, function_application_storage< 2 >, function_application_storage< 3 >, function_application_storage< 4 >, function_application_storage< 5 >, function_application_storage< 6 >, function_application_storage< 7 > > m_appl_storage
Storage for function applications with a fixed number of arguments.
Definition aterm_pool.h:207
integer_term_storage m_int_storage
Storage for integral terms.
Definition aterm_pool.h:195
void resize_if_needed(mcrl2::utilities::shared_mutex &shared)
Resizes all storages if necessary.
void enable_garbage_collection(bool enable)
Enable garbage collection when passing true and disable otherwise.
Definition aterm_pool.h:130
std::size_t capacity() const noexcept
The number of terms that can be stored without resizing.
This class stores a set of function symbols.
const function_symbol & as_int() noexcept
const function_symbol & as_empty_list() noexcept
const function_symbol & as_list() noexcept
A thread specific aterm pool that provides a local interface to the global term pool....
Definition aterm_pool.h:40
void mark()
Mark the terms created by this thread to prevent them being garbage collected.
Definition aterm_pool.h:46
thread_aterm_pool_interface(aterm_pool &pool, std::function< void()> mark_function, std::function< void()> print_function, std::function< std::size_t()> protection_set_size_function)
Definition aterm_pool.h:226
void print_local_performance_statistics() const
Print performance statistics for data stored for this thread.
Definition aterm_pool.h:52
std::function< std::size_t()> m_protection_set_size_function
Definition aterm_pool.h:70
This is a thread's specific access to the global aterm pool which ensures that garbage collection and...
This is simply an exclusive lock based on the standard library with the ability to perform no locks w...
Definition mutex.h:23
Inherit from this class to prevent it from being copyable.
Definition noncopyable.h:21
a pool allocator class
static constexpr bool EnableGarbageCollection
Enable garbage collection.
aterm_pool_storage< _aterm, aterm_hasher_finite< 0 >, aterm_equals_finite< 0 >, 0 > term_storage
Definition aterm_pool.h:25
constexpr std::size_t DynamicNumberOfArguments
Indicates that the number of arguments is not known at compile time.
Definition aterm_hash.h:78
The main namespace for the aterm++ library.
Definition algorithm.h:21
void(* term_callback)(const aterm &)
Definition aterm.h:194
STL namespace.
Returns true iff first and second are value-equivalent.
Definition aterm_hash.h:125
Computes the hash of the given term.
Definition aterm_hash.h:101
Computes the hash of the given term.
Definition aterm_hash.h:85