Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
crash_handler.h
Go to the documentation of this file.
1
7#pragma once
8
9/*****************************************************************************
10BSD 3-Clause License
11
12Copyright (c) 2025, kcenon
13All rights reserved.
14*****************************************************************************/
15
16#include <functional>
17#include <vector>
18#include <mutex>
19#include <atomic>
20#include <string>
21#include <csignal>
22#include <memory>
23
24namespace kcenon::thread {
25
30 minimal, // Basic signal handling
31 standard, // Standard recovery with logging
32 paranoid // Maximum safety with redundancy
33};
34
40 std::string signal_name;
42 std::string stack_trace;
43 std::chrono::system_clock::time_point crash_time;
44 std::thread::id crashing_thread;
45};
46
50using crash_callback = std::function<void(const crash_context&)>;
51
63public:
69
76 bool enable_core_dumps = false);
77
85 size_t register_crash_callback(const std::string& name,
87 int priority = 100);
88
93 void unregister_crash_callback(size_t registration_id);
94
101 void register_cleanup(const std::string& name,
102 std::function<void()> cleanup,
103 uint32_t timeout_ms = 1000);
104
109 void set_crash_log_directory(const std::string& directory);
110
115 void set_stack_trace_enabled(bool enable);
116
122
127 bool is_handling_crash() const;
128
132 struct crash_stats {
136 std::chrono::system_clock::time_point last_crash_time;
137 };
139
140private:
141 crash_handler() = default;
143
144 // Non-copyable, non-movable
145 crash_handler(const crash_handler&) = delete;
147
149 size_t id;
150 std::string name;
153 };
154
156 std::string name;
157 std::function<void()> cleanup;
158 uint32_t timeout_ms;
159 };
160
161 // Internal crash handling
162 static void signal_handler(int signal);
163 void handle_crash(int signal);
164 void execute_callbacks(const crash_context& context);
166 std::string generate_stack_trace();
167 void write_crash_log(const crash_context& context);
168
169 // Configuration
171 bool enable_core_dumps_ = false;
173 std::string crash_log_directory_ = "./crash_logs";
174
175 // Callbacks and cleanup
176 std::vector<callback_entry> callbacks_;
177 std::vector<cleanup_entry> cleanups_;
178 mutable std::mutex callbacks_mutex_;
179 std::atomic<size_t> next_callback_id_{1};
180
181 // State tracking
182 std::atomic<bool> initialized_{false};
183 std::atomic<bool> handling_crash_{false};
184 std::atomic<size_t> total_crashes_{0};
185 std::atomic<size_t> successful_cleanups_{0};
186 std::atomic<size_t> failed_cleanups_{0};
187 std::chrono::system_clock::time_point last_crash_time_;
188
189 // Platform-specific data
190 #ifdef _WIN32
191 void* previous_handler_ = nullptr;
192 #else
193 struct sigaction previous_handlers_[32];
194 #endif
195};
196
218
223public:
229 static void enable_for_pool(const std::string& pool_name, class thread_pool& pool);
230
235 static void set_job_crash_handler(std::function<void(const std::string& pool_name,
236 const crash_context&)> handler);
237
238private:
239 static void handle_job_crash(const std::string& pool_name, const crash_context& context);
240};
241
242} // namespace kcenon::thread
Thread-safe crash handler for the entire thread system.
std::atomic< bool > initialized_
std::vector< cleanup_entry > cleanups_
void unregister_crash_callback(size_t registration_id)
Unregister a crash callback.
struct sigaction previous_handlers_[32]
void write_crash_log(const crash_context &context)
crash_safety_level safety_level_
std::string generate_stack_trace()
std::atomic< size_t > next_callback_id_
std::atomic< size_t > failed_cleanups_
void initialize(crash_safety_level level=crash_safety_level::standard, bool enable_core_dumps=false)
Initialize crash handling with specified safety level.
void handle_crash(int signal)
void register_cleanup(const std::string &name, std::function< void()> cleanup, uint32_t timeout_ms=1000)
Register a resource cleanup function.
bool is_handling_crash() const
Check if crash handler is currently active.
std::chrono::system_clock::time_point last_crash_time_
void execute_callbacks(const crash_context &context)
static crash_handler & instance()
Get the global crash handler instance.
static void signal_handler(int signal)
crash_stats get_stats() const
std::atomic< size_t > total_crashes_
void set_stack_trace_enabled(bool enable)
Enable/disable automatic stack trace generation.
std::atomic< bool > handling_crash_
crash_handler(const crash_handler &)=delete
size_t register_crash_callback(const std::string &name, crash_callback callback, int priority=100)
Register a callback to be called during crash handling.
void set_crash_log_directory(const std::string &directory)
Set custom crash log directory.
std::vector< callback_entry > callbacks_
void trigger_crash_handling(const crash_context &context)
Manually trigger crash handling (for testing)
std::atomic< size_t > successful_cleanups_
crash_handler & operator=(const crash_handler &)=delete
RAII helper for automatic crash callback registration.
scoped_crash_callback & operator=(scoped_crash_callback &&)=default
scoped_crash_callback(scoped_crash_callback &&)=default
scoped_crash_callback(const scoped_crash_callback &)=delete
scoped_crash_callback & operator=(const scoped_crash_callback &)=delete
scoped_crash_callback(const std::string &name, crash_callback callback, int priority=100)
Thread pool crash safety extensions.
static void set_job_crash_handler(std::function< void(const std::string &pool_name, const crash_context &)> handler)
Register job crash handler.
static void handle_job_crash(const std::string &pool_name, const crash_context &context)
static void enable_for_pool(const std::string &pool_name, class thread_pool &pool)
Enable crash safety for a thread pool.
A thread pool for concurrent execution of jobs using multiple worker threads.
@ callback
Call user callback for custom decision.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
crash_safety_level
Crash safety levels for different scenarios.
std::function< void(const crash_context &)> crash_callback
Callback function type for crash handling.
@ priority
Priority-based scheduling.
Crash context information.
std::chrono::system_clock::time_point crash_time
std::string name
size_t id
crash_callback callback
int priority
uint32_t timeout_ms
std::function< void()> cleanup
std::string name
std::chrono::system_clock::time_point last_crash_time