16 #define UNUSED(x) (void)x 18 #if defined(_WIN32) && !defined(__MINGW32__) 19 typedef signed char int8_t;
20 typedef unsigned char uint8_t;
21 typedef short int16_t;
22 typedef unsigned short uint16_t;
24 typedef unsigned int uint32_t;
25 typedef __int64 int64_t;
26 typedef unsigned __int64 uint64_t;
33 #define __small_endian 36 #define __attribute__(x) 41 typedef uint8_t _size_t;
44 typedef uint64_t _size_t;
45 #define THREAD_PROC __stdcall 47 typedef uint32_t _size_t;
48 #define THREAD_PROC __stdcall 49 #elif defined (_M_X64) 50 typedef uint64_t _size_t;
51 #define THREAD_PROC __stdcall 52 #elif defined (__GNUC__) 53 typedef unsigned long _size_t;
55 #elif defined (__ICCARM__) 56 typedef uint32_t _size_t;
60 typedef _size_t (THREAD_PROC * thread_proc_t ) (
void * );
62 typedef int32_t result_t;
63 typedef uint64_t TTimeStamp;
66 #define RESULT_TIMEOUT -1 67 #define RESULT_FAIL -2 69 #define INVALID_TIMESTAMP (0) 71 #define IS_OK(x) ( (x) == RESULT_OK ) 72 #define IS_TIMEOUT(x) ( (x) == RESULT_TIMEOUT ) 73 #define IS_FAIL(x) ( (x) == RESULT_FAIL ) 77 DEVICE_DRIVER_TYPE_SERIALPORT = 0x0,
78 DEVICE_DRIVER_TYPE_TCP = 0x1,
82 #if !defined(_countof) 83 #define _countof(_Array) (int)(sizeof(_Array) / sizeof(_Array[0])) 87 #define M_PI 3.1415926 90 #define DEG2RAD(x) ((x)*M_PI/180.) 94 #if __APPLE__ || _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE 99 static volatile sig_atomic_t g_signal_status = 0;
102 static struct sigaction old_action;
104 typedef void (* signal_handler_t)(int);
105 static signal_handler_t old_signal_handler = 0;
109 inline struct sigaction
110 set_sigaction(int signal_value, const struct sigaction & action)
112 inline signal_handler_t
113 set_signal_handler(
int signal_value, signal_handler_t signal_handler)
117 struct sigaction old_action;
118 ssize_t ret = sigaction(signal_value, &action, &old_action);
121 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);
132 if (msg != error_string) {
133 strncpy(error_string, msg, error_length);
134 msg[error_length - 1] =
'\0';
137 int error_status = strerror_r(errno, error_string, error_length);
138 if (error_status != 0) {
139 throw std::runtime_error(
"Failed to get error string for errno: " + std::to_string(errno));
143 strerror_s(error_string, error_length, errno);
146 std::ostringstream stm ;
148 throw std::runtime_error(
149 std::string(
"Failed to set SIGINT signal handler: (" + stm.str() +
")") +
157 return old_signal_handler;
161 inline void trigger_interrupt_guard_condition(
int signal_value) {
162 g_signal_status = signal_value;
163 signal(signal_value, SIG_DFL);
168 signal_handler(
int signal_value, siginfo_t * siginfo,
void * context)
170 signal_handler(
int signal_value)
174 printf(
"signal_handler(%d)\n", signal_value);
177 if (old_action.sa_flags & SA_SIGINFO) {
178 if (old_action.sa_sigaction != NULL) {
179 old_action.sa_sigaction(signal_value, siginfo, context);
183 old_action.sa_handler != NULL &&
184 old_action.sa_handler != SIG_DFL &&
185 old_action.sa_handler != SIG_IGN)
187 old_action.sa_handler(signal_value);
191 if (old_signal_handler) {
192 old_signal_handler(signal_value);
196 trigger_interrupt_guard_condition(signal_value);
201 inline void init(
int argc,
char *argv[]) {
205 struct sigaction action;
206 memset(&action, 0,
sizeof(action));
207 sigemptyset(&action.sa_mask);
208 action.sa_sigaction = ::signal_handler;
209 action.sa_flags = SA_SIGINFO;
210 ::old_action = set_sigaction(SIGINT, action);
211 set_sigaction(SIGTERM, action);
214 ::old_signal_handler = set_signal_handler(SIGINT, ::signal_handler);
219 return g_signal_status == 0;
221 inline void shutdownNow() {
222 trigger_interrupt_guard_condition(SIGINT);
228 #endif // V8STDINT_H_ Definition: v8stdint.h:199