| 35 | } |
| 36 | |
| 37 | void Uthread_switch(benchmark::State& state) { |
| 38 | async_simple::executors::SimpleExecutor executor(10); |
| 39 | auto test = [&]() { |
| 40 | auto Job = [&]() -> Future<int> { |
| 41 | Promise<int> p; |
| 42 | auto f = p.getFuture().via(&executor); |
| 43 | delayedTask( |
| 44 | &executor, |
| 45 | [p = std::move(p)]() mutable { |
| 46 | auto value = 1024; |
| 47 | p.setValue(value); |
| 48 | }, |
| 49 | 100); |
| 50 | return f; |
| 51 | }; |
| 52 | |
| 53 | constexpr size_t n = 10; |
| 54 | std::atomic<int> running = n; |
| 55 | |
| 56 | auto UthreadTask = [&executor, &running, &Job]() { |
| 57 | Uthread task(Attribute{&executor}, [&running, &Job]() { |
| 58 | await(Job()); |
| 59 | running--; |
| 60 | }); |
| 61 | task.detach(); |
| 62 | }; |
| 63 | |
| 64 | for (size_t i = 0; i < n; i++) |
| 65 | executor.schedule(UthreadTask); |
| 66 | |
| 67 | while (running) { |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | for ([[maybe_unused]] const auto& _ : state) |
| 72 | test(); |
| 73 | } |
| 74 | |
| 75 | void Uthread_async(benchmark::State& state) { |
| 76 | async_simple::executors::SimpleExecutor executor(10); |