call_with_lock.hh
author Volker Birk <vb@pep-project.org>
Thu, 14 Nov 2019 23:22:05 +0100
changeset 134 b6e70c7067b3
parent 108 52a3ec390f8b
permissions -rw-r--r--
merging
roker@108
     1
// this file is under GNU GPL 3.0, see LICENSE.txt
roker@108
     2
roker@108
     3
#ifndef PEP_CALL_WITH_LOCK_HH
roker@108
     4
#define PEP_CALL_WITH_LOCK_HH
roker@108
     5
roker@108
     6
#include <mutex>
roker@108
     7
roker@108
     8
namespace pEp
roker@108
     9
{
roker@108
    10
	extern std::mutex call_with_lock_mutex;
roker@108
    11
roker@108
    12
	// TODO: use && and std::forward<> to avoid copying of the arguments.
roker@108
    13
	// It is not relevant, yet, because at the moment we use this function template only
roker@108
    14
	// for init() and release() which have cheap-to-copy pointer parameters only
roker@108
    15
	template<class R, class... Args>
roker@108
    16
	R call_with_lock( R(*fn)(Args...), Args... args)
roker@108
    17
	{
roker@108
    18
		std::lock_guard<std::mutex> L(call_with_lock_mutex);
roker@108
    19
		return fn(args...);
roker@108
    20
	}
roker@108
    21
roker@108
    22
roker@108
    23
}
roker@108
    24
roker@108
    25
#endif // PEP_CALL_WITH_LOCK_HH