]> xenbits.xensource.com Git - people/pauldu/xeniface.git/commitdiff
Track active device in service class
authorOwen Smith <owen.smith@citrix.com>
Tue, 28 Jun 2016 12:18:53 +0000 (13:18 +0100)
committerPaul Durrant <paul.durrant@citrix.com>
Thu, 7 Jul 2016 10:30:54 +0000 (11:30 +0100)
Hold a CriticalSection protected local pointer to the XenIfaceDevice
that is currently available. This will be used for all accesses to
the interfaces provided.

Signed-off-by: Owen Smith <owen.smith@citrix.com>
src/xenagent/service.cpp
src/xenagent/service.h

index 192d682e405071d2ff2ee65a6e3206ba22ffe205..6d84b0f04115efa1d17b1e49113565fa96029934 100644 (file)
 #include "service.h"
 #include "messages.h"
 
+class CCritSec
+{
+public:
+    CCritSec(LPCRITICAL_SECTION crit);
+    ~CCritSec();
+private:
+    LPCRITICAL_SECTION m_crit;
+};
+
+CCritSec::CCritSec(LPCRITICAL_SECTION crit) : m_crit(crit)
+{
+    EnterCriticalSection(m_crit);
+}
+CCritSec::~CCritSec()
+{
+    LeaveCriticalSection(m_crit);
+}
+
 int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE ignore, LPSTR lpCmdLine, int nCmdShow)
 {
     if (strlen(lpCmdLine) != 0) {
@@ -151,7 +169,8 @@ static CXenAgent s_service;
     return s_service.__ServiceControlHandlerEx(req, evt, data, ctxt);
 }
 
-CXenAgent::CXenAgent() : m_handle(NULL), m_evtlog(NULL), m_devlist(GUID_INTERFACE_XENIFACE)
+CXenAgent::CXenAgent() : m_handle(NULL), m_evtlog(NULL),
+    m_devlist(GUID_INTERFACE_XENIFACE), m_device(NULL)
 {
     m_status.dwServiceType        = SERVICE_WIN32;
     m_status.dwCurrentState       = SERVICE_START_PENDING;
@@ -162,11 +181,15 @@ CXenAgent::CXenAgent() : m_handle(NULL), m_evtlog(NULL), m_devlist(GUID_INTERFAC
     m_status.dwWaitHint           = 0;
 
     m_svc_stop = CreateEvent(FALSE, NULL, NULL, FALSE);
+
+    InitializeCriticalSection(&m_crit);
 }
 
 CXenAgent::~CXenAgent()
 {
     CloseHandle(m_svc_stop);
+
+    DeleteCriticalSection(&m_crit);
 }
 
 /*virtual*/ CDevice* CXenAgent::Create(const wchar_t* path)
@@ -177,11 +200,21 @@ CXenAgent::~CXenAgent()
 /*virtual*/ void CXenAgent::OnDeviceAdded(CDevice* dev)
 {
     CXenAgent::Log("OnDeviceAdded(%ws)\n", dev->Path());
+
+    CCritSec crit(&m_crit);
+    if (m_device == NULL) {
+        m_device = (CXenIfaceDevice*)dev;
+    }
 }
 
 /*virtual*/ void CXenAgent::OnDeviceRemoved(CDevice* dev)
 {
     CXenAgent::Log("OnDeviceRemoved(%ws)\n", dev->Path());
+
+    CCritSec crit(&m_crit);
+    if (m_device == dev) {
+        m_device = NULL;
+    }
 }
 
 void CXenAgent::OnServiceStart()
index af554c9ec58f5a5e668ca5de6b12d11f6d302cfe..3b89127751006caab38b165292f8c7540d33bf65 100644 (file)
@@ -79,6 +79,8 @@ private: // service support
     HANDLE                  m_svc_stop;
 
     CDeviceList             m_devlist;
+    CXenIfaceDevice*        m_device;
+    CRITICAL_SECTION        m_crit;
 };
 
 #endif