Browse Source

extra callbacks for sync thread

sync
Volker Birk 7 years ago
parent
commit
e99d28569c
  1. 16
      Adapter.cc
  2. 8
      Adapter.hh
  3. 16
      Adapter.hxx
  4. 12
      locked_queue.hh

16
Adapter.cc

@ -34,6 +34,8 @@ namespace pEp {
namespace Adapter { namespace Adapter {
messageToSend_t _messageToSend = nullptr; messageToSend_t _messageToSend = nullptr;
notifyHandshake_t _notifyHandshake = nullptr; notifyHandshake_t _notifyHandshake = nullptr;
messageToSend_t _messageToSend_sync = nullptr;
notifyHandshake_t _notifyHandshake_sync = nullptr;
std::thread *_sync_thread = nullptr; std::thread *_sync_thread = nullptr;
::utility::locked_queue< SYNC_EVENT, &free_Sync_event> q; ::utility::locked_queue< SYNC_EVENT, &free_Sync_event> q;
@ -87,6 +89,11 @@ namespace pEp {
PEP_SESSION session(session_action action) PEP_SESSION session(session_action action)
{ {
lock_guard<mutex> lock(m); lock_guard<mutex> 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; thread_local static PEP_SESSION _session = nullptr;
PEP_STATUS status = PEP_STATUS_OK; PEP_STATUS status = PEP_STATUS_OK;
@ -100,8 +107,12 @@ namespace pEp {
break; break;
case init: case init:
if (!_session) if (!_session) {
status = ::init(&_session, _messageToSend, _inject_sync_event); if (in_sync && _messageToSend_sync)
status = ::init(&_session, _messageToSend_sync, _inject_sync_event);
else
status = ::init(&_session, _messageToSend, _inject_sync_event);
}
break; break;
default: default:
@ -124,4 +135,3 @@ namespace pEp {
} }
} }
} }

8
Adapter.hh

@ -21,8 +21,12 @@ namespace pEp {
}; };
namespace Adapter { namespace Adapter {
template<class T> void startup(messageToSend_t messageToSend, template<class T> void startup(
notifyHandshake_t notifyHandshake, T *obj = nullptr, 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 *) > _startup = nullptr,
function< void (T *) > _shutdown = nullptr function< void (T *) > _shutdown = nullptr
); );

16
Adapter.hxx

@ -8,6 +8,8 @@ namespace pEp {
namespace Adapter { namespace Adapter {
extern messageToSend_t _messageToSend; extern messageToSend_t _messageToSend;
extern notifyHandshake_t _notifyHandshake; extern notifyHandshake_t _notifyHandshake;
extern messageToSend_t _messageToSend_sync;
extern notifyHandshake_t _notifyHandshake_sync;
extern std::thread *_sync_thread; extern std::thread *_sync_thread;
extern ::utility::locked_queue< SYNC_EVENT, &free_Sync_event > q; extern ::utility::locked_queue< SYNC_EVENT, &free_Sync_event > q;
@ -33,8 +35,12 @@ namespace pEp {
_shutdown(obj); _shutdown(obj);
} }
template< class T > void startup(messageToSend_t messageToSend, template< class T > void startup(
notifyHandshake_t notifyHandshake, T *obj, messageToSend_t messageToSend,
notifyHandshake_t notifyHandshake,
messageToSend_t messageToSend_sync,
notifyHandshake_t notifyHandshake_sync,
T *obj,
function< void(T *) > _startup, function< void(T *) > _startup,
function< void(T *) > _shutdown function< void(T *) > _shutdown
) )
@ -45,6 +51,12 @@ namespace pEp {
if (notifyHandshake) if (notifyHandshake)
_notifyHandshake = notifyHandshake; _notifyHandshake = notifyHandshake;
if (messageToSend_sync)
_messageToSend_sync = messageToSend_sync;
if (notifyHandshake_sync)
_notifyHandshake_sync = notifyHandshake_sync;
session(); session();
{ {

12
locked_queue.hh

@ -1,7 +1,7 @@
// this file is under GNU GPL 3.0, see LICENSE.txt // this file is under GNU GPL 3.0, see LICENSE.txt
#ifndef PEP_LOCKED_QUEUE
#pragma once #define PEP_LOCKED_QUEUE
#include <list> #include <list>
#include <condition_variable> #include <condition_variable>
@ -9,7 +9,7 @@
namespace utility namespace utility
{ {
template<class T, void(*Deleter)(T) = [](T e) { delete e; } > template<class T, void(*deleter)(T) = [](T e) { delete e; } >
class locked_queue class locked_queue
{ {
typedef std::recursive_mutex Mutex; typedef std::recursive_mutex Mutex;
@ -30,7 +30,7 @@ namespace utility
Lock L(_mtx); Lock L(_mtx);
for(auto& element : _q) for(auto& element : _q)
{ {
Deleter(element); deleter(element);
} }
_q.clear(); _q.clear();
} }
@ -134,4 +134,6 @@ namespace utility
return _q.empty(); return _q.empty();
} }
}; };
}; }
#endif // PEP_LOCKED_QUEUE

Loading…
Cancel
Save