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

kotlin.concurrent.Locks.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
package kotlin.concurrent

import java.util.concurrent.locks.Lock
import java.util.concurrent.locks.ReadWriteLock
import java.util.concurrent.locks.ReentrantReadWriteLock
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock
import java.util.concurrent.CountDownLatch

/**
Executes given calculation under lock
Returns result of the calculation
*/
public inline fun  Lock.withLock(action: ()->T) : T {
    lock()
    try {
        return action()
    }
    finally {
        unlock();
    }
}

/**
Executes given calculation under read lock
Returns result of the calculation
*/
public inline fun  ReentrantReadWriteLock.read(action: ()->T) : T {
    val rl = readLock()
    rl.lock()
    try {
        return action()
    }
    finally {
        rl.unlock()
    }
}

/**
Executes given calculation under write lock.
The method does upgrade from read to write lock if needed
If such write has been initiated by checking some condition, the condition must be rechecked inside the action to avoid possible races
Returns result of the calculation
*/
public inline fun  ReentrantReadWriteLock.write(action: ()->T) : T {
    val rl = readLock()

    val readCount = if (getWriteHoldCount() == 0) getReadHoldCount() else 0
    readCount times { rl.unlock() }

    val wl = writeLock()
    wl.lock()
    try {
        return action()
    }
    finally {
        readCount times { rl.lock() }
        wl.unlock()
    }
}

/**
Execute given calculation and await for CountDownLatch
Returns result of the calculation
*/
fun  Int.latch(op:  CountDownLatch.() -> T) : T {
    val cdl = CountDownLatch(this)
    val res = cdl.op()
    cdl.await()
    return res
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy