ALPSCore reference
signal.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
9 //#include <alps/hdf5/archive.hpp> //FIXME - see below
10 
11 #include <cstring>
12 #include <sstream>
13 #include <cstdlib>
14 #include <iostream>
15 #include <stdexcept>
16 #include <cstdio>
17 
18 #include <signal.h>
19 
20 namespace alps {
21 
23  #if not ( defined BOOST_MSVC || defined ALPS_NO_SIGNALS )
24  static bool initialized;
25  if (!initialized) {
26  initialized = true;
27 
28  static struct sigaction action;
29  memset(&action, 0, sizeof(action));
30  action.sa_handler = &signal::slot;
31  sigaction(SIGINT, &action, NULL);
32  sigaction(SIGTERM, &action, NULL);
33  sigaction(SIGXCPU, &action, NULL);
34  sigaction(SIGQUIT, &action, NULL);
35  sigaction(SIGUSR1, &action, NULL);
36  sigaction(SIGUSR2, &action, NULL);
37  }
38  #endif
39  listen();
40  }
41 
42  bool signal::empty() const {
43  return end_ == begin_;
44  }
45 
46  int signal::top() const {
47  return signals_[(end_ - 1) & 0x1F];
48  }
49 
50  void signal::pop() {
51  --end_ &= 0x1F;
52  }
53 
54  void signal::listen() {
55  #if not ( defined BOOST_MSVC || defined ALPS_NO_SIGNALS )
56  static bool initialized;
57  if (!initialized) {
58  initialized = true;
59 
60  static struct sigaction action;
61  memset(&action, 0, sizeof(action));
62  action.sa_handler = &signal::segfault;
63  sigaction(SIGSEGV, &action, NULL);
64  sigaction(SIGBUS, &action, NULL);
65  }
66  #endif
67  }
68 
69  void signal::slot(int signal) {
70  fprintf(stderr, "Received signal %i\n", signal);
71  signals_[end_] = signal;
72  ++end_ &= 0x1F;
73  if (begin_ == end_)
74  ++begin_ &= 0x1F;
75  }
76 
78  //hdf5::archive::abort(); //FIXME
79  fprintf(stderr, "Abort by signal %i\n", signal);
80  std::cerr << ALPS_STACKTRACE;
81  std::abort();
82  }
83 
84  std::size_t signal::begin_ = 0;
85  std::size_t signal::end_ = 0;
86  boost::array<int, 32> signal::signals_;
87 }
static void slot(int signal)
Definition: signal.cpp:69
int top() const
Definition: signal.cpp:46
static void listen()
Definition: signal.cpp:54
#define ALPS_STACKTRACE
Definition: stacktrace.hpp:37
static void segfault(int signal)
Definition: signal.cpp:77
bool empty() const
Definition: signal.cpp:42
void pop()
Definition: signal.cpp:50