diff --git a/Adapter.cc b/Adapter.cc index 4aa56a6..5051047 100644 --- a/Adapter.cc +++ b/Adapter.cc @@ -34,6 +34,8 @@ namespace pEp { namespace Adapter { messageToSend_t _messageToSend = nullptr; notifyHandshake_t _notifyHandshake = nullptr; + messageToSend_t _messageToSend_sync = nullptr; + notifyHandshake_t _notifyHandshake_sync = nullptr; std::thread *_sync_thread = nullptr; ::utility::locked_queue< SYNC_EVENT, &free_Sync_event> q; @@ -87,6 +89,11 @@ namespace pEp { PEP_SESSION session(session_action action) { lock_guard lock(m); + bool in_sync; + if (_sync_thread && _sync_thread->get_id() == this_thread::get_id()) + in_sync = true; + else + in_sync = false; thread_local static PEP_SESSION _session = nullptr; PEP_STATUS status = PEP_STATUS_OK; @@ -100,8 +107,12 @@ namespace pEp { break; case init: - if (!_session) - status = ::init(&_session, _messageToSend, _inject_sync_event); + if (!_session) { + if (in_sync && _messageToSend_sync) + status = ::init(&_session, _messageToSend_sync, _inject_sync_event); + else + status = ::init(&_session, _messageToSend, _inject_sync_event); + } break; default: @@ -124,4 +135,3 @@ namespace pEp { } } } - diff --git a/Adapter.hh b/Adapter.hh index 6de2a46..e023740 100644 --- a/Adapter.hh +++ b/Adapter.hh @@ -21,8 +21,12 @@ namespace pEp { }; namespace Adapter { - template void startup(messageToSend_t messageToSend, - notifyHandshake_t notifyHandshake, T *obj = nullptr, + template void startup( + messageToSend_t messageToSend, + notifyHandshake_t notifyHandshake, + messageToSend_t messageToSend_sync = nullptr, + notifyHandshake_t notifyHandshake_sync = nullptr, + T *obj = nullptr, function< void (T *) > _startup = nullptr, function< void (T *) > _shutdown = nullptr ); diff --git a/Adapter.hxx b/Adapter.hxx index 0ab39b6..26cc2f2 100644 --- a/Adapter.hxx +++ b/Adapter.hxx @@ -8,6 +8,8 @@ namespace pEp { namespace Adapter { extern messageToSend_t _messageToSend; extern notifyHandshake_t _notifyHandshake; + extern messageToSend_t _messageToSend_sync; + extern notifyHandshake_t _notifyHandshake_sync; extern std::thread *_sync_thread; extern ::utility::locked_queue< SYNC_EVENT, &free_Sync_event > q; @@ -33,8 +35,12 @@ namespace pEp { _shutdown(obj); } - template< class T > void startup(messageToSend_t messageToSend, - notifyHandshake_t notifyHandshake, T *obj, + template< class T > void startup( + messageToSend_t messageToSend, + notifyHandshake_t notifyHandshake, + messageToSend_t messageToSend_sync, + notifyHandshake_t notifyHandshake_sync, + T *obj, function< void(T *) > _startup, function< void(T *) > _shutdown ) @@ -45,6 +51,12 @@ namespace pEp { if (notifyHandshake) _notifyHandshake = notifyHandshake; + if (messageToSend_sync) + _messageToSend_sync = messageToSend_sync; + + if (notifyHandshake_sync) + _notifyHandshake_sync = notifyHandshake_sync; + session(); { diff --git a/locked_queue.hh b/locked_queue.hh index fd5e142..6246525 100644 --- a/locked_queue.hh +++ b/locked_queue.hh @@ -1,7 +1,7 @@ // this file is under GNU GPL 3.0, see LICENSE.txt - -#pragma once +#ifndef PEP_LOCKED_QUEUE +#define PEP_LOCKED_QUEUE #include #include @@ -9,7 +9,7 @@ namespace utility { - template + template class locked_queue { typedef std::recursive_mutex Mutex; @@ -30,7 +30,7 @@ namespace utility Lock L(_mtx); for(auto& element : _q) { - Deleter(element); + deleter(element); } _q.clear(); } @@ -134,4 +134,6 @@ namespace utility return _q.empty(); } }; -}; +} + +#endif // PEP_LOCKED_QUEUE