Browse Source

use .joinable() instead of pointer value to detect if sync is running

pull/1/head Release_2.1.0-RC24
Volker Birk 2 years ago
parent
commit
0260722d6c
5 changed files with 49 additions and 56 deletions
  1. +6
    -26
      Adapter.cc
  2. +4
    -7
      Adapter.hxx
  3. +1
    -2
      callback_dispatcher.cc
  4. +29
    -21
      test/framework.cc
  5. +9
    -0
      test/test_leave_device_group.cc

+ 6
- 26
Adapter.cc View File

@ -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()


+ 4
- 7
Adapter.hxx View File

@ -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<T>, obj, _startup, _shutdown);
_sync_thread = std::thread(sync_thread<T>, 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);
}
}
}
}


+ 1
- 2
callback_dispatcher.cc View File

@ -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) {


+ 29
- 21
test/framework.cc View File

@ -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()


+ 9
- 0
test/test_leave_device_group.cc View File

@ -1,4 +1,5 @@
#include <iostream>
#include <unistd.h>
#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;


Loading…
Cancel
Save