misc: Replace usleep with std::this_thread::sleep_for

This commit is contained in:
Peter Keresztes Schmidt 2021-06-13 20:29:53 +02:00
parent f675afc3f2
commit 6114d40593
7 changed files with 64 additions and 60 deletions

View File

@ -21,7 +21,6 @@
#include "zm_packet.h"
#include <sys/stat.h>
#include <unistd.h>
FileCamera::FileCamera(
const Monitor *monitor,
@ -71,7 +70,7 @@ void FileCamera::Terminate() {
}
int FileCamera::PreCapture() {
struct stat statbuf;
struct stat statbuf = {};
if (stat(path, &statbuf) < 0) {
Error("Can't stat %s: %s", path, strerror(errno));
return -1;
@ -82,7 +81,7 @@ int FileCamera::PreCapture() {
// 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);
std::this_thread::sleep_for(Milliseconds(100));
}
return 0;
}

View File

@ -1315,7 +1315,7 @@ int Monitor::actionBrightness(int p_brightness) {
int wait_loops = 10;
while (shared_data->action & SET_SETTINGS) {
if (wait_loops--) {
usleep(100000);
std::this_thread::sleep_for(Milliseconds(100));
} else {
Warning("Timed out waiting to set brightness");
return -1;
@ -1326,7 +1326,7 @@ int Monitor::actionBrightness(int p_brightness) {
int wait_loops = 10;
while (shared_data->action & GET_SETTINGS) {
if (wait_loops--) {
usleep(100000);
std::this_thread::sleep_for(Milliseconds(100));
} else {
Warning("Timed out waiting to get brightness");
return -1;
@ -1346,7 +1346,7 @@ int Monitor::actionContrast(int p_contrast) {
int wait_loops = 10;
while (shared_data->action & SET_SETTINGS) {
if (wait_loops--) {
usleep(100000);
std::this_thread::sleep_for(Milliseconds(100));
} else {
Warning("Timed out waiting to set contrast");
return -1;
@ -1357,7 +1357,7 @@ int Monitor::actionContrast(int p_contrast) {
int wait_loops = 10;
while (shared_data->action & GET_SETTINGS) {
if (wait_loops--) {
usleep(100000);
std::this_thread::sleep_for(Milliseconds(100));
} else {
Warning("Timed out waiting to get contrast");
return -1;
@ -1377,7 +1377,7 @@ int Monitor::actionHue(int p_hue) {
int wait_loops = 10;
while (shared_data->action & SET_SETTINGS) {
if (wait_loops--) {
usleep(100000);
std::this_thread::sleep_for(Milliseconds(100));
} else {
Warning("Timed out waiting to set hue");
return -1;
@ -1388,7 +1388,7 @@ int Monitor::actionHue(int p_hue) {
int wait_loops = 10;
while (shared_data->action & GET_SETTINGS) {
if (wait_loops--) {
usleep(100000);
std::this_thread::sleep_for(Milliseconds(100));
} else {
Warning("Timed out waiting to get hue");
return -1;
@ -1408,7 +1408,7 @@ int Monitor::actionColour(int p_colour) {
int wait_loops = 10;
while (shared_data->action & SET_SETTINGS) {
if (wait_loops--) {
usleep(100000);
std::this_thread::sleep_for(Milliseconds(100));
} else {
Warning("Timed out waiting to set colour");
return -1;
@ -1419,7 +1419,7 @@ int Monitor::actionColour(int p_colour) {
int wait_loops = 10;
while (shared_data->action & GET_SETTINGS) {
if (wait_loops--) {
usleep(100000);
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;

View File

@ -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);

View File

@ -300,13 +300,15 @@ int RemoteCameraHttp::ReadData(Buffer &buffer, unsigned int bytes_expected) {
} // end readData
int RemoteCameraHttp::GetData() {
time_t start_time = time(nullptr);
int buffer_len = 0;
TimePoint start_time = std::chrono::steady_clock::now();
int buffer_len;
while (!(buffer_len = ReadData(buffer))) {
if (zm_terminate or ( (time(nullptr) - start_time) > ZM_WATCH_MAX_DELAY ))
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");
usleep(100000);
std::this_thread::sleep_for(Milliseconds(100));
}
return buffer_len;
}

View File

@ -126,9 +126,10 @@ 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()) {
Error("No RTSP sources");
return -1;

View File

@ -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";

View File

@ -93,7 +93,6 @@ Options for use with monitors:
#include "zm_monitor.h"
#include "zm_local_camera.h"
#include <getopt.h>
#include <unistd.h>
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());