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,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;
}

View File

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

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,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() {

View File

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

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