Could someone ELI5 <https://github.com/Kotlin/kotl...
# coroutines
e
Could someone ELI5 https://github.com/Kotlin/kotlinx.coroutines/pull/4053? I'm curious πŸ‘€
d
(The author of the PR) Subscribing to this thread. I, too, would be delighted to see someone ELI5 this.
πŸ‘ 1
πŸ˜‚ 10
s
Coroutines jobs can have children, and there are some rules governing the relationship between parent and child. In particular, if the parent is cancelled or fails, its child jobs are also cancelled. But we can create new child jobs at any time, for example by calling
launch
inside another coroutine. Since we're operating in a highly concurrent environment, that means we could end up in a situation where we're trying to add a new child to a parent job at the same time that the parent job is in the middle of being cancelled. We need to make sure we always end up with a consistent result, such that both the parent and child end up properly cancelled, and get a chance to run any completion handlers they might have registered. DCSS (or RDCSS) is a multi-word compare-and-set algorithm, which is a fancy way of saying that it lets us interact with several pieces of state at the same time in an atomic way. In this case it's being used to add a child job to the parent's list of children, but only if the parent hasn't already been cancelled. If the parent's state changes during the operation, its list of children won't be modified at all and we'll have to try again. Without that extra atomicity (or linearizability), we could end up in a situation like this: we add a child to a parent job, thinking the parent is still active, but by the time we're done, the parent job has actually become cancelled. But avoiding that by making the whole operation atomic is pretty complicated. If we just remove the RDCSS and allow the state changes to overlap, the code will be simpler and probably faster. The downside is that we'll have to go back afterwards and check that we don't have any child jobs that snuck in during the cancellation.
πŸ‘ 4
πŸ‘πŸ» 1
πŸ‘πŸΎ 1
How did I do? πŸ˜„
d
I think it's completely not in the spirit of ELI5 as I understand it. Let me try it. Several children play together in the yard. Sam is the rich kid ("parent job") who owns a collection of toys and lets other kids ("child jobs") play with them. When it's time for Sam to go eat ("cancellation" happens), Sam asks all the children to put the toys back. A new kid can arrive to the yard at any time and grab a toy freely. But we don't want a new kid to grab a toy when it's time for everyone to go home. The old policy was: while grabbing a toy, every kid must double-check that it's still allowed. This was a bit awkward: bending over to grab a toy truck, then looking around to see that Sam doesn't show the signs of wanting to eat, and only then starting to play is, well, strange. What is proposed instead: when it's time for Sam to go eat, he goes to the place where toys are and closes the door. You can't get in? It means you can't have the toy. You can get in? Go ahead and don't worry. This can lead to new kinds of uncomfortable situations. If a kid looks at Sam and sees that Sam is fine, they can then manage to snatch a toy even if Sam got hungry in the meantime. Before, Sam's hunger was an immediate signal that no toys can be dispensed. Or another problem: when Sam gets hungry, before, we were sure that every kid would see this on their own (while grabbing the toy), but now, it can happen that Sam will have to tell a kid (who had the chance to see this on their own), "Hey, I'm hungry, let's finish this game." We are not sure if these uncomfortable situations are actually problems: relationships between kids can be complicated.
πŸ‘ 3
πŸ‘πŸΎ 1
There are many ways to define the protocol incorrectly: toys could be lost, some children could get two toys, etc. One especially devious example: Sam could close the door when he hears that his dinner is almost ready, but before he has the appetite to eat it. In this case, a kid could throw a tantrum: "I want to grab a toy, but the door is closed, and I see that Sam isn't hungry yet!" This is problematic, but somewhat okay if Sam does eventually go dine: then the tantrum is over. But what if the tantrum spoils Sam's appetite? Then the children are stuck! The door is closed, Sam is ready to leave, but his mood is sour, and the guest kid refuses to leave, knowing that Sam is still not going to stop the playground activities.
s
Thanks for the correction! I spotted the bit about closing the list in the PR description, but I misunderstood what it was saying. It makes perfect sense after reading your message πŸ‘.
k
This is very cool, thanks for explaining!