stateflow
2022-02-10 02:44:41 0 举报
登录查看完整内容
stateflow 源码流程
作者其他创作
大纲/内容
符合相同的值不发送
StateFlowImpl奇偶性
拿到StateFlowImpl内的 slots ,做遍历
单个slot
判断前一个值是否和当前值相同
slot3
开始
StateFlowImpl.sequence +2返回true
数据发送者
符合
collect
奇数
slot2
当前 => slot._state = cont协程阻塞等待resume唤醒
不符合
curSequence = StateFlowImpl.sequence
StateFlowImpl类内部各类状态
协程,订阅者
不符合循环
外部收到数据需要处理的业务
sateflow.emit
循环符合
内部
expectedStatenewState
StateFlowImpl.sequence +1返回true
oldState == newState
返回false
退出同步sequence++
符合对cas的支持
值相同
内部包含curSequence //一个 线程或者协程处理的一个当前版本的记录
同步处理2
不符合值不相同
传参数expectedState = nullnewState = 需要发送的数据
sequence == curSequence
同步操作1
循环
_state:atomic:用于表示当前的状态
cas 处理slot._state如果等于 cont,则更改值为none唤醒阻塞的协程如果否则其他情况,均不唤醒线程
slot //内部持有cont //协程上下文prevState // 前一个值
传参数expectedState = 前提数据newState = 需要发送的数据
cas 处理slot._state_state == NONE
slots :
修改StateFlowImpl._state.value值为 newState
stateFlow.compareAndSet
slot1
expectedState != nulloldState != expectedState
判断 slot._state是否是pending
返回true
值不相同
0 条评论
回复 删除
下一页