Cleanup concurrent
This commit is contained in:
committed by
Andrey Breslav
parent
3ab1901f06
commit
c074b4317e
@@ -1,47 +1,43 @@
|
||||
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
|
||||
import kotlin.InlineOption.ONLY_LOCAL_RETURN
|
||||
|
||||
/**
|
||||
Executes given calculation under lock
|
||||
Returns result of the calculation
|
||||
*/
|
||||
* Executes given calculation under lock
|
||||
* Returns result of the calculation
|
||||
*/
|
||||
public inline fun <T> Lock.withLock([inlineOptions(ONLY_LOCAL_RETURN)] action: () -> T): T {
|
||||
lock()
|
||||
try {
|
||||
return action()
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Executes given calculation under read lock
|
||||
Returns result of the calculation
|
||||
*/
|
||||
* Executes given calculation under read lock
|
||||
* Returns result of the calculation
|
||||
*/
|
||||
public inline fun <T> ReentrantReadWriteLock.read([inlineOptions(ONLY_LOCAL_RETURN)] action: () -> T): T {
|
||||
val rl = readLock()
|
||||
rl.lock()
|
||||
try {
|
||||
return action()
|
||||
}
|
||||
finally {
|
||||
} 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
|
||||
*/
|
||||
* 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 <T> ReentrantReadWriteLock.write([inlineOptions(ONLY_LOCAL_RETURN)] action: () -> T): T {
|
||||
val rl = readLock()
|
||||
|
||||
@@ -52,20 +48,19 @@ public inline fun <T> ReentrantReadWriteLock.write([inlineOptions(ONLY_LOCAL_RET
|
||||
wl.lock()
|
||||
try {
|
||||
return action()
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
readCount times { rl.lock() }
|
||||
wl.unlock()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Execute given calculation and await for CountDownLatch
|
||||
Returns result of the calculation
|
||||
*/
|
||||
public fun <T> Int.latch(op: CountDownLatch.() -> T): T {
|
||||
val cdl = CountDownLatch(this)
|
||||
val res = cdl.op()
|
||||
cdl.await()
|
||||
return res
|
||||
* Execute given calculation and await for CountDownLatch
|
||||
* Returns result of the calculation
|
||||
*/
|
||||
public fun <T> Int.latch(operation: CountDownLatch.() -> T): T {
|
||||
val latch = CountDownLatch(this)
|
||||
val result = latch.operation()
|
||||
latch.await()
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -1,31 +1,36 @@
|
||||
package kotlin.concurrent
|
||||
|
||||
import java.util.concurrent.Executor
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Future
|
||||
import java.util.concurrent.Callable
|
||||
import java.util.concurrent.*
|
||||
|
||||
public val currentThread: Thread
|
||||
get() = Thread.currentThread()
|
||||
|
||||
public var Thread.name: String
|
||||
get() = getName()
|
||||
set(name: String) { setName(name) }
|
||||
set(value) {
|
||||
setName(value)
|
||||
}
|
||||
|
||||
public var Thread.daemon: Boolean
|
||||
get() = isDaemon()
|
||||
set(on: Boolean) { setDaemon(on) }
|
||||
set(value) {
|
||||
setDaemon(value)
|
||||
}
|
||||
|
||||
public val Thread.alive: Boolean
|
||||
get() = isAlive()
|
||||
|
||||
public var Thread.priority: Int
|
||||
get() = getPriority()
|
||||
set(prio: Int) { setPriority(prio) }
|
||||
set(value) {
|
||||
setPriority(value)
|
||||
}
|
||||
|
||||
public var Thread.contextClassLoader: ClassLoader?
|
||||
get() = getContextClassLoader()
|
||||
set(loader: ClassLoader?) { setContextClassLoader(loader) }
|
||||
set(value) {
|
||||
setContextClassLoader(value)
|
||||
}
|
||||
|
||||
public fun thread(start: Boolean = true, daemon: Boolean = false, contextClassLoader: ClassLoader? = null, name: String? = null, priority: Int = -1, block: () -> Unit): Thread {
|
||||
val thread = object : Thread() {
|
||||
@@ -50,14 +55,14 @@ public fun thread(start: Boolean = true, daemon: Boolean = false, contextClassLo
|
||||
* Allows you to use the executor as a function to
|
||||
* execute the given block on the [[Executor]].
|
||||
*/
|
||||
public /*inline*/ fun Executor.invoke(action: () -> Unit) {
|
||||
execute(runnable(action))
|
||||
public fun Executor.invoke(action: () -> Unit) {
|
||||
execute(action)
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to use the executor as a function to
|
||||
* execute the given block on the [[Executor]].
|
||||
*/
|
||||
public /*inline*/ fun <T>ExecutorService.invoke(action: () -> T): Future<T> {
|
||||
* Allows you to use the executor as a function to
|
||||
* execute the given block on the [[Executor]].
|
||||
*/
|
||||
public fun <T> ExecutorService.invoke(action: () -> T): Future<T> {
|
||||
return submit(action)
|
||||
}
|
||||
@@ -4,67 +4,67 @@ import java.util.Timer
|
||||
import java.util.TimerTask
|
||||
import java.util.Date
|
||||
|
||||
public fun Timer.schedule(delay: Long, action: TimerTask.()->Unit) : TimerTask {
|
||||
public fun Timer.schedule(delay: Long, action: TimerTask.() -> Unit): TimerTask {
|
||||
val task = timerTask(action)
|
||||
schedule(task, delay)
|
||||
return task
|
||||
}
|
||||
|
||||
public fun Timer.schedule(time: Date, action: TimerTask.()->Unit) : TimerTask {
|
||||
public fun Timer.schedule(time: Date, action: TimerTask.() -> Unit): TimerTask {
|
||||
val task = timerTask(action)
|
||||
schedule(task, time)
|
||||
return task
|
||||
}
|
||||
|
||||
public fun Timer.schedule(delay: Long, period: Long, action: TimerTask.()->Unit) : TimerTask {
|
||||
public fun Timer.schedule(delay: Long, period: Long, action: TimerTask.() -> Unit): TimerTask {
|
||||
val task = timerTask(action)
|
||||
schedule(task, delay, period)
|
||||
return task
|
||||
}
|
||||
|
||||
public fun Timer.schedule(time: Date, period: Long, action: TimerTask.()->Unit) : TimerTask {
|
||||
public fun Timer.schedule(time: Date, period: Long, action: TimerTask.() -> Unit): TimerTask {
|
||||
val task = timerTask(action)
|
||||
schedule(task, time, period)
|
||||
return task
|
||||
}
|
||||
|
||||
public fun Timer.scheduleAtFixedRate(delay: Long, period: Long, action: TimerTask.()->Unit) : TimerTask {
|
||||
public fun Timer.scheduleAtFixedRate(delay: Long, period: Long, action: TimerTask.() -> Unit): TimerTask {
|
||||
val task = timerTask(action)
|
||||
scheduleAtFixedRate(task, delay, period)
|
||||
return task
|
||||
}
|
||||
|
||||
public fun Timer.scheduleAtFixedRate(time: Date, period: Long, action: TimerTask.()->Unit) : TimerTask {
|
||||
public fun Timer.scheduleAtFixedRate(time: Date, period: Long, action: TimerTask.() -> Unit): TimerTask {
|
||||
val task = timerTask(action)
|
||||
scheduleAtFixedRate(task, time, period)
|
||||
return task
|
||||
}
|
||||
|
||||
public fun timer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.toLong(), period: Long, action: TimerTask.()->Unit) : Timer {
|
||||
val timer = if(name == null) Timer(daemon) else Timer(name, daemon)
|
||||
public fun timer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.toLong(), period: Long, action: TimerTask.() -> Unit): Timer {
|
||||
val timer = if (name == null) Timer(daemon) else Timer(name, daemon)
|
||||
timer.schedule(initialDelay, period, action)
|
||||
return timer
|
||||
}
|
||||
|
||||
public fun timer(name: String? = null, daemon: Boolean = false, startAt: Date, period: Long, action: TimerTask.()->Unit) : Timer {
|
||||
val timer = if(name == null) Timer(daemon) else Timer(name, daemon)
|
||||
public fun timer(name: String? = null, daemon: Boolean = false, startAt: Date, period: Long, action: TimerTask.() -> Unit): Timer {
|
||||
val timer = if (name == null) Timer(daemon) else Timer(name, daemon)
|
||||
timer.schedule(startAt, period, action)
|
||||
return timer
|
||||
}
|
||||
|
||||
public fun fixedRateTimer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.toLong(), period: Long, action: TimerTask.()->Unit) : Timer {
|
||||
val timer = if(name == null) Timer(daemon) else Timer(name, daemon)
|
||||
public fun fixedRateTimer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.toLong(), period: Long, action: TimerTask.() -> Unit): Timer {
|
||||
val timer = if (name == null) Timer(daemon) else Timer(name, daemon)
|
||||
timer.scheduleAtFixedRate(initialDelay, period, action)
|
||||
return timer
|
||||
}
|
||||
|
||||
public fun fixedRateTimer(name: String? = null, daemon: Boolean = false, startAt: Date, period : Long, action: TimerTask.()->Unit) : Timer {
|
||||
val timer = if(name == null) Timer(daemon) else Timer(name, daemon)
|
||||
public fun fixedRateTimer(name: String? = null, daemon: Boolean = false, startAt: Date, period: Long, action: TimerTask.() -> Unit): Timer {
|
||||
val timer = if (name == null) Timer(daemon) else Timer(name, daemon)
|
||||
timer.scheduleAtFixedRate(startAt, period, action)
|
||||
return timer
|
||||
}
|
||||
|
||||
public fun timerTask(action: TimerTask.()->Unit) : TimerTask = object: TimerTask() {
|
||||
public fun timerTask(action: TimerTask.() -> Unit): TimerTask = object : TimerTask() {
|
||||
public override fun run() {
|
||||
action()
|
||||
}
|
||||
|
||||
@@ -12,23 +12,15 @@ class TimerTest {
|
||||
test fun scheduledTask() {
|
||||
val counter = AtomicInteger(0)
|
||||
val timer = Timer()
|
||||
/*
|
||||
TODO this generates a compiler error!
|
||||
|
||||
val task = timer.scheduleAtFixedRate(1000, 1000) {
|
||||
val task = timer.scheduleAtFixedRate(1000, 100) {
|
||||
val current = counter.incrementAndGet()
|
||||
println("Timer fired at $current")
|
||||
}
|
||||
*/
|
||||
val task = timerTask {
|
||||
val current = counter.incrementAndGet()
|
||||
println("Timer fired at $current")
|
||||
}
|
||||
timer.scheduleAtFixedRate(task, 1000, 1000)
|
||||
Thread.sleep(5000)
|
||||
Thread.sleep(1500)
|
||||
task.cancel()
|
||||
|
||||
val value = counter.get()
|
||||
assertTrue(value > 2, "current counter is $value")
|
||||
assertTrue(value > 4, "current counter is $value")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user