Pablichjenkov
07/28/2019, 11:58 PMsuspended send
waits until the Queue has more capacity available. Or in other words how the send coroutine is informed that the Queue has capacity available.
It is usual although consider a bad practice in Java, to synchronize on a queue monitor and wait for its size to change. Then whatever consuming thread that acquires the monitor will consume some items release the monitor and notify other waiters.
I guess that in coroutines, above mechanics is not used since it blocks/sleeps the waiter
thread. It probably works by re-scheduling another suspend send
coroutine in the executor. Maybe not, maybe it spins lock on the Queue capacity.
Does anyone can explain what happens internally?octylFractal
07/29/2019, 12:06 AMsuspendCoroutineUninterceptedOrReturn
or some variant of it will give you a Continuation object. Either you immediately return a result (no suspension occurs), or you return COROUTINE_SUSPENDED, and at a later point call Continuation.resumeWith(result)
, which starts up the coroutine again.
Presumably send
stores the continuation somewhere, and when an item is taken, the continuation is resumed to check if the item can be inserted. The actual mechanics are likely more complicated, but that would be the basics.Pablichjenkov
07/29/2019, 2:34 AMoctylFractal
07/29/2019, 2:40 AMPablichjenkov
07/29/2019, 3:37 PMZach Klippenstein (he/him) [MOD]
07/29/2019, 5:24 PMPablichjenkov
07/29/2019, 6:08 PM