From ffd992cd74a4caef6918e589f05dfd854d8e5dfc Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Sat, 29 Mar 2025 23:39:44 +0000 Subject: [PATCH] refactor(kernel): improve future implementation Signed-off-by: EnderIce2 --- Kernel/Makefile | 2 +- Kernel/include_std/future | 21 ++++++----- Kernel/include_std/type_traits | 4 +- Kernel/library/std/future.cpp | 8 +++- Kernel/tests/stl/future_test.cpp | 63 ++++++++++++++++++++++---------- 5 files changed, 64 insertions(+), 34 deletions(-) diff --git a/Kernel/Makefile b/Kernel/Makefile index 0585d8f3..17d8342f 100644 --- a/Kernel/Makefile +++ b/Kernel/Makefile @@ -67,7 +67,7 @@ ifeq ($(DEBUG), 1) # CFLAGS += --coverage # CFLAGS += -pg # CFLAGS += -finstrument-functions - CFLAGS += -DDEBUG -ggdb3 -O0 -fdiagnostics-color=always -fstack-usage -fsanitize=undefined + CFLAGS += -DDEBUG -ggdb3 -O0 -fdiagnostics-color=always -fstack-usage -fsanitize=undefined -fdiagnostics-all-candidates ifeq ($(OSARCH), amd64) CFLAGS += -fverbose-asm endif # amd64 diff --git a/Kernel/include_std/future b/Kernel/include_std/future index 980f07ab..85e3c2bb 100644 --- a/Kernel/include_std/future +++ b/Kernel/include_std/future @@ -57,11 +57,7 @@ namespace std }; error_condition make_error_condition(future_errc e) noexcept; - const error_category &future_category() noexcept - { - static const error_category cat; - return cat; - } + const error_category &future_category() noexcept; inline std::error_code make_error_code(future_errc e) noexcept { @@ -155,13 +151,16 @@ namespace std template class promise { + private: public: - promise(); + promise() = default; + template promise(allocator_arg_t, const Allocator &a); - promise(promise &&rhs) noexcept; + + promise(promise &&rhs) noexcept = default; promise(const promise &) = delete; - ~promise(); + ~promise() = default; promise &operator=(promise &&rhs) noexcept; promise &operator=(const promise &) = delete; @@ -176,7 +175,11 @@ namespace std void set_exception(exception_ptr p); - void set_value_at_thread_exit(/* see description */); + void set_value_at_thread_exit(const R &value); + void set_value_at_thread_exit(R &&value); + void set_value_at_thread_exit(R &value); + void set_value_at_thread_exit(); + void set_exception_at_thread_exit(exception_ptr p); }; diff --git a/Kernel/include_std/type_traits b/Kernel/include_std/type_traits index dc610ab2..b38de010 100644 --- a/Kernel/include_std/type_traits +++ b/Kernel/include_std/type_traits @@ -691,6 +691,6 @@ namespace std template using result_of_t = typename result_of::type; - // template - // using invoke_result_t = typename invoke_result::type; + template + using invoke_result_t = typename invoke_result::type; } diff --git a/Kernel/library/std/future.cpp b/Kernel/library/std/future.cpp index 24c2b2ee..3d850b55 100644 --- a/Kernel/library/std/future.cpp +++ b/Kernel/library/std/future.cpp @@ -15,9 +15,13 @@ along with Fennix Kernel. If not, see . */ -// #include +#include namespace std { - + const error_category &future_category() noexcept + { + static const error_category cat; + return cat; + } } diff --git a/Kernel/tests/stl/future_test.cpp b/Kernel/tests/stl/future_test.cpp index 48e4d825..b81617cc 100644 --- a/Kernel/tests/stl/future_test.cpp +++ b/Kernel/tests/stl/future_test.cpp @@ -15,22 +15,45 @@ along with Fennix Kernel. If not, see . */ -// #include -// #include -// #include +#include +#include +#include void test_stl_future() { - // { - // std::future f = std::async(std::launch::async, [] - // { - // for (uint64_t i = 0; i < 100000; ++i); - // return 1; }); + // { + // std::packaged_task task([] + // { return 7; }); + // std::future f1 = task.get_future(); + // std::thread t(std::move(task)); - // debug("waiting for future"); - // int result = f.get(); - // debug("future result is %d", result); - // } + // std::future f2 = std::async(std::launch::async, [] + // { return 8; }); + + // std::promise p; + // std::future f3 = p.get_future(); + // std::thread([&p] + // { p.set_value_at_thread_exit(9); }) + // .detach(); + + // debug("Waiting for futures..."); + // f1.wait(); + // f2.wait(); + // f3.wait(); + // debug("results: %d %d %d", f1.get(), f2.get(), f3.get()); + // t.join(); + // } + + // { + // std::future f = std::async(std::launch::async, [] + // { + // for (uint64_t i = 0; i < 100000; ++i); + // return 1; }); + + // debug("waiting for future"); + // int result = f.get(); + // debug("future result is %d", result); + // } // { // std::promise p; @@ -57,16 +80,16 @@ void test_stl_future() // assert(future.get() == 42); // } - // { - // std::promise p; - // std::future f = p.get_future(); - // std::shared_future sf = f.share(); + // { + // std::promise p; + // std::future f = p.get_future(); + // std::shared_future sf = f.share(); - // p.set_value(42); + // p.set_value(42); - // assert(sf.get() == 42); - // assert(sf.get() == 42); - // } + // assert(sf.get() == 42); + // assert(sf.get() == 42); + // } // { // std::promise p;