master
h 55 lines 1.1 KB
Raw
1 // Copyright (C) Microsoft Corporation. All rights reserved.
2 #pragma once
3
4 namespace p9fs {
5
6 class IWorkItem;
7
8 class Scheduler
9 {
10 public:
11 using Coroutine = std::coroutine_handle<>;
12
13 struct Unblocker
14 {
15 Scheduler& m_Scheduler;
16 bool m_Run{};
17
18 bool await_ready() const
19 {
20 return m_Run;
21 }
22
23 void await_suspend(Coroutine handle)
24 {
25 m_Scheduler.Schedule(handle);
26 }
27
28 static void await_resume()
29 {
30 }
31 };
32
33 Scheduler();
34 void Schedule(Coroutine coroutine) noexcept;
35 void DonateThreadAndResume(Coroutine coroutine) noexcept;
36 bool Block() noexcept;
37 struct Unblocker Unblock() noexcept;
38
39 private:
40 void RunAndRelease() noexcept;
41 bool Claim(bool owner) noexcept;
42 void WorkerCallback() noexcept;
43
44 std::shared_mutex m_Lock;
45 std::queue<Coroutine> m_Queue;
46 std::unique_ptr<IWorkItem> m_Work;
47 bool m_Running;
48 bool m_ThreadEnqueued;
49 static thread_local bool tls_Blocked;
50 static thread_local bool tls_SchedulerThread;
51 };
52
53 extern Scheduler g_Scheduler;
54
55 } // namespace p9fs