diff options
author | Pierre Ossman <ossman@cendio.se> | 2016-07-07 15:35:13 +0200 |
---|---|---|
committer | Pierre Ossman <ossman@cendio.se> | 2016-07-07 15:36:36 +0200 |
commit | 338e73aef8dcd3e7e43e4337071ea9d77568c18b (patch) | |
tree | f1981aeeb22eb65c79dd7e76372c02f7fd0f565a /win/winvnc | |
parent | ff872614b507d0aa8bfbd09ef41550390cfe658a (diff) | |
download | tigervnc-338e73aef8dcd3e7e43e4337071ea9d77568c18b.tar.gz tigervnc-338e73aef8dcd3e7e43e4337071ea9d77568c18b.zip |
Replace Windows specific thread handling
Use the platform independent primitives instead.
Diffstat (limited to 'win/winvnc')
-rw-r--r-- | win/winvnc/QueryConnectDialog.cxx | 4 | ||||
-rw-r--r-- | win/winvnc/QueryConnectDialog.h | 8 | ||||
-rw-r--r-- | win/winvnc/STrayIcon.cxx | 22 | ||||
-rw-r--r-- | win/winvnc/STrayIcon.h | 20 | ||||
-rw-r--r-- | win/winvnc/VNCServerWin32.cxx | 57 | ||||
-rw-r--r-- | win/winvnc/VNCServerWin32.h | 14 |
6 files changed, 84 insertions, 41 deletions
diff --git a/win/winvnc/QueryConnectDialog.cxx b/win/winvnc/QueryConnectDialog.cxx index 8e506966..103621ee 100644 --- a/win/winvnc/QueryConnectDialog.cxx +++ b/win/winvnc/QueryConnectDialog.cxx @@ -41,7 +41,7 @@ static IntParameter timeout("QueryConnectTimeout", QueryConnectDialog::QueryConnectDialog(network::Socket* sock_, const char* userName_, VNCServerWin32* s) -: Thread("QueryConnectDialog"), Dialog(GetModuleHandle(0)), +: Dialog(GetModuleHandle(0)), sock(sock_), approve(false), server(s) { peerIp.buf = sock->getPeerAddress(); userName.buf = strDup(userName_); @@ -54,7 +54,7 @@ void QueryConnectDialog::startDialog() { // - Thread overrides -void QueryConnectDialog::run() { +void QueryConnectDialog::worker() { countdown = timeout; try { if (desktopChangeRequired() && !changeDesktop()) diff --git a/win/winvnc/QueryConnectDialog.h b/win/winvnc/QueryConnectDialog.h index b28de198..de5e31ee 100644 --- a/win/winvnc/QueryConnectDialog.h +++ b/win/winvnc/QueryConnectDialog.h @@ -21,24 +21,26 @@ #ifndef __WINVNC_QUERY_CONNECT_DIALOG_H__ #define __WINVNC_QUERY_CONNECT_DIALOG_H__ -#include <rfb/Threading.h> #include <rfb_win32/Dialog.h> #include <rfb/util.h> +namespace os { class Thread; } + namespace network { class Socket; } namespace winvnc { class VNCServerWin32; - class QueryConnectDialog : public rfb::Thread, rfb::win32::Dialog { + class QueryConnectDialog : public os::Thread, rfb::win32::Dialog { public: QueryConnectDialog(network::Socket* sock, const char* userName, VNCServerWin32* s); virtual void startDialog(); - virtual void run(); network::Socket* getSock() {return sock;} bool isAccepted() const {return approve;} protected: + // Thread methods + virtual void worker(); // Dialog methods (protected) virtual void initDialog(); diff --git a/win/winvnc/STrayIcon.cxx b/win/winvnc/STrayIcon.cxx index 762a56af..05a38d6e 100644 --- a/win/winvnc/STrayIcon.cxx +++ b/win/winvnc/STrayIcon.cxx @@ -22,14 +22,19 @@ #include <winvnc/VNCServerService.h> #include <winvnc/resource.h> +#include <os/Mutex.h> +#include <os/Thread.h> + #include <rfb/LogWriter.h> #include <rfb/Configuration.h> + #include <rfb_win32/LaunchProcess.h> #include <rfb_win32/TrayIcon.h> #include <rfb_win32/AboutDialog.h> #include <rfb_win32/MsgBox.h> #include <rfb_win32/Service.h> #include <rfb_win32/CurrentUser.h> + #include <winvnc/ControlPanel.h> using namespace rfb; @@ -209,7 +214,7 @@ public: case WM_SET_TOOLTIP: { - rfb::Lock l(thread.lock); + os::AutoMutex a(thread.lock); if (thread.toolTip.buf) setToolTip(thread.toolTip.buf); } @@ -231,15 +236,24 @@ protected: STrayIconThread::STrayIconThread(VNCServerWin32& sm, UINT inactiveIcon_, UINT activeIcon_, UINT dis_inactiveIcon_, UINT dis_activeIcon_, UINT menu_) -: Thread("TrayIcon"), windowHandle(0), server(sm), +: thread_id(-1), windowHandle(0), server(sm), inactiveIcon(inactiveIcon_), activeIcon(activeIcon_), dis_inactiveIcon(dis_inactiveIcon_), dis_activeIcon(dis_activeIcon_), menu(menu_), runTrayIcon(true) { + lock = new os::Mutex; start(); + while (thread_id == (DWORD)-1) + Sleep(0); } +STrayIconThread::~STrayIconThread() { + runTrayIcon = false; + PostThreadMessage(thread_id, WM_QUIT, 0, 0); + delete lock; +} -void STrayIconThread::run() { +void STrayIconThread::worker() { + thread_id = GetCurrentThreadId(); while (runTrayIcon) { if (rfb::win32::desktopChangeRequired() && !rfb::win32::changeDesktop()) @@ -260,7 +274,7 @@ void STrayIconThread::run() { void STrayIconThread::setToolTip(const TCHAR* text) { if (!windowHandle) return; - Lock l(lock); + os::AutoMutex a(lock); delete [] toolTip.buf; toolTip.buf = tstrDup(text); PostMessage(windowHandle, WM_SET_TOOLTIP, 0, 0); diff --git a/win/winvnc/STrayIcon.h b/win/winvnc/STrayIcon.h index 309d3f4a..09066362 100644 --- a/win/winvnc/STrayIcon.h +++ b/win/winvnc/STrayIcon.h @@ -22,20 +22,19 @@ #include <winvnc/VNCServerWin32.h> #include <rfb_win32/TCharArray.h> #include <rfb/Configuration.h> -#include <rfb/Threading.h> + +namespace os { + class Mutex; + class Thread; +} namespace winvnc { - class STrayIconThread : rfb::Thread { + class STrayIconThread : os::Thread { public: STrayIconThread(VNCServerWin32& sm, UINT inactiveIcon, UINT activeIcon, UINT dis_inactiveIcon, UINT dis_activeIcon, UINT menu); - virtual ~STrayIconThread() { - runTrayIcon = false; - PostThreadMessage(getThreadId(), WM_QUIT, 0, 0); - } - - virtual void run(); + virtual ~STrayIconThread(); void setToolTip(const TCHAR* text); @@ -44,7 +43,10 @@ namespace winvnc { friend class STrayIcon; protected: - rfb::Mutex lock; + virtual void worker(); + + os::Mutex* lock; + DWORD thread_id; HWND windowHandle; rfb::TCharArray toolTip; VNCServerWin32& server; diff --git a/win/winvnc/VNCServerWin32.cxx b/win/winvnc/VNCServerWin32.cxx index 27aea9f6..d86384da 100644 --- a/win/winvnc/VNCServerWin32.cxx +++ b/win/winvnc/VNCServerWin32.cxx @@ -21,9 +21,13 @@ #include <winvnc/VNCServerWin32.h> #include <winvnc/resource.h> #include <winvnc/STrayIcon.h> + +#include <os/Mutex.h> + #include <rfb_win32/ComputerName.h> #include <rfb_win32/CurrentUser.h> #include <rfb_win32/Service.h> + #include <rfb/Hostname.h> #include <rfb/LogWriter.h> @@ -53,16 +57,21 @@ static BoolParameter showTrayIcon("ShowTrayIcon", VNCServerWin32::VNCServerWin32() - : command(NoCommand), commandSig(commandLock), + : command(NoCommand), commandEvent(CreateEvent(0, TRUE, FALSE, 0)), sessionEvent(isServiceProcess() ? CreateEvent(0, FALSE, FALSE, "Global\\SessionEventTigerVNC") : 0), vncServer(CStr(ComputerName().buf), &desktop), - hostThread(0), runServer(false), isDesktopStarted(false), + thread_id(-1), runServer(false), isDesktopStarted(false), httpServer(&vncServer), config(&sockMgr), rfbSock(&sockMgr), httpSock(&sockMgr), trayIcon(0), queryConnectDialog(0) { + commandLock = new os::Mutex; + commandSig = new os::Condition(commandLock); + + runLock = new os::Mutex; + // Initialise the desktop desktop.setStatusLocation(&isDesktopStarted); @@ -85,8 +94,15 @@ VNCServerWin32::~VNCServerWin32() { desktop.setStatusLocation(0); // Join the Accept/Reject dialog thread - if (queryConnectDialog) - delete queryConnectDialog->join(); + if (queryConnectDialog) { + queryConnectDialog->wait(); + delete queryConnectDialog; + } + + delete runLock; + + delete commandSig; + delete commandLock; } @@ -149,8 +165,9 @@ void VNCServerWin32::regConfigChanged() { int VNCServerWin32::run() { - { Lock l(runLock); - hostThread = Thread::self(); + { + os::AutoMutex a(runLock); + thread_id = GetCurrentThreadId(); runServer = true; } @@ -195,19 +212,20 @@ int VNCServerWin32::run() { vlog.error("%s", e.str()); } - { Lock l(runLock); + { + os::AutoMutex a(runLock); runServer = false; - hostThread = 0; + thread_id = (DWORD)-1; } return result; } void VNCServerWin32::stop() { - Lock l(runLock); + os::AutoMutex a(runLock); runServer = false; - if (hostThread) - PostThreadMessage(hostThread->getThreadId(), WM_QUIT, 0, 0); + if (thread_id != (DWORD)-1) + PostThreadMessage(thread_id, WM_QUIT, 0, 0); } @@ -261,17 +279,17 @@ void VNCServerWin32::queryConnectionComplete() { bool VNCServerWin32::queueCommand(Command cmd, const void* data, int len, bool wait) { - Lock l(commandLock); + os::AutoMutex a(commandLock); while (command != NoCommand) - commandSig.wait(); + commandSig->wait(); command = cmd; commandData = data; commandDataLen = len; SetEvent(commandEvent); if (wait) { while (command != NoCommand) - commandSig.wait(); - commandSig.signal(); + commandSig->wait(); + commandSig->signal(); } return true; } @@ -282,7 +300,7 @@ void VNCServerWin32::processEvent(HANDLE event_) { if (event_ == commandEvent.h) { // If there is no command queued then return immediately { - Lock l(commandLock); + os::AutoMutex a(commandLock); if (command == NoCommand) return; } @@ -312,7 +330,8 @@ void VNCServerWin32::processEvent(HANDLE event_) { vncServer.approveConnection(queryConnectDialog->getSock(), queryConnectDialog->isAccepted(), "Connection rejected by user"); - delete queryConnectDialog->join(); + queryConnectDialog->wait(); + delete queryConnectDialog; queryConnectDialog = 0; break; @@ -322,9 +341,9 @@ void VNCServerWin32::processEvent(HANDLE event_) { // Clear the command and signal completion { - Lock l(commandLock); + os::AutoMutex a(commandLock); command = NoCommand; - commandSig.signal(); + commandSig->signal(); } } else if (event_ == sessionEvent.h) { stop(); diff --git a/win/winvnc/VNCServerWin32.h b/win/winvnc/VNCServerWin32.h index b85814a4..225e6342 100644 --- a/win/winvnc/VNCServerWin32.h +++ b/win/winvnc/VNCServerWin32.h @@ -30,6 +30,12 @@ #include <winvnc/JavaViewer.h> #include <winvnc/ManagedListener.h> +namespace os { + class Mutex; + class Condition; + class Thread; +} + namespace winvnc { class STrayIconThread; @@ -99,16 +105,16 @@ namespace winvnc { Command command; const void* commandData; int commandDataLen; - rfb::Mutex commandLock; - rfb::Condition commandSig; + os::Mutex* commandLock; + os::Condition* commandSig; rfb::win32::Handle commandEvent; rfb::win32::Handle sessionEvent; // VNCServerWin32 Server-internal state rfb::win32::SDisplay desktop; rfb::VNCServerST vncServer; - rfb::Mutex runLock; - rfb::Thread* hostThread; + os::Mutex* runLock; + DWORD thread_id; bool runServer; bool isDesktopStarted; JavaViewerServer httpServer; |