9
9
#include < bitset>
10
10
#include < cassert>
11
11
#include < cerrno>
12
+ #include < chrono>
12
13
#include < cstdint>
13
14
#include < cstdio>
14
15
#include < cstdlib>
16
+ #include < future>
15
17
#include < thread>
16
18
#include < unordered_map>
17
19
35
37
#define SATURATE_IL 1600
36
38
#define NIBBLE_TO_HEX (n ) ((n) < 10 ? (n) + ' 0' : ((n) - 10 ) + ' a' )
37
39
38
- std::atomic<bool > safety_setter_thread_running (false );
39
40
std::atomic<bool > ignition (false );
40
41
41
42
ExitHandler do_exit;
42
43
43
- void safety_setter_thread (Panda *panda) {
44
+ using namespace std ::chrono_literals;
45
+
46
+ bool safety_setter_thread (Panda *panda) {
44
47
LOGD (" Starting safety setter thread" );
45
48
// diagnostic only is the default, needed for VIN query
46
49
panda->set_safety_model (cereal::CarParams::SafetyModel::ELM327);
@@ -50,8 +53,7 @@ void safety_setter_thread(Panda *panda) {
50
53
// switch to SILENT when CarVin param is read
51
54
while (true ) {
52
55
if (do_exit || !panda->connected ) {
53
- safety_setter_thread_running = false ;
54
- return ;
56
+ return false ;
55
57
};
56
58
57
59
std::string value_vin = p.get (" CarVin" );
@@ -71,8 +73,7 @@ void safety_setter_thread(Panda *panda) {
71
73
LOGW (" waiting for params to set safety model" );
72
74
while (true ) {
73
75
if (do_exit || !panda->connected ) {
74
- safety_setter_thread_running = false ;
75
- return ;
76
+ return false ;
76
77
};
77
78
78
79
if (p.getBool (" ControlsReady" )) {
@@ -94,8 +95,7 @@ void safety_setter_thread(Panda *panda) {
94
95
LOGW (" setting safety model: %d with param %d" , (int )safety_model, safety_param);
95
96
96
97
panda->set_safety_model (safety_model, safety_param);
97
-
98
- safety_setter_thread_running = false ;
98
+ return true ;
99
99
}
100
100
101
101
@@ -265,6 +265,7 @@ void panda_state_thread(Panda *&panda, bool spoofing_started) {
265
265
util::sleep_for (500 );
266
266
}
267
267
268
+ std::future<bool > safety_future;
268
269
// run at 2hz
269
270
while (!do_exit && panda->connected ) {
270
271
health_t pandaState = panda->get_state ();
@@ -301,10 +302,8 @@ void panda_state_thread(Panda *&panda, bool spoofing_started) {
301
302
// clear VIN, CarParams, and set new safety on car start
302
303
if (ignition && !ignition_last) {
303
304
params.clearAll (CLEAR_ON_IGNITION_ON);
304
-
305
- if (!safety_setter_thread_running) {
306
- safety_setter_thread_running = true ;
307
- std::thread (safety_setter_thread, panda).detach ();
305
+ if (!safety_future.valid () || safety_future.wait_for (0ms) == std::future_status::ready) {
306
+ safety_future = std::async (std::launch::async, safety_setter_thread, panda);
308
307
} else {
309
308
LOGW (" Safety setter thread already running" );
310
309
}
0 commit comments