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

Method LockGuard

src/include/spinlock.hpp:142–173  ·  view source on GitHub ↗

* @brief 构造函数,自动获取锁 * @param mutex 要保护的锁对象 */

Source from the content-addressed store, hash-verified

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 析构函数,自动释放锁

Callers

nothing calls this directly

Calls 5

GetCurrentCoreIdFunction · 0.85
RawPutFunction · 0.85
PauseFunction · 0.85
messageMethod · 0.80
LockMethod · 0.45

Tested by

no test coverage detected