All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.mzarnowski.infra.Writer.kt Maven / Gradle / Ivy

The newest version!
package dev.mzarnowski.infra

class Writer internal constructor(val buffer: Buffer) {
    private val mask = buffer.mask
    private val length = buffer.capacity

    @Volatile
    private var first = 0

    private var available = length - 1

    fun position(): Int = first

    fun claim(amount: Int): Int {
        if (available < amount) {
            // a slot just before the reader.
            val lastReader = buffer.readers.findClosest()
            available = (lastReader - first - 1) and mask
        }

        return available
    }

    fun release(n: Int) {
        first = (first + n) and mask
        available -= n
    }

    fun write(offset: Int, value: A) {
        buffer.write((first + offset) and mask, value)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy