cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
AbortHandler.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2013-2023 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef CepGen_Utils_AbortHandler_h
20#define CepGen_Utils_AbortHandler_h
21
22#include <atomic>
23#include <csignal>
24
26
27namespace cepgen {
28 namespace utils {
29 extern std::atomic<int> gSignal;
31 struct RunAbortedException : std::runtime_error {
32 RunAbortedException() : std::runtime_error("CepGen run aborted") {}
33 ~RunAbortedException() noexcept { CG_INFO("RunAbortedException") << "Run aborted by user interaction."; }
34
35 const char* what() const noexcept override { return "User abort through C-c."; }
36 };
37
40 public:
42 explicit AbortHandler(int flags = SA_SIGINFO) {
43 action_.sa_sigaction = handle_ctrl_c;
44 sigemptyset(&action_.sa_mask);
45 action_.sa_flags = flags;
46 init();
47 }
48
49 private:
50 static void handle_ctrl_c(int signal, siginfo_t* si, void*) {
51 gSignal = signal;
52 if (abs(si->si_code) != SIGABRT)
53 throw RunAbortedException();
54 }
55 void init() {
56 if (sigaction(SIGINT, &action_, nullptr) != 0 || sigaction(SIGTERM, &action_, nullptr) != 0)
57 throw CG_FATAL("AbortHandler") << "Failed to initialise the C-c handler!";
58 }
59 struct sigaction action_;
60 };
61 } // namespace utils
62} // namespace cepgen
63
64#endif
#define CG_FATAL(mod)
Definition Exception.h:61
#define CG_INFO(mod)
Definition Message.h:216
Object handling an user-driven process abortion.
AbortHandler(int flags=SA_SIGINFO)
Define a process abortion procedure.
std::atomic< int > gSignal
Abort signal handler.
Common namespace for this Monte Carlo generator.
Exception raised when the user terminates the process.
const char * what() const noexcept override