MT#65612 RegThread: use `run_cond.wait_for()` instead of sleeping

Instead of blindly sleeping, use the `run_cond.wait_for()`
for the case, when AmThread sends `.notify_all()` upon stopping.
Hence we don't have to block the whole thread for 10 seconds if
suddenly the `stop()` is requested by AmThread at the very beginning
of the sleep.

Add a dedicated `RegThread::wait_or_stop()` wrapper to handle this.

Also differentiate whether the `RegThread::wait_or_stop()` returns
due to timer exceed or there was an actual thread stop requested.
Act accordingly during the `run()` execution.

Change-Id: I1e603c462af983e633c811d10ed2d0ac506f46d3
mr26.2
Donat Zenichev 2 weeks ago
parent f0193d91b7
commit e9995a63f9

@ -31,6 +31,7 @@
#include "sems.h"
#include "log.h"
#include <chrono>
#include <unistd.h>
#include "ampi/SIPRegistrarClientAPI.h"
@ -115,7 +116,6 @@ AmSession* RegistrationAgentFactory::onInvite(const AmSipRequest& req, const str
return NULL;
}
void RegThread::add_reg(const RegInfo& ri) {
registrations.push_back(ri);
}
@ -176,12 +176,30 @@ bool RegThread::check_registration(const RegInfo& ri) {
return false;
}
/**
* true => stop was requested
* false => timeout elapsed normally
*/
bool RegThread::wait_or_stop(unsigned int seconds)
{
/* it should use the same mutex that protects the condition */
std::unique_lock<std::mutex> l(run_mut);
/* wait for `run_cond.notify_all()` / `notify_one()` or timeout */
return run_cond.wait_for(
l,
std::chrono::seconds(seconds),
[this] { return stop_requested_unlocked(); });
}
void RegThread::run() {
DBG("registrar client started.\n");
/* wait for sems to completely start up */
sleep(2);
/* wait for sems to completely start up, but allow clean shutdown */
if (wait_or_stop(2)) {
DBG("Stop was requested, return..\n");
return;
}
while (!stop_requested())
{
@ -196,7 +214,11 @@ void RegThread::run() {
create_registration(*it);
}
}
sleep(10); /* 10 seconds */
/* wait until next retry interval, or exit immediately on stop() */
if (wait_or_stop(10)) {
DBG("Stop was requested, return..\n");
break;
}
}
}

@ -57,6 +57,7 @@ class RegThread : public AmThread {
void run();
void on_stop();
public:
bool wait_or_stop(unsigned int seconds);
void add_reg(const RegInfo& ri);
void postEvent(AmEvent* ev);
};

Loading…
Cancel
Save