in kotlinx-coroutines-core/common/src/channels/BroadcastChannel.kt [161:184]
override suspend fun send(element: E) {
val subs = lock.withLock { // protected by lock
// Is this channel closed for send?
if (isClosedForSend) throw sendException
// Update the last sent element if this broadcast is conflated.
if (capacity == CONFLATED) lastConflatedElement = element
// Get a reference to the list of subscribers under the lock.
subscribers
}
// The lock has been released. Send the element to the
// subscribers one-by-one, and finish immediately
// when this broadcast discovered in the closed state.
// Note that this implementation is non-linearizable;
// see this method documentation for details.
subs.forEach {
// We use special function to send the element,
// which returns `true` on success and `false`
// if the subscriber is closed.
val success = it.sendBroadcast(element)
// The sending attempt has failed.
// Check whether the broadcast is closed.
if (!success && isClosedForSend) throw sendException
}
}