1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential Utility library
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "mem_config.h"
46 #include <util/signal.hpp>
48 namespace datadifferential
{
51 #define MAGIC_MEMORY 123569
53 bool SignalThread::is_shutdown()
56 pthread_mutex_lock(&shutdown_mutex
);
57 ret
= bool(__shutdown
!= SHUTDOWN_RUNNING
);
58 pthread_mutex_unlock(&shutdown_mutex
);
63 void SignalThread::set_shutdown(shutdown_t arg
)
65 pthread_mutex_lock(&shutdown_mutex
);
67 pthread_mutex_unlock(&shutdown_mutex
);
69 if (arg
== SHUTDOWN_GRACEFUL
)
71 if (pthread_kill(thread
, SIGUSR2
) == 0)
74 pthread_join(thread
, &retval
);
79 shutdown_t
SignalThread::get_shutdown()
82 pthread_mutex_lock(&shutdown_mutex
);
84 pthread_mutex_unlock(&shutdown_mutex
);
89 void SignalThread::post()
94 void SignalThread::test()
96 assert(magic_memory
== MAGIC_MEMORY
);
97 assert(sigismember(&set
, SIGABRT
));
98 assert(sigismember(&set
, SIGINT
));
99 assert(sigismember(&set
, SIGQUIT
));
100 assert(sigismember(&set
, SIGTERM
));
101 assert(sigismember(&set
, SIGUSR2
));
104 void SignalThread::sighup(signal_callback_fn
* arg
)
109 void SignalThread::sighup()
117 SignalThread::~SignalThread()
119 if (not is_shutdown())
121 set_shutdown(SHUTDOWN_GRACEFUL
);
125 if (pthread_equal(thread
, pthread_self()) != 0 and (pthread_kill(thread
, 0) == ESRCH
) == true)
128 pthread_join(thread
, &retval
);
136 static void *sig_thread(void *arg
)
138 SignalThread
*context
= (SignalThread
*)arg
;
143 while (context
->get_shutdown() == SHUTDOWN_RUNNING
)
147 if (context
->wait(sig
) == -1)
149 std::cerr
<< "sigwait() returned errno:" << strerror(errno
) << std::endl
;
166 if (context
->is_shutdown() == false)
168 context
->set_shutdown(SHUTDOWN_FORCED
);
171 if (context
->exit_on_signal())
179 std::cerr
<< "Signal handling thread got unexpected signal " << strsignal(sig
) << std::endl
;
189 SignalThread::SignalThread(bool exit_on_signal_arg
) :
190 _exit_on_signal(exit_on_signal_arg
),
191 magic_memory(MAGIC_MEMORY
),
192 __shutdown(SHUTDOWN_RUNNING
),
193 thread(pthread_self()),
196 pthread_mutex_init(&shutdown_mutex
, NULL
);
199 sigaddset(&set
, SIGABRT
);
200 sigaddset(&set
, SIGINT
);
201 sigaddset(&set
, SIGQUIT
);
202 sigaddset(&set
, SIGTERM
);
203 sigaddset(&set
, SIGUSR2
);
205 sem_init(&lock
, 0, 0);
209 bool SignalThread::setup()
211 set_shutdown(SHUTDOWN_RUNNING
);
214 if ((error
= pthread_sigmask(SIG_BLOCK
, &set
, NULL
)) != 0)
216 std::cerr
<< "pthread_sigmask() died during pthread_sigmask(" << strerror(error
) << ")" << std::endl
;
220 if ((error
= pthread_create(&thread
, NULL
, &sig_thread
, this)) != 0)
222 std::cerr
<< "pthread_create() died during pthread_create(" << strerror(error
) << ")" << std::endl
;
231 } /* namespace util */
232 } /* namespace datadifferential */