2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/config.h"
|
|
|
|
|
|
|
|
#ifdef TD_THREAD_PTHREAD
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/Destructor.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/invoke.h"
|
|
|
|
#include "td/utils/MovableValue.h"
|
|
|
|
#include "td/utils/port/detail/ThreadIdGuard.h"
|
|
|
|
#include "td/utils/port/thread_local.h"
|
2019-07-06 13:29:15 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <tuple>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
|
2019-08-26 14:35:08 +02:00
|
|
|
#include <sys/types.h>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
|
|
|
class ThreadPthread {
|
|
|
|
public:
|
|
|
|
ThreadPthread() = default;
|
|
|
|
ThreadPthread(const ThreadPthread &other) = delete;
|
|
|
|
ThreadPthread &operator=(const ThreadPthread &other) = delete;
|
2019-09-28 04:14:21 +02:00
|
|
|
ThreadPthread(ThreadPthread &&other) noexcept : is_inited_(std::move(other.is_inited_)), thread_(other.thread_) {
|
2019-09-09 01:04:30 +02:00
|
|
|
}
|
2021-10-18 13:36:15 +02:00
|
|
|
ThreadPthread &operator=(ThreadPthread &&other) noexcept {
|
2018-09-11 16:55:00 +02:00
|
|
|
join();
|
|
|
|
is_inited_ = std::move(other.is_inited_);
|
|
|
|
thread_ = other.thread_;
|
2018-11-03 20:36:52 +01:00
|
|
|
return *this;
|
2018-09-11 16:55:00 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class Function, class... Args>
|
2021-12-09 22:27:13 +01:00
|
|
|
explicit ThreadPthread(Function &&f, Args &&...args) {
|
2018-09-11 16:55:00 +02:00
|
|
|
auto func = create_destructor([args = std::make_tuple(decay_copy(std::forward<Function>(f)),
|
|
|
|
decay_copy(std::forward<Args>(args))...)]() mutable {
|
|
|
|
invoke_tuple(std::move(args));
|
|
|
|
clear_thread_locals();
|
|
|
|
});
|
2019-08-26 16:44:30 +02:00
|
|
|
do_pthread_create(&thread_, nullptr, run_thread, func.release());
|
2018-12-31 20:04:05 +01:00
|
|
|
is_inited_ = true;
|
|
|
|
}
|
2019-08-26 16:44:30 +02:00
|
|
|
~ThreadPthread() {
|
|
|
|
join();
|
|
|
|
}
|
|
|
|
|
2019-08-26 14:35:08 +02:00
|
|
|
void set_name(CSlice name);
|
2019-08-26 16:44:30 +02:00
|
|
|
|
2019-08-26 14:35:08 +02:00
|
|
|
void join();
|
2018-09-11 16:55:00 +02:00
|
|
|
|
2019-08-26 14:35:08 +02:00
|
|
|
void detach();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-08-26 14:35:08 +02:00
|
|
|
static unsigned hardware_concurrency();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
using id = pthread_t;
|
|
|
|
|
|
|
|
private:
|
|
|
|
MovableValue<bool> is_inited_;
|
|
|
|
pthread_t thread_;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
std::decay_t<T> decay_copy(T &&v) {
|
|
|
|
return std::forward<T>(v);
|
|
|
|
}
|
|
|
|
|
2021-10-18 14:37:21 +02:00
|
|
|
static int do_pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *),
|
|
|
|
void *arg);
|
2019-08-26 14:35:08 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static void *run_thread(void *ptr) {
|
|
|
|
ThreadIdGuard thread_id_guard;
|
2018-09-27 03:19:03 +02:00
|
|
|
auto func = unique_ptr<Destructor>(static_cast<Destructor *>(ptr));
|
2018-12-31 20:04:05 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace this_thread_pthread {
|
2019-08-26 14:35:08 +02:00
|
|
|
void yield();
|
|
|
|
ThreadPthread::id get_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace this_thread_pthread
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
#endif
|