From c1cf0882bd4e0fce5e1a581536f6a0e7afb640e6 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 5 Jan 2021 01:03:11 +0800 Subject: [PATCH] Replace usleep with util::sleep_for (#19643) * replace usleep with sleep_for * sleep microseconds * sleep nanoseconds * Trigger Build --- installer/updater/updater.cc | 6 +++--- selfdrive/boardd/boardd.cc | 16 ++++++++-------- selfdrive/boardd/panda.cc | 4 ++-- selfdrive/boardd/pigeon.cc | 11 ++++++----- selfdrive/camerad/cameras/camera_qcom.cc | 3 ++- selfdrive/camerad/cameras/camera_qcom2.cc | 3 ++- selfdrive/common/params.cc | 2 +- selfdrive/loggerd/loggerd.cc | 2 +- selfdrive/loggerd/tests/testraw.cc | 4 ++-- selfdrive/modeld/dmonitoringmodeld.cc | 3 ++- selfdrive/modeld/modeld.cc | 3 ++- selfdrive/proclogd/proclogd.cc | 2 +- selfdrive/sensord/sensors/bmx055_magn.cc | 3 ++- 13 files changed, 34 insertions(+), 28 deletions(-) diff --git a/installer/updater/updater.cc b/installer/updater/updater.cc index d737ea24..24e47439 100644 --- a/installer/updater/updater.cc +++ b/installer/updater/updater.cc @@ -465,7 +465,7 @@ struct Updater { while(battery_cap < min_battery_cap) { battery_cap = battery_capacity(); battery_cap_text = std::to_string(battery_cap); - usleep(1000000); + util::sleep_for(1000); } set_running(); } @@ -483,7 +483,7 @@ struct Updater { while(battery_cap < min_battery_cap) { battery_cap = battery_capacity(); battery_cap_text = std::to_string(battery_cap); - usleep(1000000); + util::sleep_for(1000); } set_running(); } @@ -756,7 +756,7 @@ struct Updater { assert(glGetError() == GL_NO_ERROR); // no simple way to do 30fps vsync with surfaceflinger... - usleep(30000); + util::sleep_for(30); } if (update_thread_handle.joinable()) { diff --git a/selfdrive/boardd/boardd.cc b/selfdrive/boardd/boardd.cc index 0045dea2..d8da30be 100644 --- a/selfdrive/boardd/boardd.cc +++ b/selfdrive/boardd/boardd.cc @@ -22,6 +22,7 @@ #include "cereal/gen/cpp/car.capnp.h" #include "common/util.h" +#include "common/utilpp.h" #include "common/params.h" #include "common/swaglog.h" #include "common/timing.h" @@ -85,7 +86,7 @@ void safety_setter_thread() { LOGW("got CarVin %s", str_vin.c_str()); break; } - usleep(100*1000); + util::sleep_for(100); } // VIN query done, stop listening to OBDII @@ -101,7 +102,7 @@ void safety_setter_thread() { params = Params().read_db_bytes("CarParams"); if (params.size() > 0) break; - usleep(100*1000); + util::sleep_for(100); } LOGW("got %d bytes CarParams", params.size()); @@ -193,7 +194,7 @@ bool usb_connect() { // must be called before threads or with mutex void usb_retry_connect() { LOGW("attempting to connect"); - while (!usb_connect()) { usleep(100*1000); } + while (!usb_connect()) { util::sleep_for(100); } LOGW("connected to board"); } @@ -260,8 +261,7 @@ void can_recv_thread() { uint64_t cur_time = nanos_since_boot(); int64_t remaining = next_frame_time - cur_time; if (remaining > 0){ - useconds_t sleep = remaining / 1000; - usleep(sleep); + std::this_thread::sleep_for(std::chrono::nanoseconds(remaining)); } else { if (ignition){ LOGW("missed cycles (%d) %lld", (int)-1*remaining/dt, remaining); @@ -288,7 +288,7 @@ void can_health_thread() { healthData.setHwType(cereal::HealthData::HwType::UNKNOWN); pm.send("health", msg); - usleep(500*1000); + util::sleep_for(500); } // run at 2hz @@ -388,7 +388,7 @@ void can_health_thread() { } pm.send("health", msg); panda->send_heartbeat(); - usleep(500*1000); + util::sleep_for(500); } } @@ -508,7 +508,7 @@ void pigeon_thread() { ignition_last = ignition; // 10ms - 100 Hz - usleep(10*1000); + util::sleep_for(10); } delete pigeon; diff --git a/selfdrive/boardd/panda.cc b/selfdrive/boardd/panda.cc index d89e302f..0eea2b49 100644 --- a/selfdrive/boardd/panda.cc +++ b/selfdrive/boardd/panda.cc @@ -6,7 +6,7 @@ #include "common/swaglog.h" #include "common/gpio.h" - +#include "common/utilpp.h" #include "panda.h" #ifdef QCOM2 @@ -30,7 +30,7 @@ void panda_set_power(bool power){ err += gpio_set(GPIO_STM_RST_N, is_legacy ? false : true); err += gpio_set(GPIO_STM_BOOT0, false); - usleep(100*1000); // 100 ms + util::sleep_for(100); // 100 ms err += gpio_set(GPIO_STM_RST_N, is_legacy ? power : (!power)); assert(err == 0); diff --git a/selfdrive/boardd/pigeon.cc b/selfdrive/boardd/pigeon.cc index 4ec7ebf8..5d2a9379 100644 --- a/selfdrive/boardd/pigeon.cc +++ b/selfdrive/boardd/pigeon.cc @@ -6,6 +6,7 @@ #include "common/swaglog.h" #include "common/gpio.h" +#include "common/utilpp.h" #include "pigeon.h" @@ -32,27 +33,27 @@ Pigeon * Pigeon::connect(const char * tty){ } void Pigeon::init() { - usleep(1000*1000); + util::sleep_for(1000); LOGW("panda GPS start"); // power off pigeon set_power(0); - usleep(100*1000); + util::sleep_for(100); // 9600 baud at init set_baud(9600); // power on pigeon set_power(1); - usleep(500*1000); + util::sleep_for(500); // baud rate upping send("\x24\x50\x55\x42\x58\x2C\x34\x31\x2C\x31\x2C\x30\x30\x30\x37\x2C\x30\x30\x30\x33\x2C\x34\x36\x30\x38\x30\x30\x2C\x30\x2A\x31\x35\x0D\x0A"s); - usleep(100*1000); + util::sleep_for(100); // set baud rate to 460800 set_baud(460800); - usleep(100*1000); + util::sleep_for(100); // init from ubloxd // To generate this data, run test/ubloxd.py with the print statements enabled in the write function in panda/python/serial.py diff --git a/selfdrive/camerad/cameras/camera_qcom.cc b/selfdrive/camerad/cameras/camera_qcom.cc index 79f84f5b..899139ae 100644 --- a/selfdrive/camerad/cameras/camera_qcom.cc +++ b/selfdrive/camerad/cameras/camera_qcom.cc @@ -21,6 +21,7 @@ #include "msm_cam_sensor.h" #include "common/util.h" +#include "common/utilpp.h" #include "common/timing.h" #include "common/swaglog.h" #include "common/params.h" @@ -2025,7 +2026,7 @@ static void* ops_thread(void* arg) { front_op_id_last = front_op.op_id; } - usleep(50000); + util::sleep_for(50); } return NULL; diff --git a/selfdrive/camerad/cameras/camera_qcom2.cc b/selfdrive/camerad/cameras/camera_qcom2.cc index b45c094f..b81c21a9 100644 --- a/selfdrive/camerad/cameras/camera_qcom2.cc +++ b/selfdrive/camerad/cameras/camera_qcom2.cc @@ -13,6 +13,7 @@ #include #include "common/util.h" +#include "common/utilpp.h" #include "common/swaglog.h" #include "camera_qcom2.h" @@ -1087,7 +1088,7 @@ static void* ae_thread(void* arg) { } } - usleep(50000); + util::sleep_for(50); } return NULL; diff --git a/selfdrive/common/params.cc b/selfdrive/common/params.cc index 6a4e9169..ffae79fa 100644 --- a/selfdrive/common/params.cc +++ b/selfdrive/common/params.cc @@ -291,7 +291,7 @@ int Params::read_db_value_blocking(const char* key, char** value, size_t* value_ if (result == 0) { break; } else { - usleep(100000); // 0.1 s + util::sleep_for(100); // 0.1 s } } diff --git a/selfdrive/loggerd/loggerd.cc b/selfdrive/loggerd/loggerd.cc index f2166fe3..a247aaff 100644 --- a/selfdrive/loggerd/loggerd.cc +++ b/selfdrive/loggerd/loggerd.cc @@ -242,7 +242,7 @@ void encoder_thread(RotateState *rotate_state, int cam_idx) { int err = visionstream_init(&stream, cameras_logged[cam_idx].stream_type, false, &buf_info); if (err != 0) { LOGD("visionstream connect fail"); - usleep(100000); + util::sleep_for(100); continue; } diff --git a/selfdrive/loggerd/tests/testraw.cc b/selfdrive/loggerd/tests/testraw.cc index 80858e6f..9111a9ff 100644 --- a/selfdrive/loggerd/tests/testraw.cc +++ b/selfdrive/loggerd/tests/testraw.cc @@ -6,7 +6,7 @@ #include "common/visionipc.h" #include "common/timing.h" - +#include "common/utilpp.h" #include "RawLogger.h" int main() { @@ -19,7 +19,7 @@ int main() { err = visionstream_init(&stream, VISION_STREAM_YUV, false, &buf_info); if (err != 0) { printf("visionstream fail\n"); - usleep(100000); + util::sleep_for(100); } break; } diff --git a/selfdrive/modeld/dmonitoringmodeld.cc b/selfdrive/modeld/dmonitoringmodeld.cc index 80af643d..0b711273 100644 --- a/selfdrive/modeld/dmonitoringmodeld.cc +++ b/selfdrive/modeld/dmonitoringmodeld.cc @@ -5,6 +5,7 @@ #include #include +#include "common/utilpp.h" #include "common/visionbuf.h" #include "common/visionipc.h" #include "common/swaglog.h" @@ -42,7 +43,7 @@ int main(int argc, char **argv) { err = visionstream_init(&stream, VISION_STREAM_YUV_FRONT, true, &buf_info); if (err) { printf("visionstream connect fail\n"); - usleep(100000); + util::sleep_for(100); continue; } LOGW("connected with buffer size: %d", buf_info.buf_len); diff --git a/selfdrive/modeld/modeld.cc b/selfdrive/modeld/modeld.cc index d3b193b7..2614f6f4 100644 --- a/selfdrive/modeld/modeld.cc +++ b/selfdrive/modeld/modeld.cc @@ -8,6 +8,7 @@ #include "common/visionipc.h" #include "common/swaglog.h" #include "common/clutil.h" +#include "common/utilpp.h" #include "models/driving.h" #include "messaging.hpp" @@ -132,7 +133,7 @@ int main(int argc, char **argv) { err = visionstream_init(&stream, VISION_STREAM_YUV, true, &buf_info); if (err) { LOGW("visionstream connect failed"); - usleep(100000); + util::sleep_for(100); continue; } LOGW("connected with buffer size: %d", buf_info.buf_len); diff --git a/selfdrive/proclogd/proclogd.cc b/selfdrive/proclogd/proclogd.cc index 85d1ba60..968d35ca 100644 --- a/selfdrive/proclogd/proclogd.cc +++ b/selfdrive/proclogd/proclogd.cc @@ -235,7 +235,7 @@ int main() { publisher.send("procLog", msg); - usleep(2000000); // 2 secs + util::sleep_for(2000); // 2 secs } return 0; diff --git a/selfdrive/sensord/sensors/bmx055_magn.cc b/selfdrive/sensord/sensors/bmx055_magn.cc index fbe43c20..885fceeb 100644 --- a/selfdrive/sensord/sensors/bmx055_magn.cc +++ b/selfdrive/sensord/sensors/bmx055_magn.cc @@ -3,6 +3,7 @@ #include #include "common/swaglog.h" +#include "common/utilpp.h" #include "bmx055_magn.hpp" @@ -37,7 +38,7 @@ int BMX055_Magn::init(){ LOGE("Enabling power failed: %d", ret); goto fail; } - usleep(5 * 1000); // wait until the chip is powered on + util::sleep_for(5); // wait until the chip is powered on // read chip ID ret = read_register(BMX055_MAGN_I2C_REG_ID, buffer, 1);