use .joinable() instead of pointer value to detect if sync is running Release_2.1.0-RC24
authorVolker Birk <vb@pep-project.org>
Thu, 13 Aug 2020 14:48:19 +0200
changeset 35378998b15268a
parent 352 2af560479b89
child 354 58d9c9485459
use .joinable() instead of pointer value to detect if sync is running
Adapter.cc
Adapter.hxx
callback_dispatcher.cc
test/framework.cc
test/test_leave_device_group.cc
     1.1 --- a/Adapter.cc	Thu Aug 13 12:19:35 2020 +0200
     1.2 +++ b/Adapter.cc	Thu Aug 13 14:48:19 2020 +0200
     1.3 @@ -38,17 +38,14 @@
     1.4      namespace Adapter {
     1.5          messageToSend_t _messageToSend = nullptr;
     1.6          notifyHandshake_t _notifyHandshake = nullptr;
     1.7 -        std::thread *_sync_thread = nullptr;
     1.8 +        std::thread _sync_thread;
     1.9  
    1.10          ::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > q;
    1.11          std::mutex m;
    1.12  
    1.13          std::thread::id sync_thread_id()
    1.14          {
    1.15 -            if (_sync_thread)
    1.16 -                return _sync_thread->get_id();
    1.17 -            else
    1.18 -                return std::thread::id();
    1.19 +            return _sync_thread.get_id();
    1.20          }
    1.21  
    1.22          int _inject_sync_event(SYNC_EVENT ev, void *management)
    1.23 @@ -65,21 +62,6 @@
    1.24              catch (exception&) {
    1.25                  return 1;
    1.26              }
    1.27 -            if (ev == nullptr) {
    1.28 -                if (!on_sync_thread()) {
    1.29 -                    if(_sync_thread->joinable()) {
    1.30 -                        pEpLog("Waiting for Sync thread to join...");
    1.31 -                        _sync_thread->join();
    1.32 -                        delete _sync_thread;
    1.33 -                        _sync_thread = nullptr;
    1.34 -                        pEpLog("...thread joined");
    1.35 -                        q.clear();
    1.36 -                    } else  {
    1.37 -                        //FATAL
    1.38 -                        pEpLog("FATAL: sync thread not joinable/detached");
    1.39 -                    }
    1.40 -                }
    1.41 -            }
    1.42              return 0;
    1.43          }
    1.44  
    1.45 @@ -102,10 +84,7 @@
    1.46  
    1.47          bool on_sync_thread()
    1.48          {
    1.49 -            if (_sync_thread && _sync_thread->get_id() == this_thread::get_id())
    1.50 -                return true;
    1.51 -            else
    1.52 -                return false;
    1.53 +            return _sync_thread.get_id() == this_thread::get_id();
    1.54          }
    1.55  
    1.56          PEP_SESSION session(session_action action)
    1.57 @@ -140,15 +119,16 @@
    1.58          void shutdown()
    1.59          {
    1.60          	pEpLog("called");
    1.61 -            if (_sync_thread) {
    1.62 +            if (_sync_thread.joinable()) {
    1.63              	pEpLog("sync_is_running - injecting null event");
    1.64                  _inject_sync_event(nullptr, nullptr);
    1.65 +                _sync_thread.join();
    1.66              }
    1.67          }
    1.68  
    1.69          bool is_sync_running()
    1.70          {
    1.71 -            return _sync_thread != nullptr;
    1.72 +            return _sync_thread.joinable();
    1.73          }
    1.74  
    1.75          bool in_shutdown()
     2.1 --- a/Adapter.hxx	Thu Aug 13 12:19:35 2020 +0200
     2.2 +++ b/Adapter.hxx	Thu Aug 13 14:48:19 2020 +0200
     2.3 @@ -14,7 +14,7 @@
     2.4  
     2.5          extern messageToSend_t _messageToSend;
     2.6          extern notifyHandshake_t _notifyHandshake;
     2.7 -        extern std::thread *_sync_thread;
     2.8 +        extern std::thread _sync_thread;
     2.9  
    2.10          extern ::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > q;
    2.11          extern std::mutex m;
    2.12 @@ -75,17 +75,14 @@
    2.13  
    2.14              session();
    2.15  
    2.16 -            if (!_sync_thread) {
    2.17 +            if (!_sync_thread.joinable()) {
    2.18                  register_done = false;
    2.19 -                _sync_thread = new std::thread(sync_thread<T>, obj, _startup, _shutdown);
    2.20 +                _sync_thread = std::thread(sync_thread<T>, obj, _startup, _shutdown);
    2.21                  while (!register_done)
    2.22                      std::this_thread::sleep_for(std::chrono::milliseconds(100));
    2.23  
    2.24 -                if (_ex) {
    2.25 -                    delete _sync_thread;
    2.26 -                    _sync_thread = nullptr;
    2.27 +                if (_ex)
    2.28                      std::rethrow_exception(_ex);
    2.29 -                }
    2.30              }
    2.31          }
    2.32      }
     3.1 --- a/callback_dispatcher.cc	Thu Aug 13 12:19:35 2020 +0200
     3.2 +++ b/callback_dispatcher.cc	Thu Aug 13 14:48:19 2020 +0200
     3.3 @@ -81,8 +81,7 @@
     3.4      void CallbackDispatcher::stop_sync()
     3.5      {
     3.6          callback_dispatcher.semaphore.stop();
     3.7 -        Adapter::q.clear();
     3.8 -        Adapter::q.push_back(nullptr);
     3.9 +        Adapter::shutdown();
    3.10          callback_dispatcher.semaphore.go();
    3.11  
    3.12          for (auto target : callback_dispatcher.targets) {
     4.1 --- a/test/framework.cc	Thu Aug 13 12:19:35 2020 +0200
     4.2 +++ b/test/framework.cc	Thu Aug 13 14:48:19 2020 +0200
     4.3 @@ -22,7 +22,7 @@
     4.4  
     4.5  pEp::Test::Transport pEp::Test::transport;
     4.6  std::string pEp::Test::path;
     4.7 -extern std::thread *pEp::Adapter::_sync_thread;
     4.8 +extern std::thread pEp::Adapter::_sync_thread;
     4.9  
    4.10  namespace pEp {
    4.11      namespace Test {
    4.12 @@ -127,33 +127,41 @@
    4.13              PEP_decrypt_flags_t flags = 0;
    4.14              PEP_STATUS status = ::decrypt_message(session(), msg.get(), &_dst, &keylist, &rating, &flags);
    4.15              throw_status(status);
    4.16 -            Message dst = make_message(_dst);
    4.17  
    4.18 -            for (auto a = _dst->attachments; a && a->value; a = a->next) {
    4.19 -                if (string("application/pEp.sync") == a->mime_type) {
    4.20 -                    char *_text;
    4.21 -                    status = PER_to_XER_Sync_msg(a->value, a->size, &_text);
    4.22 -                    throw_status(status);
    4.23 -                    text += _text;
    4.24 -                    pEp_free(_text);
    4.25 -                    return text;
    4.26 -                }
    4.27 -                else if (string("application/pEp.distribution") == a->mime_type) {
    4.28 -                    char *_text;
    4.29 -                    status = PER_to_XER_Distribution_msg(a->value, a->size, &_text);
    4.30 -                    throw_status(status);
    4.31 -                    text += _text;
    4.32 -                    pEp_free(_text);
    4.33 -                    return text;
    4.34 +            Message dst;
    4.35 +            if (_dst)
    4.36 +                dst = make_message(_dst);
    4.37 +            else
    4.38 +                dst = msg;
    4.39 +
    4.40 +            if (dst.get()->attachments) {
    4.41 +                for (auto a = dst.get()->attachments; a && a->value; a = a->next) {
    4.42 +                    if (string("application/pEp.sync") == a->mime_type) {
    4.43 +                        char *_text;
    4.44 +                        status = PER_to_XER_Sync_msg(a->value, a->size, &_text);
    4.45 +                        throw_status(status);
    4.46 +                        text += _text;
    4.47 +                        pEp_free(_text);
    4.48 +                        return text;
    4.49 +                    }
    4.50 +                    else if (string("application/pEp.distribution") == a->mime_type) {
    4.51 +                        char *_text;
    4.52 +                        status = PER_to_XER_Distribution_msg(a->value, a->size, &_text);
    4.53 +                        throw_status(status);
    4.54 +                        text += _text;
    4.55 +                        pEp_free(_text);
    4.56 +                        return text;
    4.57 +                    }
    4.58                  }
    4.59              }
    4.60 -            
    4.61 +
    4.62              return text;
    4.63          }
    4.64  
    4.65          void join_sync_thread()
    4.66          {
    4.67 -            _sync_thread->join();
    4.68 +            if (_sync_thread.joinable())
    4.69 +                _sync_thread.join();
    4.70          }
    4.71  
    4.72          Message Transport::recv()
     5.1 --- a/test/test_leave_device_group.cc	Thu Aug 13 12:19:35 2020 +0200
     5.2 +++ b/test/test_leave_device_group.cc	Thu Aug 13 14:48:19 2020 +0200
     5.3 @@ -1,4 +1,5 @@
     5.4  #include <iostream>
     5.5 +#include <unistd.h>
     5.6  
     5.7  #include "framework.hh"
     5.8  #include "passphrase_cache.hh"
     5.9 @@ -76,6 +77,14 @@
    5.10      // wait for sync shutdown and release first session
    5.11  
    5.12      Test::join_sync_thread();
    5.13 +
    5.14 +    // switch off and on again
    5.15 +
    5.16 +    CallbackDispatcher::start_sync();
    5.17 +    sleep(2);
    5.18 +    CallbackDispatcher::stop_sync();
    5.19 +    Test::join_sync_thread();
    5.20 +
    5.21      session(Adapter::release);
    5.22  
    5.23      return 0;