1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

pow-testgiver support

This commit is contained in:
ton 2020-07-06 17:07:20 +03:00
parent dbde9c1c40
commit f064b1047a
257 changed files with 6665 additions and 2608 deletions

View file

@ -26,6 +26,8 @@ char disable_linker_warning_about_empty_file_epoll_cpp TD_UNUSED;
#include "td/utils/logging.h"
#include "td/utils/Status.h"
#include <cerrno>
#include <unistd.h>
namespace td {
@ -82,7 +84,8 @@ void Epoll::unsubscribe(PollableFdRef fd_ref) {
int err = epoll_ctl(epoll_fd_.fd(), EPOLL_CTL_DEL, native_fd, nullptr);
auto epoll_ctl_errno = errno;
LOG_IF(FATAL, err == -1) << Status::PosixError(epoll_ctl_errno, "epoll_ctl DEL failed")
<< ", epoll_fd = " << epoll_fd_.fd() << ", fd = " << native_fd << fd.native_fd().validate();
<< ", epoll_fd = " << epoll_fd_.fd() << ", fd = " << native_fd
<< ", status = " << fd.native_fd().validate();
}
void Epoll::unsubscribe_before_close(PollableFdRef fd) {

View file

@ -28,6 +28,8 @@ char disable_linker_warning_about_empty_file_event_fd_bsd_cpp TD_UNUSED;
#include "td/utils/port/SocketFd.h"
#include "td/utils/Slice.h"
#include <cerrno>
#include <fcntl.h>
#include <poll.h>
#include <sys/socket.h>

View file

@ -25,10 +25,13 @@ char disable_linker_warning_about_empty_file_event_fd_linux_cpp TD_UNUSED;
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/port/detail/NativeFd.h"
#include "td/utils/port/detail/skip_eintr.h"
#include "td/utils/port/PollFlags.h"
#include "td/utils/ScopeGuard.h"
#include "td/utils/Slice.h"
#include <cerrno>
#include <poll.h>
#include <sys/eventfd.h>
#include <unistd.h>

View file

@ -25,6 +25,7 @@ char disable_linker_warning_about_empty_file_kqueue_cpp TD_UNUSED;
#include "td/utils/logging.h"
#include "td/utils/Status.h"
#include <cerrno>
#include <utility>
#include <sys/time.h>

View file

@ -38,7 +38,9 @@ namespace td {
class FdSet {
public:
void on_create_fd(NativeFd::Fd fd) {
CHECK(is_valid(fd));
if (!is_valid(fd)) {
return;
}
if (is_stdio(fd)) {
return;
}
@ -64,7 +66,9 @@ class FdSet {
}
void on_close_fd(NativeFd::Fd fd) {
CHECK(is_valid(fd));
if (!is_valid(fd)) {
return;
}
if (is_stdio(fd)) {
return;
}
@ -112,7 +116,7 @@ FdSet &get_fd_set() {
Status NativeFd::validate() const {
#if TD_FD_DEBUG
return get_fd_set().validate(fd_.get());
return get_fd_set().validate(fd_);
#else
return Status::OK();
#endif
@ -121,13 +125,13 @@ Status NativeFd::validate() const {
NativeFd::NativeFd(Fd fd) : fd_(fd) {
VLOG(fd) << *this << " create";
#if TD_FD_DEBUG
get_fd_set().on_create_fd(fd_.get());
get_fd_set().on_create_fd(fd_);
#endif
}
NativeFd::NativeFd(Fd fd, bool nolog) : fd_(fd) {
#if TD_FD_DEBUG
get_fd_set().on_create_fd(fd_.get());
get_fd_set().on_create_fd(fd_);
#endif
}
@ -135,18 +139,26 @@ NativeFd::NativeFd(Fd fd, bool nolog) : fd_(fd) {
NativeFd::NativeFd(Socket socket) : fd_(reinterpret_cast<Fd>(socket)), is_socket_(true) {
VLOG(fd) << *this << " create";
#if TD_FD_DEBUG
get_fd_set().on_create_fd(fd_.get());
get_fd_set().on_create_fd(fd_);
#endif
}
#endif
NativeFd &NativeFd::operator=(NativeFd &&from) {
CHECK(this != &from);
close();
fd_ = std::move(from.fd_);
NativeFd::NativeFd(NativeFd &&other) : fd_(other.fd_) {
#if TD_PORT_WINDOWS
is_socket_ = from.is_socket_;
is_socket_ = other.is_socket_;
#endif
other.fd_ = empty_fd();
}
NativeFd &NativeFd::operator=(NativeFd &&other) {
CHECK(this != &other);
close();
fd_ = other.fd_;
#if TD_PORT_WINDOWS
is_socket_ = other.is_socket_;
#endif
other.fd_ = empty_fd();
return *this;
}
@ -155,7 +167,7 @@ NativeFd::~NativeFd() {
}
NativeFd::operator bool() const {
return fd_.get() != empty_fd();
return fd_ != empty_fd();
}
NativeFd::Fd NativeFd::empty_fd() {
@ -167,7 +179,7 @@ NativeFd::Fd NativeFd::empty_fd() {
}
NativeFd::Fd NativeFd::fd() const {
return fd_.get();
return fd_;
}
NativeFd::Socket NativeFd::socket() const {
@ -175,7 +187,7 @@ NativeFd::Socket NativeFd::socket() const {
return fd();
#elif TD_PORT_WINDOWS
CHECK(is_socket_);
return reinterpret_cast<Socket>(fd_.get());
return reinterpret_cast<Socket>(fd_);
#endif
}
@ -239,13 +251,13 @@ void NativeFd::close() {
auto error = OS_ERROR("Close fd");
LOG(ERROR) << error;
}
fd_ = {};
fd_ = empty_fd();
}
NativeFd::Fd NativeFd::release() {
VLOG(fd) << *this << " release";
auto res = fd_.get();
fd_ = {};
auto res = fd_;
fd_ = empty_fd();
#if TD_FD_DEBUG
get_fd_set().on_close_fd(res);
#endif

View file

@ -21,7 +21,6 @@
#include "td/utils/port/config.h"
#include "td/utils/common.h"
#include "td/utils/MovableValue.h"
#include "td/utils/Status.h"
#include "td/utils/StringBuilder.h"
@ -37,8 +36,6 @@ class NativeFd {
using Socket = SOCKET;
#endif
NativeFd() = default;
NativeFd(NativeFd &&) = default;
NativeFd &operator=(NativeFd &&);
explicit NativeFd(Fd fd);
NativeFd(Fd fd, bool nolog);
#if TD_PORT_WINDOWS
@ -46,12 +43,12 @@ class NativeFd {
#endif
NativeFd(const NativeFd &) = delete;
NativeFd &operator=(const NativeFd &) = delete;
NativeFd(NativeFd &&other);
NativeFd &operator=(NativeFd &&other);
~NativeFd();
explicit operator bool() const;
static Fd empty_fd();
Fd fd() const;
Socket socket() const;
@ -67,10 +64,10 @@ class NativeFd {
Status validate() const;
private:
#if TD_PORT_POSIX
MovableValue<Fd, -1> fd_;
#elif TD_PORT_WINDOWS
MovableValue<Fd, INVALID_HANDLE_VALUE> fd_;
static Fd empty_fd();
Fd fd_ = empty_fd();
#if TD_PORT_WINDOWS
bool is_socket_{false};
#endif
};

View file

@ -28,6 +28,8 @@ char disable_linker_warning_about_empty_file_poll_cpp TD_UNUSED;
#include "td/utils/ScopeGuard.h"
#include "td/utils/Status.h"
#include <cerrno>
namespace td {
namespace detail {

View file

@ -29,7 +29,6 @@
#include <atomic>
#include <memory>
#include <type_traits>
namespace td {
@ -220,30 +219,6 @@ inline const NativeFd &PollableFd::native_fd() const {
return fd_info_->native_fd();
}
#if TD_PORT_POSIX
namespace detail {
template <class F>
auto skip_eintr(F &&f) {
decltype(f()) res;
static_assert(std::is_integral<decltype(res)>::value, "integral type expected");
do {
errno = 0; // just in case
res = f();
} while (res < 0 && errno == EINTR);
return res;
}
template <class F>
auto skip_eintr_cstr(F &&f) {
char *res;
do {
errno = 0; // just in case
res = f();
} while (res == nullptr && errno == EINTR);
return res;
}
} // namespace detail
#endif
template <class FdT>
bool can_read(const FdT &fd) {
return fd.get_poll_info().get_flags().can_read() || fd.get_poll_info().get_flags().has_pending_error();

View file

@ -0,0 +1,114 @@
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#include "td/utils/port/detail/ThreadPthread.h"
char disable_linker_warning_about_empty_file_thread_pthread_cpp TD_UNUSED;
#if TD_THREAD_PTHREAD
#include "td/utils/misc.h"
#include <pthread.h>
#include <sched.h>
#if TD_FREEBSD || TD_OPENBSD || TD_NETBSD
#include <sys/sysctl.h>
#endif
#include <unistd.h>
namespace td {
namespace detail {
unsigned ThreadPthread::hardware_concurrency() {
// Linux and macOS
#if defined(_SC_NPROCESSORS_ONLN)
{
auto res = sysconf(_SC_NPROCESSORS_ONLN);
if (res > 0) {
return narrow_cast<unsigned>(res);
}
}
#endif
#if TD_FREEBSD || TD_OPENBSD || TD_NETBSD
#if defined(HW_AVAILCPU) && defined(CTL_HW)
{
int mib[2] = {CTL_HW, HW_AVAILCPU};
int res{0};
size_t len = sizeof(res);
if (sysctl(mib, 2, &res, &len, nullptr, 0) == 0 && res != 0) {
return res;
}
}
#endif
#if defined(HW_NCPU) && defined(CTL_HW)
{
int mib[2] = {CTL_HW, HW_NCPU};
int res{0};
size_t len = sizeof(res);
if (sysctl(mib, 2, &res, &len, nullptr, 0) == 0 && res != 0) {
return res;
}
}
#endif
#endif
// Just in case
return 8;
}
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
}
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 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();
}
ThreadPthread::id get_id() {
return pthread_self();
}
} // namespace this_thread_pthread
} // namespace detail
} // namespace td
#endif

View file

@ -34,8 +34,7 @@
#include <type_traits>
#include <utility>
#include <pthread.h>
#include <sched.h>
#include <sys/types.h>
namespace td {
namespace detail {
@ -44,7 +43,8 @@ class ThreadPthread {
ThreadPthread() = default;
ThreadPthread(const ThreadPthread &other) = delete;
ThreadPthread &operator=(const ThreadPthread &other) = delete;
ThreadPthread(ThreadPthread &&) = default;
ThreadPthread(ThreadPthread &&other) noexcept : is_inited_(std::move(other.is_inited_)), thread_(other.thread_) {
}
ThreadPthread &operator=(ThreadPthread &&other) {
join();
is_inited_ = std::move(other.is_inited_);
@ -58,36 +58,20 @@ 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) {
#if defined(_GNU_SOURCE) && defined(__GLIBC_PREREQ)
#if __GLIBC_PREREQ(2, 12)
pthread_setname_np(thread_, name.c_str());
#endif
#endif
}
void join() {
if (is_inited_.get()) {
is_inited_ = false;
pthread_join(thread_, nullptr);
}
}
void detach() {
if (is_inited_.get()) {
is_inited_ = false;
pthread_detach(thread_);
}
}
~ThreadPthread() {
join();
}
static unsigned hardware_concurrency() {
return 8;
}
void set_name(CSlice name);
void join();
void detach();
static unsigned hardware_concurrency();
using id = pthread_t;
@ -100,6 +84,8 @@ class ThreadPthread {
return std::forward<T>(v);
}
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));
@ -108,12 +94,8 @@ class ThreadPthread {
};
namespace this_thread_pthread {
inline void yield() {
sched_yield();
}
inline ThreadPthread::id get_id() {
return pthread_self();
}
void yield();
ThreadPthread::id get_id();
} // namespace this_thread_pthread
} // namespace detail
} // namespace td

View file

@ -0,0 +1,52 @@
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#pragma once
#include <cerrno>
#include <type_traits>
namespace td {
#if TD_PORT_POSIX
namespace detail {
template <class F>
auto skip_eintr(F &&f) {
decltype(f()) res;
static_assert(std::is_integral<decltype(res)>::value, "integral type expected");
do {
errno = 0; // just in case
res = f();
} while (res < 0 && errno == EINTR);
return res;
}
template <class F>
auto skip_eintr_cstr(F &&f) {
char *res;
do {
errno = 0; // just in case
res = f();
} while (res == nullptr && errno == EINTR);
return res;
}
} // namespace detail
#endif
} // namespace td