Skip to content

Commit

Permalink
Align Synchronization.kt styles between ui:ui, ui:ui-text, foundation…
Browse files Browse the repository at this point in the history
…:foundation modules
  • Loading branch information
pjBooms committed Oct 30, 2024
1 parent 246143f commit 71b6ad1
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 30 deletions.
4 changes: 0 additions & 4 deletions compose/foundation/foundation/api/desktop/foundation.api
Original file line number Diff line number Diff line change
Expand Up @@ -1731,10 +1731,6 @@ public final class androidx/compose/foundation/pager/PagerStateKt {
public static final fun rememberPagerState (IFLkotlin/jvm/functions/Function0;Landroidx/compose/runtime/Composer;II)Landroidx/compose/foundation/pager/PagerState;
}

public final class androidx/compose/foundation/platform/SynchronizationKt {
public static final fun synchronized (Landroidx/compose/foundation/platform/SynchronizedObject;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
}

public abstract interface class androidx/compose/foundation/relocation/BringIntoViewRequester {
public abstract fun bringIntoView (Landroidx/compose/ui/geometry/Rect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun bringIntoView$default (Landroidx/compose/foundation/relocation/BringIntoViewRequester;Landroidx/compose/ui/geometry/Rect;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@

package androidx.compose.foundation.platform

internal expect class SynchronizedObject()
internal expect class SynchronizedObject

/**
* Returns [ref] as a [SynchronizedObject] on platforms where [Any] is a valid [SynchronizedObject],
* or a new [SynchronizedObject] instance if [ref] is null or this is not supported on the current
* platform.
*/
internal expect inline fun makeSynchronizedObject(ref: Any? = null): SynchronizedObject

@PublishedApi
internal expect inline fun <R> synchronized(lock: SynchronizedObject, block: () -> R): R

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package androidx.compose.foundation.text.selection

import androidx.compose.foundation.platform.SynchronizedObject
import androidx.compose.foundation.platform.makeSynchronizedObject
import androidx.compose.foundation.platform.synchronized
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
Expand All @@ -32,7 +32,7 @@ internal class MultiWidgetSelectionDelegate(
private val coordinatesCallback: () -> LayoutCoordinates?,
private val layoutResultCallback: () -> TextLayoutResult?
) : Selectable {
private val lock = SynchronizedObject()
private val lock = makeSynchronizedObject(this)

private var _previousTextLayoutResult: TextLayoutResult? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import kotlin.jvm.JvmName

internal actual class SynchronizedObject : kotlinx.atomicfu.locks.SynchronizedObject()

@PublishedApi
internal actual inline fun makeSynchronizedObject(ref: Any?) = SynchronizedObject()

internal actual inline fun <R> synchronized(lock: SynchronizedObject, block: () -> R): R =
kotlinx.atomicfu.locks.synchronized(lock, block)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.createFontFamilyResolver
import androidx.compose.ui.text.font.toFontFamily
import androidx.compose.ui.text.platform.SynchronizedObject
import androidx.compose.ui.text.platform.createSynchronizedObject
import androidx.compose.ui.text.platform.makeSynchronizedObject
import androidx.compose.ui.text.platform.synchronized
import androidx.compose.ui.text.style.ResolvedTextDirection
import androidx.compose.ui.text.style.TextOverflow
Expand Down Expand Up @@ -272,7 +272,7 @@ private constructor(private val fontFamilyResolver: FontFamily.Resolver) : Font.
// call getFontResourceLoader, and evaluate if FontFamily.Resolver is being correctly cached
// (via e.g. remember)
var cache = mutableMapOf<FontFamily.Resolver, Font.ResourceLoader>()
val lock: SynchronizedObject = createSynchronizedObject()
val lock: SynchronizedObject = makeSynchronizedObject()

fun from(fontFamilyResolver: FontFamily.Resolver): Font.ResourceLoader {
synchronized(lock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package androidx.compose.ui.text.font

import androidx.collection.SieveCache
import androidx.compose.runtime.State
import androidx.compose.ui.text.platform.createSynchronizedObject
import androidx.compose.ui.text.platform.makeSynchronizedObject
import androidx.compose.ui.text.platform.synchronized
import androidx.compose.ui.util.fastMap

Expand Down Expand Up @@ -165,7 +165,7 @@ internal sealed interface TypefaceResult : State<Any> {
}

internal class TypefaceRequestCache {
internal val lock = createSynchronizedObject()
internal val lock = makeSynchronizedObject()
// @GuardedBy("lock")
private val resultCache = SieveCache<TypefaceRequest, TypefaceResult>(16, 16)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.text.platform.FontCacheManagementDispatcher
import androidx.compose.ui.text.platform.createSynchronizedObject
import androidx.compose.ui.text.platform.makeSynchronizedObject
import androidx.compose.ui.text.platform.synchronized
import androidx.compose.ui.util.fastDistinctBy
import androidx.compose.ui.util.fastFilter
Expand Down Expand Up @@ -367,7 +367,7 @@ internal class AsyncTypefaceCache {
// @GuardedBy("cacheLock")
private val permanentCache = mutableScatterMapOf<Key, AsyncTypefaceResult>()

private val cacheLock = createSynchronizedObject()
private val cacheLock = makeSynchronizedObject()

fun put(
font: Font,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ package androidx.compose.ui.text.platform

internal expect class SynchronizedObject

internal expect fun createSynchronizedObject(): SynchronizedObject
/**
* Returns [ref] as a [SynchronizedObject] on platforms where [Any] is a valid [SynchronizedObject],
* or a new [SynchronizedObject] instance if [ref] is null or this is not supported on the current
* platform.
*/
internal expect inline fun makeSynchronizedObject(ref: Any? = null): SynchronizedObject

@PublishedApi
internal expect inline fun <R> synchronized(lock: SynchronizedObject, block: () -> R): R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlin.jvm.JvmName

internal actual class SynchronizedObject : kotlinx.atomicfu.locks.SynchronizedObject()

internal actual fun createSynchronizedObject() = SynchronizedObject()
internal actual inline fun makeSynchronizedObject(ref: Any?) = SynchronizedObject()

@PublishedApi
internal actual inline fun <R> synchronized(lock: SynchronizedObject, block: () -> R): R =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package androidx.compose.ui.autofill

import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.platform.createSynchronizedObject
import androidx.compose.ui.platform.makeSynchronizedObject
import androidx.compose.ui.platform.synchronized

/**
Expand Down Expand Up @@ -73,7 +73,7 @@ class AutofillNode(
/*@GuardedBy("this")*/
private var previousId = 0

private val lock = createSynchronizedObject()
private val lock = makeSynchronizedObject(this)
private fun generateId() = synchronized(lock) { ++previousId }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import androidx.compose.ui.graphics.PathFillType
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.StrokeJoin
import androidx.compose.ui.internal.checkPrecondition
import androidx.compose.ui.platform.createSynchronizedObject
import androidx.compose.ui.platform.makeSynchronizedObject
import androidx.compose.ui.platform.synchronized
import androidx.compose.ui.unit.Dp

Expand Down Expand Up @@ -378,7 +378,7 @@ internal constructor(

companion object {
private var imageVectorCount = 0
private val lock = createSynchronizedObject()
private val lock = makeSynchronizedObject(this)

internal fun generateImageVectorId(): Int {
synchronized(lock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import androidx.compose.ui.node.requireLayoutNode
import androidx.compose.ui.platform.InspectorInfo
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.ViewConfiguration
import androidx.compose.ui.platform.createSynchronizedObject
import androidx.compose.ui.platform.makeSynchronizedObject
import androidx.compose.ui.platform.synchronized
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntSize
Expand Down Expand Up @@ -546,7 +546,7 @@ internal class SuspendingPointerInputModifierNodeImpl(
private val pointerHandlers =
mutableVectorOf<SuspendingPointerInputModifierNodeImpl.PointerEventHandlerCoroutine<*>>()

private val pointerHandlersLock = createSynchronizedObject()
private val pointerHandlersLock = makeSynchronizedObject(pointerHandlers)

/**
* Scratch list for dispatching to handlers for a particular phase. Used to hold a copy of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ package androidx.compose.ui.platform

internal expect class SynchronizedObject

internal expect fun createSynchronizedObject(): SynchronizedObject
/**
* Returns [ref] as a [SynchronizedObject] on platforms where [Any] is a valid [SynchronizedObject],
* or a new [SynchronizedObject] instance if [ref] is null or this is not supported on the current
* platform.
*/
internal expect inline fun makeSynchronizedObject(ref: Any? = null): SynchronizedObject

@PublishedApi
internal expect inline fun <R> synchronized(lock: SynchronizedObject, block: () -> R): R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package androidx.compose.ui.node

import androidx.compose.runtime.snapshots.Snapshot
import androidx.compose.ui.platform.createSynchronizedObject
import androidx.compose.ui.platform.makeSynchronizedObject
import androidx.compose.ui.getCurrentThreadId
import androidx.compose.ui.platform.synchronized
import kotlinx.atomicfu.atomic
Expand Down Expand Up @@ -105,7 +105,7 @@ internal class SnapshotInvalidationTracker(
private class CommandList(
private var onNewCommand: () -> Unit
) {
private val lock = createSynchronizedObject()
private val lock = makeSynchronizedObject()
private val list = mutableListOf<() -> Unit>()
private val listCopy = mutableListOf<() -> Unit>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ internal class FlushCoroutineDispatcher(
private val scope = CoroutineScope(scope.coroutineContext.minusKey(Job))
private var immediateTasks = ArrayDeque<Runnable>()
private val delayedTasks = ArrayDeque<Runnable>()
private val immediateTasksLock = createSynchronizedObject()
private val delayedTasksLock = createSynchronizedObject()
private val immediateTasksLock = makeSynchronizedObject()
private val delayedTasksLock = makeSynchronizedObject()
private var immediateTasksSwap = ArrayDeque<Runnable>()
@Volatile
private var isPerformingRun = false
private val runLock = createSynchronizedObject()
private val runLock = makeSynchronizedObject()

override fun dispatch(context: CoroutineContext, block: Runnable) {
synchronized(immediateTasksLock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlin.jvm.JvmName

internal actual class SynchronizedObject : kotlinx.atomicfu.locks.SynchronizedObject()

internal actual fun createSynchronizedObject() = SynchronizedObject()
internal actual inline fun makeSynchronizedObject(ref: Any?) = SynchronizedObject()

@PublishedApi
internal actual inline fun <R> synchronized(lock: SynchronizedObject, block: () -> R): R =
Expand Down

0 comments on commit 71b6ad1

Please sign in to comment.