1 #ifndef __XBOT_MATLOGGER2_UTIL_THREAD_REPLACEMENT_H__ 2 #define __XBOT_MATLOGGER2_UTIL_THREAD_REPLACEMENT_H__ 11 namespace XBot {
namespace matlogger2
18 template <
typename Callable,
typename... Args>
19 thread(Callable&& callable, Args&&... args):
20 _main_func(
std::bind(callable, args...))
22 create(SCHED_OTHER, 10);
25 template <
typename Callable,
typename... Args>
26 thread(
int sched_policy,
int prio, Callable&& callable, Args&&... args):
27 _main_func(
std::bind(callable, args...))
29 create(sched_policy, prio);
34 int ret = pthread_join(_handle,
nullptr);
38 throw std::runtime_error(
"error while joining thread");
47 void create(
int sched_policy,
int prio)
50 pthread_attr_init (&attr);
51 pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
52 pthread_attr_setschedpolicy (&attr, sched_policy);
53 sched_param schedparam;
54 schedparam.sched_priority = prio;
55 pthread_attr_setschedparam(&attr, &schedparam);
56 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
58 int ret = pthread_create(&_handle, &attr, &thread::thread_main,
this);
62 throw std::runtime_error(
"unable to start thread");
65 pthread_attr_destroy(&attr);
68 static void * thread_main(
void * arg)
70 ((
thread *)arg)->_main_func();
75 std::function<void(void)> _main_func;
86 pthread_mutexattr_t attr;
87 pthread_mutexattr_init(&attr);
89 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
90 int ret = pthread_mutex_init(&_handle, &attr);
92 throw std::runtime_error(
"error initializing the mutex (" + std::to_string(ret) +
")");
95 pthread_mutexattr_destroy(&attr);
100 int ret = pthread_mutex_lock(&_handle);
102 throw std::runtime_error(
"error acquiring the mutex (" + std::to_string(ret) +
")");
108 int ret = pthread_mutex_trylock(&_handle);
113 throw std::runtime_error(
"error acquiring the mutex (" + std::to_string(ret) +
")");
120 int ret = pthread_mutex_unlock(&_handle);
122 throw std::runtime_error(
"error releasing the mutex (" + std::to_string(ret) +
")");
140 pthread_mutex_t _handle;
154 pthread_condattr_t attr;
155 pthread_condattr_init(&attr);
156 int ret_1 = pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
159 throw std::runtime_error(
"error in pthread_condattr_setpshared (" + std::to_string(ret_1) +
")");
162 int ret = pthread_cond_init(&_handle, &attr);
164 throw std::runtime_error(
"error initializing condition_variable (" + std::to_string(ret) +
")");
168 template <
typename Predicate>
169 void wait(std::unique_lock<mutex>& lock,
const Predicate& pred)
171 pthread_mutex_t *
mutex = lock.
mutex()->get_native_handle();
175 int ret = pthread_cond_wait(&_handle, mutex);
177 throw std::runtime_error(
"error in pthread_cond_wait (" + std::to_string(ret) +
")");
184 int ret = pthread_cond_signal(&_handle);
186 throw std::runtime_error(
"error in pthread_cond_signal (" + std::to_string(ret) +
")");
192 pthread_cond_t _handle;
void wait(std::unique_lock< mutex > &lock, const Predicate &pred)
pthread_mutex_t * get_native_handle()
thread(int sched_policy, int prio, Callable &&callable, Args &&...args)
thread(Callable &&callable, Args &&...args)