put mutex on engine init() and release() - branched for testing. mutex
authorkrista
Sun, 01 Oct 2017 23:46:20 +0200
branchmutex
changeset 261db956fa86d4e
parent 260 07e9a01ce3a8
child 262 c599247b93b3
child 263 bba361e63d7f
put mutex on engine init() and release() - branched for testing.
CpEpEngine.cpp
CpEpEngine.h
     1.1 --- a/CpEpEngine.cpp	Thu Aug 10 18:47:14 2017 +0200
     1.2 +++ b/CpEpEngine.cpp	Sun Oct 01 23:46:20 2017 +0200
     1.3 @@ -2,11 +2,13 @@
     1.4  
     1.5  #include "stdafx.h"
     1.6  #include "CpEpEngine.h"
     1.7 +#include <mutex>
     1.8  
     1.9  using namespace std;
    1.10  using namespace pEp::utility;
    1.11  
    1.12  // CpEpEngine
    1.13 +std::mutex CpEpEngine::init_mutex;
    1.14  
    1.15  STDMETHODIMP CpEpEngine::InterfaceSupportsErrorInfo(REFIID riid)
    1.16  {
    1.17 @@ -1143,9 +1145,12 @@
    1.18      keysync_abort_requested = false;
    1.19  
    1.20      // Init our keysync session
    1.21 -    PEP_STATUS status = ::init(&keysync_session);
    1.22 -    ::register_sync_callbacks(keysync_session, (void*)this, messageToSend, notifyHandshake, inject_sync_msg, retrieve_next_sync_msg);
    1.23 -    assert(status == PEP_STATUS_OK);
    1.24 +	{ // begin lock scope
    1.25 +		std::lock_guard<std::mutex> lock(init_mutex);
    1.26 +		PEP_STATUS status = ::init(&keysync_session);
    1.27 +		::register_sync_callbacks(keysync_session, (void*)this, messageToSend, notifyHandshake, inject_sync_msg, retrieve_next_sync_msg);
    1.28 +		assert(status == PEP_STATUS_OK);
    1.29 +	} // end lock scope
    1.30  
    1.31      attach_sync_session(get_session(), keysync_session);
    1.32  
    1.33 @@ -1225,6 +1230,8 @@
    1.34  
    1.35      ::detach_sync_session(get_session());
    1.36      ::unregister_sync_callbacks(keysync_session);
    1.37 +
    1.38 +	std::lock_guard<std::mutex> releaselock(init_mutex);
    1.39      release(keysync_session);
    1.40      keysync_session = NULL;
    1.41  }
     2.1 --- a/CpEpEngine.h	Thu Aug 10 18:47:14 2017 +0200
     2.2 +++ b/CpEpEngine.h	Sun Oct 01 23:46:20 2017 +0200
     2.3 @@ -8,6 +8,7 @@
     2.4  #include "utf8_helper.h"
     2.5  #include "pEp_utility.h"
     2.6  #include <queue>
     2.7 +#include <mutex>
     2.8  
     2.9  #if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
    2.10  #error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
    2.11 @@ -17,7 +18,6 @@
    2.12  using namespace utility;
    2.13  using namespace pEp::utility;
    2.14  
    2.15 -
    2.16  // CpEpEngine
    2.17  
    2.18  class ATL_NO_VTABLE CpEpEngine :
    2.19 @@ -26,14 +26,17 @@
    2.20  	public ISupportErrorInfo,
    2.21  	public IpEpEngine2
    2.22  {
    2.23 +
    2.24  protected:
    2.25      static int examine_identity(pEp_identity *ident, void *management);
    2.26  
    2.27  public:
    2.28      CpEpEngine() : keymanagement_thread(NULL), identity_queue(NULL), verbose_mode(false)
    2.29  	{
    2.30 -        PEP_STATUS status = ::init(&m_session);
    2.31 -        assert(status == PEP_STATUS_OK);
    2.32 +		std::lock_guard<std::mutex> lock(init_mutex);
    2.33 +		PEP_STATUS status = ::init(&m_session);
    2.34 +		assert(status == PEP_STATUS_OK);
    2.35 +
    2.36          ::register_examine_function(m_session, CpEpEngine::examine_identity, (void *)this);
    2.37          ::log_event(m_session, "Startup", "pEp COM Adapter", NULL, NULL);
    2.38      }
    2.39 @@ -43,7 +46,8 @@
    2.40          stop_keysync();
    2.41          StopKeyserverLookup();
    2.42          ::log_event(m_session, "Shutdown", "pEp COM Adapter", NULL, NULL);
    2.43 -        ::release(m_session);
    2.44 +		std::lock_guard<std::mutex> lock(init_mutex);
    2.45 +		::release(m_session);
    2.46      }
    2.47  
    2.48  DECLARE_REGISTRY_RESOURCEID(IDR_PEPENGINE)
    2.49 @@ -126,6 +130,7 @@
    2.50      thread *keymanagement_thread;
    2.51      bool verbose_mode;
    2.52  
    2.53 +
    2.54  	IpEpEngineCallbacks* client_callbacks = NULL;
    2.55      IpEpEngineCallbacks* client_callbacks_on_sync_thread = NULL;
    2.56      IpEpEngineCallbacks2* client_callbacks2_on_sync_thread = NULL;
    2.57 @@ -138,6 +143,8 @@
    2.58      static void do_keysync_in_thread(CpEpEngine* self, LPSTREAM marshaled_callbacks);
    2.59      void stop_keysync();
    2.60  
    2.61 +	static std::mutex init_mutex;
    2.62 +
    2.63      std::recursive_mutex keysync_mutex;
    2.64      std::condition_variable_any keysync_condition;
    2.65      std::thread *keysync_thread = NULL;