From 0051e6e1af5baba158d75f12a3638429997b4964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dietmar=20K=C3=BChl?= Date: Mon, 23 Mar 2026 20:32:35 +0000 Subject: [PATCH 1/2] added a TLS using example for task --- examples/CMakeLists.txt | 1 + examples/alloc-2.cpp | 83 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 examples/alloc-2.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index de068e5..a1539d0 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -33,6 +33,7 @@ if(NOT MSVC) APPEND ALL_EXAMPLES task-sender alloc-1 + alloc-2 bulk c++now-allocator c++now-cancel diff --git a/examples/alloc-2.cpp b/examples/alloc-2.cpp new file mode 100644 index 0000000..0213cf4 --- /dev/null +++ b/examples/alloc-2.cpp @@ -0,0 +1,83 @@ +// examples/alloc-1.cpp -*-C++-*- +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace ex = beman::execution; + +// ---------------------------------------------------------------------------- + +struct tls_allocator + : std::pmr::polymorphic_allocator { + thread_local static std::pmr::memory_resource* alloc; + + tls_allocator(): std::pmr::polymorphic_allocator(alloc) {} + + static void set(std::pmr::memory_resource* a) { alloc = a; } +}; +thread_local std::pmr::memory_resource* tls_allocator::alloc{std::pmr::new_delete_resource()}; + +// ---------------------------------------------------------------------------- + +void* operator new(std::size_t n) { + auto p = std::malloc(n); + std::cout << " global new(" << n << ")->" << p << "\n"; + return p; +} +void operator delete(void* ptr) noexcept { + std::cout << " global operator delete(" << ptr << ")\n"; + std::free(ptr); +} +void operator delete(void* ptr, std::size_t size) noexcept { + std::cout << " global operator delete(" << ptr << ", " << size << ")\n"; + std::free(ptr); +} + +struct resource : std::pmr::memory_resource { + void* do_allocate(std::size_t n, std::size_t) override { + auto p{std::malloc(n)}; + std::cout << " resource::allocate(" << n << ")->" << p << "\n"; + return p; + } + void do_deallocate(void* p, std::size_t n, std::size_t) override { + std::cout << " resource::deallocate(" << p << ", " << n << ")\n"; + std::free(p); + } + bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { return this == &other; } +}; + +// ---------------------------------------------------------------------------- + +using allocator_type = tls_allocator; +struct alloc_env { + using allocator_type = ::allocator_type; +}; +template +using a_task = ex::task; + +a_task async_fun(int value) { co_return value; } + +int main(int ac, char*[]) { + resource res{}; + + std::cout << "not setting up an allocator:\n"; + ex::sync_wait([ac]() -> a_task<> { + auto result{co_await async_fun(ac)}; + std::cout << " result=" << result << "\n"; + }()); + + std::cout << "setting up an allocator:\n"; + tls_allocator::set(&res); + ex::sync_wait([ac]() -> a_task<> { + auto result{co_await async_fun(ac)}; + std::cout << " result=" << result << "\n"; + }()); +} From ebc86d46f0887a13f68d72bfce66b338c5606cf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dietmar=20K=C3=BChl?= Date: Mon, 23 Mar 2026 20:34:32 +0000 Subject: [PATCH 2/2] fix formatting --- examples/alloc-2.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/alloc-2.cpp b/examples/alloc-2.cpp index 0213cf4..1877661 100644 --- a/examples/alloc-2.cpp +++ b/examples/alloc-2.cpp @@ -15,11 +15,10 @@ namespace ex = beman::execution; // ---------------------------------------------------------------------------- -struct tls_allocator - : std::pmr::polymorphic_allocator { +struct tls_allocator : std::pmr::polymorphic_allocator { thread_local static std::pmr::memory_resource* alloc; - tls_allocator(): std::pmr::polymorphic_allocator(alloc) {} + tls_allocator() : std::pmr::polymorphic_allocator(alloc) {} static void set(std::pmr::memory_resource* a) { alloc = a; } }; @@ -66,7 +65,7 @@ using a_task = ex::task; a_task async_fun(int value) { co_return value; } int main(int ac, char*[]) { - resource res{}; + resource res{}; std::cout << "not setting up an allocator:\n"; ex::sync_wait([ac]() -> a_task<> {