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:
parent
eba003a721
commit
bf5033f6bc
4 changed files with 14 additions and 9 deletions
|
@ -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})
|
||||
|
|
|
@ -84,7 +84,7 @@ public:
|
|||
return suspended;
|
||||
}
|
||||
|
||||
async::result<void> yield();
|
||||
async::result<bool> yield();
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
|
8
test.cpp
8
test.cpp
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue