Skip to content

Unnecessary locking in pthread mutex drop? #141020

@CXWorks

Description

@CXWorks

Hi, thanks for your time. I noticed that in the drop implementation of pthread_mutex lock, the std lib tries to get the lock and then unlock it before destory the lock.

// We're not allowed to pthread_mutex_destroy a locked mutex,
// so check first if it's unlocked.
if unsafe { pal.as_ref().try_lock() } {
unsafe { pal.as_ref().unlock() };
drop(pal)
} else {
// The mutex is locked. This happens if a MutexGuard is leaked.
// In this case, we just leak the Mutex too.
forget(pal)
}

Here (in safe Rust), since all the MutexGuard should be borrowed from this Mutex, is it impossible to have a locked status of the Mutex in its drop, why do we need to check and unlock here? Only to be compatible with potential unsafe Rust?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions