From 40fae0463fd96781f8a6e59b3d882d663e052b26 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 28 Jan 2016 21:29:25 +0300 Subject: [PATCH] Inline-only in kotlin.concurrent, make all timer utilities inline. --- .../stdlib/src/kotlin/concurrent/Locks.kt | 3 ++ .../stdlib/src/kotlin/concurrent/Thread.kt | 1 + .../stdlib/src/kotlin/concurrent/Timer.kt | 53 ++++++++++++------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/libraries/stdlib/src/kotlin/concurrent/Locks.kt b/libraries/stdlib/src/kotlin/concurrent/Locks.kt index 4380dbb1110..61ce0dc663c 100644 --- a/libraries/stdlib/src/kotlin/concurrent/Locks.kt +++ b/libraries/stdlib/src/kotlin/concurrent/Locks.kt @@ -10,6 +10,7 @@ import java.util.concurrent.CountDownLatch * Executes the given [action] under this lock. * @return the return value of the action. */ +@kotlin.internal.InlineOnly public inline fun Lock.withLock(action: () -> T): T { lock() try { @@ -23,6 +24,7 @@ public inline fun Lock.withLock(action: () -> T): T { * Executes the given [action] under the read lock of this lock. * @return the return value of the action. */ +@kotlin.internal.InlineOnly public inline fun ReentrantReadWriteLock.read(action: () -> T): T { val rl = readLock() rl.lock() @@ -39,6 +41,7 @@ public inline fun 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. * @return the return value of the action. */ +@kotlin.internal.InlineOnly public inline fun ReentrantReadWriteLock.write(action: () -> T): T { val rl = readLock() diff --git a/libraries/stdlib/src/kotlin/concurrent/Thread.kt b/libraries/stdlib/src/kotlin/concurrent/Thread.kt index 9badb7958c9..931c5615aa1 100644 --- a/libraries/stdlib/src/kotlin/concurrent/Thread.kt +++ b/libraries/stdlib/src/kotlin/concurrent/Thread.kt @@ -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 * is stored for the current thread and then returned. */ +@kotlin.internal.InlineOnly public inline fun ThreadLocal.getOrSet(default: () -> T): T { return get() ?: default().apply { set(this) } } diff --git a/libraries/stdlib/src/kotlin/concurrent/Timer.kt b/libraries/stdlib/src/kotlin/concurrent/Timer.kt index cbe9c1ab2e2..0120df8b426 100644 --- a/libraries/stdlib/src/kotlin/concurrent/Timer.kt +++ b/libraries/stdlib/src/kotlin/concurrent/Timer.kt @@ -9,7 +9,8 @@ import java.util.TimerTask /** * 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) schedule(task, delay) 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]. */ -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) schedule(task, time) 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 * 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) schedule(task, delay, period) 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 * 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) schedule(task, time, period) 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 * 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) scheduleAtFixedRate(task, delay, period) 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 * 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) scheduleAtFixedRate(task, time, period) 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] * (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 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 { - val timer = if (name == null) Timer(daemon) else Timer(name, daemon) +@kotlin.internal.InlineOnly +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) 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 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 { - val timer = if (name == null) Timer(daemon) else Timer(name, daemon) +@kotlin.internal.InlineOnly +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) 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 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 { - val timer = if (name == null) Timer(daemon) else Timer(name, daemon) +@kotlin.internal.InlineOnly +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) 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 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 { - val timer = if (name == null) Timer(daemon) else Timer(name, daemon) +@kotlin.internal.InlineOnly +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) return timer } @@ -123,8 +141,7 @@ public fun fixedRateTimer(name: String? = null, daemon: Boolean = false, startAt /** * Wraps the specified [action] in a [TimerTask]. */ -public fun timerTask(action: TimerTask.() -> Unit): TimerTask = object : TimerTask() { - public override fun run() { - action() - } +@kotlin.internal.InlineOnly +public inline fun timerTask(crossinline action: TimerTask.() -> Unit): TimerTask = object : TimerTask() { + override fun run() = action() } \ No newline at end of file