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

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