1 /*
<lambda>null2 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3 */
4
5 package kotlinx.coroutines.flow
6
7 import kotlinx.atomicfu.*
8 import kotlinx.coroutines.*
9 import kotlinx.coroutines.channels.*
10 import kotlinx.coroutines.flow.internal.*
11 import kotlinx.coroutines.internal.*
12 import kotlin.coroutines.*
13
14 /**
15 * A [SharedFlow] that represents a read-only state with a single updatable data [value] that emits updates
16 * to the value to its collectors. A state flow is a _hot_ flow because its active instance exists independently
17 * of the presence of collectors. Its current value can be retrieved via the [value] property.
18 *
19 * **State flow never completes**. A call to [Flow.collect] on a state flow never completes normally, and
20 * neither does a coroutine started by the [Flow.launchIn] function. An active collector of a state flow is called a _subscriber_.
21 *
22 * A [mutable state flow][MutableStateFlow] is created using `MutableStateFlow(value)` constructor function with
23 * the initial value. The value of mutable state flow can be updated by setting its [value] property.
24 * Updates to the [value] are always [conflated][Flow.conflate]. So a slow collector skips fast updates,
25 * but always collects the most recently emitted value.
26 *
27 * [StateFlow] is useful as a data-model class to represent any kind of state.
28 * Derived values can be defined using various operators on the flows, with [combine] operator being especially
29 * useful to combine values from multiple state flows using arbitrary functions.
30 *
31 * For example, the following class encapsulates an integer state and increments its value on each call to `inc`:
32 *
33 * ```
34 * class CounterModel {
35 * private val _counter = MutableStateFlow(0) // private mutable state flow
36 * val counter = _counter.asStateFlow() // publicly exposed as read-only state flow
37 *
38 * fun inc() {
39 * _counter.update { count -> count + 1 } // atomic, safe for concurrent use
40 * }
41 * }
42 * ```
43 *
44 * Having two instances of the above `CounterModel` class one can define the sum of their counters like this:
45 *
46 * ```
47 * val aModel = CounterModel()
48 * val bModel = CounterModel()
49 * val sumFlow: Flow<Int> = aModel.counter.combine(bModel.counter) { a, b -> a + b }
50 * ```
51 *
52 * As an alternative to the above usage with the `MutableStateFlow(...)` constructor function,
53 * any _cold_ [Flow] can be converted to a state flow using the [stateIn] operator.
54 *
55 * ### Strong equality-based conflation
56 *
57 * Values in state flow are conflated using [Any.equals] comparison in a similar way to
58 * [distinctUntilChanged] operator. It is used to conflate incoming updates
59 * to [value][MutableStateFlow.value] in [MutableStateFlow] and to suppress emission of the values to collectors
60 * when new value is equal to the previously emitted one. State flow behavior with classes that violate
61 * the contract for [Any.equals] is unspecified.
62 *
63 * ### State flow is a shared flow
64 *
65 * State flow is a special-purpose, high-performance, and efficient implementation of [SharedFlow] for the narrow,
66 * but widely used case of sharing a state. See the [SharedFlow] documentation for the basic rules,
67 * constraints, and operators that are applicable to all shared flows.
68 *
69 * State flow always has an initial value, replays one most recent value to new subscribers, does not buffer any
70 * more values, but keeps the last emitted one, and does not support [resetReplayCache][MutableSharedFlow.resetReplayCache].
71 * A state flow behaves identically to a shared flow when it is created
72 * with the following parameters and the [distinctUntilChanged] operator is applied to it:
73 *
74 * ```
75 * // MutableStateFlow(initialValue) is a shared flow with the following parameters:
76 * val shared = MutableSharedFlow(
77 * replay = 1,
78 * onBufferOverflow = BufferOverflow.DROP_OLDEST
79 * )
80 * shared.tryEmit(initialValue) // emit the initial value
81 * val state = shared.distinctUntilChanged() // get StateFlow-like behavior
82 * ```
83 *
84 * Use [SharedFlow] when you need a [StateFlow] with tweaks in its behavior such as extra buffering, replaying more
85 * values, or omitting the initial value.
86 *
87 * ### StateFlow vs ConflatedBroadcastChannel
88 *
89 * Conceptually, state flow is similar to [ConflatedBroadcastChannel]
90 * and is designed to completely replace it.
91 * It has the following important differences:
92 *
93 * * `StateFlow` is simpler, because it does not have to implement all the [Channel] APIs, which allows
94 * for faster, garbage-free implementation, unlike `ConflatedBroadcastChannel` implementation that
95 * allocates objects on each emitted value.
96 * * `StateFlow` always has a value which can be safely read at any time via [value] property.
97 * Unlike `ConflatedBroadcastChannel`, there is no way to create a state flow without a value.
98 * * `StateFlow` has a clear separation into a read-only `StateFlow` interface and a [MutableStateFlow].
99 * * `StateFlow` conflation is based on equality like [distinctUntilChanged] operator,
100 * unlike conflation in `ConflatedBroadcastChannel` that is based on reference identity.
101 * * `StateFlow` cannot be closed like `ConflatedBroadcastChannel` and can never represent a failure.
102 * All errors and completion signals should be explicitly _materialized_ if needed.
103 *
104 * `StateFlow` is designed to better cover typical use-cases of keeping track of state changes in time, taking
105 * more pragmatic design choices for the sake of convenience.
106 *
107 * To migrate [ConflatedBroadcastChannel] usage to [StateFlow], start by replacing usages of the `ConflatedBroadcastChannel()`
108 * constructor with `MutableStateFlow(initialValue)`, using `null` as an initial value if you don't have one.
109 * Replace [send][ConflatedBroadcastChannel.send] and [trySend][ConflatedBroadcastChannel.trySend] calls
110 * with updates to the state flow's [MutableStateFlow.value], and convert subscribers' code to flow operators.
111 * You can use the [filterNotNull] operator to mimic behavior of a `ConflatedBroadcastChannel` without initial value.
112 *
113 * ### Concurrency
114 *
115 * All methods of state flow are **thread-safe** and can be safely invoked from concurrent coroutines without
116 * external synchronization.
117 *
118 * ### Operator fusion
119 *
120 * Application of [flowOn][Flow.flowOn], [conflate][Flow.conflate],
121 * [buffer] with [CONFLATED][Channel.CONFLATED] or [RENDEZVOUS][Channel.RENDEZVOUS] capacity,
122 * [distinctUntilChanged][Flow.distinctUntilChanged], or [cancellable] operators to a state flow has no effect.
123 *
124 * ### Implementation notes
125 *
126 * State flow implementation is optimized for memory consumption and allocation-freedom. It uses a lock to ensure
127 * thread-safety, but suspending collector coroutines are resumed outside of this lock to avoid dead-locks when
128 * using unconfined coroutines. Adding new subscribers has `O(1)` amortized cost, but updating a [value] has `O(N)`
129 * cost, where `N` is the number of active subscribers.
130 *
131 * ### Not stable for inheritance
132 *
133 * **`The StateFlow` interface is not stable for inheritance in 3rd party libraries**, as new methods
134 * might be added to this interface in the future, but is stable for use.
135 * Use the `MutableStateFlow(value)` constructor function to create an implementation.
136 */
137 public interface StateFlow<out T> : SharedFlow<T> {
138 /**
139 * The current value of this state flow.
140 */
141 public val value: T
142 }
143
144 /**
145 * A mutable [StateFlow] that provides a setter for [value].
146 * An instance of `MutableStateFlow` with the given initial `value` can be created using
147 * `MutableStateFlow(value)` constructor function.
148
149 * See the [StateFlow] documentation for details on state flows.
150 * Note that all emission-related operators, such as [value]'s setter, [emit], and [tryEmit], are conflated using [Any.equals].
151 *
152 * ### Not stable for inheritance
153 *
154 * **The `MutableStateFlow` interface is not stable for inheritance in 3rd party libraries**, as new methods
155 * might be added to this interface in the future, but is stable for use.
156 * Use the `MutableStateFlow()` constructor function to create an implementation.
157 */
158 public interface MutableStateFlow<T> : StateFlow<T>, MutableSharedFlow<T> {
159 /**
160 * The current value of this state flow.
161 *
162 * Setting a value that is [equal][Any.equals] to the previous one does nothing.
163 *
164 * This property is **thread-safe** and can be safely updated from concurrent coroutines without
165 * external synchronization.
166 */
167 public override var value: T
168
169 /**
170 * Atomically compares the current [value] with [expect] and sets it to [update] if it is equal to [expect].
171 * The result is `true` if the [value] was set to [update] and `false` otherwise.
172 *
173 * This function use a regular comparison using [Any.equals]. If both [expect] and [update] are equal to the
174 * current [value], this function returns `true`, but it does not actually change the reference that is
175 * stored in the [value].
176 *
177 * This method is **thread-safe** and can be safely invoked from concurrent coroutines without
178 * external synchronization.
179 */
compareAndSetnull180 public fun compareAndSet(expect: T, update: T): Boolean
181 }
182
183 /**
184 * Creates a [MutableStateFlow] with the given initial [value].
185 */
186 @Suppress("FunctionName")
187 public fun <T> MutableStateFlow(value: T): MutableStateFlow<T> = StateFlowImpl(value ?: NULL)
188
189 // ------------------------------------ Update methods ------------------------------------
190
191 /**
192 * Updates the [MutableStateFlow.value] atomically using the specified [function] of its value, and returns the new
193 * value.
194 *
195 * [function] may be evaluated multiple times, if [value] is being concurrently updated.
196 */
197 public inline fun <T> MutableStateFlow<T>.updateAndGet(function: (T) -> T): T {
198 while (true) {
199 val prevValue = value
200 val nextValue = function(prevValue)
201 if (compareAndSet(prevValue, nextValue)) {
202 return nextValue
203 }
204 }
205 }
206
207 /**
208 * Updates the [MutableStateFlow.value] atomically using the specified [function] of its value, and returns its
209 * prior value.
210 *
211 * [function] may be evaluated multiple times, if [value] is being concurrently updated.
212 */
getAndUpdatenull213 public inline fun <T> MutableStateFlow<T>.getAndUpdate(function: (T) -> T): T {
214 while (true) {
215 val prevValue = value
216 val nextValue = function(prevValue)
217 if (compareAndSet(prevValue, nextValue)) {
218 return prevValue
219 }
220 }
221 }
222
223
224 /**
225 * Updates the [MutableStateFlow.value] atomically using the specified [function] of its value.
226 *
227 * [function] may be evaluated multiple times, if [value] is being concurrently updated.
228 */
updatenull229 public inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) {
230 while (true) {
231 val prevValue = value
232 val nextValue = function(prevValue)
233 if (compareAndSet(prevValue, nextValue)) {
234 return
235 }
236 }
237 }
238
239 // ------------------------------------ Implementation ------------------------------------
240
241 private val NONE = Symbol("NONE")
242
243 private val PENDING = Symbol("PENDING")
244
245 // StateFlow slots are allocated for its collectors
246 private class StateFlowSlot : AbstractSharedFlowSlot<StateFlowImpl<*>>() {
247 /**
248 * Each slot can have one of the following states:
249 *
250 * * `null` -- it is not used right now. Can [allocateLocked] to new collector.
251 * * `NONE` -- used by a collector, but neither suspended nor has pending value.
252 * * `PENDING` -- pending to process new value.
253 * * `CancellableContinuationImpl<Unit>` -- suspended waiting for new value.
254 *
255 * It is important that default `null` value is used, because there can be a race between allocation
256 * of a new slot and trying to do [makePending] on this slot.
257 */
258 private val _state = atomic<Any?>(null)
259
allocateLockednull260 override fun allocateLocked(flow: StateFlowImpl<*>): Boolean {
261 // No need for atomic check & update here, since allocated happens under StateFlow lock
262 if (_state.value != null) return false // not free
263 _state.value = NONE // allocated
264 return true
265 }
266
freeLockednull267 override fun freeLocked(flow: StateFlowImpl<*>): Array<Continuation<Unit>?> {
268 _state.value = null // free now
269 return EMPTY_RESUMES // nothing more to do
270 }
271
272 @Suppress("UNCHECKED_CAST")
makePendingnull273 fun makePending() {
274 _state.loop { state ->
275 when {
276 state == null -> return // this slot is free - skip it
277 state === PENDING -> return // already pending, nothing to do
278 state === NONE -> { // mark as pending
279 if (_state.compareAndSet(state, PENDING)) return
280 }
281 else -> { // must be a suspend continuation state
282 // we must still use CAS here since continuation may get cancelled and free the slot at any time
283 if (_state.compareAndSet(state, NONE)) {
284 (state as CancellableContinuationImpl<Unit>).resume(Unit)
285 return
286 }
287 }
288 }
289 }
290 }
291
statenull292 fun takePending(): Boolean = _state.getAndSet(NONE)!!.let { state ->
293 assert { state !is CancellableContinuationImpl<*> }
294 return state === PENDING
295 }
296
297 @Suppress("UNCHECKED_CAST")
awaitPendingnull298 suspend fun awaitPending(): Unit = suspendCancellableCoroutine sc@ { cont ->
299 assert { _state.value !is CancellableContinuationImpl<*> } // can be NONE or PENDING
300 if (_state.compareAndSet(NONE, cont)) return@sc // installed continuation, waiting for pending
301 // CAS failed -- the only possible reason is that it is already in pending state now
302 assert { _state.value === PENDING }
303 cont.resume(Unit)
304 }
305 }
306
307 private class StateFlowImpl<T>(
308 initialState: Any // T | NULL
309 ) : AbstractSharedFlow<StateFlowSlot>(), MutableStateFlow<T>, CancellableFlow<T>, FusibleFlow<T> {
310 private val _state = atomic(initialState) // T | NULL
311 private var sequence = 0 // serializes updates, value update is in process when sequence is odd
312
313 @Suppress("UNCHECKED_CAST")
314 public override var value: T
315 get() = NULL.unbox(_state.value)
316 set(value) { updateState(null, value ?: NULL) }
317
compareAndSetnull318 override fun compareAndSet(expect: T, update: T): Boolean =
319 updateState(expect ?: NULL, update ?: NULL)
320
321 private fun updateState(expectedState: Any?, newState: Any): Boolean {
322 var curSequence: Int
323 var curSlots: Array<StateFlowSlot?>? // benign race, we will not use it
324 synchronized(this) {
325 val oldState = _state.value
326 if (expectedState != null && oldState != expectedState) return false // CAS support
327 if (oldState == newState) return true // Don't do anything if value is not changing, but CAS -> true
328 _state.value = newState
329 curSequence = sequence
330 if (curSequence and 1 == 0) { // even sequence means quiescent state flow (no ongoing update)
331 curSequence++ // make it odd
332 sequence = curSequence
333 } else {
334 // update is already in process, notify it, and return
335 sequence = curSequence + 2 // change sequence to notify, keep it odd
336 return true // updated
337 }
338 curSlots = slots // read current reference to collectors under lock
339 }
340 /*
341 Fire value updates outside of the lock to avoid deadlocks with unconfined coroutines.
342 Loop until we're done firing all the changes. This is a sort of simple flat combining that
343 ensures sequential firing of concurrent updates and avoids the storm of collector resumes
344 when updates happen concurrently from many threads.
345 */
346 while (true) {
347 // Benign race on element read from array
348 curSlots?.forEach {
349 it?.makePending()
350 }
351 // check if the value was updated again while we were updating the old one
352 synchronized(this) {
353 if (sequence == curSequence) { // nothing changed, we are done
354 sequence = curSequence + 1 // make sequence even again
355 return true // done, updated
356 }
357 // reread everything for the next loop under the lock
358 curSequence = sequence
359 curSlots = slots
360 }
361 }
362 }
363
364 override val replayCache: List<T>
365 get() = listOf(value)
366
tryEmitnull367 override fun tryEmit(value: T): Boolean {
368 this.value = value
369 return true
370 }
371
emitnull372 override suspend fun emit(value: T) {
373 this.value = value
374 }
375
376 @Suppress("UNCHECKED_CAST")
resetReplayCachenull377 override fun resetReplayCache() {
378 throw UnsupportedOperationException("MutableStateFlow.resetReplayCache is not supported")
379 }
380
collectnull381 override suspend fun collect(collector: FlowCollector<T>): Nothing {
382 val slot = allocateSlot()
383 try {
384 if (collector is SubscribedFlowCollector) collector.onSubscription()
385 val collectorJob = currentCoroutineContext()[Job]
386 var oldState: Any? = null // previously emitted T!! | NULL (null -- nothing emitted yet)
387 // The loop is arranged so that it starts delivering current value without waiting first
388 while (true) {
389 // Here the coroutine could have waited for a while to be dispatched,
390 // so we use the most recent state here to ensure the best possible conflation of stale values
391 val newState = _state.value
392 // always check for cancellation
393 collectorJob?.ensureActive()
394 // Conflate value emissions using equality
395 if (oldState == null || oldState != newState) {
396 collector.emit(NULL.unbox(newState))
397 oldState = newState
398 }
399 // Note: if awaitPending is cancelled, then it bails out of this loop and calls freeSlot
400 if (!slot.takePending()) { // try fast-path without suspending first
401 slot.awaitPending() // only suspend for new values when needed
402 }
403 }
404 } finally {
405 freeSlot(slot)
406 }
407 }
408
createSlotnull409 override fun createSlot() = StateFlowSlot()
410 override fun createSlotArray(size: Int): Array<StateFlowSlot?> = arrayOfNulls(size)
411
412 override fun fuse(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow) =
413 fuseStateFlow(context, capacity, onBufferOverflow)
414 }
415
416 internal fun <T> StateFlow<T>.fuseStateFlow(
417 context: CoroutineContext,
418 capacity: Int,
419 onBufferOverflow: BufferOverflow
420 ): Flow<T> {
421 // state flow is always conflated so additional conflation does not have any effect
422 assert { capacity != Channel.CONFLATED } // should be desugared by callers
423 if ((capacity in 0..1 || capacity == Channel.BUFFERED) && onBufferOverflow == BufferOverflow.DROP_OLDEST) {
424 return this
425 }
426 return fuseSharedFlow(context, capacity, onBufferOverflow)
427 }
428