You're breathtaking!
This commit is contained in:
131
Source/Engine/Threading/ThreadPoolTask.h
Normal file
131
Source/Engine/Threading/ThreadPoolTask.h
Normal file
@@ -0,0 +1,131 @@
|
||||
// Copyright (c) 2012-2020 Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Task.h"
|
||||
#include "Engine/Core/Types/String.h"
|
||||
|
||||
class ThreadPool;
|
||||
|
||||
/// <summary>
|
||||
/// General purpose task executed using Thread Pool.
|
||||
/// </summary>
|
||||
/// <seealso cref="Task" />
|
||||
class ThreadPoolTask : public Task
|
||||
{
|
||||
friend ThreadPool;
|
||||
|
||||
protected:
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ThreadPoolTask"/> class.
|
||||
/// </summary>
|
||||
ThreadPoolTask()
|
||||
: Task()
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
// [Task]
|
||||
String ToString() const override
|
||||
{
|
||||
return String::Format(TEXT("Thread Pool Task ({0})"), ::ToString(GetState()));
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
// [Task]
|
||||
void Enqueue() override;
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// General purpose task executing custom action using Thread Pool.
|
||||
/// </summary>
|
||||
/// <seealso cref="ThreadPoolTask" />
|
||||
/// <seealso cref="Task" />
|
||||
class ThreadPoolActionTask : public ThreadPoolTask
|
||||
{
|
||||
protected:
|
||||
|
||||
Function<void()> _action1;
|
||||
Function<bool()> _action2;
|
||||
Object* _target;
|
||||
|
||||
public:
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ThreadPoolActionTask"/> class.
|
||||
/// </summary>
|
||||
/// <param name="action">The action.</param>
|
||||
/// <param name="target">The target object.</param>
|
||||
ThreadPoolActionTask(Function<void()>& action, Object* target = nullptr)
|
||||
: ThreadPoolTask()
|
||||
, _action1(action)
|
||||
, _target(target)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ThreadPoolActionTask"/> class.
|
||||
/// </summary>
|
||||
/// <param name="action">The action.</param>
|
||||
/// <param name="target">The target object.</param>
|
||||
ThreadPoolActionTask(Function<void()>::Signature action, Object* target = nullptr)
|
||||
: ThreadPoolTask()
|
||||
, _action1(action)
|
||||
, _target(target)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ThreadPoolActionTask"/> class.
|
||||
/// </summary>
|
||||
/// <param name="action">The action.</param>
|
||||
/// <param name="target">The target object.</param>
|
||||
ThreadPoolActionTask(Function<bool()>& action, Object* target = nullptr)
|
||||
: ThreadPoolTask()
|
||||
, _action2(action)
|
||||
, _target(target)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ThreadPoolActionTask"/> class.
|
||||
/// </summary>
|
||||
/// <param name="action">The action.</param>
|
||||
/// <param name="target">The target object.</param>
|
||||
ThreadPoolActionTask(Function<bool()>::Signature action, Object* target = nullptr)
|
||||
: ThreadPoolTask()
|
||||
, _action2(action)
|
||||
, _target(target)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
// [ThreadPoolTask]
|
||||
bool HasReference(Object* obj) const override
|
||||
{
|
||||
return obj == _target;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
// [ThreadPoolTask]
|
||||
bool Run() override
|
||||
{
|
||||
if (_action1.IsBinded())
|
||||
{
|
||||
_action1();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_action2.IsBinded())
|
||||
{
|
||||
return _action2();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user