From 3ac1527e1f2c695e0c787519a8bd63506018aecd Mon Sep 17 00:00:00 2001 From: proller Date: Sat, 27 Jul 2024 13:59:44 +0200 Subject: [PATCH] Dont use fm stuff --- src/network/lan.cpp | 4 +- src/network/lan.h | 7 +- src/script/lua_api/l_mainmenu.cpp | 1 + src/threading/concurrent_map.h | 209 ------------------------------ src/threading/lock.cpp | 131 ------------------- src/threading/lock.h | 164 ----------------------- 6 files changed, 9 insertions(+), 507 deletions(-) delete mode 100644 src/threading/concurrent_map.h delete mode 100644 src/threading/lock.cpp delete mode 100644 src/threading/lock.h diff --git a/src/network/lan.cpp b/src/network/lan.cpp index 80b4c96712344..7762edfdefc30 100644 --- a/src/network/lan.cpp +++ b/src/network/lan.cpp @@ -30,9 +30,10 @@ along with Freeminer. If not, see . #include "server/serverlist.h" #include "debug.h" #include "json/json.h" +#include +#include #include "porting.h" #include "threading/thread.h" -#include "threading/concurrent_map.h" #include "network/address.h" //copypaste from ../socket.cpp @@ -303,6 +304,7 @@ void *lan_adv::run() if (p["port"].isInt()) { p["address"] = addr_str; auto key = addr_str + ":" + p["port"].asString(); + std::unique_lock lock(mutex); if (p["cmd"].asString() == "shutdown") { //infostream << "server shutdown " << key << "\n"; collected.erase(key); diff --git a/src/network/lan.h b/src/network/lan.h index ea058f1ae7127..085a597a2728b 100644 --- a/src/network/lan.h +++ b/src/network/lan.h @@ -20,10 +20,11 @@ along with Freeminer. If not, see . #pragma once #include "json/json.h" +#include +#include #include #include #include "threading/thread.h" -#include "threading/concurrent_map.h" class lan_adv : public Thread @@ -37,7 +38,9 @@ class lan_adv : public Thread void serve(unsigned short port); - concurrent_map collected; + std::map collected; + std::shared_mutex mutex; + std::atomic_bool fresh; std::atomic_int clients_num; diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index 6a637f7d766e0..f7ba929ccaa2b 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -1101,6 +1101,7 @@ int ModApiMainMenu::l_get_lan_servers(lua_State *L) int top = lua_gettop(L); unsigned int index = 1; + std::shared_lock lock(ServerList::lan_adv_client.mutex); for (const auto &server : ServerList::lan_adv_client.collected) { lua_pushnumber(L, index); diff --git a/src/threading/concurrent_map.h b/src/threading/concurrent_map.h deleted file mode 100644 index c07971d59b196..0000000000000 --- a/src/threading/concurrent_map.h +++ /dev/null @@ -1,209 +0,0 @@ -/* -Copyright (C) 2024 proller -*/ - -/* -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 . -*/ - -#pragma once - -#include - -#include "lock.h" - -template , - class Allocator = std::allocator>> -class concurrent_map_ : public std::map, public LOCKER -{ -public: - typedef typename std::map full_type; - typedef Key key_type; - typedef T mapped_type; - - mapped_type &operator[](const key_type &k) = delete; - mapped_type &operator[](key_type &&k) = delete; - - mapped_type nothing = {}; - - template - mapped_type& get(Args &&...args) - { - auto lock = LOCKER::lock_shared_rec(); - - //if (!full_type::contains(std::forward(args)...)) - if (full_type::find(std::forward(args)...) == full_type::end()) - return nothing; - - return full_type::operator[](std::forward(args)...); - } - - template - decltype(auto) at(Args &&...args) - { - auto lock = LOCKER::lock_shared_rec(); - return full_type::at(std::forward(args)...); - } - - template - decltype(auto) assign(Args &&...args) - { - auto lock = LOCKER::lock_unique_rec(); - return full_type::assign(std::forward(args)...); - } - - template - decltype(auto) insert(Args &&...args) - { - auto lock = LOCKER::lock_unique_rec(); - return full_type::insert(std::forward(args)...); - } - - template - decltype(auto) emplace(Args &&...args) - { - auto lock = LOCKER::lock_unique_rec(); - return full_type::emplace(std::forward(args)...); - } - - template - decltype(auto) emplace_try(Args &&...args) - { - auto lock = LOCKER::try_lock_unique_rec(); - if (!lock->owns_lock()) - return false; - return full_type::emplace(std::forward(args)...).second; - } - - template - decltype(auto) insert_or_assign(Args &&...args) - { - auto lock = LOCKER::lock_unique_rec(); - return full_type::insert_or_assign(std::forward(args)...); - } - - template - decltype(auto) empty(Args &&...args) const noexcept - { - auto lock = LOCKER::lock_shared_rec(); - return full_type::empty(std::forward(args)...); - } - - template - decltype(auto) size(Args &&...args) const - { - auto lock = LOCKER::lock_shared_rec(); - return full_type::size(std::forward(args)...); - } - - template - decltype(auto) count(Args &&...args) const - { - auto lock = LOCKER::lock_shared_rec(); - return full_type::count(std::forward(args)...); - } - - template - decltype(auto) contains(Args &&...args) const - { - auto lock = LOCKER::lock_shared_rec(); - return full_type::contains(std::forward(args)...); - } - - template - decltype(auto) find(Args &&...args) - { - auto lock = LOCKER::lock_shared_rec(); - return full_type::find(std::forward(args)...); - } - - template - decltype(auto) begin(Args &&...args) - { - auto lock = LOCKER::lock_shared_rec(); - return full_type::begin(std::forward(args)...); - } - - template - decltype(auto) rbegin(Args &&...args) - { - auto lock = LOCKER::lock_shared_rec(); - return full_type::rbegin(std::forward(args)...); - } - - template - decltype(auto) end(Args &&...args) - { - auto lock = LOCKER::lock_shared_rec(); - return full_type::end(std::forward(args)...); - } - - template - decltype(auto) rend(Args &&...args) - { - auto lock = LOCKER::lock_shared_rec(); - return full_type::rend(std::forward(args)...); - } - - - template - decltype(auto) erase(Args &&...args) - { - auto lock = LOCKER::lock_unique_rec(); - return full_type::erase(std::forward(args)...); - } - - template - decltype(auto) clear(Args &&...args) - { - auto lock = LOCKER::lock_unique_rec(); - return full_type::clear(std::forward(args)...); - } -}; - -template , - class Allocator = std::allocator>> -using concurrent_map = concurrent_map_, Key, T, Compare, Allocator>; - -template , class Allocator = std::allocator>> -using concurrent_shared_map = concurrent_map_; - -#if ENABLE_THREADS - -template , - class Allocator = std::allocator>> -using maybe_concurrent_map = concurrent_map; - -#else - -template , - class Allocator = std::allocator>> -class not_concurrent_map : public std::map, - public dummy_locker -{ -public: - typedef typename std::map full_type; - typedef Key key_type; - typedef T mapped_type; - - mapped_type &get(const key_type &k) { return full_type::operator[](k); } -}; - -template , - class Allocator = std::allocator>> -using maybe_concurrent_map = not_concurrent_map; - -#endif \ No newline at end of file diff --git a/src/threading/lock.cpp b/src/threading/lock.cpp deleted file mode 100644 index d800554476612..0000000000000 --- a/src/threading/lock.cpp +++ /dev/null @@ -1,131 +0,0 @@ -#include "lock.h" -#include "log.h" -#include "profiler.h" - -#if !defined(NDEBUG) && !defined(LOCK_PROFILE) -//#define LOCK_PROFILE 1 -#endif - -#if LOCK_PROFILE -#define SCOPE_PROFILE(a) ScopeProfiler scp___(g_profiler, "Lock: " a); -#else -#define SCOPE_PROFILE(a) -#endif - -template -recursive_lock::recursive_lock(MUTEX & mtx, std::atomic & thread_id_, bool try_lock): - thread_id(thread_id_) { - auto thread_me = std::hash()(std::this_thread::get_id()); - if(thread_me != thread_id) { - if (try_lock) { - SCOPE_PROFILE("try_lock"); - lock = new GUARD(mtx, try_to_lock); - if (lock->owns_lock()) { - thread_id = thread_me; - return; - } else { -#if LOCK_PROFILE - g_profiler->add("Lock: try_lock fail", 1); -#endif - //infostream<<"not locked "<<" thread="<add("Lock: recursive", 1); -#endif - } - lock = nullptr; -} - -template -recursive_lock::~recursive_lock() { - unlock(); -} - -template -bool recursive_lock::owns_lock() { - if (lock) - return lock; - auto thread_me = std::hash()(std::this_thread::get_id()); - return thread_id == thread_me; -} - -template -void recursive_lock::unlock() { - if(lock) { - thread_id = 0; - lock->unlock(); - delete lock; - lock = nullptr; - } -} - - -template -locker::locker() { - thread_id = 0; -} - -template -std::unique_ptr locker::lock_unique() { - return std::make_unique(mtx); -} - -template -std::unique_ptr locker::try_lock_unique() { - SCOPE_PROFILE("locker::try_lock_unique"); - return std::make_unique(mtx, std::try_to_lock); -} - -template -std::unique_ptr locker::lock_shared() const { - SCOPE_PROFILE("locker::lock_shared"); - return std::make_unique(mtx); -} - -template -std::unique_ptr locker::try_lock_shared() { - SCOPE_PROFILE("locker::try_lock_shared"); - return std::make_unique(mtx, std::try_to_lock); -} - -template -std::unique_ptr> locker::lock_unique_rec() const { - SCOPE_PROFILE("locker::lock_unique_rec"); - return std::make_unique(mtx, thread_id); -} - -template -std::unique_ptr> locker::try_lock_unique_rec() { - SCOPE_PROFILE("locker::try_lock_unique_rec"); - return std::make_unique(mtx, thread_id, true); -} - -template -std::unique_ptr> locker::lock_shared_rec() const { - SCOPE_PROFILE("locker::lock_shared_rec"); - return std::make_unique(mtx, thread_id); -} - -template -std::unique_ptr> locker::try_lock_shared_rec() { - SCOPE_PROFILE("locker::try_lock_shared_rec"); - return std::make_unique(mtx, thread_id, true); -} - - -template class recursive_lock>; -template class locker<>; -#if LOCK_TWO -template class recursive_lock; -template class recursive_lock, try_shared_mutex>; - -template class locker, std::shared_lock>; -#endif \ No newline at end of file diff --git a/src/threading/lock.h b/src/threading/lock.h deleted file mode 100644 index a3cd43e295887..0000000000000 --- a/src/threading/lock.h +++ /dev/null @@ -1,164 +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 . -*/ - -#pragma once - -#include -#include -#include -#include - -#include "../config.h" - -#ifdef _WIN32 - -//#include "../threading/mutex.h" -using use_mutex = std::mutex; -using try_shared_mutex = use_mutex; -using try_shared_lock = std::unique_lock; -using unique_lock = std::unique_lock; -const auto try_to_lock = std::try_to_lock; - -#else - -typedef std::mutex use_mutex; - - -#if USE_BOOST // not finished - -//#include -#include -//#include -typedef boost::shared_mutex try_shared_mutex; -typedef boost::shared_lock try_shared_lock; -typedef boost::unique_lock unique_lock; -const auto try_to_lock = boost::try_to_lock; -#define LOCK_TWO 1 - -#elif HAVE_SHARED_MUTEX -//#elif __cplusplus >= 201305L - -#include -using try_shared_mutex = std::shared_mutex; -using try_shared_lock = std::shared_lock; -using unique_lock = std::unique_lock; -const auto try_to_lock = std::try_to_lock; -#define LOCK_TWO 1 - -#else - -using try_shared_mutex = use_mutex; -using try_shared_lock = std::unique_lock ; -using unique_lock = std::unique_lock ; -const auto try_to_lock = std::try_to_lock; -#endif - -#endif - - -// http://stackoverflow.com/questions/4792449/c0x-has-no-semaphores-how-to-synchronize-threads -/* uncomment when need -#include -class semaphore { -private: - std::mutex mtx; - std::condition_variable cv; - int count; - -public: - semaphore(int count_ = 0):count(count_){;} - void notify() { - std::unique_lock lck(mtx); - ++count; - cv.notify_one(); - } - void wait() { - std::unique_lock lck(mtx); - while(count == 0){ - cv.wait(lck); - } - count--; - } -}; -*/ - -template -class recursive_lock { -public: - GUARD * lock; - std::atomic & thread_id; - recursive_lock(MUTEX & mtx, std::atomic & thread_id_, bool try_lock = false); - ~recursive_lock(); - bool owns_lock(); - void unlock(); -}; - -template , class shared_lock = std::unique_lock > -class locker { -public: - using lock_rec_shared = recursive_lock; - using lock_rec_unique = recursive_lock; - - mutable mutex mtx; - mutable std::atomic thread_id; - - locker(); - std::unique_ptr lock_unique(); - std::unique_ptr try_lock_unique(); - std::unique_ptr lock_shared() const; - std::unique_ptr try_lock_shared(); - std::unique_ptr lock_unique_rec() const; - std::unique_ptr try_lock_unique_rec(); - std::unique_ptr lock_shared_rec() const; - std::unique_ptr try_lock_shared_rec(); -}; - -using shared_locker = locker; - -class dummy_lock { -public: - ~dummy_lock() {}; //no unused variable warning - bool owns_lock() {return true;} - bool operator!() {return true;} - dummy_lock * operator->() {return this; } - void unlock() {}; -}; - -class dummy_locker { -public: - dummy_lock lock_unique() { return {}; }; - dummy_lock try_lock_unique() { return {}; }; - dummy_lock lock_shared() { return {}; }; - dummy_lock try_lock_shared() { return {}; }; - dummy_lock lock_unique_rec() { return {}; }; - dummy_lock try_lock_unique_rec() { return {}; }; - dummy_lock lock_shared_rec() { return {}; }; - dummy_lock try_lock_shared_rec() { return {}; }; -}; - - -#if ENABLE_THREADS - -using maybe_locker = locker<>; -using maybe_shared_locker = shared_locker; - -#else - -using maybe_locker = dummy_locker; -using maybe_shared_locker = dummy_locker; - -#endif \ No newline at end of file