-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.hpp
More file actions
200 lines (155 loc) · 5.03 KB
/
Copy pathworker.hpp
File metadata and controls
200 lines (155 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Copyright 2025, Aleksandar Colic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#ifndef UMS_WORKER_HPP
#define UMS_WORKER_HPP
#include <atomic>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <thread>
#include "intrusive_list.hpp"
#include "io.hpp"
#include "os_specific.hpp"
#include "types.hpp"
#include "util.hpp"
namespace ums {
class Scheduler;
class Condition_variable;
class TaskBase;
class Worker final {
friend class Scheduler;
friend class Condition_variable;
public:
enum class State : u8 {
initializing,
idle,
waiting,
pending_io,
yielded,
runnable,
running,
exiting
};
Worker(u64 id, Scheduler* scheduler);
~Worker();
Worker(const Worker&) = delete;
Worker& operator=(const Worker&) = delete;
Worker(Worker&&) noexcept = delete;
Worker& operator=(Worker&&) = delete;
void entry_point();
void main_loop();
void yield();
void read_file(void* file_handle, IO_Buffer buffer, u64 offset);
void write_file(void* file_handle, IO_Buffer buffer, u64 offset);
void wait_cond_or_sleep();
template<class Clock, class Duration>
void sleep_until(const std::chrono::time_point<Clock, Duration>& time_point)
{
sleep_until_internal(time_point);
}
template<class Rep, class Period>
void sleep_for(const std::chrono::duration<Rep, Period>& time_to_sleep)
{
sleep_until(now() + time_to_sleep);
}
void set_state(State state) noexcept;
void wait(std::unique_lock<std::mutex>& lock);
void notify(const std::unique_lock<std::mutex>& lock) noexcept;
[[nodiscard]] constexpr u64 id() const noexcept { return m_id; }
[[nodiscard]] constexpr State state() const noexcept { return m_state; }
[[nodiscard]] bool exit() const noexcept;
private:
// Helper struct used for info about waiting worker. Worker will sleep
// until condition is set or until sleep time expires. This helper struct
// is useful for mutex and condition variable, since there are functions which
// requires waiting on a condition with some timeout.
//
struct Wait_info {
std::atomic<bool> m_cond{false};
Time_point m_sleep_time_point{Time_point::max()};
};
void set_cond() noexcept { m_wait_info.m_cond.store(true, std::memory_order_relaxed); };
void clear_cond() noexcept { m_wait_info.m_cond.store(false, std::memory_order_relaxed); };
[[nodiscard]] bool check_cond() const noexcept
{
return m_wait_info.m_cond.load(std::memory_order_relaxed);
};
void set_sleep(Time_point sleep_time_point) noexcept
{
m_wait_info.m_sleep_time_point = sleep_time_point;
}
void clear_sleep() noexcept { m_wait_info.m_sleep_time_point = Time_point::max(); }
[[nodiscard]] bool check_sleep() const noexcept
{
return now() >= m_wait_info.m_sleep_time_point;
}
public:
[[nodiscard]] Time_point sleep_time_point() const noexcept
{
return m_wait_info.m_sleep_time_point;
}
void set_wait_info(bool cond, Time_point sleep_time_point) noexcept
{
if (cond)
set_cond();
else
clear_cond();
set_sleep(sleep_time_point);
}
void clear_wait_info() noexcept
{
clear_cond();
clear_sleep();
}
[[nodiscard]] bool check_wait_info() const noexcept { return check_cond() || check_sleep(); }
void notify_waiter() noexcept;
void sleep_until_internal(const Time_point& time_point);
private:
// TODO: reorganize data members for quicker access.
//
u64 m_id;
Scheduler* m_scheduler;
stl::INode<Worker> m_node;
stl::INode<Worker> m_waiter_node; // Used in Condition_variable waiters list.
std::condition_variable m_cv;
State m_state{State::initializing};
bool m_running{true}; // Flag used for spurious wakeup check.
Wait_info m_wait_info;
std::shared_ptr<TaskBase> m_task{nullptr};
std::unique_ptr<IO_Request> m_io_request{nullptr};
std::thread m_thread;
};
/**
* API similar to std::thread::
*/
namespace worker {
Worker* get() noexcept;
u64 get_id() noexcept;
void yield() noexcept;
template<class Clock, class Duration>
void sleep_until(const std::chrono::time_point<Clock, Duration>& abs_time)
{
get()->sleep_until(abs_time);
}
template<class Rep, class Period>
void sleep_for(const std::chrono::duration<Rep, Period>& rel_time)
{
get()->sleep_for(rel_time);
}
}; // namespace worker
} // namespace ums
#endif // UMS_WORKER_HPP