1
0
Fork 0
mirror of https://gitlab.com/niansa/cosched.git synced 2025-03-06 20:53:26 +01:00

Changed the way termination is handled

This commit is contained in:
niansa/tuxifan 2023-05-04 11:27:37 +02:00
parent eba003a721
commit bf5033f6bc
4 changed files with 14 additions and 9 deletions

View file

@ -11,5 +11,8 @@ add_library(cosched STATIC scheduler.cpp include/scheduler.hpp)
target_link_libraries(cosched PUBLIC async)
target_include_directories(cosched PUBLIC include/)
#add_executable(test test.cpp)
#target_link_libraries(test PRIVATE cosched)
install(TARGETS cosched
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

View file

@ -84,7 +84,7 @@ public:
return suspended;
}
async::result<void> yield();
async::result<bool> yield();
};

View file

@ -5,18 +5,20 @@
async::result<void> CoSched::Task::yield() {
async::result<bool> CoSched::Task::yield() {
if (state == TaskState::terminating) {
// If it was terminating, it can finally be declared dead now
state = TaskState::dead;
} else {
// It's just sleeping otherwise
state = TaskState::sleeping;
co_return false;
}
// It's just sleeping
state = TaskState::sleeping;
// Let's wait until we're back up!
stopped_at = std::chrono::system_clock::now();
co_await resume.async_wait();
// Here we go, let's keep going...
state = TaskState::running;
co_return true;
}

View file

@ -5,10 +5,10 @@
async::result<void> test_task(CoSched::TaskPtr t) {
for (unsigned x = 100; x != 0; x--) {
std::cout << t->get_name() << ": " << x << '\n';
co_await t->yield();
async::result<void> test_task(CoSched::TaskPtr task) {
for (unsigned x = 100; co_await task->yield(); x--) {
std::cout << task->get_name() << ": " << x << '\n';
if (x == 10) task->terminate();
}
}