From d17f3a7f45b23d885848e97e69ddf08e1eda5925 Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Fri, 12 Oct 2018 16:14:50 +0200 Subject: [PATCH] simplify and switch to _Thrd_sleep() on Windoze --- Adapter.cc | 37 ++--- Adapter.hh | 9 ++ libpEpAdapter/ReadMe.txt | 29 ++++ libpEpAdapter/libpEpAdapter.vcxproj | 146 ++++++++++++++++++++ libpEpAdapter/libpEpAdapter.vcxproj.filters | 30 ++++ 5 files changed, 233 insertions(+), 18 deletions(-) create mode 100644 libpEpAdapter/ReadMe.txt create mode 100644 libpEpAdapter/libpEpAdapter.vcxproj create mode 100644 libpEpAdapter/libpEpAdapter.vcxproj.filters diff --git a/Adapter.cc b/Adapter.cc index 035fc4c..444d25f 100644 --- a/Adapter.cc +++ b/Adapter.cc @@ -22,25 +22,21 @@ namespace pEp { stringstream build; build << setfill('0') << "p≡p 0x" << setw(4) << hex << status; - throw runtime_error(build.str()); + throw RuntimeError(build.str(), status); + } + + RuntimeError::RuntimeError(string _text, PEP_STATUS _status) + : text(_text), runtime_error(text.c_str()), status(_status) + { + } namespace Adapter { static messageToSend_t _messageToSend = nullptr; static notifyHandshake_t _notifyHandshake = nullptr; static std::thread *_sync_thread = nullptr; - - static ::utility::locked_queue< SYNC_EVENT >& queue() - { - static ::utility::locked_queue< SYNC_EVENT > q; - return q; - } - - static std::mutex& mtx() - { - static std::mutex m; - return m; - } + static ::utility::locked_queue< SYNC_EVENT > q; + static std::mutex m; static int _inject_sync_event(SYNC_EVENT ev, void *management) { @@ -50,7 +46,7 @@ namespace pEp { } try { - queue().push_front(ev); + q.push_front(ev); } catch (exception&) { return 1; @@ -63,7 +59,7 @@ namespace pEp { time_t started = time(nullptr); bool timeout = false; - while (queue().empty()) { + while (q.empty()) { int i = 0; ++i; if (i > 10) { @@ -73,13 +69,18 @@ namespace pEp { } i = 0; } +#ifdef WIN32 + const xtime xt[] = { { 0, 100000000L } }; + _Thrd_sleep(xt); +#else nanosleep((const struct timespec[]){{0, 100000000L}}, NULL); +#endif } if (timeout) return new_sync_timeout_event(); - return queue().pop_front(); + return q.pop_front(); } static void sync_thread(void *obj) @@ -106,7 +107,7 @@ namespace pEp { session(); { - lock_guard lock(mtx()); + lock_guard lock(m); if (!_sync_thread) _sync_thread = new thread(sync_thread, obj); @@ -115,7 +116,7 @@ namespace pEp { PEP_SESSION session(session_action action) { - lock_guard lock(mtx()); + lock_guard lock(m); thread_local static PEP_SESSION _session = nullptr; PEP_STATUS status = PEP_STATUS_OK; diff --git a/Adapter.hh b/Adapter.hh index 04b0cfc..e9861de 100644 --- a/Adapter.hh +++ b/Adapter.hh @@ -5,11 +5,20 @@ #include "locked_queue.hh" #include +#include #include +using namespace std; + namespace pEp { void throw_status(PEP_STATUS status); + struct RuntimeError : runtime_error { + RuntimeError(string _text, PEP_STATUS _status); + string text; + PEP_STATUS status; + }; + namespace Adapter { void startup(messageToSend_t messageToSend, notifyHandshake_t notifyHandshake, void *obj = nullptr); diff --git a/libpEpAdapter/ReadMe.txt b/libpEpAdapter/ReadMe.txt new file mode 100644 index 0000000..06d70bd --- /dev/null +++ b/libpEpAdapter/ReadMe.txt @@ -0,0 +1,29 @@ +======================================================================== + STATIC LIBRARY : libpEpAdapter Project Overview +======================================================================== + +AppWizard has created this libpEpAdapter library project for you. + +No source files were created as part of your project. + + +libpEpAdapter.vcxproj + This is the main project file for VC++ projects generated using an Application Wizard. + It contains information about the version of Visual C++ that generated the file, and + information about the platforms, configurations, and project features selected with the + Application Wizard. + +libpEpAdapter.vcxproj.filters + This is the filters file for VC++ projects generated using an Application Wizard. + It contains information about the association between the files in your project + and the filters. This association is used in the IDE to show grouping of files with + similar extensions under a specific node (for e.g. ".cpp" files are associated with the + "Source Files" filter). + +///////////////////////////////////////////////////////////////////////////// +Other notes: + +AppWizard uses "TODO:" comments to indicate parts of the source code you +should add to or customize. + +///////////////////////////////////////////////////////////////////////////// diff --git a/libpEpAdapter/libpEpAdapter.vcxproj b/libpEpAdapter/libpEpAdapter.vcxproj new file mode 100644 index 0000000..466daad --- /dev/null +++ b/libpEpAdapter/libpEpAdapter.vcxproj @@ -0,0 +1,146 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {EC44FEC9-2F3A-4A0C-B60E-0F22AA43EF58} + Win32Proj + libpEpAdapter + 8.1 + + + + StaticLibrary + true + v140 + Unicode + + + StaticLibrary + false + v140 + true + Unicode + + + StaticLibrary + true + v140 + Unicode + + + StaticLibrary + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + C:\Users\vb\Documents\Visual Studio 2015\Projects\pEp_for_Outlook_Solution;C:\Users\vb\Documents\Visual Studio 2015\Projects\pEp_for_Outlook_Solution\pEpEngine\src;$(IncludePath) + + + C:\Users\vb\Documents\Visual Studio 2015\Projects\pEp_for_Outlook_Solution;C:\Users\vb\Documents\Visual Studio 2015\Projects\pEp_for_Outlook_Solution\pEpEngine\src;$(IncludePath) + + + + + + Level3 + Disabled + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + + + + + + + Level3 + Disabled + _DEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + true + true + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/libpEpAdapter/libpEpAdapter.vcxproj.filters b/libpEpAdapter/libpEpAdapter.vcxproj.filters new file mode 100644 index 0000000..ec57bb8 --- /dev/null +++ b/libpEpAdapter/libpEpAdapter.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + + + + Source Files + + + + + Header Files + + + \ No newline at end of file