25#if !defined(TIMEMORY_LIBRARY_SOURCE)
26# define TIMEMORY_LIBRARY_SOURCE 1
29#include "timemory/backends/threading.hpp"
39#if !defined(TIMEMORY_WINDOWS)
53 TIMEMORY_VISIBILITY(
"default");
57 return tim::get_env(
"TIMEMORY_ENABLE_PTHREAD_GOTCHA_WRAPPER",
false);
65 template <
size_t... Idx>
69 std::array<std::function<void()>,
sizeof...(Idx)> _data{};
78 typedef void* (*routine_t)(
void*);
80 TIMEMORY_DEFAULT_OBJECT(wrapper)
82 wrapper(routine_t _routine,
void* _arg,
bool _debug)
88 void* operator()()
const {
return m_routine(m_arg); }
90 bool debug()
const {
return m_debug; }
91 static void* wrap(
void* _arg)
97 auto _tid = tim::threading::get_id();
99 wrapper* _wrapper =
static_cast<wrapper*
>(_arg);
100 if(_wrapper->debug())
101 PRINT_HERE(
"[T%li] Creating timemory manager", (
long int) _tid);
104# if defined(TIMEMORY_INTERNAL_TESTING)
106 throw std::runtime_error(
"nullptr to manager instance");
113 if(_wrapper->debug())
114 PRINT_HERE(
"[T%li] Executing original function", (
long int) _tid);
116 auto*
_ret = (*_wrapper)();
117 if(_wrapper->debug())
118 PRINT_HERE(
"[T%li] Executing finalizing callbacks", (
long int) _tid);
120 for(
auto& itr : _final)
122 if(_wrapper->debug())
123 PRINT_HERE(
"[T%li] Returning from thread", (
long int) _tid);
129 routine_t m_routine =
nullptr;
130 void* m_arg =
nullptr;
131 bool m_debug =
false;
135 int operator()(pthread_t* thread,
const pthread_attr_t* attr,
136 void* (*start_routine)(
void*),
void* arg)
139 auto _tid = tim::threading::get_id();
140 auto*
_settings = tim::settings::instance<TIMEMORY_API>();
143 PRINT_HERE(
"[T%li] Creating new thread", (
long int) _tid);
144 auto* _obj =
new wrapper(start_routine, arg, _debug);
145 return pthread_create(thread, attr, &wrapper::wrap,
static_cast<void*
>(_obj));
149 template <
size_t Idx,
size_t N>
150 static void get_storage_impl(std::array<std::function<
void()>, N>& _data)
172# if defined(TIMEMORY_USE_GOTCHA)
179 return std::make_shared<pthread_bundle_t>();
181 return std::shared_ptr<pthread_bundle_t>{};
195auto pthread_gotcha_handle =
false;
This is a variadic component wrapper where all components are allocated on the stack and cannot be di...
static pointer_t instance()
Get a shared pointer to the instance for the current thread.
#define TIMEMORY_WEAK_PREFIX
#define TIMEMORY_WEAK_POSTFIX
#define TIMEMORY_C_GOTCHA(...)
typename enumerator< Idx >::type enumerator_t
const hash_alias_ptr_t hash_value_t std::string *& _ret
std::make_integer_sequence< size_t, Num > make_index_sequence
Alias template make_index_sequence.
Tp get_env(const std::string &env_id, Tp _default, bool _store)
char argparse::argument_parser tim::settings * _settings
std::integer_sequence< size_t, Idx... > index_sequence
Alias template index_sequence.
TIMEMORY_WEAK_PREFIX bool timemory_enable_pthread_gotcha_wrapper() TIMEMORY_WEAK_POSTFIX
auto setup_pthread_gotcha()
Definition for various functions for storage_initializer in operations.
static base_storage_type * get_storage()
The gotcha component rewrites the global offset table such that calling the wrapped function actually...
static get_initializer_t & get_initializer()
#define TIMEMORY_FOLD_EXPRESSION(...)