Tiny threads fixes & a little bit of safety

This commit is contained in:
k1-801 2020-11-15 16:00:59 +04:00 committed by Adam Honse
parent 9a53709db0
commit dde857dfb4
18 changed files with 85 additions and 154 deletions

View file

@ -11,32 +11,8 @@
#include <string>
#include <cstring>
//Include thread libraries for Windows or Linux
#ifdef WIN32
#include <process.h>
#else
#include "pthread.h"
#include "unistd.h"
#endif
//Thread functions have different types in Windows and Linux
#ifdef WIN32
#define THREAD static void
#define THREADRETURN
#else
#define THREAD static void*
#define THREADRETURN return(NULL);
#endif
using namespace std::chrono_literals;
THREAD keepalive_thread(void *param)
{
CorsairLightingNodeController* corsair = static_cast<CorsairLightingNodeController*>(param);
corsair->KeepaliveThread();
THREADRETURN
}
CorsairLightingNodeController::CorsairLightingNodeController(hid_device* dev_handle, const char* path)
{
dev = dev_handle;
@ -50,21 +26,20 @@ CorsairLightingNodeController::CorsairLightingNodeController(hid_device* dev_han
| to not revert back into rainbow mode. Start a thread |
| to continuously send a keepalive packet every 5s |
\*-----------------------------------------------------*/
#ifdef WIN32
_beginthread(keepalive_thread, 0, this);
#else
pthread_t thread;
pthread_create(&thread, NULL, &keepalive_thread, this);
#endif
keepalive_thread_run = 1;
keepalive_thread = new std::thread(&CorsairLightingNodeController::KeepaliveThread, this);
}
CorsairLightingNodeController::~CorsairLightingNodeController()
{
keepalive_thread_run = 0;
keepalive_thread->join();
delete keepalive_thread;
}
void CorsairLightingNodeController::KeepaliveThread()
{
while(1)
while(keepalive_thread_run.load())
{
if((std::chrono::steady_clock::now() - last_commit_time) > std::chrono::seconds(5))
{

View file

@ -112,6 +112,8 @@ private:
hid_device* dev;
std::string firmware_version;
std::string location;
std::thread* keepalive_thread;
std::atomic<bool> keepalive_thread_run;
std::chrono::time_point<std::chrono::steady_clock> last_commit_time;
void SendFirmwareRequest();

View file

@ -261,12 +261,21 @@ RGBController_E131::RGBController_E131(std::vector<E131Device> device_list)
if(keepalive_delay.count() > 0)
{
KeepaliveThread = new std::thread(&RGBController_E131::KeepaliveThreadFunction, this);
keepalive_thread_run = 1;
keepalive_thread = new std::thread(&RGBController_E131::KeepaliveThreadFunction, this);
}
else
{
keepalive_thread_run = 0;
keepalive_thread = nullptr;
}
}
RGBController_E131::~RGBController_E131()
{
keepalive_thread_run = 0;
keepalive_thread->join();
delete keepalive_thread;
/*---------------------------------------------------------*\
| Delete the matrix map |
\*---------------------------------------------------------*/
@ -413,7 +422,7 @@ void RGBController_E131::DeviceUpdateMode()
void RGBController_E131::KeepaliveThreadFunction()
{
while(1)
while(keepalive_thread_run.load())
{
if((std::chrono::steady_clock::now() - last_update_time) > ( keepalive_delay * 0.95f ) )
{

View file

@ -78,7 +78,8 @@ private:
std::vector<e131_addr_t> dest_addrs;
std::vector<unsigned int> universes;
int sockfd;
std::thread * KeepaliveThread;
std::thread * keepalive_thread;
std::atomic<bool> keepalive_thread_run;
std::chrono::milliseconds keepalive_delay;
std::chrono::time_point<std::chrono::steady_clock> last_update_time;
};

View file

@ -9,32 +9,8 @@
#include "RGBController_HyperXAlloyOrigins.h"
//Include thread libraries for Windows or Linux
#ifdef WIN32
#include <process.h>
#else
#include "pthread.h"
#include "unistd.h"
#endif
//Thread functions have different types in Windows and Linux
#ifdef WIN32
#define THREAD static void
#define THREADRETURN
#else
#define THREAD static void*
#define THREADRETURN return(NULL);
#endif
using namespace std::chrono_literals;
THREAD keepalive_thread(void *param)
{
RGBController_HyperXAlloyOrigins* controller = static_cast<RGBController_HyperXAlloyOrigins*>(param);
controller->KeepaliveThread();
THREADRETURN
}
//0xFFFFFFFF indicates an unused entry in matrix
#define NA 0xFFFFFFFF
@ -215,16 +191,16 @@ RGBController_HyperXAlloyOrigins::RGBController_HyperXAlloyOrigins(HyperXAlloyOr
| to not revert back into rainbow mode. Start a thread |
| to continuously send a keepalive packet every 5s |
\*-----------------------------------------------------*/
#ifdef WIN32
_beginthread(keepalive_thread, 0, this);
#else
pthread_t thread;
pthread_create(&thread, NULL, &keepalive_thread, this);
#endif
keepalive_thread_run = 1;
keepalive_thread = new std::thread(&RGBController_HyperXAlloyOrigins::KeepaliveThread, this);
}
RGBController_HyperXAlloyOrigins::~RGBController_HyperXAlloyOrigins()
{
keepalive_thread_run = 0;
keepalive_thread->join();
delete keepalive_thread;
/*---------------------------------------------------------*\
| Delete the matrix map |
\*---------------------------------------------------------*/
@ -313,7 +289,7 @@ void RGBController_HyperXAlloyOrigins::DeviceUpdateMode()
void RGBController_HyperXAlloyOrigins::KeepaliveThread()
{
while(1)
while(keepalive_thread_run.load())
{
if(active_mode == 0)
{

View file

@ -34,6 +34,7 @@ public:
private:
HyperXAlloyOriginsController* hyperx;
std::thread* keepalive_thread;
std::atomic<bool> keepalive_thread_run;
std::chrono::time_point<std::chrono::steady_clock> last_update_time;
};

View file

@ -230,15 +230,15 @@ RGBController_HyperXKeyboard::RGBController_HyperXKeyboard(HyperXKeyboardControl
| to not revert back into rainbow mode. Start a thread |
| to continuously send a keepalive packet every 5s |
\*-----------------------------------------------------*/
KeepaliveThreadRunning = true;
KeepaliveThread = new std::thread(&RGBController_HyperXKeyboard::KeepaliveThreadFunction, this);
keepalive_thread_run = true;
keepalive_thread = new std::thread(&RGBController_HyperXKeyboard::KeepaliveThreadFunction, this);
}
RGBController_HyperXKeyboard::~RGBController_HyperXKeyboard()
{
KeepaliveThreadRunning = false;
KeepaliveThread->join();
delete KeepaliveThread;
keepalive_thread_run = false;
keepalive_thread->join();
delete keepalive_thread;
/*---------------------------------------------------------*\
| Delete the matrix map |
@ -345,7 +345,7 @@ void RGBController_HyperXKeyboard::DeviceUpdateMode()
void RGBController_HyperXKeyboard::KeepaliveThreadFunction()
{
while(KeepaliveThreadRunning)
while(keepalive_thread_run)
{
if(active_mode == 0)
{

View file

@ -36,9 +36,7 @@ public:
private:
HyperXKeyboardController* hyperx;
std::atomic<bool> KeepaliveThreadRunning;
std::thread* KeepaliveThread;
std::atomic<bool> keepalive_thread_run;
std::thread* keepalive_thread;
std::chrono::time_point<std::chrono::steady_clock> last_update_time;
};

View file

@ -9,32 +9,8 @@
#include "RGBController_HyperXPulsefireSurge.h"
//Include thread libraries for Windows or Linux
#ifdef WIN32
#include <process.h>
#else
#include "pthread.h"
#include "unistd.h"
#endif
//Thread functions have different types in Windows and Linux
#ifdef WIN32
#define THREAD static void
#define THREADRETURN
#else
#define THREAD static void*
#define THREADRETURN return(NULL);
#endif
using namespace std::chrono_literals;
THREAD keepalive_thread(void *param)
{
RGBController_HyperXPulsefireSurge* controller = static_cast<RGBController_HyperXPulsefireSurge*>(param);
controller->KeepaliveThread();
THREADRETURN
}
RGBController_HyperXPulsefireSurge::RGBController_HyperXPulsefireSurge(HyperXPulsefireSurgeController* hyperx_ptr)
{
hyperx = hyperx_ptr;
@ -59,17 +35,15 @@ RGBController_HyperXPulsefireSurge::RGBController_HyperXPulsefireSurge(HyperXPul
| to not revert back into rainbow mode. Start a thread |
| to continuously send a keepalive packet every 5s |
\*-----------------------------------------------------*/
#ifdef WIN32
_beginthread(keepalive_thread, 0, this);
#else
pthread_t thread;
pthread_create(&thread, NULL, &keepalive_thread, this);
#endif
keepalive_thread_run = 1;
keepalive_thread = new std::thread(&RGBController_HyperXPulsefireSurge::KeepaliveThread, this);
};
RGBController_HyperXPulsefireSurge::~RGBController_HyperXPulsefireSurge()
{
keepalive_thread_run = 0;
keepalive_thread->join();
delete keepalive_thread;
}
void RGBController_HyperXPulsefireSurge::SetupZones()
@ -156,7 +130,7 @@ void RGBController_HyperXPulsefireSurge::DeviceUpdateMode()
void RGBController_HyperXPulsefireSurge::KeepaliveThread()
{
while(1)
while(keepalive_thread_run.load())
{
if(active_mode == 0)
{

View file

@ -34,6 +34,7 @@ public:
private:
HyperXPulsefireSurgeController* hyperx;
std::thread* keepalive_thread;
std::atomic<bool> keepalive_thread_run;
std::chrono::time_point<std::chrono::steady_clock> last_update_time;
};

View file

@ -9,32 +9,8 @@
#include "RGBController_HyperXMousemat.h"
//Include thread libraries for Windows or Linux
#ifdef WIN32
#include <process.h>
#else
#include "pthread.h"
#include "unistd.h"
#endif
//Thread functions have different types in Windows and Linux
#ifdef WIN32
#define THREAD static void
#define THREADRETURN
#else
#define THREAD static void*
#define THREADRETURN return(NULL);
#endif
using namespace std::chrono_literals;
THREAD keepalive_thread(void *param)
{
RGBController_HyperXMousemat* controller = static_cast<RGBController_HyperXMousemat*>(param);
controller->KeepaliveThread();
THREADRETURN
}
RGBController_HyperXMousemat::RGBController_HyperXMousemat(HyperXMousematController* hyperx_ptr)
{
hyperx = hyperx_ptr;
@ -59,17 +35,15 @@ RGBController_HyperXMousemat::RGBController_HyperXMousemat(HyperXMousematControl
| to not revert back into rainbow mode. Start a thread |
| to continuously send a keepalive packet every 5s |
\*-----------------------------------------------------*/
#ifdef WIN32
_beginthread(keepalive_thread, 0, this);
#else
pthread_t thread;
pthread_create(&thread, NULL, &keepalive_thread, this);
#endif
keepalive_thread_run = 1;
keepalive_thread = new std::thread(&RGBController_HyperXMousemat::KeepaliveThread, this);
};
RGBController_HyperXMousemat::~RGBController_HyperXMousemat()
{
keepalive_thread_run = 0;
keepalive_thread->join();
delete keepalive_thread;
}
void RGBController_HyperXMousemat::SetupZones()
@ -156,7 +130,7 @@ void RGBController_HyperXMousemat::DeviceUpdateMode()
void RGBController_HyperXMousemat::KeepaliveThread()
{
while(1)
while(keepalive_thread_run.load())
{
if(active_mode == 0)
{

View file

@ -34,6 +34,7 @@ public:
private:
HyperXMousematController* hyperx;
std::thread* keepalive_thread;
std::atomic<bool> keepalive_thread_run;
std::chrono::time_point<std::chrono::steady_clock> last_update_time;
};

View file

@ -25,6 +25,7 @@ PhilipsWizController::PhilipsWizController(std::string ip)
/*-----------------------------------------------------------------*\
| Start a thread to handle responses received from the Wiz device |
\*-----------------------------------------------------------------*/
ReceiveThreadRun = 1;
ReceiveThread = new std::thread(&PhilipsWizController::ReceiveThreadFunction, this);
/*-----------------------------------------------------------------*\
@ -35,7 +36,9 @@ PhilipsWizController::PhilipsWizController(std::string ip)
PhilipsWizController::~PhilipsWizController()
{
ReceiveThreadRun = 0;
ReceiveThread->join();
delete ReceiveThread;
}
std::string PhilipsWizController::GetLocation()
@ -90,7 +93,7 @@ void PhilipsWizController::ReceiveThreadFunction()
{
char recv_buf[1024];
while(1)
while(ReceiveThreadRun.load())
{
/*-----------------------------------------------------------------*\
| Receive up to 1024 bytes from the device |

View file

@ -37,4 +37,5 @@ private:
std::string location;
net_port port;
std::thread* ReceiveThread;
std::atomic<bool> ReceiveThreadRun;
};

View file

@ -9,14 +9,6 @@
#include "RGBController_SteelSeriesApex.h"
//Include thread libraries for Windows or Linux
#ifdef WIN32
#include <process.h>
#else
#include "pthread.h"
#include "unistd.h"
#endif
using namespace std::chrono_literals;
//0xFFFFFFFF indicates an unused entry in matrix

View file

@ -42,7 +42,7 @@ NetworkClient::NetworkClient(std::vector<RGBController *>& control) : controller
NetworkClient::~NetworkClient()
{
delete ConnectionThread;
StopClient();
}
void NetworkClient::ClientInfoChanged()
@ -145,8 +145,17 @@ void NetworkClient::StopClient()
}
if(ListenThread)
{
ListenThread->join();
delete ListenThread;
ListenThread = nullptr;
}
if(ConnectionThread)
{
ConnectionThread->join();
delete ConnectionThread;
ConnectionThread = nullptr;
}
/*-------------------------------------------------*\
| Client info has changed, call the callbacks |

View file

@ -37,6 +37,12 @@ NetworkServer::NetworkServer(std::vector<RGBController *>& control) : controller
{
port_num = OPENRGB_SDK_PORT;
server_online = false;
ConnectionThread = nullptr;
}
NetworkServer::~NetworkServer()
{
StopServer();
}
void NetworkServer::ClientInfoChanged()
@ -214,6 +220,13 @@ void NetworkServer::StopServer()
ServerClients.clear();
ServerClientsMutex.unlock();
if(ConnectionThread)
{
ConnectionThread->join();
delete ConnectionThread;
ConnectionThread = nullptr;
}
/*-------------------------------------------------*\
| Client info has changed, call the callbacks |
\*-------------------------------------------------*/

View file

@ -30,6 +30,7 @@ class NetworkServer
{
public:
NetworkServer(std::vector<RGBController *>& control);
~NetworkServer();
unsigned short GetPort();
bool GetOnline();