From 68a2f889032fba4ffc47ba7874ac2fbaf1d2aa63 Mon Sep 17 00:00:00 2001 From: Roker Date: Tue, 23 Apr 2019 17:05:17 +0200 Subject: [PATCH] add call_with_lock(). Moved from Json Adapter. --- call_with_lock.cc | 8 ++++++++ call_with_lock.hh | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 call_with_lock.cc create mode 100644 call_with_lock.hh diff --git a/call_with_lock.cc b/call_with_lock.cc new file mode 100644 index 0000000..cd04003 --- /dev/null +++ b/call_with_lock.cc @@ -0,0 +1,8 @@ +// this file is under GNU GPL 3.0, see LICENSE.txt + +#include "call_with_lock.hh" + +namespace pEp +{ + std::mutex call_with_lock_mutex; +} diff --git a/call_with_lock.hh b/call_with_lock.hh new file mode 100644 index 0000000..1877bcc --- /dev/null +++ b/call_with_lock.hh @@ -0,0 +1,25 @@ +// this file is under GNU GPL 3.0, see LICENSE.txt + +#ifndef PEP_CALL_WITH_LOCK_HH +#define PEP_CALL_WITH_LOCK_HH + +#include + +namespace pEp +{ + extern std::mutex call_with_lock_mutex; + + // TODO: use && and std::forward<> to avoid copying of the arguments. + // It is not relevant, yet, because at the moment we use this function template only + // for init() and release() which have cheap-to-copy pointer parameters only + template + R call_with_lock( R(*fn)(Args...), Args... args) + { + std::lock_guard L(call_with_lock_mutex); + return fn(args...); + } + + +} + +#endif // PEP_CALL_WITH_LOCK_HH