| 233 | } |
| 234 | } |
| 235 | static ErrorCode do_lock_normal(Thread *thread, ptr<umutex> m, uint flags, |
| 236 | std::uint64_t ut, umutex_lock_mode mode) { |
| 237 | ORBIS_LOG_TRACE(__FUNCTION__, thread->tid, m, flags, ut, mode); |
| 238 | |
| 239 | auto [chain, key, lock] = umtxStorage->getUmtxChain1(thread, flags, m); |
| 240 | ErrorCode error = {}; |
| 241 | while (true) { |
| 242 | int owner = m->owner.load(std::memory_order_acquire); |
| 243 | if (mode == umutex_lock_mode::wait) { |
| 244 | if (owner == kUmutexUnowned || owner == kUmutexContested) |
| 245 | return {}; |
| 246 | } else { |
| 247 | owner = kUmutexUnowned; |
| 248 | if (m->owner.compare_exchange_strong(owner, thread->tid)) |
| 249 | return {}; |
| 250 | if (owner == kUmutexContested) { |
| 251 | if (m->owner.compare_exchange_strong(owner, |
| 252 | thread->tid | kUmutexContested)) |
| 253 | return {}; |
| 254 | continue; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if ((flags & kUmutexErrorCheck) != 0 && |
| 259 | (owner & ~kUmutexContested) == thread->tid) |
| 260 | return ErrorCode::DEADLK; |
| 261 | |
| 262 | if (mode == umutex_lock_mode::try_) |
| 263 | return ErrorCode::BUSY; |
| 264 | |
| 265 | if (error != ErrorCode{}) |
| 266 | return error; |
| 267 | |
| 268 | auto node = chain.enqueue(key, thread); |
| 269 | if (m->owner.compare_exchange_strong(owner, owner | kUmutexContested)) { |
| 270 | { |
| 271 | orbis::scoped_unblock unblock; |
| 272 | error = orbis::toErrorCode(node->second.cv.wait(chain.mtx, ut)); |
| 273 | } |
| 274 | if (error == ErrorCode{} && node->second.thr == thread && |
| 275 | m->owner.load() != 0) { |
| 276 | error = ErrorCode::TIMEDOUT; |
| 277 | } |
| 278 | } |
| 279 | if (node->second.thr == thread) |
| 280 | chain.erase(node); |
| 281 | } |
| 282 | } |
| 283 | static ErrorCode do_lock_pi(Thread *thread, ptr<umutex> m, uint flags, |
| 284 | std::uint64_t ut, umutex_lock_mode mode) { |
| 285 | // ORBIS_LOG_TODO(__FUNCTION__, m, flags, ut, mode); |
no test coverage detected