Library: inline the thread operation when possible
The Lumiera thread-wrapper accepts the operation to be performed within the new thread as a function object, function reference or lambda. Some of these types can be directly instantiated in the threadMain function, and thus possibly inlined altogether. This is especially relevant for Lambdas. OTOH, we can not instantiate function references or bound member functions; in those cases we fall back to using a std::function object, possibly incurring heap allocations.
This commit is contained in:
parent
a19a79b611
commit
8cb67fd9fa
2 changed files with 10 additions and 6 deletions
|
|
@ -42,6 +42,7 @@
|
|||
|
||||
#include "lib/error.hpp"
|
||||
#include "include/logging.h"
|
||||
#include "lib/meta/function.hpp"
|
||||
#include "lib/result.hpp"
|
||||
|
||||
extern "C" {
|
||||
|
|
@ -50,14 +51,11 @@ extern "C" {
|
|||
#include "backend/threadpool-init.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
|
||||
|
||||
namespace backend {
|
||||
|
||||
using std::bind;
|
||||
using std::function;
|
||||
using lib::Literal;
|
||||
namespace error = lumiera::error;
|
||||
using error::LUMIERA_ERROR_STATE;
|
||||
|
|
@ -125,7 +123,8 @@ namespace backend {
|
|||
static void
|
||||
threadMain (void* arg)
|
||||
{
|
||||
function<void()> _doIt_{forwardInitialiser<FUN> (arg)};
|
||||
using Fun= typename lib::meta::_Fun<FUN>::Functor;
|
||||
Fun _doIt_{forwardInitialiser<FUN> (arg)};
|
||||
|
||||
lumiera_thread_sync (); // sync point: arguments handed over
|
||||
|
||||
|
|
|
|||
|
|
@ -104,13 +104,17 @@ namespace meta{
|
|||
template<typename FUN, typename SEL =void>
|
||||
struct _Fun
|
||||
: std::false_type
|
||||
{ };
|
||||
{
|
||||
using Functor = FUN;
|
||||
};
|
||||
|
||||
/** Specialisation for function objects and lambdas */
|
||||
template<typename FUN>
|
||||
struct _Fun<FUN, enable_if<has_FunctionOperator<FUN>> >
|
||||
: _Fun<decltype(&FUN::operator())>
|
||||
{ };
|
||||
{
|
||||
using Functor = FUN;
|
||||
};
|
||||
|
||||
/** Specialisation for a bare function signature */
|
||||
template<typename RET, typename...ARGS>
|
||||
|
|
@ -120,6 +124,7 @@ namespace meta{
|
|||
using Ret = RET;
|
||||
using Args = Types<ARGS...>;
|
||||
using Sig = RET(ARGS...);
|
||||
using Functor = std::function<Sig>;
|
||||
};
|
||||
/** Specialisation for using a function pointer */
|
||||
template<typename SIG>
|
||||
|
|
|
|||
Loading…
Reference in a new issue