Skip to content

Commit 1af2fe8

Browse files
committed
async_lock_lf
1 parent 601aa33 commit 1af2fe8

File tree

10 files changed

+159
-2001
lines changed

10 files changed

+159
-2001
lines changed

include/pot/coroutines/task.h

Lines changed: 43 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -7,78 +7,11 @@
77
#include <type_traits>
88
#include <utility>
99
#include <variant>
10-
#include <memory>
1110

1211
#include "pot/memory/coro_memory.h"
1312

14-
namespace pot::coroutines
15-
{
16-
template<typename T>
17-
class task;
18-
19-
template<typename T>
20-
class lazy_task;
21-
}
22-
23-
namespace std
24-
{
25-
26-
template <typename T, typename... Args>
27-
struct coroutine_traits<pot::coroutines::lazy_task<T>, Args...>
28-
{
29-
using promise_type = typename pot::coroutines::lazy_task<T>::promise_type;
30-
};
31-
} // namespace std
32-
33-
namespace pot::traits
34-
{
35-
template <typename T> struct is_lazy_task : std::false_type
36-
{
37-
};
38-
template <typename U> struct is_lazy_task<pot::coroutines::lazy_task<U>> : std::true_type
39-
{
40-
};
41-
template <typename T> inline constexpr bool is_lazy_task_v = is_lazy_task<T>::value;
42-
43-
template <typename T> struct awaitable_value
44-
{
45-
using type = T;
46-
};
47-
template <typename U> struct awaitable_value<coroutines::lazy_task<U>>
48-
{
49-
using type = U;
50-
};
51-
template <typename T> using awaitable_value_t = typename awaitable_value<T>::type;
52-
template <typename T> struct is_task : std::false_type
53-
{
54-
};
55-
template <typename U> struct is_task<pot::coroutines::task<U>> : std::true_type
56-
{
57-
};
58-
template <typename T> inline constexpr bool is_task_v = is_task<T>::value;
59-
60-
template <typename U> struct awaitable_value<coroutines::task<U>>
61-
{
62-
using type = U;
63-
};
64-
} // namespace pot::traits
65-
66-
67-
namespace pot::coroutines
68-
{
69-
struct task_meta
70-
{
71-
std::atomic<size_t> run_count{0};
72-
73-
// ~task_meta() {
74-
// std::printf("Task finished with run_count = %zu\n", run_count.load());
75-
// }
76-
};
77-
}
78-
7913
namespace pot::coroutines::detail
8014
{
81-
8215
template <typename T> struct basic_promise_type
8316
{
8417
using value_type = T;
@@ -172,7 +105,6 @@ template <typename T> struct basic_promise_type
172105
std::atomic<bool> m_ready{false};
173106
std::atomic<void *> m_awaiter{nullptr};
174107
variant_type m_data{std::monostate{}};
175-
std::shared_ptr<pot::coroutines::task_meta> m_meta{std::make_shared<pot::coroutines::task_meta>()};
176108
};
177109
} // namespace pot::coroutines::detail
178110

@@ -291,17 +223,6 @@ template <typename T> class task
291223
}
292224
}
293225

294-
std::shared_ptr<pot::coroutines::task_meta> meta() const
295-
{
296-
if (m_handle) return m_handle.promise().m_meta;
297-
return nullptr;
298-
}
299-
300-
void set_meta(std::shared_ptr<pot::coroutines::task_meta> meta)
301-
{
302-
if (m_handle) m_handle.promise().m_meta = std::move(meta);
303-
}
304-
305226
~task()
306227
{
307228
if (m_handle)
@@ -412,18 +333,6 @@ template <typename T> class lazy_task
412333
}
413334
}
414335

415-
std::shared_ptr<pot::coroutines::task_meta> meta() const
416-
{
417-
if (m_handle) return m_handle.promise().m_meta;
418-
return nullptr;
419-
}
420-
421-
void set_meta(std::shared_ptr<pot::coroutines::task_meta> meta)
422-
{
423-
if (m_handle) m_handle.promise().m_meta = std::move(meta);
424-
}
425-
426-
427336
~lazy_task()
428337
{
429338
if (m_handle)
@@ -507,3 +416,46 @@ template <> struct lazy_task_promise_type_impl<void> final : detail::basic_promi
507416
};
508417

