Ayfri
09/08/2021, 9:39 PM.let
or similar functions but I can't come up with somethingJoffrey
09/08/2021, 9:44 PMprivate fun enableAction() = (!action).also { action = true }
Ayfri
09/08/2021, 9:47 PMephemient
09/08/2021, 10:28 PMval action = AtomicBoolean()
fun enableAction() = !action.getAndSet(true)
kotlinx.atomicfu can do similar for common code as wellJoffrey
09/08/2021, 10:31 PMAtomicBoolean
is thread-safe, so this might incur a performance penaltyJoffrey
09/08/2021, 10:31 PMephemient
09/08/2021, 10:46 PMTomasz Krakowiak
09/09/2021, 5:55 AMTomasz Krakowiak
09/09/2021, 6:02 AMTomasz Krakowiak
09/09/2021, 6:04 AMephemient
09/09/2021, 8:08 AMTomasz Krakowiak
09/09/2021, 8:41 AMThe specifications of these methods enable implementations to employ efficient machine-level atomic instructions that are available on contemporary processors. However on some platforms, support may entail some form of internal locking. Thus the methods are not strictly guaranteed to be non-blocking -- a thread may block transiently before performing the operation.https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/package-summary.html And there's also exclusion from volatile semantics for
weakCompareAndSet
atomically reads and conditionally writes a variable but does not create any happens-before orderings, so provides no guarantees with respect to previous or subsequent reads and writes of any variables other than the target of theweakCompareAndSet
.weakCompareAndSet
Muhammet Emin Gündoğar
09/13/2021, 9:13 PMJoffrey
09/13/2021, 9:24 PMaction
to true, 2) returns true if action
has changed (because it stores the old value and compares with the new). So effectively, since action
is always true in the end, it only changes when it was initially false. So if we forget about setting action to true, we need to return !action
. Using also
allows to perform extra actions without changing the value of the expression, so I used that to set action to true after calculating the expression !action
(so it doesn't affect the result)Muhammet Emin Gündoğar
09/14/2021, 12:12 PM