ivan.savytskyi
03/10/2019, 5:41 AMTransferMode.UNSAFE
for Worker
transfer mode? Can we say if we are 100% sure that no one is going to mutate the thing we passed from the producer to the job, then it’s ok to use unsafe mode (and avoid freezing)? And second question what consequences to expect if smth goes wrong? (one thing I can think about is mem corruption issue, is there anything to watch out?)olonho
03/10/2019, 8:18 AMSAFE
shows that you need freezing, using UNSAFE
will lead to random crasheskpgalligan
03/10/2019, 3:44 PMivan.savytskyi
03/10/2019, 6:47 PMivan.savytskyi
03/10/2019, 6:57 PMassert(NSThread.isMainThread())
val callbackRef = StableRef.create(callback).asCPointer()
worker.execute(
mode = TransferMode.SAFE,
producer = { (callbackRef to { execute() }).freeze() },
job = {
val callbackRef = it.first
val result = it.second()
dispatch_async_f(
queue = NSOperationQueue.mainQueue.underlyingQueue,
context = DetachedObjectGraph { (callbackRef to result).freeze() }.asCPointer(),
work = staticCFunction { it ->
val callbackRefAndResult = DetachedObjectGraph<Pair<COpaquePointer, R>>(it).attach()
val callbackRef = callbackRefAndResult.first.asStableRef<(R) -> Unit>()
val callback = callbackRef.get()
val result = callbackRefAndResult.second
callbackRef.dispose()
callback(result)
}
)
}
)
ivan.savytskyi
03/10/2019, 6:59 PMkpgalligan
03/10/2019, 7:02 PMkpgalligan
03/10/2019, 7:10 PMolonho
03/10/2019, 7:11 PMvoid*
and holds the reference (by incrementing reference counter). So the answer is yes, if you want to use it in the thread object is attached to, and no otherwise.kpgalligan
03/10/2019, 7:11 PMkpgalligan
03/10/2019, 7:12 PMolonho
03/10/2019, 7:13 PMkpgalligan
03/10/2019, 7:13 PMivan.savytskyi
03/11/2019, 2:01 PM