Replace Windows specific thread handling

Use the platform independent primitives instead.
diff --git a/win/winvnc/QueryConnectDialog.cxx b/win/winvnc/QueryConnectDialog.cxx
index 8e50696..103621e 100644
--- a/win/winvnc/QueryConnectDialog.cxx
+++ b/win/winvnc/QueryConnectDialog.cxx
@@ -41,7 +41,7 @@
 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 @@
 
 // - 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 b28de19..de5e31e 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 762a56a..05a38d6 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 @@
 
     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 @@
 
 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::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 309d3f4..0906636 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 @@
 
     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 27aea9f..d86384d 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 @@
 
 
 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 @@
   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 @@
 
 
 int VNCServerWin32::run() {
-  { Lock l(runLock);
-    hostThread = Thread::self();
+  {
+    os::AutoMutex a(runLock);
+    thread_id = GetCurrentThreadId();
     runServer = true;
   }
 
@@ -195,19 +212,20 @@
     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 @@
 
 
 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 @@
   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 @@
       vncServer.approveConnection(queryConnectDialog->getSock(),
                                   queryConnectDialog->isAccepted(),
                                   "Connection rejected by user");
-      delete queryConnectDialog->join();
+      queryConnectDialog->wait();
+      delete queryConnectDialog;
       queryConnectDialog = 0;
       break;
 
@@ -322,9 +341,9 @@
 
     // 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 b85814a..225e634 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 @@
     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;