ENGINE-524: merge in default ENGINE-524
authorClaudio Luck <claudio.luck@pep.foundation>
Thu, 30 May 2019 02:19:48 +0200
branchENGINE-524
changeset 1281d8c67dd7933
parent 127 3fdc8e549724
parent 125 dc6e1f416ac0
child 129 e9685e51de81
ENGINE-524: merge in default
     1.1 --- a/Adapter.hh	Sat May 25 01:22:28 2019 +0200
     1.2 +++ b/Adapter.hh	Thu May 30 02:19:48 2019 +0200
     1.3 @@ -29,7 +29,7 @@
     1.4                  T *obj = nullptr,
     1.5                  std::function< void (T *) > _startup = nullptr,
     1.6                  std::function< void (T *) > _shutdown = nullptr
     1.7 -            );
     1.8 +            ) throw (RuntimeError);
     1.9  
    1.10          // returns 'true' when called from the "sync" thread, 'false' otherwise.
    1.11          bool on_sync_thread();
     2.1 --- a/Adapter.hxx	Sat May 25 01:22:28 2019 +0200
     2.2 +++ b/Adapter.hxx	Thu May 30 02:19:48 2019 +0200
     2.3 @@ -4,11 +4,10 @@
     2.4  #include "locked_queue.hh"
     2.5  #include <cassert>
     2.6  
     2.7 -
     2.8  namespace pEp {
     2.9      namespace Adapter {
    2.10          using std::function;
    2.11 -    
    2.12 +
    2.13          extern messageToSend_t _messageToSend;
    2.14          extern notifyHandshake_t _notifyHandshake;
    2.15          extern std::thread *_sync_thread;
    2.16 @@ -18,6 +17,9 @@
    2.17  
    2.18          SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold);
    2.19  
    2.20 +        static std::exception_ptr _ex;
    2.21 +        static bool register_done = false;
    2.22 +
    2.23          template< class T > void sync_thread(T *obj, function< void(T *) > _startup, function< void(T *) > _shutdown)
    2.24          {
    2.25              assert(_messageToSend);
    2.26 @@ -27,9 +29,19 @@
    2.27  
    2.28              session();
    2.29  
    2.30 -            PEP_STATUS status = register_sync_callbacks(session(), nullptr,
    2.31 -                _notifyHandshake, _retrieve_next_sync_event);
    2.32 -            throw_status(status);
    2.33 +            {
    2.34 +                PEP_STATUS status = register_sync_callbacks(session(), nullptr,
    2.35 +                    _notifyHandshake, _retrieve_next_sync_event);
    2.36 +                try {
    2.37 +                    throw_status(status);
    2.38 +                    register_done = true;
    2.39 +                }
    2.40 +                catch (...) {
    2.41 +                    _ex = std::current_exception();
    2.42 +                    register_done = true;
    2.43 +                    return;
    2.44 +                }
    2.45 +            }
    2.46  
    2.47              do_sync_protocol(session(), (void *)obj);
    2.48              unregister_sync_callbacks(session());
    2.49 @@ -47,6 +59,7 @@
    2.50              function< void(T *) > _startup,
    2.51              function< void(T *) > _shutdown
    2.52          )
    2.53 +            throw (RuntimeError)
    2.54          {
    2.55              if (messageToSend)
    2.56                  _messageToSend = messageToSend;
    2.57 @@ -56,11 +69,16 @@
    2.58  
    2.59              session();
    2.60  
    2.61 -            {
    2.62 -                std::lock_guard<std::mutex> lock(m);
    2.63 +            if (!_sync_thread) {
    2.64 +                register_done = false;
    2.65 +                _sync_thread = new std::thread(sync_thread<T>, obj, _startup, _shutdown);
    2.66 +                while (!register_done)
    2.67 +                    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    2.68  
    2.69 -                if (!_sync_thread)
    2.70 -                    _sync_thread = new std::thread(sync_thread<T>, obj, _startup, _shutdown);
    2.71 +                if (_ex) {
    2.72 +                    _sync_thread = nullptr;
    2.73 +                    std::rethrow_exception(_ex);
    2.74 +                }
    2.75              }
    2.76          }
    2.77      }