Remove locking in mailbox executor service

This commit is contained in:
DBotThePony 2024-02-04 20:20:03 +07:00
parent a1f7eaeee8
commit abf91f445e
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 52 additions and 53 deletions

View File

@ -4,7 +4,7 @@ kotlin.code.style=official
specifyKotlinAsDependency=false
projectGroup=ru.dbotthepony.kommons
projectVersion=1.7.7
projectVersion=1.7.8
guavaDepVersion=33.0.0
gsonDepVersion=2.8.9

View File

@ -51,7 +51,6 @@ class MailboxExecutorService(thread: Thread = Thread.currentThread()) : Schedule
private val timers = LinkedList<Timer<*>>()
private val repeatableTimers = LinkedList<RepeatableTimer>()
private val executionLock = ReentrantLock()
@Volatile
private var isShutdown = false
@ -138,60 +137,61 @@ class MailboxExecutorService(thread: Thread = Thread.currentThread()) : Schedule
if (!isTerminated) {
isTerminated = true
executionLock.withLock {
timers.clear()
repeatableTimers.clear()
futureQueue.forEach {
it.cancel(false)
}
futureQueue.clear()
timers.clear()
repeatableTimers.clear()
return
}
}
executionLock.withLock {
var next = futureQueue.poll()
var next = futureQueue.poll()
while (next != null) {
if (isTerminated) return
next.run()
while (next != null) {
if (isTerminated) return
next.run()
Thread.interrupted()
next = futureQueue.poll()
}
while (!timers.isEmpty()) {
if (isTerminated) return
val first = timers.first
if (first.isCancelled) {
timers.removeFirst()
} else if (first.executeAt <= timeOrigin.nanos) {
first.run()
Thread.interrupted()
next = futureQueue.poll()
timers.removeFirst()
} else {
break
}
}
while (!timers.isEmpty()) {
if (repeatableTimers.isNotEmpty()) {
val executed = LinkedList<RepeatableTimer>()
while (repeatableTimers.isNotEmpty()) {
if (isTerminated) return
val first = timers.first
val first = repeatableTimers.first
if (first.isCancelled) {
timers.removeFirst()
} else if (first.executeAt <= timeOrigin.nanos) {
first.run()
Thread.interrupted()
timers.removeFirst()
if (first.isDone) {
repeatableTimers.removeFirst()
} else if (first.next <= timeOrigin.nanos) {
first.runAndReset()
executed.add(first)
repeatableTimers.removeFirst()
} else {
break
}
}
if (repeatableTimers.isNotEmpty()) {
val executed = LinkedList<RepeatableTimer>()
while (repeatableTimers.isNotEmpty()) {
if (isTerminated) return
val first = repeatableTimers.first
if (first.isDone) {
repeatableTimers.removeFirst()
} else if (first.next <= timeOrigin.nanos) {
first.runAndReset()
executed.add(first)
repeatableTimers.removeFirst()
} else {
break
}
}
executed.forEach { repeatableTimers.enqueue(it) }
}
executed.forEach { repeatableTimers.enqueue(it) }
}
}
@ -210,27 +210,26 @@ class MailboxExecutorService(thread: Thread = Thread.currentThread()) : Schedule
isShutdown = true
}
override fun shutdownNow(): MutableList<Runnable> {
override fun shutdownNow(): List<Runnable> {
if (isTerminated) return listOf()
isShutdown = true
isTerminated = true
val result = ArrayList<Runnable>()
executionLock.withLock {
futureQueue.forEach {
it.cancel(false)
result.add(it)
}
futureQueue.clear()
timers.forEach { it.cancel(false) }
repeatableTimers.forEach { it.cancel(false) }
timers.clear()
repeatableTimers.clear()
futureQueue.forEach {
it.cancel(false)
result.add(it)
}
futureQueue.clear()
timers.forEach { it.cancel(false) }
repeatableTimers.forEach { it.cancel(false) }
timers.clear()
repeatableTimers.clear()
return result
}