Merge branch 'maint'
[hoomd-blue.git] / libhoomd / utils / SignalHandler.cc
blobb1000061ed19492824021b18e2d266d83bc86ff0
1 /*
2 Highly Optimized Object-oriented Many-particle Dynamics -- Blue Edition
3 (HOOMD-blue) Open Source Software License Copyright 2009-2014 The Regents of
4 the University of Michigan All rights reserved.
6 HOOMD-blue may contain modifications ("Contributions") provided, and to which
7 copyright is held, by various Contributors who have granted The Regents of the
8 University of Michigan the right to modify and/or distribute such Contributions.
10 You may redistribute, use, and create derivate works of HOOMD-blue, in source
11 and binary forms, provided you abide by the following conditions:
13 * Redistributions of source code must retain the above copyright notice, this
14 list of conditions, and the following disclaimer both in the code and
15 prominently in any materials provided with the distribution.
17 * Redistributions in binary form must reproduce the above copyright notice, this
18 list of conditions, and the following disclaimer in the documentation and/or
19 other materials provided with the distribution.
21 * All publications and presentations based on HOOMD-blue, including any reports
22 or published results obtained, in whole or in part, with HOOMD-blue, will
23 acknowledge its use according to the terms posted at the time of submission on:
24 http://codeblue.umich.edu/hoomd-blue/citations.html
26 * Any electronic documents citing HOOMD-Blue will link to the HOOMD-Blue website:
27 http://codeblue.umich.edu/hoomd-blue/
29 * Apart from the above required attributions, neither the name of the copyright
30 holder nor the names of HOOMD-blue's contributors may be used to endorse or
31 promote products derived from this software without specific prior written
32 permission.
34 Disclaimer
36 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS'' AND
37 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND/OR ANY
39 WARRANTIES THAT THIS SOFTWARE IS FREE OF INFRINGEMENT ARE DISCLAIMED.
41 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
42 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
43 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
45 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
46 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 // Maintainer: joaander
52 #include <signal.h>
53 #include "SignalHandler.h"
54 #include <iostream>
56 using namespace std;
58 /*! \file SignalHandler.cc
59 \brief Defines variables and functions related to handling signals
62 //! Tracks the previous signal handler that was set to make a chain
63 void (*prev_sigint_handler)(int) = NULL;
65 volatile sig_atomic_t g_sigint_recvd = 0;
67 //! The actual signal handler
68 extern "C" void sigint_handler(int sig)
70 // ignore if we didn't get SIGINT
71 if (sig != SIGINT)
72 return;
74 // call the previous signal handler, but only if it is well defined
75 if (prev_sigint_handler && prev_sigint_handler != SIG_ERR && prev_sigint_handler != SIG_DFL && prev_sigint_handler != SIG_IGN)
76 prev_sigint_handler(sig);
78 // set the global
79 g_sigint_recvd = 1;
82 /*! Call only once at the start of program execution. This method
83 installs a signal handler for SIGING that will set \c g_sigint_recvd
84 to 1. It will also call the previously set signal handler.
86 void InstallSIGINTHandler()
88 void (*retval)(int) = NULL;
89 retval = signal(SIGINT, sigint_handler);
91 if (retval == SIG_ERR)
93 cerr << "Error setting signal handler" << endl;
94 return;
97 // set the previous signal handler, but only if it is not the same as the
98 // one we just set. That would make for a fun infinite loop!
99 if (retval != sigint_handler)
100 prev_sigint_handler = retval;
101 else
102 prev_sigint_handler = NULL;