From 2ecdb94ba6333647986505a263461709ed92e554 Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Sat, 4 Jul 2020 21:54:54 +0200 Subject: [PATCH] need a semaphore here --- Semaphore.hh | 39 +++++++++++++++++++++++++++++++++++++++ callback_dispatcher.cc | 28 ++++++++-------------------- callback_dispatcher.hh | 4 ++-- 3 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 Semaphore.hh diff --git a/Semaphore.hh b/Semaphore.hh new file mode 100644 index 0000000..a7328bb --- /dev/null +++ b/Semaphore.hh @@ -0,0 +1,39 @@ +#include +#include + +namespace pEp { + class Semaphore { + public: + Semaphore() : _stop(false) {} + + void stop() + { + std::unique_lock lock(mtx); + _stop = true; + } + + void try_wait() + { + std::unique_lock lock(mtx); + if (!_stop) + return; + + while(_stop){ + cv.wait(lock); + } + } + + void go() + { + std::unique_lock lock(mtx); + _stop = false; + cv.notify_all(); + } + + private: + std::mutex mtx; + std::condition_variable cv; + bool _stop; + }; +} + diff --git a/callback_dispatcher.cc b/callback_dispatcher.cc index ec1eb8c..da1e209 100644 --- a/callback_dispatcher.cc +++ b/callback_dispatcher.cc @@ -30,11 +30,8 @@ namespace pEp { targets.push_back({messageToSend, notifyHandshake, on_startup, shutdown}); - if (!Adapter::on_sync_thread()) { - // try_unlock() possibly waiting messageToSend - sync_mtx.try_lock(); - sync_mtx.unlock(); - } + if (!Adapter::on_sync_thread()) + semaphore.go(); } void CallbackDispatcher::remove(::messageToSend_t messageToSend) @@ -71,9 +68,7 @@ namespace pEp { void CallbackDispatcher::start_sync() { - // try_unlock() - callback_dispatcher.sync_mtx.try_lock(); - callback_dispatcher.sync_mtx.unlock(); + callback_dispatcher.semaphore.go(); pEp::Adapter::startup(CallbackDispatcher::messageToSend, CallbackDispatcher::notifyHandshake, &callback_dispatcher, @@ -88,10 +83,7 @@ namespace pEp { void CallbackDispatcher::stop_sync() { - // try_unlock() - callback_dispatcher.sync_mtx.try_lock(); - callback_dispatcher.sync_mtx.unlock(); - + callback_dispatcher.semaphore.go(); pEp::Adapter::shutdown(); for (auto target : callback_dispatcher.targets) { @@ -103,21 +95,17 @@ namespace pEp { PEP_STATUS CallbackDispatcher::_messageToSend(::message *msg) { if (Adapter::on_sync_thread() && !msg) { + semaphore.try_wait(); + PEP_STATUS status = PassphraseCache::messageToSend(passphrase_cache, Adapter::session()); // if the cache has no valid passphrase ask the app if (status == PEP_PASSPHRASE_REQUIRED || status == PEP_WRONG_PASSPHRASE) { - // lock mutex and call async - sync_mtx.lock(); - notifyHandshake(nullptr, nullptr, SYNC_PASSPHRASE_REQUIRED); - - // wait() until mutex was unlocked by add() - sync_mtx.lock(); - sync_mtx.unlock(); + semaphore.stop(); } // the pEp engine must try again - return PEP_STATUS_OK; + return status; } for (auto target : targets) { diff --git a/callback_dispatcher.hh b/callback_dispatcher.hh index 95e5a31..6ced956 100644 --- a/callback_dispatcher.hh +++ b/callback_dispatcher.hh @@ -4,6 +4,7 @@ #include #include #include "Adapter.hh" +#include "Semaphore.hh" namespace pEp { // use this class when implementing a desktop adapter @@ -21,8 +22,7 @@ namespace pEp { proc on_shutdown; }; std::vector targets; - - std::mutex sync_mtx; + Semaphore semaphore; public: void add(