From 1c9eeb93f658d33d08f0ac74aa64d5d52dd7dbb6 Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Tue, 28 Jan 2020 12:01:50 +0100 Subject: [PATCH] shutdown sync for single threaded script --- src/adapter.cc | 11 ++++++++++- src/adapter.hh | 14 ++++++++++---- src/message.cc | 2 +- src/message_api.cc | 3 ++- src/pEpmodule.cc | 21 +++++++++++++++++++++ 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/adapter.cc b/src/adapter.cc index 559d615..87eb107 100644 --- a/src/adapter.cc +++ b/src/adapter.cc @@ -7,7 +7,7 @@ namespace pEp { namespace PythonAdapter { Adapter::Adapter(bool unregister_this) - : flag_unregister(unregister_this) + : flag_unregister(unregister_this), flag_sync_enabled(false) { session(init); } @@ -55,6 +55,15 @@ namespace pEp { return _session; } + ::utility::locked_queue< SYNC_EVENT > * Adapter::q = nullptr; + + void Adapter::shutdown_sync() + { + if (queue_active()) + queue().push_front(nullptr); + flag_sync_enabled = false; + } + PyObject *Adapter::ui_object(PyObject *value) { lock_guard lock(mtx()); diff --git a/src/adapter.hh b/src/adapter.hh index d1e921a..16013d9 100644 --- a/src/adapter.hh +++ b/src/adapter.hh @@ -14,6 +14,7 @@ namespace pEp { class Adapter { bool flag_unregister; + bool flag_sync_enabled; public: Adapter(bool unregister_this = false); @@ -28,16 +29,21 @@ namespace pEp { PEP_SESSION session(session_action action = none); static ::utility::locked_queue< SYNC_EVENT >& queue() { - static ::utility::locked_queue< SYNC_EVENT > q; - return q; + if (!q) + q = new ::utility::locked_queue< SYNC_EVENT >(); + return *q; } - - void shutdown_sync() { queue().push_front(nullptr); } + void script_is_implementing_sync() { flag_sync_enabled = true; } + void shutdown_sync(); + bool is_sync_active() { return flag_sync_enabled; } protected: static PyObject *ui_object(PyObject *value = nullptr); static int _inject_sync_event(SYNC_EVENT ev, void *management); + static ::utility::locked_queue< SYNC_EVENT > *q; + bool queue_active() { return !!q; } + private: static mutex& mtx() { diff --git a/src/message.cc b/src/message.cc index 8c186c3..8c7ec9d 100644 --- a/src/message.cc +++ b/src/message.cc @@ -306,7 +306,7 @@ namespace pEp { } boost::python::tuple Message::decrypt(int flags) { - return decrypt_message(*this, flags); + return pEp::PythonAdapter::decrypt_message(*this, flags); } int Message::outgoing_rating() diff --git a/src/message_api.cc b/src/message_api.cc index 2f7b2cc..8fdb07f 100644 --- a/src/message_api.cc +++ b/src/message_api.cc @@ -7,6 +7,7 @@ #include #include #include +#include namespace pEp { namespace PythonAdapter { @@ -47,7 +48,7 @@ namespace pEp { PEP_decrypt_flags_t _flags = (PEP_decrypt_flags_t) flags; message *_src = src; - PEP_STATUS status = decrypt_message(adapter.session(), _src, &_dst, &_keylist, + PEP_STATUS status = ::decrypt_message(adapter.session(), _src, &_dst, &_keylist, &_rating, &_flags); _throw_status(status); diff --git a/src/pEpmodule.cc b/src/pEpmodule.cc index fdf0e18..f007ad3 100644 --- a/src/pEpmodule.cc +++ b/src/pEpmodule.cc @@ -122,6 +122,14 @@ namespace pEp { void leave_device_group() { ::leave_device_group(adapter.session()); } + + void script_is_implementing_sync() { + adapter.script_is_implementing_sync(); + } + + bool is_sync_active() { + return adapter.is_sync_active(); + } } } @@ -557,6 +565,19 @@ BOOST_PYTHON_MODULE(pEp) "\n" "call this for a grouped device, which should leave\n" ); + + def("script_is_implementing_sync", &pEp::PythonAdapter::script_is_implementing_sync, + "script_is_implementing_sync()\n" + "\n" + "call this in case the Python script is implementing sync to make\n" + "is_sync_active() working\n" + ); + + def("is_sync_active", &pEp::PythonAdapter::is_sync_active, + "is_sync_active()\n" + "\n" + "True if sync is active, False otherwise\n" + ); // codecs