tdlight/tdutils/td/utils/port/detail/ThreadPthread.h

114 lines
2.8 KiB
C
Raw Normal View History

//
2022-01-01 01:35:39 +01:00
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// 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
#include "td/utils/port/config.h"
#ifdef TD_THREAD_PTHREAD
#include "td/utils/common.h"
#include "td/utils/Destructor.h"
#include "td/utils/invoke.h"
#include "td/utils/MovableValue.h"
#include "td/utils/port/detail/ThreadIdGuard.h"
#include "td/utils/port/thread_local.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include <tuple>
#include <type_traits>
#include <utility>
#if TD_OPENBSD || TD_SOLARIS
#include <pthread.h>
#endif
#include <sys/types.h>
2022-09-17 20:58:00 +02:00
#if TD_LINUX || TD_FREEBSD || TD_NETBSD
#define TD_HAVE_THREAD_AFFINITY 1
#endif
namespace td {
namespace detail {
2022-08-21 23:46:22 +02:00
class ThreadPthread {
public:
ThreadPthread() = default;
ThreadPthread(const ThreadPthread &other) = delete;
ThreadPthread &operator=(const ThreadPthread &other) = delete;
ThreadPthread(ThreadPthread &&other) noexcept : is_inited_(std::move(other.is_inited_)), thread_(other.thread_) {
}
ThreadPthread &operator=(ThreadPthread &&other) noexcept {
join();
is_inited_ = std::move(other.is_inited_);
thread_ = other.thread_;
return *this;
}
template <class Function, class... Args>
2021-12-09 22:27:13 +01:00
explicit ThreadPthread(Function &&f, Args &&...args) {
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();
});
do_pthread_create(&thread_, nullptr, run_thread, func.release());
is_inited_ = true;
}
~ThreadPthread() {
join();
}
void set_name(CSlice name);
void join();
void detach();
static unsigned hardware_concurrency();
using id = pthread_t;
2022-10-04 21:48:55 +02:00
id get_id() noexcept {
return thread_;
}
2022-08-21 23:46:22 +02:00
static void send_real_time_signal(id thread_id, int real_time_signal_number);
2022-09-17 20:58:00 +02:00
#if TD_HAVE_THREAD_AFFINITY
static Status set_affinity_mask(id thread_id, uint64 mask);
static uint64 get_affinity_mask(id thread_id);
2022-09-17 20:58:00 +02:00
#endif
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);
static void *run_thread(void *ptr) {
ThreadIdGuard thread_id_guard;
auto func = unique_ptr<Destructor>(static_cast<Destructor *>(ptr));
return nullptr;
}
};
namespace this_thread_pthread {
ThreadPthread::id get_id();
} // namespace this_thread_pthread
2022-08-21 23:46:22 +02:00
} // namespace detail
} // namespace td
#endif