Backing out all commits after 159:3275abf4bb79

sync
heck 2020-04-22 16:06:11 +02:00
parent ac9f433f17
commit a62e51bec8
11 changed files with 33 additions and 113 deletions

View File

@ -9,5 +9,3 @@ test_adapter
lib
local.conf
build/
# Default ignored files
.idea/

View File

@ -6,15 +6,12 @@
#include <iomanip>
#include <assert.h>
#include "status_to_string.hh"
#include "utils.hh"
using namespace std;
namespace pEp {
void throw_status(PEP_STATUS status)
{
//pEpLog("called");
if (status == PEP_STATUS_OK)
return;
if (status >= 0x400 && status <= 0x4ff)
@ -31,7 +28,7 @@ namespace pEp {
RuntimeError::RuntimeError(const std::string& _text, PEP_STATUS _status)
: std::runtime_error(_text.c_str()), text(_text), status(_status)
{
pEpLog("called");
}
namespace Adapter {
@ -39,25 +36,21 @@ namespace pEp {
notifyHandshake_t _notifyHandshake = nullptr;
std::thread *_sync_thread = nullptr;
::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > sync_q;
std::mutex mutex_global;
::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > q;
std::mutex m;
int _inject_sync_event(SYNC_EVENT ev, void *management)
{
pEpLog("called");
try {
if (ev == nullptr) {
pEpLog("SYNC_EVENT: NULL");
sync_q.clear();
sync_q.push_back(ev);
q.clear();
q.push_back(ev);
}
else {
pEpLog("SYNC_EVENT:" << ev);
sync_q.push_front(ev);
q.push_front(ev);
}
}
catch (exception&) {
pEpErr("Exception");
return 1;
}
if (ev == nullptr) {
@ -68,7 +61,7 @@ namespace pEp {
delete _sync_thread;
_sync_thread = nullptr;
pEpLog("...thread joined");
sync_q.clear();
q.clear();
} else {
//FATAL
pEpLog("FATAL: sync thread not joinable/detached");
@ -81,22 +74,17 @@ namespace pEp {
// threshold: max waiting time in seconds
SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold)
{
pEpLog("called");
SYNC_EVENT syncEvent = nullptr;
const bool success = sync_q.try_pop_front(syncEvent, std::chrono::seconds(threshold));
const bool success = q.try_pop_front(syncEvent, std::chrono::seconds(threshold));
if (!success) {
pEpLog("timeout after [sec]: " << threshold);
if (!success)
return new_sync_timeout_event();
}
pEpLog("returning SYNC_EVENT: " << syncEvent);
return syncEvent;
}
bool on_sync_thread()
{
//pEpLog("called");
if (_sync_thread && _sync_thread->get_id() == this_thread::get_id())
return true;
else
@ -105,8 +93,7 @@ namespace pEp {
PEP_SESSION session(session_action action)
{
pEpLog("called");
std::lock_guard<mutex> lock(mutex_global);
std::lock_guard<mutex> lock(m);
bool in_sync = on_sync_thread();
thread_local static PEP_SESSION _session = nullptr;
@ -115,19 +102,14 @@ namespace pEp {
switch (action) {
case release:
if (_session) {
pEpLog("action = release: releasing session: " << _session);
::release(_session);
_session = nullptr;
} else {
pEpLog("action = release: No session to release");
}
break;
case init:
if (!_session) {
pEpLog("action = init: creating new session");
if (!_session)
status = ::init(&_session, _messageToSend, _inject_sync_event);
}
break;
default:
@ -135,7 +117,6 @@ namespace pEp {
}
throw_status(status);
pEpLog("returning session: " << _session);
return _session;
}
@ -150,7 +131,6 @@ namespace pEp {
bool is_sync_running()
{
pEpLog("called");
return _sync_thread != nullptr;
}
}

View File

@ -8,6 +8,14 @@
#include <pEp/sync_api.h>
#include <stdexcept>
// TODO: put into not yet existing libpEpAdapter_utils.h, to be across whole libpEpAdapter
#ifdef NDEBUG
#define pEpLog(msg) do{}while(0)
#else
#include <iostream>
#define pEpLog(msg) do{std::cerr << __FILE__ << "::" << __FUNCTION__ << " - " << msg << '\n';} while(0)
#endif
namespace pEp {
// throws std::bad_alloc if status==PEP_OUT_OF_MEMORY,

View File

@ -6,7 +6,6 @@
#include <thread>
#include "locked_queue.hh"
#include <cassert>
#include "utils.hh"
namespace pEp {
namespace Adapter {
@ -17,7 +16,7 @@ namespace pEp {
extern std::thread *_sync_thread;
extern ::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > q;
extern std::mutex mutex_global;
extern std::mutex m;
SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold);
@ -26,7 +25,6 @@ namespace pEp {
template< class T > void sync_thread(T *obj, function< void(T *) > _startup, function< void(T *) > _shutdown)
{
pEpLog("called");
_ex = nullptr;
assert(_messageToSend);
assert(_notifyHandshake);
@ -69,8 +67,6 @@ namespace pEp {
)
throw (RuntimeError)
{
pEpLog("called");
if (messageToSend)
_messageToSend = messageToSend;

View File

@ -17,7 +17,7 @@ ifdef BUILD_CONFIG
$(info ================================================)
endif
.PHONY: all lib test install uninstall clean
.PHONY: all, lib, test, install, uninstall, clean
SOURCE=$(wildcard *.cc)
HEADERS=$(wildcard *.hh *.hxx)

View File

@ -32,11 +32,11 @@ else
CXXFLAGS+=-DNDEBUG=1
endif
# Defaults
######### Engine #########
ENGINE_LIB_PATH=$(HOME)/lib
ENGINE_INC_PATH=$(HOME)/include
# Config File
######### Overrides #########
-include $(HERE)/local.conf
# Add -L Prefixes to LIB/INC paths,

View File

@ -6,7 +6,7 @@ CXXFLAGS+=-I../
.PHONY=all, test_adapter, test_adapter_cxx, test_library
all: test_adapter test_adapter_cxx test_library test_peplog
all: test_adapter test_adapter_cxx test_library
test_adapter: test_adapter.cc ../libpEpAdapter.a
@ -14,9 +14,6 @@ test_adapter_cxx: test_adapter_cxx.cc ../libpEpAdapter.a
test_library: test_library.cc ../libpEpAdapter.a
test_peplog: test_peplog.cc
clean:
rm -vf test_adapter
rm -rvf test_adapter.dSYM

View File

@ -1,25 +1,24 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "Adapter.hh"
#include <iostream>
#include <assert.h>
#include <unistd.h>
#include <pEp/keymanagement.h>
#include "Adapter.hh"
#include "../utils.hh"
using namespace std;
using namespace pEp::Adapter;
PEP_STATUS messageToSend(struct _message *msg)
{
pEpLog("called");
pEpLog("called()");
return PEP_STATUS_OK;
}
PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal)
{
pEpLog("called");
pEpLog("called()");
return PEP_STATUS_OK;
}
@ -34,16 +33,17 @@ int main()
pEp::throw_status(status);
// start and stop sync repeatedly
useconds_t sleepuSec = 1000 * 1000;
unsigned long long int nrIters = 3;//1000 * 1000 * 1000;
useconds_t sleepuSec = 1000 * 100;
unsigned long long int nrIters = 1000 * 1000 * 1000;
for (int i = 0; i < nrIters; i++) {
pEpLog("RUN NR: " << i);
pEpLog("RUN NR: ");
pEpLog(i);
pEpLog("SYNC START");
pEpLog("starting the adapter including sync");
startup(messageToSend, notifyHandshake);
pEpLog("SYNC STOP");
usleep(sleepuSec);
shutdown();
usleep(sleepuSec);
}
return 0;
}

View File

@ -1,12 +1,11 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "Adapter.hh"
#include <iostream>
#include <assert.h>
#include <unistd.h>
#include <pEp/keymanagement.h>
#include "Adapter.hh"
#include "../utils.hh"
using namespace pEp::Adapter;

View File

@ -1,16 +0,0 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "utils.hh"
// Single threaded test of pEpLog() function.
// check for uniformity of time
int main()
{
pEpLog("Test Starting... observe uniformity of timestamp.");
int nr_log_lines = 1000000;
for (int i; i < nr_log_lines; i++) {
pEpLog("log line nr: " << i);
}
return 0;
}

View File

@ -1,42 +0,0 @@
#pragma once
#include <iostream>
#include <thread>
#include <sstream>
#include <iomanip>
// Returns a timestamp string featuring microseconds precision
// Uses system clock, so time might not pass uniformly
inline std::string timestamp_usec() {
std::ostringstream buffer;
std::chrono::system_clock::duration d = std::chrono::system_clock::now().time_since_epoch();
std::chrono::microseconds us = std::chrono::duration_cast<std::chrono::microseconds>(d);
std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds> (d);
std::time_t t = s.count();
std::tm tm = *std::localtime(&t);
std::size_t fractional_seconds = us.count() % 1000000;
buffer << std::put_time(&tm, "%H%:%M:%S") << "." << fractional_seconds;
return buffer.str();
}
// pEpLog(msg) - logs to STDOUT
// pEpErr(msg) - logs to STDERR
//
// Log format is:
// TIMESTAMP.usecs THREAD-ID SRC_FILE_NAME::FUNCTION_NAME - msg
//
// example:
// 21:06:04.214884 Thread:0x10f2ce5c0 test_peplog.cc::main - test run nr: 436744
//
// if -DNDEBUG=1 (for release builds), all pEpLog() calls will be optimized away.
#ifdef NDEBUG
#define pEpLog(msg) do{}while(0)
#else
#define pEpLog(msg) do{ std::cout << std::unitbuf; std::cout << timestamp_usec() << " Thread:" << std::this_thread::get_id() << ' ' <<__FILE__ << "::" << __FUNCTION__ << " - " << msg << std::endl;} while(0)
#endif
#define pEpErr(msg) do{ std::cerr << std::unitbuf; std::cerr << timestamp_usec() << " Thread:" << std::this_thread::get_id() << ' ' <<__FILE__ << "::" << __FUNCTION__ << " - " << msg << std::endl;} while(0)