diff --git a/src/bedrock/core/threading/atomicops.h b/src/bedrock/core/threading/atomicops.h new file mode 100644 index 000000000..01d654f0a --- /dev/null +++ b/src/bedrock/core/threading/atomicops.h @@ -0,0 +1,25 @@ +// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +namespace Lockless { +template +class WeakAtomic { +private: + std::atomic value_; +}; +} // namespace Lockless diff --git a/src/bedrock/core/threading/spsc_queue.h b/src/bedrock/core/threading/spsc_queue.h new file mode 100644 index 000000000..c616e8623 --- /dev/null +++ b/src/bedrock/core/threading/spsc_queue.h @@ -0,0 +1,27 @@ +// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include "bedrock/core/threading/atomicops.h" + +template +class SPSCQueue { +private: + struct Block {}; + Lockless::WeakAtomic front_block_; + char cacheline_filler_[56]; + Lockless::WeakAtomic tail_block_; + std::size_t largest_block_size_; +}; diff --git a/src/bedrock/forward.h b/src/bedrock/forward.h index d974bdf7b..a1c5ccfdb 100644 --- a/src/bedrock/forward.h +++ b/src/bedrock/forward.h @@ -82,6 +82,7 @@ class DimensionBrightnessRamp; class DimensionConversionData; class DimensionManager; class EducationLevelSettings; +class EducationOptions; class EntitySystems; class EquipmentSlot; class Experiments; diff --git a/src/bedrock/network/server_network_handler.h b/src/bedrock/network/server_network_handler.h index e83477e7b..4f792cbc8 100644 --- a/src/bedrock/network/server_network_handler.h +++ b/src/bedrock/network/server_network_handler.h @@ -73,7 +73,6 @@ class ServerNetworkHandler : public Bedrock::Threading::EnableQueueForMainThread private: friend class endstone::core::EndstoneServer; - friend class endstone::core::EndstoneLevel; // NOLINTBEGIN(*-identifier-naming) ENDSTONE_HOOK ServerPlayer &_createNewPlayer(NetworkIdentifier const &, SubClientConnectionRequest const &, diff --git a/src/bedrock/platform/assigned_thread.cpp b/src/bedrock/platform/assigned_thread.cpp index 5ede00cb5..a13e1f029 100644 --- a/src/bedrock/platform/assigned_thread.cpp +++ b/src/bedrock/platform/assigned_thread.cpp @@ -23,9 +23,4 @@ bool AssignedThread::isOnThread() const return std::this_thread::get_id() == assigned_id_; } -AssignedThread &getServerThread() -{ - return BEDROCK_CALL(&getServerThread); -} - } // namespace Bedrock::Threading diff --git a/src/bedrock/platform/threading/assigned_thread.h b/src/bedrock/platform/threading/assigned_thread.h index d4486ac37..529334755 100644 --- a/src/bedrock/platform/threading/assigned_thread.h +++ b/src/bedrock/platform/threading/assigned_thread.h @@ -28,6 +28,4 @@ class AssignedThread { std::thread::id assigned_id_; }; -AssignedThread &getServerThread(); - } // namespace Bedrock::Threading diff --git a/src/bedrock/platform/threading/thread.h b/src/bedrock/platform/threading/thread.h new file mode 100644 index 000000000..f76046b87 --- /dev/null +++ b/src/bedrock/platform/threading/thread.h @@ -0,0 +1,21 @@ +// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +namespace Bedrock::Threading { +using Thread = std::thread; +} // namespace Bedrock::Threading diff --git a/src/bedrock/server/server_instance.h b/src/bedrock/server/server_instance.h index f29326720..96b857a6c 100644 --- a/src/bedrock/server/server_instance.h +++ b/src/bedrock/server/server_instance.h @@ -16,7 +16,9 @@ #include "bedrock/app_platform_listener.h" #include "bedrock/core/file/storage_area_state_listener.h" +#include "bedrock/core/threading/spsc_queue.h" #include "bedrock/network/loopback_packet_sender.h" +#include "bedrock/platform/threading/thread.h" #include "bedrock/util/timer.h" #include "bedrock/world/game_callbacks.h" #include "bedrock/world/game_session.h" @@ -30,10 +32,20 @@ class ServerInstance : public Bedrock::EnableNonOwnerReferences, public Core::StorageAreaStateListener { public: ServerInstance(IMinecraftApp &, const Bedrock::NotNullNonOwnerPtr &); + enum InstanceState : unsigned int { + Running = 0, + Suspended = 1, + WaitingLeaveGame = 2, + Stopped = 3, + NotStarted = 4, + }; + Minecraft *getMinecraft(); PacketSender &getPacketSender(); private: + friend class endstone::core::EndstoneServer; + std::chrono::steady_clock::time_point last_sync_time_; IMinecraftApp &app_; std::unique_ptr minecraft_; @@ -41,4 +53,15 @@ class ServerInstance : public Bedrock::EnableNonOwnerReferences, std::unique_ptr packet_sender_; std::unique_ptr sim_timer_; std::unique_ptr real_timer_; + std::unique_ptr scheduler_; + std::unique_ptr education_options_; + LevelStorage *storage_; + std::atomic_bool in_update_; + std::atomic write_ref_counter_; + std::atomic_bool thread_should_join_; + Bedrock::Threading::Mutex mutex_destruction_; + Bedrock::NotNullNonOwnerPtr event_coordinator_; + std::atomic instance_state_; + SPSCQueue, 512UL> command_queue_; + Bedrock::Threading::Thread server_instance_thread_; }; diff --git a/src/bedrock/symbol_generator/symbols.toml b/src/bedrock/symbol_generator/symbols.toml index ac931aae6..6f565ca69 100644 --- a/src/bedrock/symbol_generator/symbols.toml +++ b/src/bedrock/symbol_generator/symbols.toml @@ -5,8 +5,6 @@ version = "1.21.50" "?getI18n@@YAAEAVI18n@@XZ" = 14120528 # Actor "?teleportTo@Actor@@UEAAXAEBVVec3@@_NHH1@Z" = 37887936 -# Bedrock -"?getServerThread@Threading@Bedrock@@YAAEAVAssignedThread@12@XZ" = 62131040 # BedrockLog "?log_va@BedrockLog@@YAXW4LogCategory@1@V?$bitset@$02@std@@W4LogRule@1@W4LogAreaID@@IPEBDH4PEAD@Z" = 59908992 # BlockDescriptor @@ -97,8 +95,6 @@ version = "1.21.50" "_Z7getI18nv" = 91294352 # Actor "_ZN5Actor10teleportToERK4Vec3biib" = 121564592 -# Bedrock -"_ZN7Bedrock9Threading15getServerThreadEv" = 164283584 # BedrockLog "_ZN10BedrockLog6log_vaENS_11LogCategoryENSt3__16bitsetILm3EEENS_7LogRuleE9LogAreaIDjPKciS7_P13__va_list_tag" = 160989088 # BlockDescriptor diff --git a/src/endstone/core/server.cpp b/src/endstone/core/server.cpp index 3b4cc0d9a..20f820e25 100644 --- a/src/endstone/core/server.cpp +++ b/src/endstone/core/server.cpp @@ -415,7 +415,7 @@ void EndstoneServer::broadcastMessage(const Message &message) const bool EndstoneServer::isPrimaryThread() const { - return Bedrock::Threading::getServerThread().isOnThread(); + return std::this_thread::get_id() == server_instance_->server_instance_thread_.get_id(); } Scoreboard *EndstoneServer::getScoreboard() const