Skip to content

Add Lockable to Base? #52897

Closed
Closed
@ericphanson

Description

e.g. like

struct Lockable{T, L <: Base.AbstractLock}
    value::T
    lock::L
end
Lockable(value) = Lockable(value, ReentrantLock())
Base.getindex(l::Lockable) = l.value

as in https://github.com/JuliaServices/ConcurrentUtilities.jl/blob/main/src/lockable.jl.

I think this could be ergonomic for use in Base since there's various bits of global state that each need a lock (e.g. #49778), many of which are of the form

const RESOURCE = ...
const RESOURCE_LOCK = ReentrantLock()

and one does @lock(RESOURCE_LOCK, ...access RESOURCE...). With Lockable, they could be

const RESOURCE = Lockable(...)

and used by @lock(RESOURCE, ...access RESOURCE[]...).

By grouping the resource and it's lock into a struct, it makes it slightly harder to accidentally use the resource without the lock (since one needs to index into it), and it makes it crystal clear what lock is for what resource.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions