Browse Source

use std::<atomic> types for thread synchronization. (otherwise undef behaviour)

pull/5/head
heck 1 year ago
parent
commit
966b2967d4
2 changed files with 14 additions and 6 deletions
  1. +12
    -6
      Adapter.hxx
  2. +2
    -0
      callback_dispatcher.cc

+ 12
- 6
Adapter.hxx View File

@ -22,11 +22,12 @@ namespace pEp {
SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold);
static std::exception_ptr _ex;
static bool register_done = false;
static std::atomic_bool register_done{false};
template< class T >
void sync_thread(T *obj, function< void(T *) > _startup, function< void(T *) > _shutdown)
{
pEpLog("called");
_ex = nullptr;
assert(_messageToSend);
assert(_notifyHandshake);
@ -41,11 +42,11 @@ namespace pEp {
_notifyHandshake, _retrieve_next_sync_event);
try {
throw_status(status);
register_done = true;
register_done.store(true);
}
catch (...) {
_ex = std::current_exception();
register_done = true;
register_done.store(true);
return;
}
}
@ -70,6 +71,7 @@ namespace pEp {
function< void(T *) > _startup,
function< void(T *) > _shutdown)
{
pEpLog("called");
if (messageToSend) {
_messageToSend = messageToSend;
}
@ -77,17 +79,21 @@ namespace pEp {
if (notifyHandshake) {
_notifyHandshake = notifyHandshake;
}
pEpLog("creating session");
session();
if (!_sync_thread.joinable()) {
register_done = false;
register_done.store(false);
pEpLog("creating sync-thread");
_sync_thread = std::thread(sync_thread<T>, obj, _startup, _shutdown);
while (!register_done) {
while (register_done.load() == false) {
pEpLog("waiting for sync-thread to init...");
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (_ex) {
pEpLog("exception pending, rethrowing");
std::rethrow_exception(_ex);
}
}


+ 2
- 0
callback_dispatcher.cc View File

@ -71,6 +71,7 @@ namespace pEp {
void CallbackDispatcher::start_sync()
{
pEpLog("called");
callback_dispatcher.semaphore.go();
pEp::Adapter::startup<CallbackDispatcher>(CallbackDispatcher::messageToSend,
@ -78,6 +79,7 @@ namespace pEp {
&CallbackDispatcher::on_startup,
&CallbackDispatcher::on_shutdown);
pEpLog("all targets signal: SYNC_NOTIFY_START");
for (auto target : callback_dispatcher.targets) {
if (target.notifyHandshake) {
target.notifyHandshake(nullptr, nullptr, SYNC_NOTIFY_START);


Loading…
Cancel
Save