Skip to content

Commit f0c6c9b

Browse files
committed
rebase master
1 parent 48035e9 commit f0c6c9b

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

selfdrive/boardd/boardd.cc

+11-12
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#include <bitset>
1010
#include <cassert>
1111
#include <cerrno>
12+
#include <chrono>
1213
#include <cstdint>
1314
#include <cstdio>
1415
#include <cstdlib>
16+
#include <future>
1517
#include <thread>
1618
#include <unordered_map>
1719

@@ -35,12 +37,13 @@
3537
#define SATURATE_IL 1600
3638
#define NIBBLE_TO_HEX(n) ((n) < 10 ? (n) + '0' : ((n) - 10) + 'a')
3739

38-
std::atomic<bool> safety_setter_thread_running(false);
3940
std::atomic<bool> ignition(false);
4041

4142
ExitHandler do_exit;
4243

43-
void safety_setter_thread(Panda *panda) {
44+
using namespace std::chrono_literals;
45+
46+
bool safety_setter_thread(Panda *panda) {
4447
LOGD("Starting safety setter thread");
4548
// diagnostic only is the default, needed for VIN query
4649
panda->set_safety_model(cereal::CarParams::SafetyModel::ELM327);
@@ -50,8 +53,7 @@ void safety_setter_thread(Panda *panda) {
5053
// switch to SILENT when CarVin param is read
5154
while (true) {
5255
if (do_exit || !panda->connected) {
53-
safety_setter_thread_running = false;
54-
return;
56+
return false;
5557
};
5658

5759
std::string value_vin = p.get("CarVin");
@@ -71,8 +73,7 @@ void safety_setter_thread(Panda *panda) {
7173
LOGW("waiting for params to set safety model");
7274
while (true) {
7375
if (do_exit || !panda->connected) {
74-
safety_setter_thread_running = false;
75-
return;
76+
return false;
7677
};
7778

7879
if (p.getBool("ControlsReady")) {
@@ -94,8 +95,7 @@ void safety_setter_thread(Panda *panda) {
9495
LOGW("setting safety model: %d with param %d", (int)safety_model, safety_param);
9596

9697
panda->set_safety_model(safety_model, safety_param);
97-
98-
safety_setter_thread_running = false;
98+
return true;
9999
}
100100

101101

@@ -265,6 +265,7 @@ void panda_state_thread(Panda *&panda, bool spoofing_started) {
265265
util::sleep_for(500);
266266
}
267267

268+
std::future<bool> safety_future;
268269
// run at 2hz
269270
while (!do_exit && panda->connected) {
270271
health_t pandaState = panda->get_state();
@@ -301,10 +302,8 @@ void panda_state_thread(Panda *&panda, bool spoofing_started) {
301302
// clear VIN, CarParams, and set new safety on car start
302303
if (ignition && !ignition_last) {
303304
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);
308307
} else {
309308
LOGW("Safety setter thread already running");
310309
}

0 commit comments

Comments
 (0)