coroutines on windows...

This commit is contained in:
Dynamitos
2021-11-19 15:08:56 +01:00
parent 56469a137a
commit 94be513538
11 changed files with 178 additions and 125 deletions
+6 -1
View File
@@ -117,7 +117,12 @@
"string_view": "cpp", "string_view": "cpp",
"rope": "cpp", "rope": "cpp",
"slist": "cpp", "slist": "cpp",
"numbers": "cpp" "numbers": "cpp",
"barrier": "cpp",
"latch": "cpp",
"ranges": "cpp",
"source_location": "cpp",
"syncstream": "cpp"
}, },
"cmake.skipConfigureIfCachePresent": false, "cmake.skipConfigureIfCachePresent": false,
"cmake.configureArgs": [ "cmake.configureArgs": [
+9 -3
View File
@@ -37,6 +37,10 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin/${CMAKE_BUILD
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}/bin/Debug) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}/bin/Debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/bin/Release) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/bin/Release)
set(Boost_USE_STATIC_LIBS OFF)
if(WIN32)
set(Boost_LIB_PREFIX lib)
endif()
find_package(Vulkan REQUIRED) find_package(Vulkan REQUIRED)
find_package(Boost REQUIRED COMPONENTS serialization) find_package(Boost REQUIRED COMPONENTS serialization)
@@ -117,9 +121,11 @@ if(WIN32)
add_custom_target(copy-binaries ALL add_custom_target(copy-binaries ALL
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SLANG_BINARIES} $<TARGET_FILE_DIR:SeeleEngine> COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SLANG_BINARIES} $<TARGET_FILE_DIR:SeeleEngine>
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SLANG_GLSLANG} $<TARGET_FILE_DIR:SeeleEngine> COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SLANG_GLSLANG} $<TARGET_FILE_DIR:SeeleEngine>
#COMMAND ${CMAKE_COMMAND} -E copy_if_different ${GLFW_BINARY} $<TARGET_FILE_DIR:SeeleEngine> COMMAND ${CMAKE_COMMAND} -E copy_if_different ${GLFW_BINARY} $<TARGET_FILE_DIR:SeeleEngine>
#COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ASSIMP_BINARY} $<TARGET_FILE_DIR:SeeleEngine> COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ASSIMP_BINARY} $<TARGET_FILE_DIR:SeeleEngine>
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${NSAM_BINARIES} $<TARGET_FILE_DIR:SeeleEngine>) COMMAND ${CMAKE_COMMAND} -E copy_if_different ${NSAM_BINARIES} $<TARGET_FILE_DIR:SeeleEngine>
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${NSAM_LLVM} $<TARGET_FILE_DIR:SeeleEngine>
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${KTX_BINARIES} $<TARGET_FILE_DIR:SeeleEngine>)
else() else()
add_custom_target(copy-binaries ALL COMMAND "") add_custom_target(copy-binaries ALL COMMAND "")
endif() endif()
+1 -1
View File
@@ -92,7 +92,7 @@ find_package_handle_standard_args(NSAM DEFAULT_MSG NSAM_INCLUDE_DIR NSAM_LIBRARY
if (NSAM_FOUND) if (NSAM_FOUND)
set(NSAM_LIBRARIES ${NSAM_LIBRARY}) set(NSAM_LIBRARIES ${NSAM_LIBRARY})
set(NSAM_INCLUDE_DIRS ${NSAM_INCLUDE_DIR}) set(NSAM_INCLUDE_DIRS ${NSAM_INCLUDE_DIR})
set(NSAM_BINARIES ${NSAM_BINARY} ${NSAM_LLVM}) set(NSAM_BINARIES ${NSAM_BINARY})
endif() endif()
# Hide some variables # Hide some variables
+2 -1
View File
@@ -38,7 +38,8 @@ if (WIN32)
NAMES assimp-vc${MSVC_TOOLSET_VERSION}-mt${CMAKE_DEBUG_POSTFIX}.dll NAMES assimp-vc${MSVC_TOOLSET_VERSION}-mt${CMAKE_DEBUG_POSTFIX}.dll
PATHS PATHS
$ENV{PROGRAMFILES}/bin $ENV{PROGRAMFILES}/bin
${ASSIMP_ROOT}/bin) ${ASSIMP_ROOT}/code
PATH_SUFFIXES Debug Release)
else() else()
# Find include files # Find include files
find_path( find_path(
+9 -9
View File
@@ -48,15 +48,15 @@ if (WIN32)
unset(GLFW_LIBRARY_NAME) unset(GLFW_LIBRARY_NAME)
#find_file( find_file(
# GLFW_BINARY GLFW_BINARY
# NAMES glfw3${CMAKE_DEBUG_POSTFIX}.dll NAMES glfw3${CMAKE_DEBUG_POSTFIX}.dll
# PATHS PATHS
# $ENV{PROGRAMFILES}/bin $ENV{PROGRAMFILES}/bin
# ${CMAKE_BINARY_DIR} ${CMAKE_BINARY_DIR}
# ${GLFW_ROOT}/src ${GLFW_ROOT}/src
# ${GLFW_ROOT}/bin ${GLFW_ROOT}/bin
# PATH_SUFFIXES Debug Release) PATH_SUFFIXES Debug Release)
else() else()
# Find include files # Find include files
find_path( find_path(
+50 -40
View File
@@ -17,47 +17,56 @@
include(FindPackageHandleStandardArgs) include(FindPackageHandleStandardArgs)
if (WIN32) if (WIN32)
# Find include files # Find include files
find_path( find_path(
KTX_INCLUDE_DIR KTX_INCLUDE_DIR
NAMES ktx.h NAMES ktx.h
PATHS PATHS
$ENV{PROGRAMFILES}/include $ENV{PROGRAMFILES}/include
${KTX_ROOT}/include ${KTX_ROOT}/include
DOC "The directory where ktx.h resides") DOC "The directory where ktx.h resides")
# Find library files # Find library files
find_library( find_library(
KTX_LIBRARY KTX_LIBRARY
NAMES ktx${CMAKE_DEBUG_POSTFIX}.lib NAMES ktx${CMAKE_DEBUG_POSTFIX}.lib
PATHS PATHS
$ENV{PROGRAMFILES}/lib $ENV{PROGRAMFILES}/lib
${KTX_ROOT} ${KTX_ROOT}
${KTX_ROOT}/lib ${KTX_ROOT}/lib
PATH_SUFFIXES Debug Release) PATH_SUFFIXES Debug Release)
find_file(
KTX_BINARY
NAMES ktx${CMAKE_DEBUG_POSTFIX}.dll
PATHS
$ENV{PROGRAMFILES}/lib
${KTX_ROOT}
${KTX_ROOT}/lib
PATH_SUFFIXES Debug Release)
else() else()
# Find include files # Find include files
find_path( find_path(
KTX_INCLUDE_DIR KTX_INCLUDE_DIR
NAMES ktx.h NAMES ktx.h
PATHS PATHS
/usr/include /usr/include
/usr/local/include /usr/local/include
/sw/include /sw/include
/opt/local/include /opt/local/include
${KTX_ROOT} ${KTX_ROOT}
DOC "The directory where KTX/ktx.h resides") DOC "The directory where KTX/ktx.h resides")
find_library( find_library(
KTX_LIBRARY KTX_LIBRARY
NAMES libktx${CMAKE_DEBUG_POSTFIX}.so NAMES libktx${CMAKE_DEBUG_POSTFIX}.so
PATHS PATHS
/usr/include /usr/include
/usr/local/include /usr/local/include
/sw/include /sw/include
/opt/local/include /opt/local/include
${KTX_ROOT} ${KTX_ROOT}
DOC "The directory where KTX/libktx.so resides") DOC "The directory where KTX/libktx.so resides")
endif() endif()
# Handle REQUIRD argument, define *_FOUND variable # Handle REQUIRD argument, define *_FOUND variable
@@ -65,8 +74,9 @@ find_package_handle_standard_args(KTX DEFAULT_MSG KTX_INCLUDE_DIR KTX_LIBRARY)
# Define KTX_LIBRARIES and KTX_INCLUDE_DIRS # Define KTX_LIBRARIES and KTX_INCLUDE_DIRS
if (KTX_FOUND) if (KTX_FOUND)
set(KTX_INCLUDE_DIRS ${KTX_INCLUDE_DIR}) set(KTX_INCLUDE_DIRS ${KTX_INCLUDE_DIR})
set(KTX_LIBRARIES ${KTX_LIBRARY}) set(KTX_LIBRARIES ${KTX_LIBRARY})
set(KTX_BINARIES ${KTX_BINARY})
endif() endif()
# Hide some variables # Hide some variables
+1 -1
View File
@@ -73,7 +73,7 @@ find_package_handle_standard_args(SLANG DEFAULT_MSG SLANG_INCLUDE_DIR SLANG_LIBR
# Define SLANG_LIBRARIES and SLANG_INCLUDE_DIRS # Define SLANG_LIBRARIES and SLANG_INCLUDE_DIRS
if (SLANG_FOUND) if (SLANG_FOUND)
set(SLANG_BINARIES ${SLANG_BINARY} ${SLANG_GLSLANG}) set(SLANG_BINARIES ${SLANG_BINARY})
set(SLANG_LIBRARIES ${SLANG_LIBRARY}) set(SLANG_LIBRARIES ${SLANG_LIBRARY})
set(SLANG_INCLUDE_DIRS ${SLANG_INCLUDE_DIR}) set(SLANG_INCLUDE_DIRS ${SLANG_INCLUDE_DIR})
endif() endif()
+2 -2
View File
@@ -287,9 +287,9 @@ public:
return insertedElement; return insertedElement;
} }
// front + popFront // front + popFront
value_type&& retrieve() value_type retrieve()
{ {
auto&& temp = std::move(root->data); value_type temp = std::move(root->data);
popFront(); popFront();
return std::move(temp); return std::move(temp);
} }
+46 -45
View File
@@ -133,57 +133,58 @@ void Allocation::markFree(SubAllocation *allocation)
PSubAllocation allocHandle; PSubAllocation allocHandle;
PSubAllocation freeRangeToDelete; PSubAllocation freeRangeToDelete;
std::unique_lock lck(lock);
//Join lower bound
for (auto freeRange : freeRanges)
{ {
PSubAllocation freeAlloc = freeRange.value; std::unique_lock lck(lock);
if (freeAlloc->allocatedOffset <= lowerBound //Join lower bound
&& freeAlloc->allocatedOffset + freeAlloc->allocatedSize >= upperBound) for (auto freeRange : freeRanges)
{ {
// allocation is already in a free region PSubAllocation freeAlloc = freeRange.value;
return; if (freeAlloc->allocatedOffset <= lowerBound
&& freeAlloc->allocatedOffset + freeAlloc->allocatedSize >= upperBound)
{
// allocation is already in a free region
return;
}
if (freeAlloc->allocatedOffset + freeAlloc->allocatedSize == lowerBound)
{
//extend freeAlloc by the allocatedSize
freeAlloc->allocatedSize += allocation->allocatedSize;
allocHandle = freeAlloc;
break;
}
} }
if (freeAlloc->allocatedOffset + freeAlloc->allocatedSize == lowerBound) //Join upper bound
auto foundAlloc = freeRanges.find(upperBound);
if (foundAlloc != freeRanges.end())
{ {
//extend freeAlloc by the allocatedSize freeRangeToDelete = foundAlloc->value;
freeAlloc->allocatedSize += allocation->allocatedSize; // There is a free allocation ending where the new free one ends
allocHandle = freeAlloc; if (allocHandle != nullptr)
break; {
// extend allocHandle by another foundAlloc->allocatedSize bytes
allocHandle->allocatedSize += foundAlloc->value->allocatedSize;
freeRanges.erase(foundAlloc->key);
}
else
{
// set foundAlloc back by size amount
allocHandle = foundAlloc->value;
allocHandle->allocatedOffset -= allocation->allocatedSize;
allocHandle->alignedOffset -= allocation->allocatedSize;
// place back at correct offset
freeRanges[allocHandle->allocatedOffset] = allocHandle;
// remove from offset map since key changes
freeRanges.erase(foundAlloc->key);
}
} }
if (allocHandle == nullptr)
{
allocHandle = new SubAllocation(this, allocation->alignedOffset, allocation->size, allocation->alignedOffset, allocation->allocatedSize);
freeRanges[allocation->allocatedOffset] = allocHandle;
}
activeAllocations.erase(allocation->allocatedOffset);
} }
//Join upper bound
auto foundAlloc = freeRanges.find(upperBound);
if (foundAlloc != freeRanges.end())
{
freeRangeToDelete = foundAlloc->value;
// There is a free allocation ending where the new free one ends
if (allocHandle != nullptr)
{
// extend allocHandle by another foundAlloc->allocatedSize bytes
allocHandle->allocatedSize += foundAlloc->value->allocatedSize;
freeRanges.erase(foundAlloc->key);
}
else
{
// set foundAlloc back by size amount
allocHandle = foundAlloc->value;
allocHandle->allocatedOffset -= allocation->allocatedSize;
allocHandle->alignedOffset -= allocation->allocatedSize;
// place back at correct offset
freeRanges[allocHandle->allocatedOffset] = allocHandle;
// remove from offset map since key changes
freeRanges.erase(foundAlloc->key);
}
}
if (allocHandle == nullptr)
{
allocHandle = new SubAllocation(this, allocation->alignedOffset, allocation->size, allocation->alignedOffset, allocation->allocatedSize);
freeRanges[allocation->allocatedOffset] = allocHandle;
}
activeAllocations.erase(allocation->allocatedOffset);
lock.unlock();
bytesUsed -= allocation->allocatedSize; bytesUsed -= allocation->allocatedSize;
// TODO: delete allocation when bytesUsed == 0 // TODO: delete allocation when bytesUsed == 0
} }
+41 -22
View File
@@ -47,25 +47,26 @@ ThreadPool::~ThreadPool()
void ThreadPool::addJob(Job&& job) void ThreadPool::addJob(Job&& job)
{ {
std::unique_lock lock(jobQueueLock); std::unique_lock lock(jobQueueLock);
std::cout << "Adding job " << job.id << std::endl;
jobQueue.add(std::move(job)); jobQueue.add(std::move(job));
jobQueueCV.notify_one(); jobQueueCV.notify_one();
} }
void ThreadPool::addJob(MainJob&& job) void ThreadPool::addJob(MainJob&& job)
{ {
std::unique_lock lock(mainJobLock); std::unique_lock lock(mainJobLock);
//std::cout << "Adding main job " << std::addressof(job.handle) << std::endl; std::cout << "Adding main job " << job.id << std::endl;
mainJobs.add(std::move(job)); mainJobs.add(std::move(job));
mainJobCV.notify_one(); mainJobCV.notify_one();
} }
void ThreadPool::enqueueWaiting(Event* event, Job&& job) void ThreadPool::enqueueWaiting(Event* event, Job&& job)
{ {
//std::cout << job << " waiting for event " << event->name << std::endl; std::cout << job.id << " waiting for event " << event->name << std::endl;
std::unique_lock lock(waitingLock); std::unique_lock lock(waitingLock);
waitingJobs[*event].add(std::move(job)); waitingJobs[*event].add(std::move(job));
} }
void ThreadPool::enqueueWaiting(Event* event, MainJob&& job) void ThreadPool::enqueueWaiting(Event* event, MainJob&& job)
{ {
//std::cout << std::addressof(job.handle) << " main waiting for event " << event->name << std::endl; std::cout << job.id << " main waiting for event " << event->name << std::endl;
std::unique_lock lock(waitingMainLock); std::unique_lock lock(waitingMainLock);
waitingMainJobs[*event].add(std::move(job)); waitingMainJobs[*event].add(std::move(job));
} }
@@ -76,8 +77,9 @@ void ThreadPool::notify(Event* event)
std::unique_lock lock2(waitingLock); std::unique_lock lock2(waitingLock);
while(!waitingJobs[*event].empty()) while(!waitingJobs[*event].empty())
{ {
//std::cout << "Waking up job " << job << std::endl; std::cout << "Waking up job " << waitingJobs[*event].front().id << std::endl;
jobQueue.add(std::move(waitingJobs[*event].retrieve())); Job job = std::move(waitingJobs[*event].retrieve());
jobQueue.add(std::move(job));
jobQueueCV.notify_one(); jobQueueCV.notify_one();
} }
} }
@@ -86,37 +88,54 @@ void ThreadPool::notify(Event* event)
std::unique_lock lock2(waitingMainLock); std::unique_lock lock2(waitingMainLock);
while(!waitingMainJobs[*event].empty()) while(!waitingMainJobs[*event].empty())
{ {
std::cout << "Waking up main job " << waitingMainJobs[*event].front().id << std::endl;
mainJobs.add(std::move(waitingMainJobs[*event].retrieve())); mainJobs.add(std::move(waitingMainJobs[*event].retrieve()));
mainJobCV.notify_one(); mainJobCV.notify_one();
} }
} }
} }
void ThreadPool::tryMainJob()
{
MainJob job;
{
std::unique_lock lock(mainJobLock);
if(!mainJobs.empty())
{
job = std::move(mainJobs.retrieve());
}
else
{
return;
}
}
job.resume();
}
void ThreadPool::threadLoop(const bool mainThread) void ThreadPool::threadLoop(const bool mainThread)
{ {
while(running.load()) while(running.load())
{ {
if(mainThread) if(mainThread)
{ {
std::unique_lock lock(mainJobLock); tryMainJob();
if(!mainJobs.empty()) }
Job job;
{
std::unique_lock lock(jobQueueLock);
if(jobQueue.empty())
{ {
MainJob&& job = mainJobs.retrieve(); jobQueueCV.wait(lock);
lock.unlock(); }
job.resume(); if(!jobQueue.empty())
{
job = std::move(jobQueue.retrieve());
}
else
{
continue;
} }
} }
std::unique_lock lock(jobQueueLock); std::cout << "Starting job " << job.id << std::endl;
if(jobQueue.empty()) job.resume();
{
jobQueueCV.wait(lock);
}
if(!jobQueue.empty())
{
Job&& job = jobQueue.retrieve();
lock.unlock();
//std::cout << "Starting job " << job << std::endl;
job.resume();
}
} }
} }
+11
View File
@@ -25,6 +25,7 @@ struct JobPromiseBase
exit(1); exit(1);
}; };
}; };
static std::atomic_uint64_t globalCounter;
template<bool MainJob> template<bool MainJob>
struct JobBase struct JobBase
{ {
@@ -32,10 +33,13 @@ public:
using promise_type = JobPromiseBase<MainJob>; using promise_type = JobPromiseBase<MainJob>;
explicit JobBase() explicit JobBase()
: id(-1)
{} {}
explicit JobBase(std::coroutine_handle<promise_type> handle) explicit JobBase(std::coroutine_handle<promise_type> handle)
: handle(handle) : handle(handle)
, id(globalCounter++)
{ {
std::cout << "Creating job " << id << std::endl;
/*if constexpr(MainJob) /*if constexpr(MainJob)
{ {
std::cout << "Creating mainjob " << handle.address() << std::endl; std::cout << "Creating mainjob " << handle.address() << std::endl;
@@ -48,13 +52,16 @@ public:
JobBase(const JobBase & rhs) = delete; JobBase(const JobBase & rhs) = delete;
JobBase(JobBase&& rhs) JobBase(JobBase&& rhs)
: handle(std::move(rhs.handle)) : handle(std::move(rhs.handle))
, id(std::move(rhs.id))
{ {
rhs.id = -1;
rhs.handle = nullptr; rhs.handle = nullptr;
} }
~JobBase() ~JobBase()
{ {
if(handle) if(handle)
{ {
std::cout << "Destroying job " << id << std::endl;
handle.destroy(); handle.destroy();
} }
} }
@@ -64,6 +71,8 @@ public:
if(this != &rhs) if(this != &rhs)
{ {
handle = std::move(rhs.handle); handle = std::move(rhs.handle);
id = std::move(rhs.id);
rhs.id = -1;
rhs.handle = nullptr; rhs.handle = nullptr;
} }
return *this; return *this;
@@ -73,6 +82,7 @@ public:
handle.resume(); handle.resume();
} }
std::coroutine_handle<promise_type> handle; std::coroutine_handle<promise_type> handle;
uint64 id;
private: private:
}; };
@@ -138,6 +148,7 @@ private:
Map<Event, List<MainJob>> waitingMainJobs; Map<Event, List<MainJob>> waitingMainJobs;
std::mutex waitingMainLock; std::mutex waitingMainLock;
void tryMainJob();
void threadLoop(const bool isMainThread); void threadLoop(const bool isMainThread);
}; };