diff --git a/Adapter.cc b/Adapter.cc index b545f54..9b8ec34 100644 --- a/Adapter.cc +++ b/Adapter.cc @@ -38,17 +38,14 @@ namespace pEp { namespace Adapter { messageToSend_t _messageToSend = nullptr; notifyHandshake_t _notifyHandshake = nullptr; - std::thread *_sync_thread = nullptr; + std::thread _sync_thread; ::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > q; std::mutex m; std::thread::id sync_thread_id() { - if (_sync_thread) - return _sync_thread->get_id(); - else - return std::thread::id(); + return _sync_thread.get_id(); } int _inject_sync_event(SYNC_EVENT ev, void *management) @@ -65,21 +62,6 @@ namespace pEp { catch (exception&) { return 1; } - if (ev == nullptr) { - if (!on_sync_thread()) { - if(_sync_thread->joinable()) { - pEpLog("Waiting for Sync thread to join..."); - _sync_thread->join(); - delete _sync_thread; - _sync_thread = nullptr; - pEpLog("...thread joined"); - q.clear(); - } else { - //FATAL - pEpLog("FATAL: sync thread not joinable/detached"); - } - } - } return 0; } @@ -102,10 +84,7 @@ namespace pEp { bool on_sync_thread() { - if (_sync_thread && _sync_thread->get_id() == this_thread::get_id()) - return true; - else - return false; + return _sync_thread.get_id() == this_thread::get_id(); } PEP_SESSION session(session_action action) @@ -140,15 +119,16 @@ namespace pEp { void shutdown() { pEpLog("called"); - if (_sync_thread) { + if (_sync_thread.joinable()) { pEpLog("sync_is_running - injecting null event"); _inject_sync_event(nullptr, nullptr); + _sync_thread.join(); } } bool is_sync_running() { - return _sync_thread != nullptr; + return _sync_thread.joinable(); } bool in_shutdown() diff --git a/Adapter.hxx b/Adapter.hxx index 39cea22..f8ab868 100644 --- a/Adapter.hxx +++ b/Adapter.hxx @@ -14,7 +14,7 @@ namespace pEp { extern messageToSend_t _messageToSend; extern notifyHandshake_t _notifyHandshake; - extern std::thread *_sync_thread; + extern std::thread _sync_thread; extern ::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > q; extern std::mutex m; @@ -75,17 +75,14 @@ namespace pEp { session(); - if (!_sync_thread) { + if (!_sync_thread.joinable()) { register_done = false; - _sync_thread = new std::thread(sync_thread, obj, _startup, _shutdown); + _sync_thread = std::thread(sync_thread, obj, _startup, _shutdown); while (!register_done) std::this_thread::sleep_for(std::chrono::milliseconds(100)); - if (_ex) { - delete _sync_thread; - _sync_thread = nullptr; + if (_ex) std::rethrow_exception(_ex); - } } } } diff --git a/callback_dispatcher.cc b/callback_dispatcher.cc index 2ef8930..08bf769 100644 --- a/callback_dispatcher.cc +++ b/callback_dispatcher.cc @@ -81,8 +81,7 @@ namespace pEp { void CallbackDispatcher::stop_sync() { callback_dispatcher.semaphore.stop(); - Adapter::q.clear(); - Adapter::q.push_back(nullptr); + Adapter::shutdown(); callback_dispatcher.semaphore.go(); for (auto target : callback_dispatcher.targets) { diff --git a/test/framework.cc b/test/framework.cc index 5946aaf..0dc557d 100644 --- a/test/framework.cc +++ b/test/framework.cc @@ -22,7 +22,7 @@ pEp::Test::Transport pEp::Test::transport; std::string pEp::Test::path; -extern std::thread *pEp::Adapter::_sync_thread; +extern std::thread pEp::Adapter::_sync_thread; namespace pEp { namespace Test { @@ -127,33 +127,41 @@ namespace pEp { PEP_decrypt_flags_t flags = 0; PEP_STATUS status = ::decrypt_message(session(), msg.get(), &_dst, &keylist, &rating, &flags); throw_status(status); - Message dst = make_message(_dst); - - for (auto a = _dst->attachments; a && a->value; a = a->next) { - if (string("application/pEp.sync") == a->mime_type) { - char *_text; - status = PER_to_XER_Sync_msg(a->value, a->size, &_text); - throw_status(status); - text += _text; - pEp_free(_text); - return text; - } - else if (string("application/pEp.distribution") == a->mime_type) { - char *_text; - status = PER_to_XER_Distribution_msg(a->value, a->size, &_text); - throw_status(status); - text += _text; - pEp_free(_text); - return text; + + Message dst; + if (_dst) + dst = make_message(_dst); + else + dst = msg; + + if (dst.get()->attachments) { + for (auto a = dst.get()->attachments; a && a->value; a = a->next) { + if (string("application/pEp.sync") == a->mime_type) { + char *_text; + status = PER_to_XER_Sync_msg(a->value, a->size, &_text); + throw_status(status); + text += _text; + pEp_free(_text); + return text; + } + else if (string("application/pEp.distribution") == a->mime_type) { + char *_text; + status = PER_to_XER_Distribution_msg(a->value, a->size, &_text); + throw_status(status); + text += _text; + pEp_free(_text); + return text; + } } } - + return text; } void join_sync_thread() { - _sync_thread->join(); + if (_sync_thread.joinable()) + _sync_thread.join(); } Message Transport::recv() diff --git a/test/test_leave_device_group.cc b/test/test_leave_device_group.cc index a3a1dc8..756cc7d 100644 --- a/test/test_leave_device_group.cc +++ b/test/test_leave_device_group.cc @@ -1,4 +1,5 @@ #include +#include #include "framework.hh" #include "passphrase_cache.hh" @@ -76,6 +77,14 @@ int main(int argc, char **argv) // wait for sync shutdown and release first session Test::join_sync_thread(); + + // switch off and on again + + CallbackDispatcher::start_sync(); + sleep(2); + CallbackDispatcher::stop_sync(); + Test::join_sync_thread(); + session(Adapter::release); return 0;