Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support TATAS-like usage in islocked and trylock spec #44820

Merged
merged 5 commits into from
Apr 5, 2022

Conversation

tkf
Copy link
Member

@tkf tkf commented Apr 1, 2022

NOTICE: THIS IS NOT AN ENDORSEMENT OF SPIN LOCKS

Currently, how islocked should behave is not well-specified. This PR is an RFC for allowing usage like test-and-test-and-set lock or exponential backoff lock:

nspins = 0
while true
    while islocked(lock)
        GC.safepoint()
        nspins += 1
        nspins > LIMIT && error("timeout")
    end
    trylock(lock) && break
    backoff()
end

Notably, this can be used for judging if a given implementation of islocked is correct for APIs like semaphore (#32299) and read-write lock where multiple tasks can enter the critical section in a certain way.

From this point of view, the API could have been clearer if the name was islockable. But I think the predicate islocked can also be interpreted as "am I locked out?" or "is it completely locked?"

If we decide this API contract is a good direction, we need to fix ReentrantLock implementation (maybe in this PR or later).

@tkf tkf added the multithreading Base.Threads and related functionality label Apr 1, 2022
@tkf tkf requested a review from vtjnash April 1, 2022 09:19
Copy link
Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable. I think other users have transitioned to assert_havelock or other, more correct lock usages.

base/lock.jl Outdated Show resolved Hide resolved
base/lock.jl Outdated Show resolved Hide resolved
@tkf tkf added docs This change adds or pertains to documentation merge me PR is reviewed. Merge when all tests are passing labels Apr 2, 2022
@tkf tkf changed the title RFC: A more "usable" API specification for islocked and trylock Support TATAS-like usage in islocked and trylock spec Apr 2, 2022
@tkf tkf merged commit 038c2bb into JuliaLang:master Apr 5, 2022
@tkf
Copy link
Member Author

tkf commented Apr 5, 2022

tester_linux32 is not finished but it is hopefully OK since this PR mostly only updates the texts in the docstrings. The CI issue has been reported in #44859 and #ci-failure-help slack channel.

@tkf tkf deleted the tatasable branch April 5, 2022 04:17
@giordano giordano removed the merge me PR is reviewed. Merge when all tests are passing label Apr 5, 2022
vtjnash pushed a commit that referenced this pull request Apr 7, 2022
This implements the `islocked`-`trylock` API documented in #44820 for `ReentrantLock` and `SpinLock`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation multithreading Base.Threads and related functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants