From 9bc9fa07b9ee47eaad98bbb6106d632a36ab3a2f Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Fri, 20 Mar 2020 16:51:52 +0000 Subject: [PATCH] Convert VConnect to std::thread --- vconnect.cc | 48 +++++++++++++++++++++++++++++++++--------------- vconnect.h | 22 ++++++++++++---------- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/vconnect.cc b/vconnect.cc index 6982c75..3e4d99b 100644 --- a/vconnect.cc +++ b/vconnect.cc @@ -1,5 +1,5 @@ /* - Copyright 2004-2005 Chris Tallon + Copyright 2004-2020 Chris Tallon This file is part of VOMP. @@ -17,8 +17,6 @@ along with VOMP. If not, see . */ -#include "vconnect.h" - #include "video.h" #include "colour.h" #include "command.h" @@ -30,10 +28,11 @@ #include "vserverselect.h" #include "messagequeue.h" +#include "vconnect.h" + VConnect::VConnect(char* tServer) +: server(tServer) { - server = tServer; - boxstack = BoxStack::getInstance(); vdr = VDR::getInstance(); logger = Log::getInstance(); @@ -54,9 +53,9 @@ VConnect::VConnect(char* tServer) VConnect::~VConnect() { - irun = 0; + threadReqQuit = true; vdr->cancelFindingServer(); - threadStop(); + stop(); } void VConnect::draw() @@ -72,14 +71,32 @@ int VConnect::handleCommand(int /* command */) void VConnect::run() { - threadStart(); + threadMutex.lock(); + threadReqQuit = false; + connectThread = std::thread([this] + { + threadMutex.lock(); + threadMutex.unlock(); + threadMethod(); + }); + threadMutex.unlock(); +} + +void VConnect::stop() +{ + threadMutex.lock(); + threadReqQuit = true; + threadCond.notify_one(); + threadMutex.unlock(); + connectThread.join(); } void VConnect::threadMethod() { ULONG delay = 0; int success; - irun = 1; + + std::unique_lock ul(threadMutex, std::defer_lock); do { @@ -100,7 +117,7 @@ void VConnect::threadMethod() boxstack->update(this); vdr->findServers(servers); - if (!irun) + if (threadReqQuit) { for(UINT k = 0; k < servers.size(); k++) { @@ -124,12 +141,13 @@ void VConnect::threadMethod() boxstack->add(vs); boxstack->update(vs); - threadLock(); - threadWaitForSignal(); - threadUnlock(); + ul.lock(); + if (threadReqQuit) { ul.unlock(); return; } + threadCond.wait(ul); + ul.unlock(); } - if (!irun) + if (threadReqQuit) { for(UINT k = 0; k < servers.size(); k++) { @@ -205,6 +223,6 @@ void VConnect::processMessage(Message* m) if (m->message == Message::SERVER_SELECTED) { selectedServer = m->parameter; - threadSignal(); + threadCond.notify_one(); } } diff --git a/vconnect.h b/vconnect.h index ea57612..0f4102b 100644 --- a/vconnect.h +++ b/vconnect.h @@ -20,24 +20,20 @@ #ifndef VCONNECT_H #define VCONNECT_H -#include #include #include +#include +#include +#include #include "vinfo.h" #include "vdr.h" -#ifdef WIN32 -#include "threadwin.h" -#else -#include "threadp.h" -#endif - class Log; class BoxStack; class Message; -class VConnect : public VInfo, public Thread_TYPE +class VConnect : public VInfo { public: VConnect(char* server); @@ -50,16 +46,22 @@ class VConnect : public VInfo, public Thread_TYPE void run(); private: - void threadMethod(); + void clearServerIPs(); BoxStack* boxstack; - UCHAR irun{}; VDR* vdr; Log* logger; std::vector servers; int selectedServer; char* server; + + std::thread connectThread; + std::mutex threadMutex; + std::condition_variable threadCond; + bool threadReqQuit; + void threadMethod(); + void stop(); }; #endif -- 2.39.2