Inline-only in kotlin.concurrent, make all timer utilities inline.

This commit is contained in:
Ilya Gorbunov
2016-01-28 21:29:25 +03:00
parent ce5fd3ee77
commit 40fae0463f
3 changed files with 39 additions and 18 deletions
@@ -10,6 +10,7 @@ import java.util.concurrent.CountDownLatch
* Executes the given [action] under this lock. * Executes the given [action] under this lock.
* @return the return value of the action. * @return the return value of the action.
*/ */
@kotlin.internal.InlineOnly
public inline fun <T> Lock.withLock(action: () -> T): T { public inline fun <T> Lock.withLock(action: () -> T): T {
lock() lock()
try { try {
@@ -23,6 +24,7 @@ public inline fun <T> Lock.withLock(action: () -> T): T {
* Executes the given [action] under the read lock of this lock. * Executes the given [action] under the read lock of this lock.
* @return the return value of the action. * @return the return value of the action.
*/ */
@kotlin.internal.InlineOnly
public inline fun <T> ReentrantReadWriteLock.read(action: () -> T): T { public inline fun <T> ReentrantReadWriteLock.read(action: () -> T): T {
val rl = readLock() val rl = readLock()
rl.lock() rl.lock()
@@ -39,6 +41,7 @@ public inline fun <T> ReentrantReadWriteLock.read(action: () -> T): T {
* If such write has been initiated by checking some condition, the condition must be rechecked inside the action to avoid possible races. * If such write has been initiated by checking some condition, the condition must be rechecked inside the action to avoid possible races.
* @return the return value of the action. * @return the return value of the action.
*/ */
@kotlin.internal.InlineOnly
public inline fun <T> ReentrantReadWriteLock.write(action: () -> T): T { public inline fun <T> ReentrantReadWriteLock.write(action: () -> T): T {
val rl = readLock() val rl = readLock()
@@ -52,6 +52,7 @@ public fun thread(start: Boolean = true, isDaemon: Boolean = false, contextClass
* Then if it is still `null`, the provided [default] function is called and its result * Then if it is still `null`, the provided [default] function is called and its result
* is stored for the current thread and then returned. * is stored for the current thread and then returned.
*/ */
@kotlin.internal.InlineOnly
public inline fun <T: Any> ThreadLocal<T>.getOrSet(default: () -> T): T { public inline fun <T: Any> ThreadLocal<T>.getOrSet(default: () -> T): T {
return get() ?: default().apply { set(this) } return get() ?: default().apply { set(this) }
} }
+35 -18
View File
@@ -9,7 +9,8 @@ import java.util.TimerTask
/** /**
* Schedules an [action] to be executed after the specified [delay] (expressed in milliseconds). * Schedules an [action] to be executed after the specified [delay] (expressed in milliseconds).
*/ */
public fun Timer.schedule(delay: Long, action: TimerTask.() -> Unit): TimerTask { @kotlin.internal.InlineOnly
public inline fun Timer.schedule(delay: Long, crossinline action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action) val task = timerTask(action)
schedule(task, delay) schedule(task, delay)
return task return task
@@ -18,7 +19,8 @@ public fun Timer.schedule(delay: Long, action: TimerTask.() -> Unit): TimerTask
/** /**
* Schedules an [action] to be executed at the specified [time]. * Schedules an [action] to be executed at the specified [time].
*/ */
public fun Timer.schedule(time: Date, action: TimerTask.() -> Unit): TimerTask { @kotlin.internal.InlineOnly
public inline fun Timer.schedule(time: Date, crossinline action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action) val task = timerTask(action)
schedule(task, time) schedule(task, time)
return task return task
@@ -29,7 +31,8 @@ public fun Timer.schedule(time: Date, action: TimerTask.() -> Unit): TimerTask {
* in milliseconds) and with the interval of [period] milliseconds between the end of the previous task * in milliseconds) and with the interval of [period] milliseconds between the end of the previous task
* and the start of the next one. * and the start of the next one.
*/ */
public fun Timer.schedule(delay: Long, period: Long, action: TimerTask.() -> Unit): TimerTask { @kotlin.internal.InlineOnly
public inline fun Timer.schedule(delay: Long, period: Long, crossinline action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action) val task = timerTask(action)
schedule(task, delay, period) schedule(task, delay, period)
return task return task
@@ -39,7 +42,8 @@ public fun Timer.schedule(delay: Long, period: Long, action: TimerTask.() -> Uni
* Schedules an [action] to be executed periodically, starting at the specified [time] and with the * Schedules an [action] to be executed periodically, starting at the specified [time] and with the
* interval of [period] milliseconds between the end of the previous task and the start of the next one. * interval of [period] milliseconds between the end of the previous task and the start of the next one.
*/ */
public fun Timer.schedule(time: Date, period: Long, action: TimerTask.() -> Unit): TimerTask { @kotlin.internal.InlineOnly
public inline fun Timer.schedule(time: Date, period: Long, crossinline action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action) val task = timerTask(action)
schedule(task, time, period) schedule(task, time, period)
return task return task
@@ -50,7 +54,8 @@ public fun Timer.schedule(time: Date, period: Long, action: TimerTask.() -> Unit
* in milliseconds) and with the interval of [period] milliseconds between the start of the previous task * in milliseconds) and with the interval of [period] milliseconds between the start of the previous task
* and the start of the next one. * and the start of the next one.
*/ */
public fun Timer.scheduleAtFixedRate(delay: Long, period: Long, action: TimerTask.() -> Unit): TimerTask { @kotlin.internal.InlineOnly
public inline fun Timer.scheduleAtFixedRate(delay: Long, period: Long, crossinline action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action) val task = timerTask(action)
scheduleAtFixedRate(task, delay, period) scheduleAtFixedRate(task, delay, period)
return task return task
@@ -60,12 +65,17 @@ public fun Timer.scheduleAtFixedRate(delay: Long, period: Long, action: TimerTas
* Schedules an [action] to be executed periodically, starting at the specified [time] and with the * Schedules an [action] to be executed periodically, starting at the specified [time] and with the
* interval of [period] milliseconds between the start of the previous task and the start of the next one. * interval of [period] milliseconds between the start of the previous task and the start of the next one.
*/ */
public fun Timer.scheduleAtFixedRate(time: Date, period: Long, action: TimerTask.() -> Unit): TimerTask { @kotlin.internal.InlineOnly
public inline fun Timer.scheduleAtFixedRate(time: Date, period: Long, crossinline action: TimerTask.() -> Unit): TimerTask {
val task = timerTask(action) val task = timerTask(action)
scheduleAtFixedRate(task, time, period) scheduleAtFixedRate(task, time, period)
return task return task
} }
// exposed as public
internal fun timer(name: String?, daemon: Boolean) = if (name == null) Timer(daemon) else Timer(name, daemon)
/** /**
* Creates a timer that executes the specified [action] periodically, starting after the specified [initialDelay] * Creates a timer that executes the specified [action] periodically, starting after the specified [initialDelay]
* (expressed in milliseconds) and with the interval of [period] milliseconds between the end of the previous task * (expressed in milliseconds) and with the interval of [period] milliseconds between the end of the previous task
@@ -74,8 +84,10 @@ public fun Timer.scheduleAtFixedRate(time: Date, period: Long, action: TimerTask
* @param name the name to use for the thread which is running the timer. * @param name the name to use for the thread which is running the timer.
* @param daemon if `true`, the thread is started as a daemon thread (the VM will exit when only daemon threads are running). * @param daemon if `true`, the thread is started as a daemon thread (the VM will exit when only daemon threads are running).
*/ */
public fun timer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.toLong(), period: Long, action: TimerTask.() -> Unit): Timer { @kotlin.internal.InlineOnly
val timer = if (name == null) Timer(daemon) else Timer(name, daemon) public inline fun timer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.toLong(), period: Long, crossinline action: TimerTask.() -> Unit): Timer {
@Suppress("INVISIBLE_MEMBER_FROM_INLINE")
val timer = timer(name, daemon)
timer.schedule(initialDelay, period, action) timer.schedule(initialDelay, period, action)
return timer return timer
} }
@@ -87,8 +99,10 @@ public fun timer(name: String? = null, daemon: Boolean = false, initialDelay: Lo
* @param name the name to use for the thread which is running the timer. * @param name the name to use for the thread which is running the timer.
* @param daemon if `true`, the thread is started as a daemon thread (the VM will exit when only daemon threads are running). * @param daemon if `true`, the thread is started as a daemon thread (the VM will exit when only daemon threads are running).
*/ */
public fun timer(name: String? = null, daemon: Boolean = false, startAt: Date, period: Long, action: TimerTask.() -> Unit): Timer { @kotlin.internal.InlineOnly
val timer = if (name == null) Timer(daemon) else Timer(name, daemon) public inline fun timer(name: String? = null, daemon: Boolean = false, startAt: Date, period: Long, crossinline action: TimerTask.() -> Unit): Timer {
@Suppress("INVISIBLE_MEMBER_FROM_INLINE")
val timer = timer(name, daemon)
timer.schedule(startAt, period, action) timer.schedule(startAt, period, action)
return timer return timer
} }
@@ -101,8 +115,10 @@ public fun timer(name: String? = null, daemon: Boolean = false, startAt: Date, p
* @param name the name to use for the thread which is running the timer. * @param name the name to use for the thread which is running the timer.
* @param daemon if `true`, the thread is started as a daemon thread (the VM will exit when only daemon threads are running). * @param daemon if `true`, the thread is started as a daemon thread (the VM will exit when only daemon threads are running).
*/ */
public fun fixedRateTimer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.toLong(), period: Long, action: TimerTask.() -> Unit): Timer { @kotlin.internal.InlineOnly
val timer = if (name == null) Timer(daemon) else Timer(name, daemon) public inline fun fixedRateTimer(name: String? = null, daemon: Boolean = false, initialDelay: Long = 0.toLong(), period: Long, crossinline action: TimerTask.() -> Unit): Timer {
@Suppress("INVISIBLE_MEMBER_FROM_INLINE")
val timer = timer(name, daemon)
timer.scheduleAtFixedRate(initialDelay, period, action) timer.scheduleAtFixedRate(initialDelay, period, action)
return timer return timer
} }
@@ -114,8 +130,10 @@ public fun fixedRateTimer(name: String? = null, daemon: Boolean = false, initial
* @param name the name to use for the thread which is running the timer. * @param name the name to use for the thread which is running the timer.
* @param daemon if `true`, the thread is started as a daemon thread (the VM will exit when only daemon threads are running). * @param daemon if `true`, the thread is started as a daemon thread (the VM will exit when only daemon threads are running).
*/ */
public fun fixedRateTimer(name: String? = null, daemon: Boolean = false, startAt: Date, period: Long, action: TimerTask.() -> Unit): Timer { @kotlin.internal.InlineOnly
val timer = if (name == null) Timer(daemon) else Timer(name, daemon) public inline fun fixedRateTimer(name: String? = null, daemon: Boolean = false, startAt: Date, period: Long, crossinline action: TimerTask.() -> Unit): Timer {
@Suppress("INVISIBLE_MEMBER_FROM_INLINE")
val timer = timer(name, daemon)
timer.scheduleAtFixedRate(startAt, period, action) timer.scheduleAtFixedRate(startAt, period, action)
return timer return timer
} }
@@ -123,8 +141,7 @@ public fun fixedRateTimer(name: String? = null, daemon: Boolean = false, startAt
/** /**
* Wraps the specified [action] in a [TimerTask]. * Wraps the specified [action] in a [TimerTask].
*/ */
public fun timerTask(action: TimerTask.() -> Unit): TimerTask = object : TimerTask() { @kotlin.internal.InlineOnly
public override fun run() { public inline fun timerTask(crossinline action: TimerTask.() -> Unit): TimerTask = object : TimerTask() {
action() override fun run() = action()
}
} }