functional list, queues and package for concurrent

This commit is contained in:
Alex Tkachman
2012-02-01 14:55:36 +02:00
parent 699c43fe58
commit ac729504bf
5 changed files with 87 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
package std.concurrent
abstract class FunctionalList<T>(val size: Int) {
abstract val head: T
abstract val tail: FunctionalList<T>
val empty : Boolean
get() = size == 0
fun add(element: T) : FunctionalList<T> = FunctionalList.Standard(element, this)
fun reversed() : FunctionalList<T> {
if(empty)
return this
var cur = tail
var new = of(head)
while(!cur.empty) {
new = new.add(cur.head)
cur = cur.tail
}
return new
}
fun iterator() = object: Iterator<T> {
var cur = this@FunctionalList
override fun next(): T {
if(cur.empty)
throw java.util.NoSuchElementException()
val head = cur.head
cur = cur.tail
return head
}
override val hasNext: Boolean
get() = !cur.empty
}
class object {
class Empty<T>() : FunctionalList<T>(0) {
override val head: T
get() = throw java.util.NoSuchElementException()
override val tail: FunctionalList<T>
get() = throw java.util.NoSuchElementException()
}
class Standard<T>(override val head: T, override val tail: FunctionalList<T>) : FunctionalList<T>(tail.size+1)
fun <T> emptyList() = Empty<T>()
fun <T> of(element: T) : FunctionalList<T> = FunctionalList.Standard<T>(element,emptyList())
}
}
@@ -0,0 +1,30 @@
package std.concurrent
import java.util.concurrent.Executor
import jet.Iterator
class FunctionalQueue<T>(
val input: FunctionalList<T> = FunctionalList.emptyList<T>(),
val output: FunctionalList<T> = FunctionalList.emptyList<T>()) {
val size : Int
get() = input.size + output.size
val empty : Boolean
get() = size == 0
fun add(element: T) = FunctionalQueue<T>(input add element, output)
fun addFirst(element: T) = FunctionalQueue<T>(input, output add element)
fun removeFirst() : #(T,FunctionalQueue<T>) =
if(output.empty) {
if(input.empty)
throw java.util.NoSuchElementException()
else
FunctionalQueue<T>(FunctionalList.emptyList<T>(), input.reversed()).removeFirst()
}
else {
#(output.head, FunctionalQueue<T>(input, output.tail))
}
}
+58
View File
@@ -0,0 +1,58 @@
package std.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
/*
Executes given calculation under lock
Returns result of the calculation
*/
inline fun <erased T> Lock.withLock(action: ()->T) : T {
lock()
try {
return action()
}
finally {
unlock();
}
}
/*
Executes given calculation under read lock
Returns result of the calculation
*/
inline fun <erased T> ReentrantReadWriteLock.read(action: ()->T) : T {
val rl = readLock().sure()
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
*/
inline fun <erased T> ReentrantReadWriteLock.write(action: ()->T) : T {
val rl = readLock().sure()
val readCount = if (getWriteHoldCount() == 0) getReadHoldCount() else 0
readCount times { rl.unlock() }
val wl = writeLock().sure()
wl.lock()
try {
return action()
}
finally {
readCount times { rl.lock() }
wl.unlock()
}
}
+52
View File
@@ -0,0 +1,52 @@
package std.concurrent
import java.lang.*
import java.util.concurrent.Executor
inline val currentThread = Thread.currentThread().sure()
inline var Thread.name : String
get() = getName().sure()
set(name: String) { setName(name) }
inline var Thread.daemon : Boolean
get() = isDaemon()
set(on: Boolean) { setDaemon(on) }
inline val Thread.alive : Boolean
get() = isAlive()
inline var Thread.priority : Int
get() = getPriority()
set(prio: Int) { setPriority(prio) }
inline var Thread.contextClassLoader : ClassLoader?
get() = getContextClassLoader()
set(loader: ClassLoader?) { setContextClassLoader(loader) }
fun thread(start: Boolean = true, daemon: Boolean = false, contextClassLoader: ClassLoader? = null, name: String? = null, priority: Int = -1, block: ()->Unit) : Thread {
val thread = object: Thread() {
override fun run() {
block()
}
}
if(daemon)
thread.setDaemon(true)
if(priority > 0)
thread.setPriority(priority)
if(name != null)
thread.setName(name)
if(contextClassLoader != null)
thread.setContextClassLoader(contextClassLoader)
if(start)
thread.start()
return thread
}
inline fun Executor.execute(action: ()->Unit) {
execute(object: Runnable{
override fun run() {
action()
}
})
}
+71
View File
@@ -0,0 +1,71 @@
package std.concurrent
import java.util.Timer
import java.util.TimerTask
import java.util.Date
fun Timer.schedule(delay: Long, action: TimerTask.()->Unit) : TimerTask {
val task = createTask(action)
schedule(task, delay)
return task
}
fun Timer.schedule(time: Date, action: TimerTask.()->Unit) : TimerTask {
val task = createTask(action)
schedule(task, time)
return task
}
fun Timer.schedule(delay: Long, period: Long, action: TimerTask.()->Unit) : TimerTask {
val task = createTask(action)
schedule(task, delay, period)
return task
}
fun Timer.schedule(time: Date, period: Long, action: TimerTask.()->Unit) : TimerTask {
val task = createTask(action)
schedule(task, time, period)
return task
}
fun Timer.scheduleAtFixedRate(delay: Long, period: Long, action: TimerTask.()->Unit) : TimerTask {
val task = createTask(action)
scheduleAtFixedRate(task, delay, period)
return task
}
fun Timer.scheduleAtFixedRate(time: Date, period: Long, action: TimerTask.()->Unit) : TimerTask {
val task = createTask(action)
scheduleAtFixedRate(task, time, period)
return task
}
fun timer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.lng, period: Long, action: TimerTask.()->Unit) : Timer {
val timer = if(name == null) Timer(daemon) else Timer(name, daemon)
timer.schedule(initialDelay, period, action)
return timer
}
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
}
fun fixedRateTimer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.lng, period: Long, action: TimerTask.()->Unit) : Timer {
val timer = if(name == null) Timer(daemon) else Timer(name, daemon)
timer.scheduleAtFixedRate(initialDelay, period, action)
return timer
}
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
}
private fun createTask(action: TimerTask.()->Unit) : TimerTask = object: TimerTask() {
override fun run() {
action()
}
}