| 72 | } |
| 73 | |
| 74 | auto Mutex::UnLock() -> Expected<void> { |
| 75 | auto current_task = TaskManagerSingleton::instance().GetCurrentTask(); |
| 76 | if (current_task == nullptr) { |
| 77 | klog::Err("Mutex::UnLock: Cannot unlock mutex '{}' outside task context", |
| 78 | name); |
| 79 | return std::unexpected(Error{ErrorCode::kMutexNoTaskContext}); |
| 80 | } |
| 81 | |
| 82 | Pid current_pid = current_task->pid; |
| 83 | |
| 84 | if (!IsLockedByCurrentTask()) { |
| 85 | klog::Warn( |
| 86 | "Mutex::UnLock: Task {} tried to unlock mutex '{}' it doesn't own", |
| 87 | current_pid, name); |
| 88 | return std::unexpected(Error{ErrorCode::kMutexNotOwned}); |
| 89 | } |
| 90 | |
| 91 | owner_.store(std::numeric_limits<Pid>::max(), std::memory_order_release); |
| 92 | locked_.store(false, std::memory_order_release); |
| 93 | |
| 94 | klog::Debug("Mutex::UnLock: Task {} released mutex '{}'", current_pid, name); |
| 95 | |
| 96 | TaskManagerSingleton::instance().WakeupOne(resource_id_); |
| 97 | |
| 98 | return {}; |
| 99 | } |
| 100 | |
| 101 | auto Mutex::TryLock() -> Expected<void> { |
| 102 | auto current_task = TaskManagerSingleton::instance().GetCurrentTask(); |