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
2 changes: 1 addition & 1 deletion CMake/Target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ function(AddExecutable)
endif()

if (${MSVC})
set_target_properties(${PARAMS_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$<CONFIG>)
set_target_properties(${PARAMS_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
endif()
endfunction()

Expand Down
2 changes: 1 addition & 1 deletion Editor/Include/Editor/QmlEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ namespace Editor {

Common::Path qmlSourceDir;
std::unordered_set<QmlWidget*> widgets;
Common::UniqueRef<QFileSystemWatcher> watcher;
Common::UniquePtr<QFileSystemWatcher> watcher;
};
}
2 changes: 1 addition & 1 deletion Editor/Src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main(int argc, char* argv[])
auto& qmlEngine = Editor::QmlEngine::Get();
qmlEngine.Start();

Common::UniqueRef<QWidget> mainWidget;
Common::UniquePtr<QWidget> mainWidget;
#if BUILD_CONFIG_DEBUG
if (caRunSample.GetValue()) {
mainWidget = new Editor::WidgetSamples();
Expand Down
67 changes: 44 additions & 23 deletions Engine/Source/Common/Include/Common/Concurrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

#pragma once

#include <string>
#include <vector>
#include <queue>
#include <mutex>
#include <thread>
#include <future>
#include <memory>
#include <functional>
#include <type_traits>

#include <Common/String.h>
#include <Common/Debug.h>
#include <Common/Memory.h>
#include <Common/Utility.h>

namespace Common {
Expand All @@ -24,8 +24,7 @@ namespace Common {

NamedThread();

template <typename F, typename... Args>
explicit NamedThread(const std::string& name, F&& task, Args&&... args);
template <typename F> explicit NamedThread(const std::string& name, F&& task);

void Join();

Expand All @@ -40,10 +39,12 @@ namespace Common {
ThreadPool(const std::string& name, uint8_t threadNum);
~ThreadPool();

template <typename F, typename... Args>
auto EmplaceTask(F&& task, Args&&... args);
template <typename F> auto EmplaceTask(F&& task);
template <typename F> void ExecuteTasks(size_t taskNum, F&& task);

private:
template <typename Ret> auto EmplaceTaskInternal(Common::SharedPtr<std::packaged_task<Ret()>> packedTask);

bool stop;
std::mutex mutex;
std::condition_variable condition;
Expand All @@ -58,8 +59,7 @@ namespace Common {

void Flush();

template <typename F, typename... Args>
auto EmplaceTask(F&& task, Args&&... args);
template <typename F> auto EmplaceTask(F&& task);

private:
bool stop;
Expand All @@ -73,35 +73,56 @@ namespace Common {
}

namespace Common {
template <typename F, typename... Args>
NamedThread::NamedThread(const std::string& name, F&& task, Args&& ... args)
template <typename F>
NamedThread::NamedThread(const std::string& name, F&& task)
{
thread = std::thread([this, task = std::forward<F>(task), name](Args&&... args) -> void {
thread = std::thread([this, task, name]() -> void {
SetThreadName(name);
task(args...);
}, std::forward<Args>(args)...);
task();
});
}

template <typename F, typename... Args>
auto ThreadPool::EmplaceTask(F&& task, Args&& ... args)
template <typename F>
auto ThreadPool::EmplaceTask(F&& task)
{
using RetType = std::invoke_result_t<F, Args...>;
auto packagedTask = std::make_shared<std::packaged_task<RetType()>>(std::bind(std::forward<F>(task), std::forward<Args>(args)...));
auto result = packagedTask->get_future();
using RetType = std::invoke_result_t<F>;
return EmplaceTaskInternal<RetType>(Common::MakeShared<std::packaged_task<RetType()>>(task));
}

template <typename F>
void ThreadPool::ExecuteTasks(size_t taskNum, F&& task)
{
using RetType = std::invoke_result_t<F, size_t>;

std::vector<std::future<RetType>> futures;
futures.reserve(taskNum);
for (size_t i = 0; i < taskNum; i++) {
futures.emplace_back(EmplaceTaskInternal<RetType>(Common::MakeShared<std::packaged_task<RetType()>>(std::bind(std::forward<F>(task), i))));
}

for (const auto& future : futures) {
future.wait();
}
}

template <typename Ret>
auto ThreadPool::EmplaceTaskInternal(Common::SharedPtr<std::packaged_task<Ret()>> packedTask)
{
auto result = packedTask->get_future();
{
std::unique_lock lock(mutex);
Assert(!stop);
tasks.emplace([packagedTask]() -> void { (*packagedTask)(); });
tasks.emplace([packedTask]() -> void { (*packedTask)(); });
}
condition.notify_one();
return result;
}

template <typename F, typename... Args>
auto WorkerThread::EmplaceTask(F&& task, Args&& ... args)
template <typename F>
auto WorkerThread::EmplaceTask(F&& task)
{
using RetType = std::invoke_result_t<F, Args...>;
auto packagedTask = std::make_shared<std::packaged_task<RetType()>>(std::bind(std::forward<F>(task), std::forward<Args>(args)...));
using RetType = std::invoke_result_t<F>;
auto packagedTask = Common::MakeShared<std::packaged_task<RetType()>>(task);
auto result = packagedTask->get_future();
{
std::unique_lock lock(mutex);
Expand Down
Loading
Loading