kpgalligan
02/03/2020, 3:25 PMarnaud.giuliani
02/03/2020, 4:55 PMkpgalligan
02/03/2020, 4:57 PMarnaud.giuliani
02/03/2020, 4:59 PMkpgalligan
02/03/2020, 5:00 PMarnaud.giuliani
02/03/2020, 5:01 PMkpgalligan
02/06/2020, 3:19 PMarnaud.giuliani
02/06/2020, 3:23 PMkpgalligan
02/06/2020, 3:32 PMarnaud.giuliani
02/06/2020, 3:40 PMkpgalligan
02/06/2020, 3:46 PMarnaud.giuliani
02/06/2020, 3:49 PMkpgalligan
02/06/2020, 4:04 PMarnaud.giuliani
02/06/2020, 4:37 PMThe user should not be able to access something from another thread without somehow indicating that they’re aware they’re doing that. If we just auto-freeze, it’s not a great situation (in my opinion).sure
kpgalligan
02/06/2020, 5:42 PMarnaud.giuliani
02/06/2020, 5:44 PMkpgalligan
02/06/2020, 5:45 PMarnaud.giuliani
02/06/2020, 5:45 PMMainIsolatedState
class a way to ensure stable reference in current thread?kpgalligan
02/07/2020, 12:44 PMinternal expect class MainIsolatedState<T:Any>(startVal: T) {
fun _get(): T
}
internal inline val <T:Any> MainIsolatedState<T>.value: T
get() {
assertMainThread()
return _get()
}
.value
, it checks that you’re on the main thread. If so, it calls _get()
. On JVM, you could just use synchronized to make sure you’re getting it “safely”, but on native, we actually need to make sure it’s on the right threadinternal actual class MainIsolatedState<T:Any> actual constructor(startVal: T) {
private val stableRef = StableRef.create(startVal)
init {
startVal.ensureNeverFrozen()
freeze()
}
actual fun _get(): T = stableRef.get()
}
StableRef
essentially lets you get a pointer, and you can pass that pointer around, but when you call stableRef.get()
, if you’re not on the thread that state belongs to, you have big problems..value
, and there will be an exception if you’re not on the main thread. If the call should be able to be from another thread, there’s mainOrBlock
, which will block the caller and go to the main thread for that state (if configured to allow that)accessConfig {}
and accessInvoke {}
. Something like that. In the main-thread instance, accessConfig
would throw if not in main thread, and accessInvoke
would block and move to main thread. In multi-thread, accessConfig
and accessInvoke
would just wrap calls in synchronized, although doing that just to inject could be problematic for performance.arnaud.giuliani
02/07/2020, 6:11 PMkpgalligan
02/07/2020, 6:57 PMarnaud.giuliani
02/10/2020, 6:04 PM