From 9ff9b30e0f7a338f9233cdff6fe26d0b600651d4 Mon Sep 17 00:00:00 2001 From: DustyBagel Date: Thu, 27 Jun 2024 17:45:13 -0500 Subject: [PATCH] Remove unneeded freeminer code. --- irr/src/CIrrDeviceSDL.cpp | 2 +- src/fm_porting.h | 96 -------------------------- src/log_types.cpp | 101 --------------------------- src/log_types.h | 55 --------------- src/network/lan.cpp | 11 +-- src/network/lan.h | 4 +- src/server/serverlist.cpp | 12 ---- src/threading/CMakeLists.txt | 1 - src/threading/thread_vector.cpp | 118 -------------------------------- src/threading/thread_vector.h | 37 ---------- 10 files changed, 9 insertions(+), 428 deletions(-) delete mode 100644 src/fm_porting.h delete mode 100644 src/log_types.cpp delete mode 100644 src/log_types.h delete mode 100644 src/threading/thread_vector.cpp delete mode 100644 src/threading/thread_vector.h diff --git a/irr/src/CIrrDeviceSDL.cpp b/irr/src/CIrrDeviceSDL.cpp index f8fb66da3..cc7505c72 100644 --- a/irr/src/CIrrDeviceSDL.cpp +++ b/irr/src/CIrrDeviceSDL.cpp @@ -656,7 +656,7 @@ bool CIrrDeviceSDL::run() { os::Timer::tick(); - SEvent irrevent; + SEvent irrevent{}; SDL_Event SDL_event; while (!Close && wrap_PollEvent(&SDL_event)) { diff --git a/src/fm_porting.h b/src/fm_porting.h deleted file mode 100644 index 43ca4340f..000000000 --- a/src/fm_porting.h +++ /dev/null @@ -1,96 +0,0 @@ -#pragma once - -#include "porting.h" - -#if defined(linux) || defined(__linux) - #include -#elif defined(__FreeBSD__) || defined(__OpenBSD__) - #include - #include -#elif defined(__NetBSD__) - #include -#elif defined(__APPLE__) - #include -#endif - -#if defined(linux) || defined(__linux) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) - #define PORTING_USE_PTHREAD 1 - #include -#endif - -namespace porting -{ - -extern std::atomic_bool g_sighup, g_siginfo; - - -#if defined(linux) || defined(__linux) - inline void setThreadName(const char *name) { - /* It would be cleaner to do this with pthread_setname_np, - * which was added to glibc in version 2.12, but some major - * distributions are still runing 2.11 and previous versions. - */ - prctl(PR_SET_NAME, name); - } -#elif defined(__FreeBSD__) || defined(__OpenBSD__) - inline void setThreadName(const char *name) { - pthread_set_name_np(pthread_self(), name); - } -#elif defined(__NetBSD__) - inline void setThreadName(const char *name) { - pthread_setname_np(pthread_self(), name); - } -#elif defined(_MSC_VER) - typedef struct tagTHREADNAME_INFO { - DWORD dwType; // must be 0x1000 - LPCSTR szName; // pointer to name (in user addr space) - DWORD dwThreadID; // thread ID (-1=caller thread) - DWORD dwFlags; // reserved for future use, must be zero - } THREADNAME_INFO; - - inline void setThreadName(const char *name) { - THREADNAME_INFO info; - info.dwType = 0x1000; - info.szName = name; - info.dwThreadID = -1; - info.dwFlags = 0; - __try { - RaiseException(0x406D1388, 0, sizeof(info) / sizeof(DWORD), (ULONG_PTR *) &info); - } __except (EXCEPTION_CONTINUE_EXECUTION) {} - } -#elif defined(__APPLE__) - inline void setThreadName(const char *name) { - pthread_setname_np(name); - } -#elif defined(_WIN32) || defined(__GNU__) - inline void setThreadName(const char* name) {} -#else -#ifndef __EMSCRIPTEN__ - #warning "Unrecognized platform, thread names will not be available." -#endif - - inline void setThreadName(const char* name) {} -#endif - - inline void setThreadPriority(int priority) { -#if PORTING_USE_PTHREAD - // http://en.cppreference.com/w/cpp/thread/thread/native_handle - sched_param sch; - //int policy; - //pthread_getschedparam(pthread_self(), &policy, &sch); - sch.sched_priority = priority; - if(pthread_setschedparam(pthread_self(), SCHED_FIFO /*SCHED_RR*/, &sch)) { - //std::cout << "Failed to setschedparam: " << std::strerror(errno) << '\n'; - } -#endif - } - - -#ifndef SERVER - -//void irr_device_wait_egl (irr::IrrlichtDevice * device = nullptr); - -#endif - - -} \ No newline at end of file diff --git a/src/log_types.cpp b/src/log_types.cpp deleted file mode 100644 index 6d623234f..000000000 --- a/src/log_types.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* -This file is part of Freeminer. - -Freeminer is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -Freeminer 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 General Public License for more details. - -You should have received a copy of the GNU General Public License -along with Freeminer. If not, see . -*/ - -#include "log_types.h" -#include "convert_json.h" -#include "irr_v3d.h" -#include "network/address.h" - -std::ostream &operator<<(std::ostream &s, const v2s16 &p) -{ - s << "(" << p.X << "," << p.Y << ")"; - return s; -} - -std::ostream &operator<<(std::ostream &s, const v2s32 &p) -{ - s << "(" << p.X << "," << p.Y << ")"; - return s; -} - -std::ostream &operator<<(std::ostream &s, const v2f &p) -{ - s << "(" << p.X << "," << p.Y << ")"; - return s; -} - -std::ostream &operator<<(std::ostream &s, const v3pos_t &p) -{ - s << "(" << p.X << "," << p.Y << "," << p.Z << ")"; - return s; -} - -std::ostream &operator<<(std::ostream &s, const v3f &p) -{ - s << "(" << p.X << "," << p.Y << "," << p.Z << ")"; - return s; -} - -#if USE_OPOS64 -std::ostream &operator<<(std::ostream &s, const v3opos_t &p) -{ - s << "(" << p.X << "," << p.Y << "," << p.Z << ")"; - return s; -} -#endif - -std::ostream &operator<<(std::ostream &s, const std::map &p) -{ - for (auto &i : p) - s << i.first << "=" << i.second << " "; - return s; -} - -std::ostream &operator<<(std::ostream &s, const irr::video::SColor &c) -{ - s << "c32(" << c.color << ": a=" << c.getAlpha() << ",r=" << c.getRed() - << ",g=" << c.getGreen() << ",b=" << c.getBlue() << ")"; - return s; -} - -std::ostream &operator<<(std::ostream &s, const irr::video::SColorf &c) -{ - s << "cf32(" - << "a=" << c.getAlpha() << ",r=" << c.getRed() << ",g=" << c.getGreen() - << ",b=" << c.getBlue() << ")"; - return s; -} - -#include "util/string.h" -std::ostream &operator<<(std::ostream &s, const std::wstring &w) -{ - s << wide_to_utf8(w); - return s; -} - -std::ostream &operator<<(std::ostream &s, const Json::Value &json) -{ - s << fastWriteJson(json); - return s; -} - -std::ostream &operator<<(std::ostream &s, const Address &addr) -{ - addr.print(s); - // s << addr.getPort(); - return s; -} \ No newline at end of file diff --git a/src/log_types.h b/src/log_types.h deleted file mode 100644 index 7a24f2c90..000000000 --- a/src/log_types.h +++ /dev/null @@ -1,55 +0,0 @@ -/* -This file is part of Freeminer. - -Freeminer is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -Freeminer 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 General Public License for more details. - -You should have received a copy of the GNU General Public License -along with Freeminer. If not, see . -*/ - -#ifndef LOG_TYPES_HEADER -#define LOG_TYPES_HEADER - -//#include "log.h" //for replacing log.h to log_types.h in includes - -#include "irr_v2d.h" -#include "irr_v3d.h" - -#include -std::ostream &operator<<(std::ostream &s, const v2s16 &p); -std::ostream &operator<<(std::ostream &s, const v2s32 &p); -std::ostream &operator<<(std::ostream &s, const v2f &p); -std::ostream &operator<<(std::ostream &s, const v3pos_t &p); -std::ostream &operator<<(std::ostream &s, const v3f &p); -#if USE_OPOS64 -std::ostream &operator<<(std::ostream &s, const v3opos_t &p); -#endif - -#include -std::ostream &operator<<(std::ostream &s, const irr::video::SColor &c); -std::ostream &operator<<(std::ostream &s, const irr::video::SColorf &c); - -#include -std::ostream &operator<<(std::ostream &s, const std::map &p); - -std::ostream &operator<<(std::ostream &s, const std::wstring &w); - -namespace Json -{ -class Value; -}; - -//std::ostream &operator<<(std::ostream &s, const Json::Value &json); - -class Address; -std::ostream &operator<<(std::ostream &s, const Address &addr); - -#endif \ No newline at end of file diff --git a/src/network/lan.cpp b/src/network/lan.cpp index e7f163395..63ca5e8f9 100644 --- a/src/network/lan.cpp +++ b/src/network/lan.cpp @@ -31,7 +31,7 @@ along with Freeminer. If not, see . #include "debug.h" #include "json/json.h" #include "porting.h" -#include "threading/thread_vector.h" +#include "threading/thread.h" #include "threading/concurrent_map.h" #include "network/address.h" @@ -71,13 +71,13 @@ typedef int socket_t; const static unsigned short int adv_port = 29998; static std::string ask_str; -lan_adv::lan_adv() : thread_vector("lan_adv") +lan_adv::lan_adv() : Thread("lan_adv") { } void lan_adv::ask() { - reanimate(); + if (!isRunning()) start(); if (ask_str.empty()) { Json::Value j; @@ -169,14 +169,15 @@ void lan_adv::send_string(const std::string &str) void lan_adv::serve(unsigned short port) { server_port = port; - restart(); + if (isRunning()) stop(); + start(); } void *lan_adv::run() { BEGIN_DEBUG_EXCEPTION_HANDLER; - reg("LanAdv" + (server_port ? std::string("Server") : std::string("Client"))); + setName("LanAdv" + (server_port ? std::string("Server") : std::string("Client"))); UDPSocket socket_recv(true); int set_option_off = 0, set_option_on = 1; diff --git a/src/network/lan.h b/src/network/lan.h index 6dafab2a0..c4c2eb07f 100644 --- a/src/network/lan.h +++ b/src/network/lan.h @@ -22,11 +22,11 @@ along with Freeminer. If not, see . #include #include #include -#include "threading/thread_vector.h" +#include "threading/thread.h" #include "threading/concurrent_map.h" -class lan_adv : public thread_vector +class lan_adv : public Thread { public: void *run(); diff --git a/src/server/serverlist.cpp b/src/server/serverlist.cpp index 1165ae9da..a59284f4c 100644 --- a/src/server/serverlist.cpp +++ b/src/server/serverlist.cpp @@ -50,18 +50,6 @@ void lan_get() { lan_adv_client.ask(); } -/* - if (ask_str.empty()) { - Json::Value j; - j["cmd"] = "ask"; - j["proto_min"] = Server::getProtocolVersionMin(); - j["proto_max"] = Server::getProtocolVersionMax(); - ask_str = fastWriteJson(j); - - }; - lan_adv_client.send_string(ask_str); -*/ - bool lan_fresh() { auto result = lan_adv_client.fresh.load(); lan_adv_client.fresh = false; diff --git a/src/threading/CMakeLists.txt b/src/threading/CMakeLists.txt index 25c7e955c..d7549fe02 100644 --- a/src/threading/CMakeLists.txt +++ b/src/threading/CMakeLists.txt @@ -3,6 +3,5 @@ set(JTHREAD_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/thread.cpp ${CMAKE_CURRENT_SOURCE_DIR}/semaphore.cpp ${CMAKE_CURRENT_SOURCE_DIR}/lock.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/thread_vector.cpp PARENT_SCOPE) diff --git a/src/threading/thread_vector.cpp b/src/threading/thread_vector.cpp deleted file mode 100644 index 5d206f58c..000000000 --- a/src/threading/thread_vector.cpp +++ /dev/null @@ -1,118 +0,0 @@ -#include "thread_vector.h" -#include "fm_porting.h" -#include "log.h" -#include "porting.h" - -thread_vector::thread_vector(const std::string &name, int priority) : - m_name(name), m_priority(priority) -{ - request_stop = false; -}; - -thread_vector::~thread_vector() -{ - join(); -}; - -void thread_vector::func() -{ - reg(); - run(); -}; - -void thread_vector::reg(const std::string &name, int priority) -{ - if (!name.empty()) - m_name = name; - - porting::setThreadName(m_name.c_str()); - g_logger.registerThread(m_name); - - if (priority) - m_priority = priority; - if (m_priority) - porting::setThreadPriority(m_priority); -}; - -void thread_vector::start(const size_t n) -{ -#if !NDEBUG - infostream << "start thread " << m_name << " n=" << n << std::endl; -#endif - request_stop = false; - for (size_t i = 0; i < n; ++i) { - workers.emplace_back(&thread_vector::func, this); - } -} - -void thread_vector::stop() -{ - request_stop = true; -} - -void thread_vector::join() -{ - stop(); - for (auto &worker : workers) { - try { - if (worker.joinable()) { - worker.join(); - } - } catch (...) { - } - } - workers.clear(); -} - -void thread_vector::restart(size_t n) -{ - join(); - start(n); -} -void thread_vector::reanimate(size_t n) -{ - if (workers.empty()) { - start(n); - } -} - -void thread_vector::sleep(const int seconds) -{ - for (int i = 0; i <= seconds; ++i) { - std::this_thread::sleep_for(std::chrono::seconds(1)); - if (request_stop) { - return; - } - } -} - -// JThread compat: -bool thread_vector::stopRequested() -{ - return request_stop; -} -bool thread_vector::isRunning() -{ - return !workers.empty(); -} -void thread_vector::wait() -{ - join(); -}; -void thread_vector::kill() -{ - join(); -}; -void *thread_vector::run() -{ - return nullptr; -}; - -bool thread_vector::isCurrentThread() -{ - auto thread_me = std::hash()(std::this_thread::get_id()); - for (auto &worker : workers) - if (thread_me == std::hash()(worker.get_id())) - return true; - return false; -} diff --git a/src/threading/thread_vector.h b/src/threading/thread_vector.h deleted file mode 100644 index 8172dd7b6..000000000 --- a/src/threading/thread_vector.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -class thread_vector { -public: - std::vector workers; - std::atomic_bool request_stop; - - thread_vector(const std::string &name = "Unnamed", int priority = 0); - virtual ~thread_vector(); - - virtual void func(); - - void reg (const std::string &name = "", int priority = 0); - void start (const size_t n = 1); - void restart (const size_t n = 1); - void reanimate (const size_t n = 1); - void stop (); - void join (); - - void sleep(const int second); -// Thread compat: - - bool stopRequested(); - bool isRunning(); - void wait(); - void kill(); - virtual void * run() = 0; - bool isCurrentThread(); -protected: - std::string m_name; - int m_priority = 0; -}; \ No newline at end of file