Recently I'm reading the source code of fiber::mutex, I noticed thatfiber::mutex will yield in try_lock no matter whether holding the lock or not? What's the considerations behind it, and can we just return immediately after lock? Thanks in advance.
|
|
|
bool |
|
mutex::try_lock() { |
|
context * active_ctx = context::active(); |
|
detail::spinlock_lock lk{ wait_queue_splk_ }; |
|
if ( BOOST_UNLIKELY( active_ctx == owner_) ) { |
|
throw lock_error{ |
|
std::make_error_code( std::errc::resource_deadlock_would_occur), |
|
"boost fiber: a deadlock is detected" }; |
|
} |
|
if ( nullptr == owner_) { |
|
owner_ = active_ctx; |
|
} |
|
lk.unlock(); |
|
// let other fiber release the lock |
|
active_ctx->yield(); |
|
return active_ctx == owner_; |
|
} |
Recently I'm reading the source code of
fiber::mutex, I noticed thatfiber::mutexwill yield intry_lockno matter whether holding the lock or not? What's the considerations behind it, and can we just return immediately after lock? Thanks in advance.fiber/src/mutex.cpp
Lines 43 to 60 in 238487b