Skip to content

Commit

Permalink
refactor: remove getServerThread global function
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jan 25, 2025
1 parent 2baf0e9 commit 809b3ca
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 13 deletions.
25 changes: 25 additions & 0 deletions src/bedrock/core/threading/atomicops.h
Original file line number Diff line number Diff line change
@@ -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 <atomic>

namespace Lockless {
template <typename T>
class WeakAtomic {
private:
std::atomic<T> value_;
};
} // namespace Lockless
27 changes: 27 additions & 0 deletions src/bedrock/core/threading/spsc_queue.h
Original file line number Diff line number Diff line change
@@ -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 <typename T, std::size_t MAX_BLOCK_SIZE>
class SPSCQueue {
private:
struct Block {};
Lockless::WeakAtomic<Block *> front_block_;
char cacheline_filler_[56];
Lockless::WeakAtomic<Block *> tail_block_;
std::size_t largest_block_size_;
};
1 change: 1 addition & 0 deletions src/bedrock/forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class DimensionBrightnessRamp;
class DimensionConversionData;
class DimensionManager;
class EducationLevelSettings;
class EducationOptions;
class EntitySystems;
class EquipmentSlot;
class Experiments;
Expand Down
1 change: 0 additions & 1 deletion src/bedrock/network/server_network_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 &,
Expand Down
5 changes: 0 additions & 5 deletions src/bedrock/platform/assigned_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions src/bedrock/platform/threading/assigned_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ class AssignedThread {
std::thread::id assigned_id_;
};

AssignedThread &getServerThread();

} // namespace Bedrock::Threading
21 changes: 21 additions & 0 deletions src/bedrock/platform/threading/thread.h
Original file line number Diff line number Diff line change
@@ -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 <thread>

namespace Bedrock::Threading {
using Thread = std::thread;
} // namespace Bedrock::Threading
23 changes: 23 additions & 0 deletions src/bedrock/server/server_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,15 +32,36 @@ class ServerInstance : public Bedrock::EnableNonOwnerReferences,
public Core::StorageAreaStateListener {
public:
ServerInstance(IMinecraftApp &, const Bedrock::NotNullNonOwnerPtr<ServerInstanceEventCoordinator> &);
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> minecraft_;
std::unique_ptr<ServerNetworkSystem> network_;
std::unique_ptr<LoopbackPacketSender> packet_sender_;
std::unique_ptr<Timer> sim_timer_;
std::unique_ptr<Timer> real_timer_;
std::unique_ptr<Scheduler> scheduler_;
std::unique_ptr<EducationOptions> education_options_;
LevelStorage *storage_;
std::atomic_bool in_update_;
std::atomic<int> write_ref_counter_;
std::atomic_bool thread_should_join_;
Bedrock::Threading::Mutex mutex_destruction_;
Bedrock::NotNullNonOwnerPtr<ServerInstanceEventCoordinator> event_coordinator_;
std::atomic<InstanceState> instance_state_;
SPSCQueue<std::function<void()>, 512UL> command_queue_;
Bedrock::Threading::Thread server_instance_thread_;
};
4 changes: 0 additions & 4 deletions src/bedrock/symbol_generator/symbols.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/endstone/core/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 809b3ca

Please sign in to comment.