509418
} // namespace pot::coroutines
419+
420+
namespace std
421+
{
422+
423+
template <typename T, typename... Args>
424+
struct coroutine_traits<pot::coroutines::lazy_task<T>, Args...>
425+
{
426+
using promise_type = typename pot::coroutines::lazy_task<T>::promise_type;
427+
};
428+
} // namespace std
429+
430+
namespace pot::traits
431+
{
432+
template <typename T> struct is_lazy_task : std::false_type
433+
{
434+
};
435+
template <typename U> struct is_lazy_task<pot::coroutines::lazy_task<U>> : std::true_type
436+
{
437+
};
438+
template <typename T> inline constexpr bool is_lazy_task_v = is_lazy_task<T>::value;
439+
440+
template <typename T> struct awaitable_value
441+
{
442+
using type = T;
443+
};
444+
template <typename U> struct awaitable_value<coroutines::lazy_task<U>>
445+
{
446+
using type = U;
447+
};
448+
template <typename T> using awaitable_value_t = typename awaitable_value<T>::type;
449+
template <typename T> struct is_task : std::false_type
450+
{
451+
};
452+
template <typename U> struct is_task<pot::coroutines::task<U>> : std::true_type
453+
{
454+
};
455+
template <typename T> inline constexpr bool is_task_v = is_task<T>::value;
456+
457+
template <typename U> struct awaitable_value<coroutines::task<U>>
458+
{
459+
using type = U;
460+
};
461+
} // namespace pot::traits

include/pot/executors/executor.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ class executor
1212
protected:
1313
std::string m_name;
1414

15-
virtual void derived_execute(std::function<void()> &&func,
16-
std::shared_ptr<pot::coroutines::task_meta> meta = nullptr) = 0;
15+
virtual void derived_execute(std::function<void()> &&func) = 0;
1716

1817
public:
1918
explicit executor(std::string name) : m_name(std::move(name)) {}
@@ -27,19 +26,13 @@ class executor
2726

2827
[[nodiscard]] std::string name() const { return m_name; }
2928

30-
void schedule_task(std::function<void()> func, std::shared_ptr<pot::coroutines::task_meta> meta)
31-
{
32-
derived_execute(std::move(func), std::move(meta));
33-
}
34-
3529
template <typename Func, typename... Args>
3630
requires std::is_invocable_v<Func, Args...>
3731
void run_detached(Func &&func, Args &&...args)
3832
{
39-
auto meta = std::make_shared<pot::coroutines::task_meta>();
4033
derived_execute([f = std::decay_t<Func>(std::forward<Func>(func)),
4134
... captured_args = std::decay_t<Args>(std::forward<Args>(args))]() mutable
42-
{ std::invoke(f, std::move(captured_args)...); }, std::move(meta));
35+
{ std::invoke(f, std::move(captured_args)...); });
4336
}
4437

4538
template <typename Func, typename... Args>
@@ -73,17 +66,14 @@ class executor
7366
using ReturnTaskType =
7467
std::conditional_t<Lazy, pot::coroutines::lazy_task<Val>, pot::coroutines::task<Val>>;
7568

76-
using PromiseType = typename ReturnTaskType::promise_type;
77-
7869
struct schedule_awaiter
7970
{
8071
executor *exec;
8172
bool await_ready() const noexcept { return false; }
8273

83-
void await_suspend(std::coroutine_handle<PromiseType> h)
74+
void await_suspend(std::coroutine_handle<> h)
8475
{
85-
std::shared_ptr<pot::coroutines::task_meta> meta = h.promise().m_meta;
86-
exec->derived_execute([h]() mutable { h.resume(); }, std::move(meta));
76+
exec->derived_execute([h]() mutable { h.resume(); });
8777
}
8878

8979
void await_resume() const noexcept {}

0 commit comments

Comments
 (0)