feat: 实现Containers、Memory、Threading核心模块及单元测试
- Containers: String, Array, HashMap 容器实现及测试 - Memory: Allocator, LinearAllocator, PoolAllocator, ProxyAllocator, MemoryManager 实现及测试 - Threading: Mutex, SpinLock, ReadWriteLock, Thread, Task, TaskSystem 实现及测试 - 修复Windows平台兼容性: _aligned_malloc, std::hash特化 - 修复构建错误和测试用例问题
This commit is contained in:
51
engine/include/XCEngine/Threading/TaskGroup.h
Normal file
51
engine/include/XCEngine/Threading/TaskGroup.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include "Task.h"
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Threading {
|
||||
|
||||
class TaskGroup {
|
||||
public:
|
||||
using Callback = std::function<void()>;
|
||||
|
||||
TaskGroup();
|
||||
~TaskGroup();
|
||||
|
||||
uint64_t AddTask(std::unique_ptr<ITask> task);
|
||||
uint64_t AddTask(Callback&& func, TaskPriority priority = TaskPriority::Normal);
|
||||
|
||||
void AddDependency(uint64_t taskId, uint64_t dependsOn);
|
||||
void Wait();
|
||||
bool WaitFor(std::chrono::milliseconds timeout);
|
||||
|
||||
void SetCompleteCallback(Callback&& callback);
|
||||
bool IsComplete() const;
|
||||
float GetProgress() const;
|
||||
|
||||
void Cancel();
|
||||
|
||||
private:
|
||||
struct TaskNode {
|
||||
ITask* task = nullptr;
|
||||
std::vector<uint64_t> dependencies;
|
||||
int pendingDepCount = 0;
|
||||
bool completed = false;
|
||||
};
|
||||
|
||||
std::vector<TaskNode> m_tasks;
|
||||
std::atomic<int> m_pendingCount{0};
|
||||
std::atomic<int> m_completedCount{0};
|
||||
Callback m_completeCallback;
|
||||
mutable std::mutex m_mutex;
|
||||
std::condition_variable m_condition;
|
||||
std::atomic<bool> m_canceled{false};
|
||||
};
|
||||
|
||||
} // namespace Threading
|
||||
} // namespace XCEngine
|
||||
Reference in New Issue
Block a user