TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
signal_handler.cpp
1// Copyright (c) 2013-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018 Simons Foundation
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// (at your option) 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 may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Olivier Parcollet, Nils Wentzell
19
20#include "./signal_handler.hpp"
21
22#include <csignal>
23#include <cstring>
24#include <iostream>
25#include <vector>
26
28
29 namespace {
30
31 std::vector<int> signals_list; // NOLINT
32 bool initialized = false; // NOLINT
33
34 void slot(int signal) {
35 std::cerr << "TRIQS : Received signal " << signal << std::endl;
36 signals_list.push_back(signal);
37 }
38
39 } // anonymous namespace
40
41 void start() {
42 if (initialized) return;
43 static struct sigaction action;
44 memset(&action, 0, sizeof(action));
45 action.sa_handler = slot;
46 sigaction(SIGINT, &action, NULL);
47 sigaction(SIGTERM, &action, NULL);
48 sigaction(SIGXCPU, &action, NULL);
49 sigaction(SIGQUIT, &action, NULL);
50 sigaction(SIGUSR1, &action, NULL);
51 sigaction(SIGUSR2, &action, NULL);
52 sigaction(SIGSTOP, &action, NULL);
53 initialized = true;
54 }
55
56 void stop() {
57 signals_list.clear();
58 initialized = false;
59 }
60
61 bool received(bool pop_) {
62 //if (!initialized) start();
63 bool r = signals_list.size() != 0;
64 if (r && pop_) pop();
65 return r;
66 }
67
68 int last() { return signals_list.back(); }
69
70 void pop() { return signals_list.pop_back(); }
71
72} // namespace triqs::signal_handler
Signal handling utilities for the TRIQS library.
int last()
Integer identifier of the most recently received signal.
void start()
Install the TRIQS signal handler.
void stop()
Restore the previous signal disposition.
bool received(bool pop_)
Whether at least one signal has been queued since the last reset.
void pop()
Pop the most recently received signal from the queue (no-op if empty).
Provides a signal handler for the TRIQS library.