Skip to content

Commit

Permalink
Remove related code to the old memory manager (#1489)
Browse files Browse the repository at this point in the history
* Remove related code to the old memory manager

* Some fixes
  • Loading branch information
soywiz authored Mar 30, 2023
1 parent bed4308 commit 2250634
Show file tree
Hide file tree
Showing 27 changed files with 48 additions and 115 deletions.
4 changes: 1 addition & 3 deletions e2e-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ tasks.withType<KotlinCompile> {
korge {
id = "com.sample.demo"

// useNewMemoryModel = true

// To enable all targets at once

//targetAll()
Expand Down Expand Up @@ -111,4 +109,4 @@ tasks {
checkReferencesNative.dependsOn(runTask)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import kotlin.native.concurrent.*

// @TODO: Use AtomicReference
actual class KdsAtomicRef<T> actual constructor(initial: T) {
val ref = AtomicReference((initial.freeze()))
val ref = AtomicReference(initial)
actual var value: T
get() = ref.value
set(value) { ref.value = (value.freeze()) }
set(value) { ref.value = value }
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package korlibs.datastructure.concurrent

import kotlin.native.concurrent.AtomicReference
import kotlin.native.concurrent.freeze

class ConcurrentDeque<T : Any> {
private val items = AtomicReference<List<T>>(emptyList<T>().freeze())

init {
this.freeze()
}
private val items = AtomicReference<List<T>>(emptyList<T>())

val size get() = items.value.size

fun add(item: T) {
do {
val oldList = this.items.value
val newList = (oldList + item).freeze()
val newList = (oldList + item)
} while (!this.items.compareAndSet(oldList, newList))
}

Expand All @@ -26,7 +21,7 @@ class ConcurrentDeque<T : Any> {
val oldList = this.items.value
if (oldList.isEmpty()) return null
val lastItem = oldList.first()
val newList = oldList.subList(1, oldList.size).freeze()
val newList = oldList.subList(1, oldList.size)
if (this.items.compareAndSet(oldList, newList)) return lastItem
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import kotlin.math.*
import kotlin.native.concurrent.*

@OptIn(ExperimentalStdlibApi::class)
actual val CONCURRENCY_COUNT: Int = if (Platform.memoryModel == MemoryModel.EXPERIMENTAL) Platform.getAvailableProcessors() else 1
//actual val CONCURRENCY_COUNT: Int = 1
actual val CONCURRENCY_COUNT: Int = Platform.getAvailableProcessors()

val PARALLEL_WORKERS = if (CONCURRENCY_COUNT > 1) Array(CONCURRENCY_COUNT) { Worker.start() } else emptyArray()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package korlibs.logger.atomic

import kotlin.native.concurrent.FreezableAtomicReference
import kotlin.native.concurrent.freeze
import kotlin.native.concurrent.*

internal actual class KloggerAtomicRef<T> actual constructor(initial: T) {
private val ref = FreezableAtomicReference(initial.freeze())
private val ref = AtomicReference(initial)

actual var value: T
get() = ref.value
Expand All @@ -16,7 +15,7 @@ internal actual class KloggerAtomicRef<T> actual constructor(initial: T) {
//synchronized(ref) { ref.set(ref.get()) }
do {
val old = ref.value
val new = block(old).freeze()
val new = block(old)
} while (!ref.compareAndSet(old, new))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package korlibs.logger.test
import korlibs.logger.Logger
import kotlin.native.concurrent.TransferMode
import kotlin.native.concurrent.Worker
import kotlin.native.concurrent.freeze
import kotlin.native.concurrent.isFrozen
import kotlin.test.*

class LoggerMultithreadedTest {
Expand All @@ -17,12 +15,9 @@ class LoggerMultithreadedTest {
logger.level = Logger.Level.INFO
logger.level
}
val future = worker.execute(TransferMode.SAFE, { changeLoggerLevel.freeze() }) { it() }
val future = worker.execute(TransferMode.SAFE, { changeLoggerLevel }) { it() }
val result = future.result

if (Platform.memoryModel != MemoryModel.EXPERIMENTAL) {
assertTrue(logger.isFrozen)
}
assertTrue { result == Logger.Level.INFO }
assertTrue { logger.level == Logger.Level.INFO }

Expand All @@ -33,12 +28,9 @@ class LoggerMultithreadedTest {
logger2.level
}
val worker2 = Worker.start()
val future2 = worker2.execute(TransferMode.SAFE, { getLoggerLevel.freeze() }) { it() }
val future2 = worker2.execute(TransferMode.SAFE, { getLoggerLevel }) { it() }
val result2 = future2.result

if (Platform.memoryModel != MemoryModel.EXPERIMENTAL) {
assertTrue(logger2.isFrozen)
}
assertTrue { result2 == Logger.Level.INFO }
assertTrue { logger2.level == Logger.Level.INFO }

Expand Down
9 changes: 3 additions & 6 deletions kmem/src/commonMain/kotlin/korlibs/memory/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ interface Platform {
val buildVariant: BuildVariant

/**
* JVM: true
* JVM, Android & K/N: true
* Android: true
* JS: false <-- workers have different heaps
* K/N:
* - new memory model: true
* - old memory model: false <-- frozen
* */
**/
val hasMultithreadedSharedHeap: Boolean

val isLittleEndian: Boolean get() = endian == Endian.LITTLE_ENDIAN
Expand Down Expand Up @@ -87,6 +84,6 @@ interface Platform {
override val buildVariant: BuildVariant,
override val rawPlatformName: String,
override val rawOsName: String,
override val hasMultithreadedSharedHeap: Boolean
override val hasMultithreadedSharedHeap: Boolean,
) : Platform
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package korlibs.memory.atomic

import kotlin.native.concurrent.AtomicReference
import kotlin.native.concurrent.freeze

// @TODO: Use AtomicReference
actual class KmemAtomicRef<T> actual constructor(initial: T) {
val ref = AtomicReference(initial.freeze())
val ref = AtomicReference(initial)
actual var value: T
get() = ref.value
set(value) { ref.value = value.freeze() }
set(value) { ref.value = value }
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal actual val currentArch: Arch = when (Platform.cpuArchitecture) {

internal actual val currentIsDebug: Boolean get() = Platform.isDebugBinary
internal actual val currentIsLittleEndian: Boolean get() = Platform.isLittleEndian
internal actual val multithreadedSharedHeap: Boolean = Platform.memoryModel == MemoryModel.EXPERIMENTAL
internal actual val multithreadedSharedHeap: Boolean = true

@SharedImmutable
internal actual val currentRawPlatformName: String = "native-$currentOs-$currentArch-$currentBuildVariant"
Expand Down
19 changes: 1 addition & 18 deletions korau/src/mingwMain/kotlin/korlibs/audio/sound/WaveOutProcess.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import kotlin.native.concurrent.AtomicReference
import kotlin.native.concurrent.Future
import kotlin.native.concurrent.TransferMode
import kotlin.native.concurrent.Worker
import kotlin.native.concurrent.freeze

internal object WINMM : DynamicLibrary("winmm.dll") {
val waveOutOpenExt by func<(phwo: LPHWAVEOUT?, uDeviceID: UINT, pwfx: LPCWAVEFORMATEX?, dwCallback: DWORD_PTR, dwInstance: DWORD_PTR, fdwOpen: DWORD) -> MMRESULT>()
Expand All @@ -52,9 +51,6 @@ private object WaveOutEnd : WaveOutPart
private object WaveOutFlush : WaveOutPart

private class WaveOutReopen(val freq: Int) : WaveOutPart {
init {
this.freeze()
}
}

private interface WaveOutDataBase : WaveOutPart {
Expand All @@ -63,14 +59,10 @@ private interface WaveOutDataBase : WaveOutPart {

//private class WaveOutData(val data: ShortArray) : WaveOutDataBase {
// override fun computeData(): ShortArray = data
//
// init {
// this.freeze()
// }
//}

internal class AtomicDouble(value: Double) {
val atomic = AtomicLong(value.toRawBits()).freeze()
val atomic = AtomicLong(value.toRawBits())

var value: Double
get() = Double.fromBits(atomic.value)
Expand All @@ -87,10 +79,6 @@ internal class WaveOutDataEx(
.interleaved()
.applyProps(pitch, panning, volume)
.data

init {
this.freeze()
}
}
class WaveOutProcess(val freq: Int, val nchannels: Int) {
private val sPosition = AtomicLong(0L)
Expand All @@ -107,11 +95,6 @@ class WaveOutProcess(val freq: Int, val nchannels: Int) {
val length get() = sLength.value
//val isCompleted get() = completed.value != 0L
val pendingAudio get() = numPendingChunks.value != 0L || deque.size > 0

init {
freeze()
}

val pendingCommands get() = deque.size

//fun addData(data: ShortArray) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import korlibs.io.file.std.resourcesVfs

class MainEmojiColrv1 : Scene() {
override suspend fun SContainer.sceneMain() {
val font = resourcesVfs["twemoji-glyf_colr_1.ttf"].readTtfFont(preload = false).asFallbackOf(DefaultTtfFont)
val font2 = measureTime({resourcesVfs["noto-glyf_colr_1.ttf"].readTtfFont(preload = false).asFallbackOf(DefaultTtfFont)}) {
val font = resourcesVfs["twemoji-glyf_colr_1.ttf"].readTtfFont().asFallbackOf(DefaultTtfFont)
val font2 = measureTime({resourcesVfs["noto-glyf_colr_1.ttf"].readTtfFont().asFallbackOf(DefaultTtfFont)}) {
println("Read font in... $it")
}
val font3 = SystemFont.getEmojiFont().asFallbackOf(DefaultTtfFont)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import korlibs.image.text.*
import korlibs.io.async.*
import korlibs.io.file.std.*
import korlibs.math.geom.*
import korlibs.math.geom.vector.*
import kotlin.properties.*

class MainGraphicsText : Scene() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MainTextInput : Scene() {
//sceneContainer.changeTo({ MainSWF().apply { init(it) } })
//image(atlas.bitmap)

val emojiFont = resourcesVfs["noto-glyf_colr_1.ttf"].readTtfFont(preload = false)
val emojiFont = resourcesVfs["noto-glyf_colr_1.ttf"].readTtfFont()
val font = DefaultTtfFont.withFallback(emojiFont)

uiTextInput("HELLO", width = 256.0, height = 64.0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ suspend fun resources(): Resources = injector().get()
fun resourceBitmap(@ResourcePath path: String, mipmaps: Boolean = true, cache: ResourceCache = ResourceCache.LOCAL) = resource(cache) { root[path].readBitmapSlice().also { it.bmp.mipmaps(enable = mipmaps) } }
fun resourceFont(@ResourcePath path: String, mipmaps: Boolean = true, cache: ResourceCache = ResourceCache.LOCAL) = resource(cache) { root[path].readFont(mipmaps = mipmaps) }
fun resourceBitmapFont(@ResourcePath path: String, mipmaps: Boolean = true, cache: ResourceCache = ResourceCache.LOCAL) = resource(cache) { root[path].readBitmapFont(mipmaps = mipmaps) }
fun resourceTtfFont(@ResourcePath path: String, preload: Boolean = false, cache: ResourceCache = ResourceCache.LOCAL) = resource(cache) { root[path].readTtfFont(preload = preload) }
fun resourceTtfFont(@ResourcePath path: String, preload: Boolean = false, cache: ResourceCache = ResourceCache.LOCAL) = resource(cache) { root[path].readTtfFont() }
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,11 @@ class KorgeMultithreadedTest {
}

fun <T> runInWorker(block: () -> T): T {
block.freeze()
block()

val worker = Worker.start()
return try {
worker.execute(TransferMode.SAFE, { block.freeze() }) { it().freeze() }.result
worker.execute(TransferMode.SAFE, { block }) { it() }.result
} finally {
worker.requestTermination(processScheduledJobs = false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private var DefaultTtfFontOrNull: TtfFont? = null
val DefaultTtfFont: TtfFont get() {
if (DefaultTtfFontOrNull == null) {
val res = measureTimeWithResult {
TtfFont(DefaultTtfFontBytes(), freeze = true, extName = "Default Font")
TtfFont(DefaultTtfFontBytes(), extName = "Default Font")
//TtfFont(DefaultTtfFontBytes, preloadAllGlyphs = false, extName = "Default Font")
}
//println("Loaded DefaultTtfFont in... ${res.time}")
Expand Down
4 changes: 1 addition & 3 deletions korim/src/commonMain/kotlin/korlibs/image/font/FontExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package korlibs.image.font

import korlibs.memory.readS32BE
import korlibs.image.format.ImageDecodingProps
import korlibs.image.format.ImageFormat
import korlibs.image.format.RegisteredImageFormats
import korlibs.io.file.VfsFile

suspend fun VfsFile.readFont(preload: Boolean = false, props: ImageDecodingProps = ImageDecodingProps.DEFAULT, mipmaps: Boolean = true): Font {
val header = readRangeBytes(0 until 16)
return when (header.readS32BE(0)) {
0x74746366, 0x4F54544F, 0x00010000 -> readTtfFont(preload)
0x74746366, 0x4F54544F, 0x00010000 -> readTtfFont()
//0x504B0304 -> this.openAsZip { it.listRecursive().filter { it.extensionLC == "ttf" || it.extensionLC == "fnt" || it.extensionLC == "ttc" || it.extensionLC == "otf" }.firstOrNull()?.readFont() ?: error("Can't find TTF or FNT on zip") }
else -> readBitmapFont(props, mipmaps)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ open class FolderBasedNativeSystemFontProvider(
return _namesMapLC.value!!
}

override fun loadFontByName(name: String, freeze: Boolean): TtfFont? =
runBlockingNoJs { namesMapLC[name.normalizeName()]?.let { TtfFont(it.readAll(), freeze = freeze) } }
override fun loadFontByName(name: String): TtfFont? =
runBlockingNoJs { namesMapLC[name.normalizeName()]?.let { TtfFont(it.readAll()) } }
}

abstract class TtfNativeSystemFontProvider() : NativeSystemFontProvider() {
abstract fun loadFontByName(name: String, freeze: Boolean = false): TtfFont?
abstract fun loadFontByName(name: String): TtfFont?
abstract fun defaultFont(): TtfFont

fun String.normalizeName() = this.toLowerCase().trim()
Expand All @@ -220,7 +220,7 @@ abstract class TtfNativeSystemFontProvider() : NativeSystemFontProvider() {

fun locateFontByName(name: String): TtfFont? {
val normalizedName = name.normalizeName()
return ttfCache.getOrPut(normalizedName) { loadFontByName(name, freeze = false) }
return ttfCache.getOrPut(normalizedName) { loadFontByName(name) }
}

fun ttf(systemFont: SystemFont) = locateFontByName(systemFont.name) ?: defaultFont()
Expand Down Expand Up @@ -262,6 +262,6 @@ open class FallbackNativeSystemFontProvider(val ttf: TtfFont) : TtfNativeSystemF
val vfs = VfsFileFromData(ttf.getAllBytesUnsafe(), "ttf")
override fun getTtfFromSystemFont(systemFont: SystemFont): TtfFont = ttf
override fun listFontNamesWithFiles(): Map<String, VfsFile> = mapOf(ttf.ttfCompleteName to vfs)
override fun loadFontByName(name: String, freeze: Boolean): TtfFont? = ttf
override fun loadFontByName(name: String): TtfFont? = ttf
override fun defaultFont(): TtfFont = ttf
}
Loading

0 comments on commit 2250634

Please sign in to comment.