forked from ludiazv/node-nrf24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtryabort.cc
38 lines (34 loc) · 772 Bytes
/
tryabort.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <thread>
#include <csetjmp>
#include <csignal>
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include "tryabort.hpp"
static jmp_buf env;
static char point[512];
static std::function<void ()> on_abort_func=NullFunction;
#ifdef NRF24_DEBUG
void set_debug_abort(const char *txt){
strcpy(point,txt);
}
#endif
void on_abort(std::function<void ()> func){
on_abort_func=func;
}
void _on_sigabrt (int signum)
{
std::function<void ()> f=on_abort_func;
on_abort(); // Reset on abort
//printf("Aborted on:%s thread %p\n",point, std::this_thread::get_id());
if(f) f();
longjmp (env, 1);
}
void try_and_catch_abort (std::function<void ()> func)
{
if (setjmp (env) == 0) {
signal(SIGABRT, &_on_sigabrt);
point[0]='\0';
func();
}
}