https://kotlinlang.org logo
Title
p

pambrose

09/10/2019, 8:33 PM
I have a simple example of using select{} on mutex.onLock: https://gist.github.com/pambrose/4eb82e623161ee7758551ad6a9649ab6 Based on the docs and the name “onLock” I am expecting the select to take place when an unlocked mutex is locked. However, I am seeing the opposite behavior, i.e., if I use locked mutexes, I am seeing the select take place when the mutex is unlocked. The docs for Mutex.onLock say that it “selects when the mutex is locked.” Given the behavior I am seeing, I would expect the name to be “onUnlocked”. I would appreciate someone setting me straight on this.
e

elizarov

09/11/2019, 7:11 AM
onLock
is not a listener when called the mutex was locked by something.
onLock
is a select clause that instructs select expression to attempt acquiring a lock and call the corresponding block of code when this attempt succeeds.
#coroutines if a better channel for those kind of questions
☝️ 1
So, when you
unlock()
a locked mutex, then
onLock
select clause succeeds and you get the corresponding message.
p

pambrose

09/11/2019, 2:31 PM
Sorry for not posting this to #coroutines. The name is what threw me, but I can see the need to keep Mutex.onLock consistent with Mutex.lock and Mutex.tryLock. I will put in a PR to change the Mutex.onLock docs to:
Clause for [select] expression of [lock] suspending function that selects when the mutex is unlocked.
Thanks for your answer. I really appreciate it.
👍 1
e

elizarov

09/11/2019, 2:41 PM
This would be a wrong doc, though, either. It is better to be more explicit about the fact that it selects when we can lock unlocked lock
p

pambrose

09/11/2019, 5:02 PM
Got it. Thanks. I am hugely appreciative for what you have done with coroutines and Kotlin. I have not not enjoyed programming this much in many years.