Still memory leaks in threadpool all

This commit is contained in:
Dynamitos
2022-03-28 21:18:57 +02:00
parent 3a650237b9
commit 5af5345048
9 changed files with 113 additions and 77 deletions
+1 -1
+1 -1
+1 -1
+34 -17
View File
@@ -11,30 +11,46 @@ List<Promise*> Seele::promises;
//}
Event::Event(const std::string &name, const std::source_location &location)
: name(name)
, location(location)
: state(std::make_shared<EventState>())
{
state->name = name;
state->location = location;
}
Event::Event(const std::source_location &location)
: name(location.function_name())
, location(location)
: state(std::make_shared<EventState>())
{
state->name = location.function_name();
state->location = location;
}
Event::Event(const Event& other)
{
std::scoped_lock lock(other.eventLock);
state = other.state;
}
Event::Event(Event&& other)
: name(std::move(other.name))
, location(std::move(other.location))
{
std::scoped_lock lock(other.eventLock);
state = std::move(other.state);
}
Event& Event::operator=(const Event& other)
{
if(this != &other)
{
std::scoped_lock lock(eventLock, other.eventLock);
state = other.state;
}
return *this;
}
Event& Event::operator=(Event&& other)
{
if(this != &other)
{
std::scoped_lock lock(eventLock, other.eventLock);
name = std::move(other.name);
location = std::move(other.location);
state = std::move(other.state);
}
return *this;
}
@@ -42,26 +58,26 @@ Event& Event::operator=(Event&& other)
void Event::raise()
{
std::scoped_lock lock(eventLock);
data = true;
if(waitingJobs.size() > 0)
state->data = true;
if(state->waitingJobs.size() > 0)
{
getGlobalThreadPool().scheduleBatch(waitingJobs);
getGlobalThreadPool().scheduleBatch(state->waitingJobs);
}
if(waitingMainJobs.size() > 0)
if(state->waitingMainJobs.size() > 0)
{
getGlobalThreadPool().scheduleBatch(waitingMainJobs);
getGlobalThreadPool().scheduleBatch(state->waitingMainJobs);
}
}
void Event::reset()
{
std::scoped_lock lock(eventLock);
data = false;
state->data = false;
}
bool Event::await_ready()
{
eventLock.lock();
bool result = data;
bool result = state->data;
if(result)
{
eventLock.unlock();
@@ -72,14 +88,14 @@ bool Event::await_ready()
void Event::await_suspend(std::coroutine_handle<JobPromiseBase<false>> h)
{
//h.promise().enqueue(this);
waitingJobs.add(JobBase<false>(&h.promise()));
state->waitingJobs.add(JobBase<false>(&h.promise()));
eventLock.unlock();
}
void Event::await_suspend(std::coroutine_handle<JobPromiseBase<true>> h)
{
//h.promise().enqueue(this);
waitingMainJobs.add(JobBase<true>(&h.promise()));
state->waitingMainJobs.add(JobBase<true>(&h.promise()));
eventLock.unlock();
}
@@ -117,6 +133,7 @@ void ThreadPool::waitIdle()
std::unique_lock lock(numIdlingLock);
if(numIdling == workers.size())
{
assert(promises.size() == 0);
return;
}
numIdlingIncr.wait(lock);
+51 -32
View File
@@ -19,35 +19,35 @@ public:
//Event(nullptr_t);
Event(const std::string& name, const std::source_location& location = std::source_location::current());
Event(const std::source_location& location = std::source_location::current());
Event(const Event& other) = delete;
Event(const Event& other);
Event(Event&& other);
~Event() = default;
Event& operator=(const Event& other) = delete;
Event& operator=(const Event& other);
Event& operator=(Event&& other);
auto operator<=>(const Event& other) const
{
return name <=> other.name;
return state->name <=> other.state->name;
}
bool operator==(const Event& other) const
{
return name == other.name;
return state->name == other.state->name;
}
operator bool()
{
std::scoped_lock lock(eventLock);
return data;
return state->data;
}
friend std::ostream& operator<<(std::ostream& stream, const Event& event)
{
stream
<< event.location.file_name()
<< event.state->location.file_name()
<< "("
<< event.location.line()
<< event.state->location.line()
<< ":"
<< event.location.column()
<< event.state->location.column()
<< "): "
<< event.location.function_name();
<< event.state->location.function_name();
return stream;
}
@@ -58,12 +58,16 @@ public:
void await_suspend(std::coroutine_handle<JobPromiseBase<true>> h);
constexpr void await_resume() {}
private:
mutable std::mutex eventLock;
struct EventState
{
std::string name;
std::source_location location;
std::mutex eventLock;
bool data = false;
Array<JobBase<false>> waitingJobs;
Array<JobBase<true>> waitingMainJobs;
};
std::shared_ptr<EventState> state;
friend class ThreadPool;
};
@@ -81,14 +85,14 @@ struct JobPromiseBase
DONE
};
JobPromiseBase(const std::source_location& location = std::source_location::current())
: pad0(0x7472617453)
: pad0(12345)//0x7472617453)
, handle(std::coroutine_handle<JobPromiseBase<MainJob>>::from_promise(*this))
, waitingFor(nullptr)
, continuation(nullptr)
, numRefs(0)
, finishedEvent(location)
, state(State::READY)
, pad1(0x646E45)
, pad1(12345)//0x646E45)
{
if constexpr(!MainJob)
{
@@ -119,6 +123,7 @@ struct JobPromiseBase
void resume()
{
validate();
if(!handle || handle.done() || executing())
{
return;
@@ -128,6 +133,7 @@ struct JobPromiseBase
}
void setContinuation(JobPromiseBase* cont)
{
validate();
assert(cont->ready());
continuation = cont;
cont->state = State::SCHEDULED;
@@ -136,32 +142,39 @@ struct JobPromiseBase
}
bool done()
{
validate();
return state == State::DONE;
}
bool scheduled()
{
validate();
return state == State::SCHEDULED;
}
bool waiting()
{
validate();
return state == State::WAITING;
}
bool executing()
{
validate();
return state == State::EXECUTING;
}
bool ready()
{
validate();
return state == State::READY;
}
void enqueue(Event* event);
bool schedule();
void addRef()
{
validate();
numRefs++;
}
void removeRef()
{
validate();
numRefs--;
if(numRefs == 0)
{
@@ -171,6 +184,11 @@ struct JobPromiseBase
}
}
}
void validate()
{
assert(pad0 == 12345);
assert(pad1 == 12345);
}
uint64 pad0;
std::coroutine_handle<JobPromiseBase> handle;
Event* waitingFor;
@@ -199,8 +217,11 @@ public:
JobBase(const JobBase& other)
{
promise = other.promise;
if(promise != nullptr)
{
promise->addRef();
}
}
JobBase(JobBase&& other)
{
promise = other.promise;
@@ -223,8 +244,11 @@ public:
promise->removeRef();
}
promise = other.promise;
if(promise != nullptr)
{
promise->addRef();
}
}
return *this;
}
JobBase& operator=(JobBase&& other)
@@ -245,15 +269,15 @@ public:
promise->resume();
}
template<std::invocable Callable>
inline JobBase&& then(Callable callable)
inline JobBase then(Callable callable)
{
return then(callable());
}
JobBase&& then(JobBase&& continuation)
JobBase then(JobBase continuation)
{
promise->setContinuation(continuation.promise);
promise->schedule();
return std::move(continuation);
continuation.promise->state = JobPromiseBase<MainJob>::State::SCHEDULED;
return continuation;
}
bool done()
{
@@ -267,9 +291,11 @@ public:
return promise->finishedEvent;
}
static JobBase all() = delete;
template<std::ranges::range Iterable>
requires std::same_as<std::ranges::range_value_t<Iterable>, JobBase<MainJob>>
static JobBase<MainJob> all(Iterable collection);
template<std::ranges::range Iterable>
static JobBase all(Iterable collection)
{
@@ -278,10 +304,11 @@ public:
co_await it;
}
}
template<typename... Awaitable>
static JobBase all(Awaitable&&... jobs)
static JobBase all(Awaitable... jobs)
{
return JobBase::all(Array{jobs...});
return JobBase::all(std::initializer_list{jobs...});
}
template<typename JobFunc, std::ranges::input_range Iterable>
requires std::invocable<JobFunc, std::ranges::range_reference_t<Iterable>>
@@ -299,7 +326,7 @@ using Promise = JobPromiseBase<false>;
class ThreadPool
{
public:
ThreadPool(uint32 threadCount = 1);//std::thread::hardware_concurrency());
ThreadPool(uint32 threadCount = std::thread::hardware_concurrency());
virtual ~ThreadPool();
void waitIdle();
void scheduleJob(Job job);
@@ -313,7 +340,7 @@ public:
{
//job.promise->addRef();
job.promise->state = JobPromiseBase<true>::State::SCHEDULED;
mainJobs.add(job);
mainJobs.add(std::move(job));
}
mainJobCV.notify_one();
}
@@ -326,7 +353,7 @@ public:
{
//job.promise->addRef();
job.promise->state = JobPromiseBase<false>::State::SCHEDULED;
jobQueue.add(job);
jobQueue.add(std::move(job));
}
jobQueueCV.notify_all();
}
@@ -360,12 +387,14 @@ inline JobBase<MainJob> JobPromiseBase<MainJob>::get_return_object() noexcept
template<bool MainJob>
inline auto JobPromiseBase<MainJob>::initial_suspend() noexcept
{
validate();
return std::suspend_always{};
}
template<bool MainJob>
inline auto JobPromiseBase<MainJob>::final_suspend() noexcept
{
validate();
state = State::DONE;
finishedEvent.raise();
if(continuation)
@@ -375,20 +404,10 @@ inline auto JobPromiseBase<MainJob>::final_suspend() noexcept
}
return std::suspend_always{};
}
//template<bool MainJob>
//inline void JobPromiseBase<MainJob>::enqueue(Event* event)
//{
// if(!handle || handle.done() || waiting() || scheduled())
// {
// return;
// }
// state = State::WAITING;
// waitingFor = event;
// getGlobalThreadPool().enqueueWaiting(event, std::move(JobBase<MainJob>(this)));
//}
template<bool MainJob>
inline bool JobPromiseBase<MainJob>::schedule()
{
validate();
if(!handle || done() || !ready())
{
return false;
+12 -12
View File
@@ -20,13 +20,13 @@ Seele::SceneView::SceneView(Gfx::PGraphics graphics, PWindow owner, const Viewpo
{
scene = new Scene(graphics);
scene->addActor(activeCamera);
AssetRegistry::importFile("/home/dynamitos/TestSeeleProject/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Body_Diffuse.png");
AssetRegistry::importFile("/home/dynamitos/TestSeeleProject/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Body_Lightmap.png");
AssetRegistry::importFile("/home/dynamitos/TestSeeleProject/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Face_Diffuse.png");
AssetRegistry::importFile("/home/dynamitos/TestSeeleProject/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Hair_Diffuse.png");
AssetRegistry::importFile("/home/dynamitos/TestSeeleProject/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Hair_Lightmap.png");
AssetRegistry::importFile("/home/dynamitos/TestSeeleProject/Assets/Ayaka/Avatar_Girl_Tex_FaceLightmap.png");
AssetRegistry::importFile("/home/dynamitos/TestSeeleProject/Assets/Ayaka/Ayaka.fbx");
AssetRegistry::importFile("C:\\Users\\Dynamitos\\TestSeeleProject\\Assets\\Ayaka\\Avatar_Girl_Sword_Ayaka_Tex_Body_Diffuse.png");
AssetRegistry::importFile("C:\\Users\\Dynamitos\\TestSeeleProject\\Assets\\Ayaka\\Avatar_Girl_Sword_Ayaka_Tex_Body_Lightmap.png");
AssetRegistry::importFile("C:\\Users\\Dynamitos\\TestSeeleProject\\Assets\\Ayaka\\Avatar_Girl_Sword_Ayaka_Tex_Face_Diffuse.png");
AssetRegistry::importFile("C:\\Users\\Dynamitos\\TestSeeleProject\\Assets\\Ayaka\\Avatar_Girl_Sword_Ayaka_Tex_Hair_Diffuse.png");
AssetRegistry::importFile("C:\\Users\\Dynamitos\\TestSeeleProject\\Assets\\Ayaka\\Avatar_Girl_Sword_Ayaka_Tex_Hair_Lightmap.png");
AssetRegistry::importFile("C:\\Users\\Dynamitos\\TestSeeleProject\\Assets\\Ayaka\\Avatar_Girl_Tex_FaceLightmap.png");
AssetRegistry::importFile("C:\\Users\\Dynamitos\\TestSeeleProject\\Assets\\Ayaka\\Ayaka.fbx");
PPrimitiveComponent ayaka = new PrimitiveComponent(AssetRegistry::findMesh("Ayaka"));
ayaka->setRelativeLocation(Vector(0, 0, 0));
ayaka->setRelativeScale(Vector(10, 10, 10));
@@ -34,12 +34,12 @@ Seele::SceneView::SceneView(Gfx::PGraphics graphics, PWindow owner, const Viewpo
//AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Ely\\Ely.fbx");
//AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Cube\\cube.obj");
//AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Plane\\plane.fbx");
//PPrimitiveComponent plane = new PrimitiveComponent(AssetRegistry::findMesh("plane"));
//plane->setRelativeScale(Vector(100, 100, 100));
//scene->addPrimitiveComponent(plane);
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Plane\\plane.fbx");
PPrimitiveComponent plane = new PrimitiveComponent(AssetRegistry::findMesh("plane"));
plane->setRelativeScale(Vector(100, 100, 100));
scene->addPrimitiveComponent(plane);
for(uint32 i = 0; i < 100000; ++i)
for(uint32 i = 0; i < 10; ++i)
{
PMyComponent myComp = new MyComponent();
PMyOtherComponent myOtherComp = new MyOtherComponent();
+1 -1
View File
@@ -9,7 +9,7 @@ using namespace Seele;
int main()
{
PWindowManager windowManager = new WindowManager();
AssetRegistry::init("/home/dynamitos/TestSeeleProject/");
AssetRegistry::init("C:\\Users\\Dynamitos\\TestSeeleProject");
WindowCreateInfo mainWindowInfo;
mainWindowInfo.title = "SeeleEngine";
mainWindowInfo.width = 1280;
+2
View File
@@ -7,7 +7,9 @@ include_directories(${Boost_INCLUDE_DIRS})
add_subdirectory(Engine/)
target_link_libraries(Seele_unit_tests ${Boost_LIBRARIES})
if(UNIX)
target_compile_definitions(Seele_unit_tests PRIVATE -DBOOST_TEST_DYN_LINK)
endif()
target_precompile_headers(Seele_unit_tests
PRIVATE
+2 -4
View File
@@ -1,5 +1,5 @@
#pragma once
//#include <vld.h>
#include <vld.h>
namespace Seele
{
@@ -7,16 +7,14 @@ namespace Seele
{
GlobalFixture()
{
//_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
}
~GlobalFixture();
void setup()
{
//Fibers::JobQueue::initJobQueues();
}
void teardown()
{
//Fibers::JobQueue::cleanupJobQueues();
}
};
};