Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added threadpool #8

Merged
merged 4 commits into from
Aug 13, 2024
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
4 changes: 2 additions & 2 deletions Coral.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@
<ClCompile Include="Source\Components\Particles\ParticleLightComponent.cpp" />
<ClCompile Include="Source\Components\Particles\ParticleUtilities.cpp" />
<ClCompile Include="Source\Core\Device.cpp" />
<ClCompile Include="Source\Core\ThreadPool.cpp" />
<ClCompile Include="Source\Platform\PC\Core\Renderer.cpp" />
<ClCompile Include="Source\Assets\Core\AssetHandle.cpp" />
<ClCompile Include="Source\Assets\Importers\AudioImporter.cpp">
Expand All @@ -324,7 +325,6 @@
<ClCompile Include="Source\Systems\Rendering\ParticleRenderingSystem.cpp" />
<ClCompile Include="Source\Systems\Rendering\StaticMeshRenderingSystem.cpp" />
<ClCompile Include="Source\UnitTests\AssetHandleUnitTests.cpp" />
<ClCompile Include="Source\Utilities\ASync.cpp" />
<ClCompile Include="Source\Utilities\BVH.cpp" />
<ClCompile Include="Source\Utilities\Events.cpp" />
<ClCompile Include="Source\Utilities\Imgui\WorldDetailsPanel.cpp">
Expand Down Expand Up @@ -862,13 +862,13 @@
<ClInclude Include="Include\Components\Particles\ParticleUtilities\ParticleUtilitiesImpl.h" />
<ClInclude Include="Include\Components\AudioEmitterComponent.h" />
<ClInclude Include="Include\Core\Renderer.h" />
<ClInclude Include="Include\Core\ThreadPool.h" />
<ClInclude Include="Include\EditorSystems\ThumbnailEditorSystem.h" />
<ClInclude Include="Include\Systems\AudioSystem.h" />
<ClInclude Include="Include\Systems\Particles\ParticleLightSystem.h" />
<ClInclude Include="Include\Systems\Rendering\LightRenderingSystem.h" />
<ClInclude Include="Include\Systems\Rendering\ParticleRenderingSystem.h" />
<ClInclude Include="Include\Systems\Rendering\StaticMeshRenderingSystem.h" />
<ClInclude Include="Include\Utilities\ASync.h" />
<ClInclude Include="Include\Utilities\BVH.h" />
<ClInclude Include="Include\Utilities\Geometry2d.h" />
<ClInclude Include="Include\EditorSystems\ImporterSystem.h" />
Expand Down
10 changes: 7 additions & 3 deletions Include/Assets/Level.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ namespace CE
Level& operator=(const Level&) = delete;

void CreateFromWorld(const World& world);
World CreateWorld(bool callBeginPlayImmediately) const;
void LoadIntoWorld(World& world) const;

static World CreateDefaultWorld();
// Will never return nullptr
std::unique_ptr<World> CreateWorld(bool callBeginPlayImmediately) const;

// Will never return nullptr
static std::unique_ptr<World> CreateDefaultWorld();

protected:
void OnSave(AssetSaveInfo& saveInfo) const override;
Expand Down Expand Up @@ -71,7 +75,7 @@ namespace CE
// when loading the level. Instead of
// discarding the world, we return this
// one on the first call to CreateWorld
mutable std::optional<World> mWorld{};
mutable std::unique_ptr<World> mWorld{};
mutable std::optional<BinaryGSONObject> mSerializedWorld{};

friend ReflectAccess;
Expand Down
5 changes: 3 additions & 2 deletions Include/Assets/Script.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ namespace CE
// so if this ever changes, you'll know exactly where you need to start fixing things.
static constexpr bool sIsTypeIdTheSameAsNameHash = true;

// Each component script secretly gets assigned a field of type entt::entity with this name.
// Upon constructing the component, the correct entity id is assigned.
// Each component script secretly gets assigned a field of type entt::entity and World* with these names.
// Upon constructing the component, the correct entity id and world is assigned.
static constexpr Name sNameOfOwnerField = "Owner"_Name;
static constexpr Name sNameOfWorldField = "World"_Name;

void CollectErrors(ScriptErrorInserter inserter) const;

