From 6114d4059355d1eb8431e48700d5c05ca9d51eda Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sun, 13 Jun 2021 20:29:53 +0200 Subject: [PATCH] misc: Replace usleep with std::this_thread::sleep_for --- src/zm_file_camera.cpp | 9 +++-- src/zm_monitor.cpp | 65 +++++++++++++++++------------------ src/zm_monitorstream.cpp | 2 +- src/zm_remote_camera_http.cpp | 20 ++++++----- src/zm_remote_camera_rtsp.cpp | 7 ++-- src/zm_rtsp.cpp | 7 ++-- src/zmu.cpp | 14 ++++---- 7 files changed, 64 insertions(+), 60 deletions(-) diff --git a/src/zm_file_camera.cpp b/src/zm_file_camera.cpp index 2563482f1..9740fc52e 100644 --- a/src/zm_file_camera.cpp +++ b/src/zm_file_camera.cpp @@ -21,7 +21,6 @@ #include "zm_packet.h" #include -#include FileCamera::FileCamera( const Monitor *monitor, @@ -71,8 +70,8 @@ void FileCamera::Terminate() { } int FileCamera::PreCapture() { - struct stat statbuf; - if ( stat(path, &statbuf) < 0 ) { + struct stat statbuf = {}; + if (stat(path, &statbuf) < 0) { Error("Can't stat %s: %s", path, strerror(errno)); return -1; } @@ -81,8 +80,8 @@ int FileCamera::PreCapture() { // This waits until 1 second has passed since it was modified. Effectively limiting fps to 60. // Which is kinda bogus. If we were writing to this jpg constantly faster than we are monitoring it here // we would never break out of this loop - while ( (time(nullptr) - statbuf.st_mtime) < 1 ) { - usleep(100000); + while ((time(nullptr) - statbuf.st_mtime) < 1) { + std::this_thread::sleep_for(Milliseconds(100)); } return 0; } diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 08f458438..3f05669f9 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -1308,14 +1308,14 @@ void Monitor::actionResume() { } int Monitor::actionBrightness(int p_brightness) { - if ( purpose != CAPTURE ) { - if ( p_brightness >= 0 ) { + if (purpose != CAPTURE) { + if (p_brightness >= 0) { shared_data->brightness = p_brightness; shared_data->action |= SET_SETTINGS; int wait_loops = 10; - while ( shared_data->action & SET_SETTINGS ) { - if ( wait_loops-- ) { - usleep(100000); + while (shared_data->action & SET_SETTINGS) { + if (wait_loops--) { + std::this_thread::sleep_for(Milliseconds(100)); } else { Warning("Timed out waiting to set brightness"); return -1; @@ -1324,9 +1324,9 @@ int Monitor::actionBrightness(int p_brightness) { } else { shared_data->action |= GET_SETTINGS; int wait_loops = 10; - while ( shared_data->action & GET_SETTINGS ) { - if ( wait_loops-- ) { - usleep(100000); + while (shared_data->action & GET_SETTINGS) { + if (wait_loops--) { + std::this_thread::sleep_for(Milliseconds(100)); } else { Warning("Timed out waiting to get brightness"); return -1; @@ -1339,14 +1339,14 @@ int Monitor::actionBrightness(int p_brightness) { } // end int Monitor::actionBrightness(int p_brightness) int Monitor::actionContrast(int p_contrast) { - if ( purpose != CAPTURE ) { - if ( p_contrast >= 0 ) { + if (purpose != CAPTURE) { + if (p_contrast >= 0) { shared_data->contrast = p_contrast; shared_data->action |= SET_SETTINGS; int wait_loops = 10; - while ( shared_data->action & SET_SETTINGS ) { - if ( wait_loops-- ) { - usleep(100000); + while (shared_data->action & SET_SETTINGS) { + if (wait_loops--) { + std::this_thread::sleep_for(Milliseconds(100)); } else { Warning("Timed out waiting to set contrast"); return -1; @@ -1355,9 +1355,9 @@ int Monitor::actionContrast(int p_contrast) { } else { shared_data->action |= GET_SETTINGS; int wait_loops = 10; - while ( shared_data->action & GET_SETTINGS ) { - if ( wait_loops-- ) { - usleep(100000); + while (shared_data->action & GET_SETTINGS) { + if (wait_loops--) { + std::this_thread::sleep_for(Milliseconds(100)); } else { Warning("Timed out waiting to get contrast"); return -1; @@ -1370,14 +1370,14 @@ int Monitor::actionContrast(int p_contrast) { } // end int Monitor::actionContrast(int p_contrast) int Monitor::actionHue(int p_hue) { - if ( purpose != CAPTURE ) { - if ( p_hue >= 0 ) { + if (purpose != CAPTURE) { + if (p_hue >= 0) { shared_data->hue = p_hue; shared_data->action |= SET_SETTINGS; int wait_loops = 10; - while ( shared_data->action & SET_SETTINGS ) { - if ( wait_loops-- ) { - usleep(100000); + while (shared_data->action & SET_SETTINGS) { + if (wait_loops--) { + std::this_thread::sleep_for(Milliseconds(100)); } else { Warning("Timed out waiting to set hue"); return -1; @@ -1386,9 +1386,9 @@ int Monitor::actionHue(int p_hue) { } else { shared_data->action |= GET_SETTINGS; int wait_loops = 10; - while ( shared_data->action & GET_SETTINGS ) { - if ( wait_loops-- ) { - usleep(100000); + while (shared_data->action & GET_SETTINGS) { + if (wait_loops--) { + std::this_thread::sleep_for(Milliseconds(100)); } else { Warning("Timed out waiting to get hue"); return -1; @@ -1401,14 +1401,14 @@ int Monitor::actionHue(int p_hue) { } // end int Monitor::actionHue(int p_hue) int Monitor::actionColour(int p_colour) { - if ( purpose != CAPTURE ) { - if ( p_colour >= 0 ) { + if (purpose != CAPTURE) { + if (p_colour >= 0) { shared_data->colour = p_colour; shared_data->action |= SET_SETTINGS; int wait_loops = 10; - while ( shared_data->action & SET_SETTINGS ) { - if ( wait_loops-- ) { - usleep(100000); + while (shared_data->action & SET_SETTINGS) { + if (wait_loops--) { + std::this_thread::sleep_for(Milliseconds(100)); } else { Warning("Timed out waiting to set colour"); return -1; @@ -1417,9 +1417,9 @@ int Monitor::actionColour(int p_colour) { } else { shared_data->action |= GET_SETTINGS; int wait_loops = 10; - while ( shared_data->action & GET_SETTINGS ) { - if ( wait_loops-- ) { - usleep(100000); + while (shared_data->action & GET_SETTINGS) { + if (wait_loops--) { + std::this_thread::sleep_for(Milliseconds(100)); } else { Warning("Timed out waiting to get colour"); return -1; @@ -3116,7 +3116,6 @@ void Monitor::get_ref_image() { // can't analyse it anyways, incremement packetqueue.increment_it(analysis_it); } - //usleep(10000); } if (zm_terminate) return; diff --git a/src/zm_monitorstream.cpp b/src/zm_monitorstream.cpp index e13c54bd5..8ba498b44 100644 --- a/src/zm_monitorstream.cpp +++ b/src/zm_monitorstream.cpp @@ -854,7 +854,7 @@ void MonitorStream::SingleImage(int scale) { Image scaled_image; while ((monitor->shared_data->last_write_index >= monitor->image_buffer_count) and !zm_terminate) { Debug(1, "Waiting for capture to begin"); - usleep(100000); + std::this_thread::sleep_for(Milliseconds(100)); } int index = monitor->shared_data->last_write_index % monitor->image_buffer_count; Debug(1, "write index: %d %d", monitor->shared_data->last_write_index, index); diff --git a/src/zm_remote_camera_http.cpp b/src/zm_remote_camera_http.cpp index f09db425e..5bfab0966 100644 --- a/src/zm_remote_camera_http.cpp +++ b/src/zm_remote_camera_http.cpp @@ -300,15 +300,17 @@ int RemoteCameraHttp::ReadData(Buffer &buffer, unsigned int bytes_expected) { } // end readData int RemoteCameraHttp::GetData() { - time_t start_time = time(nullptr); - int buffer_len = 0; - while (!(buffer_len = ReadData(buffer))) { - if (zm_terminate or ( (time(nullptr) - start_time) > ZM_WATCH_MAX_DELAY )) - return -1; - Debug(4, "Timeout waiting for REGEXP HEADER"); - usleep(100000); - } - return buffer_len; + TimePoint start_time = std::chrono::steady_clock::now(); + int buffer_len; + while (!(buffer_len = ReadData(buffer))) { + if (zm_terminate or std::chrono::steady_clock::now() - start_time > FPSeconds(config.watch_max_delay)) { + return -1; + } + + Debug(4, "Timeout waiting for REGEXP HEADER"); + std::this_thread::sleep_for(Milliseconds(100)); + } + return buffer_len; } int RemoteCameraHttp::GetResponse() { diff --git a/src/zm_remote_camera_rtsp.cpp b/src/zm_remote_camera_rtsp.cpp index 0055be86c..8cdecdf94 100644 --- a/src/zm_remote_camera_rtsp.cpp +++ b/src/zm_remote_camera_rtsp.cpp @@ -126,10 +126,11 @@ int RemoteCameraRtsp::Disconnect() { int RemoteCameraRtsp::PrimeCapture() { Debug(2, "Waiting for sources"); - for ( int i = 0; (i < 100) && !rtspThread->hasSources(); i++ ) { - usleep(100000); + for (int i = 0; i < 100 && !rtspThread->hasSources(); i++) { + std::this_thread::sleep_for(Microseconds(100)); } - if ( !rtspThread->hasSources() ) { + + if (!rtspThread->hasSources()) { Error("No RTSP sources"); return -1; } diff --git a/src/zm_rtsp.cpp b/src/zm_rtsp.cpp index 5f69e8374..5c4af87eb 100644 --- a/src/zm_rtsp.cpp +++ b/src/zm_rtsp.cpp @@ -331,7 +331,8 @@ void RtspThread::Run() { authTried = true; sendCommand(message); // FIXME Why sleep 1? - usleep(10000); + std::this_thread::sleep_for(Microseconds(10)); + res = recvResponse(response); if ( !res && respCode==401 ) mNeedAuth = true; @@ -583,7 +584,7 @@ void RtspThread::Run() { return; lastKeepalive = now; } - usleep( 100000 ); + std::this_thread::sleep_for(Microseconds(100)); } #if 0 message = "PAUSE "+mUrl+" RTSP/1.0\r\nSession: "+session+"\r\n"; @@ -742,7 +743,7 @@ void RtspThread::Run() { return; lastKeepalive = time(nullptr); } - usleep(100000); + std::this_thread::sleep_for(Microseconds(100)); } #if 0 message = "PAUSE "+mUrl+" RTSP/1.0\r\nSession: "+session+"\r\n"; diff --git a/src/zmu.cpp b/src/zmu.cpp index 55ee02e21..d0476cf6b 100644 --- a/src/zmu.cpp +++ b/src/zmu.cpp @@ -93,7 +93,6 @@ Options for use with monitors: #include "zm_monitor.h" #include "zm_local_camera.h" #include -#include void Usage(int status=-1) { fputs( @@ -592,13 +591,16 @@ int main(int argc, char *argv[]) { // Ensure that we are not recording. So the forced alarm is distinct from what was recording before monitor->ForceAlarmOff(); monitor->ForceAlarmOn(config.forced_alarm_score, "Forced Web"); - int wait = 10*1000*1000; // 10 seconds - while ((monitor->GetState() != Monitor::ALARM) and !zm_terminate and wait) { + + Microseconds wait_time = Seconds(10); + while ((monitor->GetState() != Monitor::ALARM) and !zm_terminate and wait_time > Seconds(0)) { // Wait for monitor to notice. - usleep(1000); - wait -= 1000; + Microseconds sleep = Microseconds(1); + std::this_thread::sleep_for(sleep); + wait_time -= sleep; } - if ( monitor->GetState() != Monitor::ALARM and !wait ) { + + if (monitor->GetState() != Monitor::ALARM and wait_time == Seconds(0)) { Error("Monitor failed to respond to forced alarm."); } else { printf("Alarmed event id: %" PRIu64 "\n", monitor->GetLastEventId());