diff --git a/Adapter.hxx b/Adapter.hxx index 4bd2544..a755075 100644 --- a/Adapter.hxx +++ b/Adapter.hxx @@ -22,11 +22,12 @@ namespace pEp { SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold); static std::exception_ptr _ex; - static bool register_done = false; + static std::atomic_bool register_done{false}; template< class T > void sync_thread(T *obj, function< void(T *) > _startup, function< void(T *) > _shutdown) { + pEpLog("called"); _ex = nullptr; assert(_messageToSend); assert(_notifyHandshake); @@ -41,11 +42,11 @@ namespace pEp { _notifyHandshake, _retrieve_next_sync_event); try { throw_status(status); - register_done = true; + register_done.store(true); } catch (...) { _ex = std::current_exception(); - register_done = true; + register_done.store(true); return; } } @@ -70,6 +71,7 @@ namespace pEp { function< void(T *) > _startup, function< void(T *) > _shutdown) { + pEpLog("called"); if (messageToSend) { _messageToSend = messageToSend; } @@ -77,17 +79,21 @@ namespace pEp { if (notifyHandshake) { _notifyHandshake = notifyHandshake; } - + pEpLog("creating session"); session(); if (!_sync_thread.joinable()) { - register_done = false; + register_done.store(false); + pEpLog("creating sync-thread"); + _sync_thread = std::thread(sync_thread, obj, _startup, _shutdown); - while (!register_done) { + while (register_done.load() == false) { + pEpLog("waiting for sync-thread to init..."); std::this_thread::sleep_for(std::chrono::milliseconds(100)); } if (_ex) { + pEpLog("exception pending, rethrowing"); std::rethrow_exception(_ex); } } diff --git a/callback_dispatcher.cc b/callback_dispatcher.cc index 704017d..46d35dd 100644 --- a/callback_dispatcher.cc +++ b/callback_dispatcher.cc @@ -71,6 +71,7 @@ namespace pEp { void CallbackDispatcher::start_sync() { + pEpLog("called"); callback_dispatcher.semaphore.go(); pEp::Adapter::startup(CallbackDispatcher::messageToSend, @@ -78,6 +79,7 @@ namespace pEp { &CallbackDispatcher::on_startup, &CallbackDispatcher::on_shutdown); + pEpLog("all targets signal: SYNC_NOTIFY_START"); for (auto target : callback_dispatcher.targets) { if (target.notifyHandshake) { target.notifyHandshake(nullptr, nullptr, SYNC_NOTIFY_START);