Skip to content

Base Mutex implementation on Semaphore and improve the latter #3016

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

Closed
wants to merge 1 commit into from

Conversation

ndkoval
Copy link
Member

@ndkoval ndkoval commented Nov 11, 2021

No description provided.

@ndkoval ndkoval requested a review from qwwdfsad November 11, 2021 15:46
@ndkoval
Copy link
Member Author

ndkoval commented Nov 11, 2021

Only those tests that require Mutex.onLock() fail -- this will be fixed with new select implementation.

Copy link
Collaborator

@qwwdfsad qwwdfsad left a comment

Choose a reason for hiding this comment

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

By the way, you can reuse nom-intrusive implementation of onLock: 7755edb#diff-86e8b6879026f347540a4b86b2c594fe25128a0fb168cc0689ef48008c8b4d5fR99

@ndkoval
Copy link
Member Author

ndkoval commented Nov 19, 2021

@qwwdfsad I already have a proper implementation with new select API

Copy link
Collaborator

@qwwdfsad qwwdfsad left a comment

Choose a reason for hiding this comment

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

I also see that the build is failing, please make it pass. You can temporary ignore onLock tests for that sake.

My biggest concern here is the relevance of the PR -- could you please elaborate on the purpose of the PR? What benefits does it bring?

affected._state.compareAndSet(this, update)
override fun resume(value: Unit, onCancellation: ((cause: Throwable) -> Unit)?) {
[email protected] = owner
cont.resume(value, onCancellation)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What will happen if this resume will have no effect, e.g. because the continuation was concurrently cancelled?

@@ -52,24 +54,35 @@ public interface Mutex {
* Note that this function does not check for cancellation when it is not suspended.
* Use [yield] or [CoroutineScope.isActive] to periodically check for cancellation in tight loops if needed.
*
* Use [tryLock] to try acquiring a lock without waiting.
* This function can be used in [select] invocation with [onLock] clause.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This part is irrelevant as onLock is being deprecated for removal

) : CancellableContinuation<Unit> by cont {
override fun tryResume(value: Unit, idempotent: Any?, onCancellation: ((cause: Throwable) -> Unit)?): Any? {
val token = cont.tryResume(value, idempotent, onCancellation)
if (token !== null) [email protected] = owner
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add a comment why it is safe to have a race here -- the continuation can be already executing and even invoking unlock while owner is not set

Copy link
Member Author

Choose a reason for hiding this comment

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

It cannot be executing before completeResume(..)

override fun toString(): String = "Empty[$locked]"
}
internal class MutexImpl(locked: Boolean) : SemaphoreImpl(1, if (locked) 1 else 0), Mutex {
private val owner = atomic<Any?>(if (locked) null else NO_OWNER)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please document possible state transitions here and their purpose

@ndkoval
Copy link
Member Author

ndkoval commented Jan 11, 2022

Thanks for the review -- I have fixed and clarified everything in #3020

@ndkoval ndkoval closed this Jan 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants