Small refactor on ThreadObject and listener (#93)

* removed pointer, slight cleaning of loop

* removed semaphore, use getters

* removed redundant log msg

* removed comment

* added const

* removed comment

* changed header
This commit is contained in:
Erik Fröjdh
2020-04-21 09:45:29 +02:00
committed by GitHub
parent 68f76e5356
commit c1ae67ac46
7 changed files with 37 additions and 78 deletions

View File

@@ -3,36 +3,27 @@
* @short creates/destroys a thread
***********************************************/
#include "ThreadObject.h"
#include "container_utils.h"
#include <iostream>
#include <sys/syscall.h>
#include <unistd.h>
ThreadObject::ThreadObject(int threadIndex, std::string threadType)
: index(threadIndex), type(threadType) {
LOG(logDEBUG) << type << " thread created: " << index;
sem_init(&semaphore,1,0);
try {
threadObject = sls::make_unique<std::thread>(&ThreadObject::RunningThread, this);
threadObject = std::thread(&ThreadObject::RunningThread, this);
} catch (...) {
throw sls::RuntimeError("Could not create " + type + " thread with index " + std::to_string(index));
}
}
ThreadObject::~ThreadObject() {
killThread = true;
sem_post(&semaphore);
threadObject->join();
threadObject.join();
sem_destroy(&semaphore);
}
@@ -50,20 +41,13 @@ void ThreadObject::StopRunning() {
void ThreadObject::RunningThread() {
LOG(logINFOBLUE) << "Created [ " << type << "Thread " << index << ", Tid: " << syscall(SYS_gettid) << "]";
LOG(logDEBUG) << type << " thread " << index << " created successfully.";
while(true) {
while(!killThread) {
while(IsRunning()) {
ThreadExecution();
}
//wait till the next acquisition
sem_wait(&semaphore);
if(killThread) {
break;
}
}
LOG(logDEBUG) << type << " thread with index " << index << " destroyed successfully.";
LOG(logINFOBLUE) << "Exiting [ " << type << " Thread " << index << ", Tid: " << syscall(SYS_gettid) << "]";
}
@@ -72,11 +56,10 @@ void ThreadObject::Continue() {
sem_post(&semaphore);
}
void ThreadObject::SetThreadPriority(int priority) {
struct sched_param param;
param.sched_priority = priority;
if (pthread_setschedparam(threadObject->native_handle(), SCHED_FIFO, &param) == EPERM) {
if (pthread_setschedparam(threadObject.native_handle(), SCHED_FIFO, &param) == EPERM) {
if (index == 0) {
LOG(logWARNING) << "Could not prioritize " << type << " thread. "
"(No Root Privileges?)";