Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions managed/CounterStrikeSharp.Tests.Native/FrameSchedulingTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
Expand Down Expand Up @@ -95,4 +97,50 @@ public async Task NextWorldUpdate_HighLevelApi_ExecutesCallback()

Assert.True(called, "NextWorldUpdate callback should have been called");
}

[Fact]
public async Task NextFrameConcurrentQueueDrainsProperly()
{
int callCount = 0;
int targetCalls = 4096;
var callsByFrame = new ConcurrentDictionary<int, int>();
for (int i = 0; i < targetCalls; i++)
{
Server.NextFrame(() =>
{
callsByFrame.AddOrUpdate(Server.TickCount, 1, (_, count) => count + 1);
Interlocked.Increment(ref callCount);
});
}

// All tasks should have been drained by latest NextFrameAsync
await Server.NextFrameAsync(() => { }).ConfigureAwait(false);

// The task should be bucketed into multiple frames
Assert.All(callsByFrame.Values, count => Assert.Equal(1024, count));
Assert.Equal(4096, callCount);
}

[Fact]
public async Task NextWorldUpdateConcurrentQueueDrainsProperly()
{
int callCount = 0;
int targetCalls = 4096;
var callsByFrame = new ConcurrentDictionary<int, int>();
for (int i = 0; i < targetCalls; i++)
{
Server.NextWorldUpdate(() =>
{
callsByFrame.AddOrUpdate(Server.TickCount, 1, (_, count) => count + 1);
Interlocked.Increment(ref callCount);
});
}

// All tasks should have been drained by latest NextFrameAsync
await Server.NextWorldUpdateAsync(() => { }).ConfigureAwait(false);

// The task should be bucketed into multiple frames
Assert.All(callsByFrame.Values, count => Assert.Equal(1024, count));
Assert.Equal(4096, callCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override void Load(bool hotReload)
{
gameThreadId = Thread.CurrentThread.ManagedThreadId;
// Loading blocks the game thread, so we use NextFrame to run our tests asynchronously.
Server.NextFrame(() => RunTests());
Server.NextWorldUpdate(() => RunTests());
AddCommand("css_run_tests", "Runs the xUnit tests for the native plugin.", (player, info) => { RunTests(); });
}

Expand Down
6 changes: 5 additions & 1 deletion src/core/managers/server_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ void ServerManager::PreWorldUpdate(bool bSimulating)

void ServerManager::AddTaskForNextWorldUpdate(std::function<void()>&& task)
{
m_nextWorldUpdateTasks.enqueue(std::forward<decltype(task)>(task));
auto success = m_nextWorldUpdateTasks.enqueue(std::forward<decltype(task)>(task));
if (!success)
{
CSSHARP_CORE_ERROR("Failed to enqueue task for next world update!");
}
}

void ServerManager::OnPrecacheResources(IEntityResourceManifest* pResourceManifest)
Expand Down
2 changes: 1 addition & 1 deletion src/core/managers/server_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ServerManager : public GlobalClass

ScriptCallback* on_server_precache_resources;

moodycamel::ConcurrentQueue<std::function<void()>> m_nextWorldUpdateTasks;
moodycamel::ConcurrentQueue<std::function<void()>> m_nextWorldUpdateTasks{ 4096 };
};

} // namespace counterstrikesharp
6 changes: 5 additions & 1 deletion src/mm_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@ void CounterStrikeSharpMMPlugin::AllPluginsLoaded()

void CounterStrikeSharpMMPlugin::AddTaskForNextFrame(std::function<void()>&& task)
{
m_nextTasks.try_enqueue(std::forward<decltype(task)>(task));
auto success = m_nextTasks.enqueue(std::forward<decltype(task)>(task));
if (!success)
{
CSSHARP_CORE_ERROR("Failed to enqueue task for next frame!");
}
}

void CounterStrikeSharpMMPlugin::Hook_GameFrame(bool simulating, bool bFirstTick, bool bLastTick)
Expand Down
2 changes: 1 addition & 1 deletion src/mm_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CounterStrikeSharpMMPlugin : public ISmmPlugin, public IMetamodListener
const char* GetLogTag() override;

private:
moodycamel::ConcurrentQueue<std::function<void()>> m_nextTasks;
moodycamel::ConcurrentQueue<std::function<void()>> m_nextTasks{ 4096 };
};

static ScriptCallback* on_activate_callback;
Expand Down
Loading