]> git.vomp.tv Git - vompclient.git/commitdiff
Convert VConnect to std::thread
authorChris Tallon <chris@vomp.tv>
Fri, 20 Mar 2020 16:51:52 +0000 (16:51 +0000)
committerChris Tallon <chris@vomp.tv>
Fri, 20 Mar 2020 16:51:52 +0000 (16:51 +0000)
vconnect.cc
vconnect.h

index 6982c751ed79b9ac874e04fc289ed0ccd117f58a..3e4d99b14ba6565c63db93c280a81824c7df4142 100644 (file)
@@ -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 <https://www.gnu.org/licenses/>.
 */
 
-#include "vconnect.h"
-
 #include "video.h"
 #include "colour.h"
 #include "command.h"
 #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<std::mutex> 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();
   }
 }
index ea576129936bc671d2b3fac39aa7f10fe3882d8b..0f4102bd126f28f39b5364336b06723aafcf8986 100644 (file)
 #ifndef VCONNECT_H
 #define VCONNECT_H
 
-#include <stdio.h>
 #include <string.h>
 #include <vector>
+#include <mutex>
+#include <thread>
+#include <condition_variable>
 
 #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<VDRServer> servers;
     int selectedServer;
     char* server;
+
+    std::thread connectThread;
+    std::mutex threadMutex;
+    std::condition_variable threadCond;
+    bool threadReqQuit;
+    void threadMethod();
+    void stop();
 };
 
 #endif