b15bf0e4663b84214040c52828b59a34b801a973
[m6w6/libmemcached] / util / signal.hpp
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23
24 #pragma once
25
26 #include <pthread.h>
27 #include <semaphore.h>
28
29 #ifdef HAVE_SIGNAL_H
30 #include <signal.h>
31 #endif
32
33 namespace datadifferential {
34 namespace util {
35
36 enum shutdown_t {
37 SHUTDOWN_RUNNING,
38 SHUTDOWN_GRACEFUL,
39 SHUTDOWN_FORCED
40 };
41
42 class SignalThread {
43 bool _exit_on_signal;
44 sigset_t set;
45 sem_t lock;
46 uint64_t magic_memory;
47 volatile shutdown_t __shutdown;
48 pthread_mutex_t shutdown_mutex;
49 pthread_t thread;
50
51 public:
52
53 SignalThread(bool exit_on_signal_arg= false);
54
55 void test();
56 void post();
57 bool setup();
58
59 bool exit_on_signal()
60 {
61 return _exit_on_signal;
62 }
63
64 int wait(int& sig)
65 {
66 return sigwait(&set, &sig);
67 }
68
69 ~SignalThread();
70
71 void set_shutdown(shutdown_t arg);
72 bool is_shutdown();
73 shutdown_t get_shutdown();
74 };
75
76 } /* namespace util */
77 } /* namespace datadifferential */