Skip to content

Why fiber::mutex yields in try_lock even after acquiring the lock successfully? #331

@rodman10

Description

@rodman10

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.

fiber/src/mutex.cpp

Lines 43 to 60 in 238487b

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_;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions