Skip to content

Commit

Permalink
Merge pull request #4498 from telefonicaid/hardening/simplifying_mana…
Browse files Browse the repository at this point in the history
…gers_init_methods

FIX simplify init methods in managers
  • Loading branch information
mapedraza authored Jan 31, 2024
2 parents d4ecf87 + 2bba4c3 commit 3f3cee0
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 37 deletions.
11 changes: 4 additions & 7 deletions src/lib/alarmMgr/AlarmManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ AlarmManager::AlarmManager()
*
* AlarmManager::init -
*/
int AlarmManager::init(bool logAlreadyRaisedAlarms)
void AlarmManager::init(bool logAlreadyRaisedAlarms)
{
notificationErrorLogAlways = logAlreadyRaisedAlarms;
badInputLogAlways = logAlreadyRaisedAlarms;
dbErrorLogAlways = logAlreadyRaisedAlarms;
mqttConnectionErrorLogAlways = logAlreadyRaisedAlarms;
forwardingErrorLogAlways = logAlreadyRaisedAlarms;

return semInit();
semInit();
}


Expand All @@ -96,15 +96,12 @@ int AlarmManager::init(bool logAlreadyRaisedAlarms)
*
* AlarmManager::semInit -
*/
int AlarmManager::semInit(void)
void AlarmManager::semInit(void)
{
if (sem_init(&sem, 0, 1) == -1)
{
LM_E(("Runtime Error (error initializing 'alarm mgr' semaphore: %s)", strerror(errno)));
return -1;
LM_X(1, ("Fatal Error (error initializing 'alarm mgr' semaphore: %s)", strerror(errno)));
}

return 0;
}


Expand Down
4 changes: 2 additions & 2 deletions src/lib/alarmMgr/AlarmManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class AlarmManager
public:
AlarmManager();

int init(bool logAlreadyRaisedAlarms);
void init(bool logAlreadyRaisedAlarms);

void semTake(void);
void semGive(void);
Expand Down Expand Up @@ -97,7 +97,7 @@ class AlarmManager
void mqttConnectionErrorGet(int64_t* active, int64_t* raised, int64_t* released);

private:
int semInit(void);
void semInit(void);
};

#endif // SRC_LIB_ALARMMGR_ALARMMANAGER_H_
15 changes: 5 additions & 10 deletions src/lib/common/sem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,32 @@ static struct timespec accTimeStatSemTime = { 0, 0 };
* -1 on failure
*
*/
int semInit(SemOpType _reqPolicy, bool semTimeStat, int shared, int takenInitially)
void semInit(SemOpType _reqPolicy, bool semTimeStat, int shared, int takenInitially)
{
if (sem_init(&reqSem, shared, takenInitially) == -1)
{
LM_E(("Runtime Error (error initializing 'req' semaphore: %s)", strerror(errno)));
return -1;
LM_X(1, ("Fatal Error (error initializing 'req' semaphore: %s)", strerror(errno)));
}

if (sem_init(&transSem, shared, takenInitially) == -1)
{
LM_E(("Runtime Error (error initializing 'transactionId' semaphore: %s)", strerror(errno)));
return -1;
LM_X(1, ("Fatal Error (error initializing 'transactionId' semaphore: %s)", strerror(errno)));
}

if (sem_init(&cacheSem, shared, takenInitially) == -1)
{
LM_E(("Runtime Error (error initializing 'cache' semaphore: %s)", strerror(errno)));
return -1;
LM_X(1, ("Fatal Error (error initializing 'cache' semaphore: %s)", strerror(errno)));
}

if (sem_init(&timeStatSem, shared, takenInitially) == -1)
{
LM_E(("Runtime Error (error initializing 'timeStat' semaphore: %s)", strerror(errno)));
return -1;
LM_X(1, ("Fatal Error (error initializing 'timeStat' semaphore: %s)", strerror(errno)));
}

reqPolicy = _reqPolicy;

// Measure accumulated semaphore waiting time?
semWaitStatistics = semTimeStat;
return 0;
}


Expand Down
2 changes: 1 addition & 1 deletion src/lib/common/sem.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ typedef enum SemOpType
*
* semInit -
*/
extern int semInit
extern void semInit
(
SemOpType _reqPolicy = SemReadWriteOp,
bool semTimeStat = false,
Expand Down
7 changes: 2 additions & 5 deletions src/lib/metricsMgr/MetricsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,15 @@ bool MetricsManager::subServiceValid(const std::string& subsrv)
* It's only one sys-call, and this way, the broker is prepared to receive 'on/off'
* via REST.
*/
bool MetricsManager::init(bool _on, bool _semWaitStatistics)
void MetricsManager::init(bool _on, bool _semWaitStatistics)
{
on = _on;
semWaitStatistics = _semWaitStatistics;

if (sem_init(&sem, 0, 1) == -1)
{
LM_E(("Runtime Error (error initializing 'metrics mgr' semaphore: %s)", strerror(errno)));
return false;
LM_X(1, ("Fatal Error (error initializing 'metrics mgr' semaphore: %s)", strerror(errno)));
}

return true;
}


Expand Down
2 changes: 1 addition & 1 deletion src/lib/metricsMgr/MetricsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class MetricsManager
public:
MetricsManager();

bool init(bool _on, bool _semWaitStatistics);
void init(bool _on, bool _semWaitStatistics);
void add(const std::string& srv, const std::string& subServ, const std::string& metric, uint64_t value);
void reset(void);
std::string toJson(bool doReset);
Expand Down
11 changes: 4 additions & 7 deletions src/lib/mqtt/MqttConnectionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ MqttConnectionManager::MqttConnectionManager(void)
*
* MqttConnectionManager::init -
*/
int MqttConnectionManager::init(long _timeout)
void MqttConnectionManager::init(long _timeout)
{
LM_T(LmtMqttNotif, ("Initializing MQTT library"));
mosquitto_lib_init();
Expand All @@ -119,7 +119,7 @@ int MqttConnectionManager::init(long _timeout)
timeout = DEFAULT_TIMEOUT;
}

return semInit();
semInit();
}


Expand Down Expand Up @@ -152,15 +152,12 @@ void MqttConnectionManager::teardown(void)
*
* MqttConnectionManager::semInit -
*/
int MqttConnectionManager::semInit(void)
void MqttConnectionManager::semInit(void)
{
if (sem_init(&sem, 0, 1) == -1)
{
LM_E(("Runtime Error (error initializing 'mqtt connection mgr' semaphore: %s)", strerror(errno)));
return -1;
LM_X(1, ("Fatal Error (error initializing 'mqtt connection mgr' semaphore: %s)", strerror(errno)));
}

return 0;
}


Expand Down
4 changes: 2 additions & 2 deletions src/lib/mqtt/MqttConnectionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class MqttConnectionManager
public:
MqttConnectionManager();

int init(long _timeout);
void init(long _timeout);
void teardown(void);

const char* semGet(void);
Expand All @@ -74,7 +74,7 @@ class MqttConnectionManager

private:
void disconnect(struct mosquitto* mosq, const std::string& endpoint);
int semInit(void);
void semInit(void);
void semTake(void);
void semGive(void);

Expand Down
3 changes: 1 addition & 2 deletions test/unittests/common/commonSem_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ TEST(commonSem, unique)
{
int s;

s = semInit();
EXPECT_EQ(0, s);
semInit();

s = reqSemGive(__FUNCTION__, "test");
EXPECT_EQ(0, s);
Expand Down

0 comments on commit 3f3cee0

Please sign in to comment.