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

Move to centralised loom safe locks #8371

Merged
merged 5 commits into from
Apr 20, 2024
Merged

Conversation

yschimke
Copy link
Collaborator

Mostly #8290

But with a centralised lock definition. Draft for discussion, but possibly we could encode the rules from docs/contribute/concurrency.md, so we fail fast if we ever break them.

internal object Locks {
inline fun <T> Dispatcher.withLock(action: () -> T): T {
contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) }
// TODO remove
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Possibly temporary checks, unless we think these are cheap based on assertion enablement?

Copy link
Member

Choose a reason for hiding this comment

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

Why do all the work to have a ReentrantLock and then forbid its use?

These checks are not fast enough to include.
https://publicobject.com/2019/11/18/kotlins-assert-is-not-like-javas-assert/

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's the opposite, this was a quick check to fail if we also had synchronization on the owner, not a concurrent Lock.


// TODO remove
assertThreadDoesntHoldLock()
// TODO can we assert we don't have the connection lock?
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What's a good structure to handle this sort of lock hierarchy?

@yschimke yschimke added android Relates to usage specifically on Android loom Relates to Loom JDK support labels Apr 17, 2024
@yschimke
Copy link
Collaborator Author

Catching the ones I missed

Http2ConnectionTest > sendGoAway() FAILED
    java.lang.AssertionError: Thread Test worker MUST NOT hold lock on okhttp3.internal.http2.Http2Connection@1e253c9d
        at okhttp3.internal.http2.Http2Connection.isHealthy(Http2Connection.kt:1205)
        at okhttp3.internal.http2.Http2ConnectionTest.sendGoAway(Http2ConnectionTest.kt:1397)

Copy link
Member

@swankjesse swankjesse left a comment

Choose a reason for hiding this comment

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

This is great!

var idleCallback: Runnable? = null
get() = this.withLock { field }
Copy link
Member

Choose a reason for hiding this comment

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

Maybe for follow up: should we do a property delegate so we can have syntax like this?

var idleCallback: Runnable? by lock.property(null)

(or similar)

internal object Locks {
inline fun <T> Dispatcher.withLock(action: () -> T): T {
contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) }
// TODO remove
Copy link
Member

Choose a reason for hiding this comment

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

Why do all the work to have a ReentrantLock and then forbid its use?

These checks are not fast enough to include.
https://publicobject.com/2019/11/18/kotlins-assert-is-not-like-javas-assert/

@yschimke yschimke marked this pull request as ready for review April 20, 2024 16:53
@yschimke yschimke merged commit d0d7557 into square:master Apr 20, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android Relates to usage specifically on Android loom Relates to Loom JDK support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants