Due to some issues, I cannot use
onReceive for my multi-channel-select use case (namely the lambdas are not inlined so I can’t return from inside). Therefore, I am using
poll coupled with a
when clause. But that channel might accept null elements. When using
ReceiveChannel.poll how do I know in thread-safe fashion whether the result is null because the channel was empty, null because the channel was closed, or null because it was sent a null? Using
isClosedForReceive before or after the fact is not thread safe and I don’t want to carry around a mutex with the channel everywhere (can’t use synchronized because this is common library code).
There is an unexposed
pollInternal that does exactly what I want. Should I open an issue requesting a
fun <T> ReceiveChannel<T>.pollOrDefault(default: () -> T): T? Or even better,
ReceiveChannel<T>.pollWithState(): Pair<T?, State>? I just need atomic poll-or-isEmpty-or-isClosedForReceive.