ThreadPthread fixes.

GitOrigin-RevId: 5c56a443f95beda913c3b1b46eacbfc068271c0e
This commit is contained in:
levlam 2019-08-26 17:44:30 +03:00
parent 80d96550ba
commit 22f4507033
2 changed files with 25 additions and 19 deletions

View File

@ -6,16 +6,21 @@
//
#include "td/utils/port/detail/ThreadPthread.h"
char disable_linker_warning_about_empty_file_thread_pthread_cpp TD_UNUSED;
#if TD_THREAD_PTHREAD
#include <unistd.h>
#include "td/utils/misc.h"
#include <pthread.h>
#include <sched.h>
#include <sys/sysctl.h>
#include <unistd.h>
namespace td {
namespace detail {
unsigned ThreadPthread::hardware_concurrency() {
// Linux and MacOs
// Linux and macOS
#if defined(_SC_NPROCESSORS_ONLN)
{
auto res = sysconf(_SC_NPROCESSORS_ONLN);
@ -28,13 +33,10 @@ unsigned ThreadPthread::hardware_concurrency() {
// *BSD
#if defined(HW_AVAILCPU) && defined(CTL_HW)
{
int mib[2];
int mib[2] = {CTL_HW, HW_AVAILCPU};
int res{0};
size_t len = sizeof(res);
mib[0] = CTL_HW;
mib[1] = HW_AVAILCPU;
len = sizeof(res);
if (sysctl(mib, 2, &res, &len, NULL, 0) == 0 && res != 0) {
if (sysctl(mib, 2, &res, &len, nullptr, 0) == 0 && res != 0) {
return res;
}
}
@ -42,13 +44,10 @@ unsigned ThreadPthread::hardware_concurrency() {
#if defined(HW_NCPU) && defined(CTL_HW)
{
int mib[2];
int mib[2] = {CTL_HW, HW_NCPU};
int res{0};
size_t len = sizeof(res);
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
len = sizeof(res);
if (sysctl(mib, 2, &res, &len, NULL, 0) == 0 && res != 0) {
if (sysctl(mib, 2, &res, &len, nullptr, 0) == 0 && res != 0) {
return res;
}
}
@ -57,6 +56,7 @@ unsigned ThreadPthread::hardware_concurrency() {
// Just in case
return 8;
}
void ThreadPthread::set_name(CSlice name) {
#if defined(_GNU_SOURCE) && defined(__GLIBC_PREREQ)
#if __GLIBC_PREREQ(2, 12)
@ -64,21 +64,25 @@ void ThreadPthread::set_name(CSlice name) {
#endif
#endif
}
void ThreadPthread::join() {
if (is_inited_.get()) {
is_inited_ = false;
pthread_join(thread_, nullptr);
}
}
void ThreadPthread::detach() {
if (is_inited_.get()) {
is_inited_ = false;
pthread_detach(thread_);
}
}
int do_pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) {
int ThreadPthread::do_pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) {
return pthread_create(thread, attr, start_routine, arg);
}
namespace this_thread_pthread {
void yield() {
sched_yield();
@ -87,6 +91,7 @@ ThreadPthread::id get_id() {
return pthread_self();
}
} // namespace this_thread_pthread
} // namespace detail
} // namespace td
#endif

View File

@ -17,7 +17,6 @@
#include "td/utils/port/detail/ThreadIdGuard.h"
#include "td/utils/port/thread_local.h"
#include "td/utils/Slice.h"
#include "td/utils/misc.h"
#include <tuple>
#include <type_traits>
@ -46,17 +45,19 @@ class ThreadPthread {
invoke_tuple(std::move(args));
clear_thread_locals();
});
pthread_create(&thread_, nullptr, run_thread, func.release());
do_pthread_create(&thread_, nullptr, run_thread, func.release());
is_inited_ = true;
}
void set_name(CSlice name);
void join();
void detach();
~ThreadPthread() {
join();
}
void set_name(CSlice name);
void join();
void detach();
static unsigned hardware_concurrency();
using id = pthread_t;