Expand Down
6 changes: 3 additions & 3 deletions Include/Components/GridSpawnerComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace CE
{
public:
void OnConstruct(World&, entt::entity owner);
void OnBeginPlay(World&, entt::entity);
void OnBeginPlay(World& world, entt::entity);

void ClearGrid();
void SpawnGrid();
void ClearGrid(World& world);
void SpawnGrid(World& world);

std::vector<AssetHandle<Prefab>> mTiles{};

Expand Down
54 changes: 54 additions & 0 deletions Include/Core/ThreadPool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once
#include <future>
#include <queue>

namespace CE
{
class ThreadPool final :
public EngineSubsystem<ThreadPool>
{
friend EngineSubsystem;

ThreadPool();
~ThreadPool(); // joins all threads
public:

template<class F, class... A>
decltype(auto) Enqueue(F&& callable, A &&...arguments);

size_t NumberOfThreads() const { return mThreads.size(); }

private:
std::vector<std::thread> mThreads{};
std::queue<std::packaged_task<void()>> mTasks{};
std::mutex mMutex{};
std::condition_variable mCondition{};
bool mStopped{};
};

// add new work item to the pool
template<class F, class... Args>
decltype(auto) ThreadPool::Enqueue(F&& f, Args&&... args)
{
using return_type = std::invoke_result_t<F, Args...>;

auto task = std::make_shared<std::packaged_task<return_type()>>(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);

std::future<return_type> res = task->get_future();
{
std::unique_lock lock{ mMutex };
mTasks.emplace([task]{ (*task)(); });
}
mCondition.notify_one();
return res;
}


template<typename T>
bool IsFutureReady(const std::future<T>& f)
{
return f.valid() && f.wait_for(std::chrono::seconds{}) == std::future_status::ready;
}
}
9 changes: 5 additions & 4 deletions Include/EditorSystems/AssetEditorSystems/ScriptEditorSystem.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "Utilities/ASync.h"
#ifdef EDITOR
#pragma once
#include "EditorSystems/AssetEditorSystems/AssetEditorSystem.h"

#include <thread>
#include <future>

#include "imnodes/imgui_node_editor.h"
#include "Assets/Script.h"
Expand Down Expand Up @@ -202,8 +201,10 @@ namespace CE
void InitialiseAllNodesTheUserCanAdd();

std::vector<NodeCategory> mAllNodesTheUserCanAdd{};
ASyncThread mNodePopularityCalculateThread{};
bool mShouldWeStopCountingNodePopularity{};

// The popularity is stored inside of each node in mAllNodesTheUserCanAdd
std::future<void> mNodePopularityCalculations{};
std::atomic<bool> mShouldWeStopCountingNodePopularity{};

ax::NodeEditor::PinId mPinTheUserRightClicked{};
ax::NodeEditor::PinId mPinTheUserIsTryingToLink{};
Expand Down
15 changes: 8 additions & 7 deletions Include/EditorSystems/ContentBrowserEditorSystem.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifdef EDITOR
#pragma once
#include <future>

#include "EditorSystems/EditorSystem.h"
#include "Core/AssetManager.h"
#include "Utilities/ASync.h"

namespace CE
{
Expand All @@ -25,18 +26,17 @@ namespace CE
private:
struct ContentFolder
{
ContentFolder() = default;
ContentFolder(const std::filesystem::path& path, std::string&& name, ContentFolder* parent) :
mActualPath(path),
mFolderName(std::move(name)),
mParent(parent)
{}

ContentFolder(const ContentFolder&) = delete;
ContentFolder(ContentFolder&&) noexcept = default;
ContentFolder(ContentFolder&&) noexcept = delete;

ContentFolder& operator=(const ContentFolder&) = delete;
ContentFolder& operator=(ContentFolder&&) noexcept = default;
ContentFolder& operator=(ContentFolder&&) noexcept = delete;

// Will be empty for the root folder
std::filesystem::path mActualPath{};
Expand All @@ -46,6 +46,7 @@ namespace CE
ContentFolder* mParent{};
std::vector<WeakAssetHandle<>> mContent{};
};

void RequestUpdateToFolderGraph();

void DisplayFolderHierarchyPanel();
Expand Down Expand Up @@ -87,11 +88,11 @@ namespace CE
static MetaType Reflect();
REFLECT_AT_START_UP(ContentBrowserEditorSystem);

ContentFolder mRootFolder{ {}, "All", nullptr };
std::reference_wrapper<const ContentFolder> mSelectedFolder = mRootFolder;
std::unique_ptr<ContentFolder> mRootFolder = std::make_unique<ContentFolder>(std::filesystem::path{}, "All", nullptr);
std::reference_wrapper<const ContentFolder> mSelectedFolder = *mRootFolder;
std::filesystem::path mSelectedFolderPath{};

ASyncFuture<ContentFolder> mPendingRootFolder{};
std::future<std::unique_ptr<ContentFolder>> mPendingRootFolder{};

float mFolderHierarchyPanelWidthPercentage = .25f;
float mContentPanelWidthPercentage = .75f;
Expand Down
9 changes: 3 additions & 6 deletions Include/EditorSystems/ImporterSystem.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include "Utilities/ASync.h"
#include "Utilities/Time.h"
#ifdef EDITOR
#include "EditorSystems/EditorSystem.h"

#include <future>

#include "Core/AssetManager.h"
#include "Assets/Core/AssetLoadInfo.h"
#include "Assets/Importers/Importer.h"
#include "Utilities/MemFunctions.h"
#include "Utilities/Time.h"

namespace CE
{
Expand Down Expand Up @@ -51,7 +48,7 @@ namespace CE
struct ImportFuture
{
ImportRequest mImportRequest{};
ASyncFuture<std::optional<std::vector<ImportPreview>>> mImportResult{};
std::future<std::optional<std::vector<ImportPreview>>> mImportResult{};
};

struct DirToWatch
Expand Down Expand Up @@ -113,7 +110,7 @@ namespace CE

std::array<DirToWatch, 2> mDirectoriesToWatch{};
Cooldown mCheckDirectoryCooldown{ 10.0f };
ASyncFuture<std::vector<ImportRequest>> mChangedFilesInDirectoriesToWatch{};
std::future<std::vector<ImportRequest>> mChangedFilesInDirectoriesToWatch{};

static inline bool sExcludeDuplicates{};
static inline bool sIgnoreReadOnly = true;
Expand Down
6 changes: 2 additions & 4 deletions Include/EditorSystems/ThumbnailEditorSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace CE
class Texture;
class World;

using GetThumbnailRet = std::variant<AssetHandle<Texture>, World>;
using GetThumbnailRet = std::variant<AssetHandle<Texture>, std::unique_ptr<World>>;

class ThumbnailEditorSystem final :
public EditorSystem
Expand Down Expand Up @@ -72,9 +72,7 @@ namespace CE

struct CurrentlyGenerating
{
CurrentlyGenerating(World&& world, WeakAssetHandle<> forAsset);

World mWorld;
std::unique_ptr<World> mWorld{};
WeakAssetHandle<> mForAsset{};
FrameBuffer mFrameBuffer{ sGeneratedThumbnailResolution };
Timer mNumSecondsSinceLastRequested{};
Expand Down
9 changes: 1 addition & 8 deletions Include/Scripting/ScriptPin.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
#pragma once
#include "ScriptIds.h"
#include "imnodes/imgui_node_editor.h"

#include "Meta/MetaAny.h"
#include "Meta/MetaTypeTraits.h"
#include "Meta/Fwd/MetaFuncFwd.h"
#include "Meta/MetaTypeTraits.h"
#include "Scripting/ScriptErrors.h"

namespace CE
{
struct MetaFuncNamedParam;
}

namespace CE
{
enum class ScriptPinKind : bool
Expand Down
81 changes: 0 additions & 81 deletions Include/Utilities/ASync.h

This file was deleted.

2 changes: 1 addition & 1 deletion Include/Utilities/Imgui/WorldInspect.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace CE
The provided doUndoStack must remain alive for the duration of this
objects lifetime.
*/
WorldInspectHelper(World&& worldThatHasNotYetBegunPlay);
WorldInspectHelper(std::unique_ptr<World> worldThatHasNotYetBegunPlay);
~WorldInspectHelper();

/*
Expand Down
Loading