mCRL2
Loading...
Searching...
No Matches
gc_stress_thread.cpp
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#include "mcrl2/atermpp/detail/gc_stress_thread.h"
11
12#include "mcrl2/atermpp/detail/aterm_configuration.h"
13#include "mcrl2/atermpp/detail/thread_aterm_pool.h"
14
15#include <atomic>
16#include <chrono>
17#include <cstdlib>
18#include <thread>
19
20namespace atermpp::detail
21{
22
23namespace
24{
25
26std::atomic<bool> g_gc_stress_running{false};
28
29} // unnamed namespace
30
32{
33 if constexpr (EnableGCStressThread && mcrl2::utilities::detail::GlobalThreadSafe)
34 {
35 g_gc_stress_running.store(true, std::memory_order_relaxed);
36 g_gc_stress_thread = std::thread([]
37 {
38 // Registering the thread-local pool connects this thread to the global pool.
39 while (g_gc_stress_running.load(std::memory_order_relaxed))
40 {
41 g_thread_term_pool().collect();
42 std::this_thread::sleep_for(std::chrono::milliseconds(1));
43 }
44 });
45 std::atexit(stop_gc_stress_thread);
46 }
47}
48
50{
51 if (g_gc_stress_thread.joinable())
52 {
53 g_gc_stress_running.store(false, std::memory_order_relaxed);
54 g_gc_stress_thread.join();
55 }
56}
57
58} // namespace atermpp::detail
A unordered_map class in which aterms can be stored.
void stop_gc_stress_thread()
Stop the background GC stress thread if it is running.
void start_gc_stress_thread()
Spawns a dedicated background thread that continuously triggers garbage collection.
The main namespace for the aterm++ library.