18 #if !defined(_MSC_VER) 22 #define UNUSED(x) (void)x 24 #if !defined(_MSC_VER) 25 # define _access access 28 #if defined(_WIN32) && !defined(__MINGW32__) 29 typedef signed char int8_t;
30 typedef unsigned char uint8_t;
31 typedef short int16_t;
32 typedef unsigned short uint16_t;
34 typedef unsigned int uint32_t;
35 typedef __int64 int64_t;
36 typedef unsigned __int64 uint64_t;
43 #define __small_endian 46 #define __attribute__(x) 51 typedef uint8_t _size_t;
54 typedef uint64_t _size_t;
55 #define THREAD_PROC __stdcall 57 typedef uint32_t _size_t;
58 #define THREAD_PROC __stdcall 59 #elif defined (_M_X64) 60 typedef uint64_t _size_t;
61 #define THREAD_PROC __stdcall 62 #elif defined (__GNUC__) 63 typedef unsigned long _size_t;
65 #elif defined (__ICCARM__) 66 typedef uint32_t _size_t;
70 typedef _size_t (THREAD_PROC *thread_proc_t)(
void *);
72 typedef int32_t result_t;
75 #define RESULT_TIMEOUT -1 76 #define RESULT_FAIL -2 78 #define INVALID_TIMESTAMP (0) 81 DEVICE_DRIVER_TYPE_SERIALPORT = 0x0,
82 DEVICE_DRIVER_TYPE_TCP = 0x1,
86 #define IS_OK(x) ( (x) == RESULT_OK ) 87 #define IS_TIMEOUT(x) ( (x) == RESULT_TIMEOUT ) 88 #define IS_FAIL(x) ( (x) == RESULT_FAIL ) 92 #if __APPLE__ || _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE 97 static volatile sig_atomic_t g_signal_status = 0;
100 static struct sigaction old_action;
102 typedef void (* signal_handler_t)(int);
103 static signal_handler_t old_signal_handler = 0;
107 inline struct sigaction
108 set_sigaction(int signal_value, const struct sigaction &action)
110 inline signal_handler_t
111 set_signal_handler(
int signal_value, signal_handler_t signal_handler)
115 struct sigaction old_action;
116 ssize_t ret = sigaction(signal_value, &action, &old_action);
120 signal_handler_t old_signal_handler = std::signal(signal_value, signal_handler);
123 if (old_signal_handler == SIG_ERR)
126 const size_t error_length = 1024;
128 char error_string[error_length];
130 #if (defined(_GNU_SOURCE) && !defined(ANDROID) &&(_POSIX_C_SOURCE >= 200112L)) 131 char *msg = strerror_r(errno, error_string, error_length);
133 if (msg != error_string) {
134 strncpy(error_string, msg, error_length);
135 msg[error_length - 1] =
'\0';
139 int error_status = strerror_r(errno, error_string, error_length);
141 if (error_status != 0) {
142 throw std::runtime_error(
"Failed to get error string for errno: " +
143 std::to_string(errno));
148 strerror_s(error_string, error_length, errno);
151 throw std::runtime_error(
152 std::string(
"Failed to set SIGINT signal handler: (" + std::to_string(errno) +
")") +
160 return old_signal_handler;
164 inline void trigger_interrupt_guard_condition(
int signal_value) {
165 g_signal_status = signal_value;
166 signal(signal_value, SIG_DFL);
171 signal_handler(
int signal_value, siginfo_t *siginfo,
void *context)
173 signal_handler(
int signal_value)
177 printf(
"signal_handler(%d)\n", signal_value);
181 if (old_action.sa_flags & SA_SIGINFO) {
182 if (old_action.sa_sigaction != NULL) {
183 old_action.sa_sigaction(signal_value, siginfo, context);
187 old_action.sa_handler != NULL &&
188 old_action.sa_handler != SIG_DFL &&
189 old_action.sa_handler != SIG_IGN) {
190 old_action.sa_handler(signal_value);
196 if (old_signal_handler) {
197 old_signal_handler(signal_value);
202 trigger_interrupt_guard_condition(signal_value);
207 inline void init(
int argc,
char *argv[]) {
211 struct sigaction action;
212 memset(&action, 0,
sizeof(action));
213 sigemptyset(&action.sa_mask);
214 action.sa_sigaction = ::signal_handler;
215 action.sa_flags = SA_SIGINFO;
216 ::old_action = set_sigaction(SIGINT, action);
217 set_sigaction(SIGTERM, action);
220 ::old_signal_handler = set_signal_handler(SIGINT, ::signal_handler);
225 return g_signal_status == 0;
227 inline void shutdownNow() {
228 trigger_interrupt_guard_condition(SIGINT);
235 inline bool fileExists(
const std::string filename) {
237 struct _stat info = {0};
238 int ret = _stat(filename.c_str(), &info);
240 struct stat info = {0};
241 int ret = stat(filename.c_str(), &info);
251 #endif // V8STDINT_H_ Definition: help_info.h:39