2019-08-26 14:35:08 +02:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2019-08-26 14:35:08 +02: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)
|
|
|
|
//
|
|
|
|
#include "td/utils/port/detail/ThreadPthread.h"
|
|
|
|
|
2019-08-26 16:44:30 +02:00
|
|
|
char disable_linker_warning_about_empty_file_thread_pthread_cpp TD_UNUSED;
|
|
|
|
|
2019-08-26 14:35:08 +02:00
|
|
|
#if TD_THREAD_PTHREAD
|
2019-08-26 16:44:30 +02:00
|
|
|
|
|
|
|
#include "td/utils/misc.h"
|
2022-09-14 14:33:16 +02:00
|
|
|
#include "td/utils/port/detail/skip_eintr.h"
|
2022-09-17 20:48:16 +02:00
|
|
|
#if TD_NETBSD
|
|
|
|
#include "td/utils/ScopeGuard.h"
|
|
|
|
#endif
|
2019-08-26 16:44:30 +02:00
|
|
|
|
2019-08-26 14:35:08 +02:00
|
|
|
#include <pthread.h>
|
2022-09-17 13:45:42 +02:00
|
|
|
#if TD_FREEBSD
|
|
|
|
#include <pthread_np.h>
|
|
|
|
#endif
|
2019-08-26 14:35:08 +02:00
|
|
|
#include <sched.h>
|
2022-08-21 23:46:22 +02:00
|
|
|
#include <signal.h>
|
2022-09-17 13:45:42 +02:00
|
|
|
#if TD_FREEBSD
|
|
|
|
#include <sys/cpuset.h>
|
|
|
|
#endif
|
2019-09-24 11:27:42 +02:00
|
|
|
#if TD_FREEBSD || TD_OPENBSD || TD_NETBSD
|
2019-08-26 14:35:08 +02:00
|
|
|
#include <sys/sysctl.h>
|
2019-09-24 11:27:42 +02:00
|
|
|
#endif
|
2019-08-26 16:44:30 +02:00
|
|
|
#include <unistd.h>
|
2019-08-26 14:35:08 +02:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
|
|
|
unsigned ThreadPthread::hardware_concurrency() {
|
2019-08-26 16:44:30 +02:00
|
|
|
// Linux and macOS
|
2019-08-26 14:35:08 +02:00
|
|
|
#if defined(_SC_NPROCESSORS_ONLN)
|
|
|
|
{
|
|
|
|
auto res = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
if (res > 0) {
|
|
|
|
return narrow_cast<unsigned>(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-10-23 04:00:04 +02:00
|
|
|
#if TD_FREEBSD || TD_OPENBSD || TD_NETBSD
|
2019-08-26 14:35:08 +02:00
|
|
|
#if defined(HW_AVAILCPU) && defined(CTL_HW)
|
|
|
|
{
|
2019-08-26 16:44:30 +02:00
|
|
|
int mib[2] = {CTL_HW, HW_AVAILCPU};
|
2019-08-26 14:35:08 +02:00
|
|
|
int res{0};
|
|
|
|
size_t len = sizeof(res);
|
2019-08-26 16:44:30 +02:00
|
|
|
if (sysctl(mib, 2, &res, &len, nullptr, 0) == 0 && res != 0) {
|
2019-08-26 14:35:08 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HW_NCPU) && defined(CTL_HW)
|
|
|
|
{
|
2019-08-26 16:44:30 +02:00
|
|
|
int mib[2] = {CTL_HW, HW_NCPU};
|
2019-08-26 14:35:08 +02:00
|
|
|
int res{0};
|
|
|
|
size_t len = sizeof(res);
|
2019-08-26 16:44:30 +02:00
|
|
|
if (sysctl(mib, 2, &res, &len, nullptr, 0) == 0 && res != 0) {
|
2019-08-26 14:35:08 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2019-10-23 04:00:04 +02:00
|
|
|
#endif
|
2019-08-26 14:35:08 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Just in case
|
|
|
|
return 8;
|
|
|
|
}
|
2019-08-26 16:44:30 +02:00
|
|
|
|
2019-08-26 14:35:08 +02:00
|
|
|
void ThreadPthread::set_name(CSlice name) {
|
|
|
|
#if defined(_GNU_SOURCE) && defined(__GLIBC_PREREQ)
|
|
|
|
#if __GLIBC_PREREQ(2, 12)
|
|
|
|
pthread_setname_np(thread_, name.c_str());
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
2019-08-26 16:44:30 +02:00
|
|
|
|
2019-08-26 14:35:08 +02:00
|
|
|
void ThreadPthread::join() {
|
|
|
|
if (is_inited_.get()) {
|
|
|
|
is_inited_ = false;
|
|
|
|
pthread_join(thread_, nullptr);
|
|
|
|
}
|
|
|
|
}
|
2019-08-26 16:44:30 +02:00
|
|
|
|
2019-08-26 14:35:08 +02:00
|
|
|
void ThreadPthread::detach() {
|
|
|
|
if (is_inited_.get()) {
|
|
|
|
is_inited_ = false;
|
|
|
|
pthread_detach(thread_);
|
|
|
|
}
|
|
|
|
}
|
2019-08-26 16:44:30 +02:00
|
|
|
|
2022-08-21 23:46:22 +02:00
|
|
|
void ThreadPthread::send_real_time_signal(id thread_id, int real_time_signal_number) {
|
|
|
|
#ifdef SIGRTMIN
|
|
|
|
pthread_kill(thread_id, SIGRTMIN + real_time_signal_number);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-09-08 02:49:16 +02:00
|
|
|
int ThreadPthread::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
|
|
|
return pthread_create(thread, attr, start_routine, arg);
|
|
|
|
}
|
2019-08-26 16:44:30 +02:00
|
|
|
|
2022-09-17 20:58:00 +02:00
|
|
|
#if TD_HAVE_THREAD_AFFINITY
|
2022-09-14 13:38:14 +02:00
|
|
|
Status ThreadPthread::set_affinity_mask(id thread_id, uint64 mask) {
|
2022-09-17 13:45:42 +02:00
|
|
|
#if TD_LINUX || TD_FREEBSD
|
|
|
|
#if TD_FREEBSD
|
|
|
|
cpuset_t cpuset;
|
|
|
|
#else
|
2022-09-14 14:33:16 +02:00
|
|
|
cpu_set_t cpuset;
|
2022-09-17 13:45:42 +02:00
|
|
|
#endif
|
2022-09-14 14:33:16 +02:00
|
|
|
CPU_ZERO(&cpuset);
|
|
|
|
for (int j = 0; j < 64 && j < CPU_SETSIZE; j++) {
|
|
|
|
if ((mask >> j) & 1) {
|
|
|
|
CPU_SET(j, &cpuset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-17 13:45:42 +02:00
|
|
|
auto res = skip_eintr([&] { return pthread_setaffinity_np(thread_id, sizeof(cpuset), &cpuset); });
|
2022-09-14 14:33:16 +02:00
|
|
|
if (res) {
|
|
|
|
return OS_ERROR("Failed to set thread affinity mask");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
2022-09-17 20:48:16 +02:00
|
|
|
#elif TD_NETBSD
|
|
|
|
cpuset_t *cpuset = cpuset_create();
|
|
|
|
if (cpuset == nullptr) {
|
|
|
|
return OS_ERROR("Failed to create cpuset");
|
|
|
|
}
|
|
|
|
SCOPE_EXIT {
|
|
|
|
cpuset_destroy(cpuset);
|
|
|
|
};
|
|
|
|
for (int j = 0; j < 64; j++) {
|
|
|
|
if ((mask >> j) & 1) {
|
|
|
|
if (cpuset_set(j, cpuset) != 0) {
|
|
|
|
return OS_ERROR("Failed to set CPU identifier");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto res = skip_eintr([&] { return pthread_setaffinity_np(thread_id, cpuset_size(cpuset), cpuset); });
|
|
|
|
if (res) {
|
|
|
|
return OS_ERROR("Failed to set thread affinity mask");
|
|
|
|
}
|
2022-09-17 22:25:42 +02:00
|
|
|
if (get_affinity_mask(thread_id) != mask) {
|
|
|
|
return Status::Error("Failed to set exact thread affinity mask");
|
|
|
|
}
|
2022-09-17 20:48:16 +02:00
|
|
|
return Status::OK();
|
2022-09-14 14:33:16 +02:00
|
|
|
#else
|
2022-09-14 13:38:14 +02:00
|
|
|
return Status::Error("Unsupported");
|
2022-09-14 14:33:16 +02:00
|
|
|
#endif
|
2022-09-14 13:38:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64 ThreadPthread::get_affinity_mask(id thread_id) {
|
2022-09-17 13:45:42 +02:00
|
|
|
#if TD_LINUX || TD_FREEBSD
|
|
|
|
#if TD_FREEBSD
|
|
|
|
cpuset_t cpuset;
|
|
|
|
#else
|
2022-09-14 14:33:16 +02:00
|
|
|
cpu_set_t cpuset;
|
2022-09-17 13:45:42 +02:00
|
|
|
#endif
|
2022-09-14 14:33:16 +02:00
|
|
|
CPU_ZERO(&cpuset);
|
2022-09-17 13:45:42 +02:00
|
|
|
auto res = skip_eintr([&] { return pthread_getaffinity_np(thread_id, sizeof(cpuset), &cpuset); });
|
2022-09-14 14:33:16 +02:00
|
|
|
if (res) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 mask = 0;
|
|
|
|
for (int j = 0; j < 64 && j < CPU_SETSIZE; j++) {
|
|
|
|
if (CPU_ISSET(j, &cpuset)) {
|
|
|
|
mask |= static_cast<uint64>(1) << j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mask;
|
2022-09-17 20:48:16 +02:00
|
|
|
#elif TD_NETBSD
|
|
|
|
cpuset_t *cpuset = cpuset_create();
|
|
|
|
if (cpuset == nullptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
SCOPE_EXIT {
|
|
|
|
cpuset_destroy(cpuset);
|
|
|
|
};
|
|
|
|
auto res = skip_eintr([&] { return pthread_getaffinity_np(thread_id, cpuset_size(cpuset), cpuset); });
|
|
|
|
if (res) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 mask = 0;
|
|
|
|
for (int j = 0; j < 64; j++) {
|
|
|
|
if (cpuset_isset(j, cpuset) > 0) {
|
|
|
|
mask |= static_cast<uint64>(1) << j;
|
|
|
|
}
|
|
|
|
}
|
2022-09-17 22:25:42 +02:00
|
|
|
if (mask == 0) {
|
|
|
|
// the mask wasn't set, all CPUs are allowed
|
|
|
|
auto proc_count = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
for (int j = 0; j < 64 && j < proc_count; j++) {
|
|
|
|
mask |= static_cast<uint64>(1) << j;
|
|
|
|
}
|
|
|
|
}
|
2022-09-17 20:48:16 +02:00
|
|
|
return mask;
|
2022-09-14 14:33:16 +02:00
|
|
|
#else
|
2022-09-14 13:38:14 +02:00
|
|
|
return 0;
|
2022-09-14 14:33:16 +02:00
|
|
|
#endif
|
2022-09-14 13:38:14 +02:00
|
|
|
}
|
2022-09-17 20:58:00 +02:00
|
|
|
#endif
|
2022-09-14 13:38:14 +02:00
|
|
|
|
2019-08-26 14:35:08 +02:00
|
|
|
namespace this_thread_pthread {
|
|
|
|
ThreadPthread::id get_id() {
|
|
|
|
return pthread_self();
|
|
|
|
}
|
|
|
|
} // namespace this_thread_pthread
|
2019-08-26 16:44:30 +02:00
|
|
|
|
2019-08-26 14:35:08 +02:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace td
|
|
|
|
#endif
|