MCPcopy Create free account
hub / github.com/Simple-XX/SimpleKernel / LockGuard

Class LockGuard

src/include/spinlock.hpp:131–220  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

129 { m.UnLock() } -> std::same_as<Expected<void>>;
130 }
131class LockGuard {
132 public:
133 using mutex_type = Mutex;
134
135 /// @name 构造/析构函数
136 /// @{
137
138 /**
139 * @brief 构造函数,自动获取锁
140 * @param mutex 要保护的锁对象
141 */
142 explicit LockGuard(mutex_type& mutex) : mutex_(mutex) {
143 mutex_.Lock().or_else([&](auto&& err) {
144 char core_buf[4] = {};
145 auto core_id = cpu_io::GetCurrentCoreId();
146 size_t pos = 0;
147 if (core_id == 0) {
148 core_buf[pos++] = '0';
149 } else {
150 char tmp[4] = {};
151 size_t tmp_pos = 0;
152 while (core_id > 0 && tmp_pos < sizeof(tmp)) {
153 tmp[tmp_pos++] = static_cast<char>('0' + (core_id % 10));
154 core_id /= 10;
155 }
156 while (tmp_pos > 0) {
157 core_buf[pos++] = tmp[--tmp_pos];
158 }
159 }
160 core_buf[pos] = '\0';
161 klog::RawPut("PANIC: LockGuard failed to acquire lock '");
162 klog::RawPut(mutex_.name);
163 klog::RawPut("' on core ");
164 klog::RawPut(core_buf);
165 klog::RawPut(": ");
166 klog::RawPut(err.message());
167 klog::RawPut("\n");
168 while (true) {
169 cpu_io::Pause();
170 }
171 return Expected<void>{};
172 });
173 }
174
175 /**
176 * @brief 析构函数,自动释放锁
177 */
178 ~LockGuard() {
179 mutex_.UnLock().or_else([&](auto&& err) {
180 char core_buf[4] = {};
181 auto core_id = cpu_io::GetCurrentCoreId();
182 size_t pos = 0;
183 if (core_id == 0) {
184 core_buf[pos++] = '0';
185 } else {
186 char tmp[4] = {};
187 size_t tmp_pos = 0;
188 while (core_id > 0 && tmp_pos < sizeof(tmp)) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected