diff --git a/libs/highlevel-try-finally b/libs/highlevel-try-finally index ef1d72c..88c8da2 160000 --- a/libs/highlevel-try-finally +++ b/libs/highlevel-try-finally @@ -1 +1 @@ -Subproject commit ef1d72ca10c361d2eead443d99241a088e130174 +Subproject commit 88c8da21e4ec11c9755b6770565685bdc29acbca diff --git a/modules/low-level/multithreading/src/commonMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/CountDownLatch.kt b/modules/low-level/multithreading/src/commonMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/CountDownLatch.kt index 150bc9a..4120750 100644 --- a/modules/low-level/multithreading/src/commonMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/CountDownLatch.kt +++ b/modules/low-level/multithreading/src/commonMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/CountDownLatch.kt @@ -3,6 +3,7 @@ package ru.landgrafhomyak.bgtu.networks0.low_level.multithreading import ru.landgrafhomyak.bgtu.networks0.utilities.CloseableRefCounter import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose1 import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose2 +import ru.landgrafhomyak.utility.highlevel_try_finally.tryFinallyChain class CountDownLatch : AutoCloseable { private val _refcnt: CloseableRefCounter @@ -37,9 +38,9 @@ class CountDownLatch : AutoCloseable { override fun close() { this._refcnt.close("Latch is still in use") - safeAutoClose1( - action = this._condition::close, - finally = this._mutex::close - ) + tryFinallyChain { chain -> + chain.action { this._condition.close() } + chain.action { this._mutex.close() } + } } } \ No newline at end of file diff --git a/modules/low-level/multithreading/src/posixMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/Condition.kt b/modules/low-level/multithreading/src/posixMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/Condition.kt index 7dde9c9..ac0001c 100644 --- a/modules/low-level/multithreading/src/posixMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/Condition.kt +++ b/modules/low-level/multithreading/src/posixMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/Condition.kt @@ -16,6 +16,7 @@ import ru.landgrafhomyak.bgtu.networks0.low_level.c_interop_utilities.PosixUtili import ru.landgrafhomyak.bgtu.networks0.utilities.CloseableRefCounter import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose1 import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose2 +import ru.landgrafhomyak.utility.highlevel_try_finally.tryFinallyChain @OptIn(ExperimentalForeignApi::class) @@ -56,13 +57,13 @@ actual class Condition : AutoCloseable { override fun close() { this._refcnt.close("There are waiting threads on this pthreads condition") - safeAutoClose1( - action = { + tryFinallyChain { chain -> + chain.action { var err = pthread_cond_destroy(this._descriptor) if (err != 0) PosixUtilities.throwErrno(err) { d -> RuntimeException("Failed to destroy pthreads condition: $d") } - }, - finally = { nativeHeap.free(this._descriptor) } - ) + } + chain.action { nativeHeap.free(this._descriptor) } + } } } \ No newline at end of file diff --git a/modules/low-level/multithreading/src/posixMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/Mutex.kt b/modules/low-level/multithreading/src/posixMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/Mutex.kt index 90ec5df..f61b84d 100644 --- a/modules/low-level/multithreading/src/posixMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/Mutex.kt +++ b/modules/low-level/multithreading/src/posixMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/Mutex.kt @@ -15,6 +15,7 @@ import ru.landgrafhomyak.bgtu.networks0.low_level.c_interop_utilities.PosixUtili import ru.landgrafhomyak.bgtu.networks0.utilities.CloseableRefCounter import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose1 import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose2 +import ru.landgrafhomyak.utility.highlevel_try_finally.tryFinallyChain @OptIn(ExperimentalForeignApi::class, Mutex.RefcntAccess::class) actual class Mutex : AutoCloseable { @@ -53,13 +54,13 @@ actual class Mutex : AutoCloseable { override fun close() { this._refcnt.close("There are waiting threads on this pthreads mutex") - safeAutoClose1( - action = { + tryFinallyChain { chain -> + chain.action { var err = pthread_mutex_destroy(this._descriptor) if (err != 0) PosixUtilities.throwErrno(err) { d -> RuntimeException("Failed to destroy pthreads mutex: $d") } - }, - finally = { nativeHeap.free(this._descriptor) } - ) + } + chain.action { nativeHeap.free(this._descriptor) } + } } } \ No newline at end of file diff --git a/modules/low-level/multithreading/src/posixMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/Thread.kt b/modules/low-level/multithreading/src/posixMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/Thread.kt index 9afc4d0..264a86e 100644 --- a/modules/low-level/multithreading/src/posixMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/Thread.kt +++ b/modules/low-level/multithreading/src/posixMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/multithreading/Thread.kt @@ -63,6 +63,7 @@ actual class Thread : AutoCloseable { safeAutoClose2(onError = { this._state.value = State.PENDING }) { this._bootstrapArgRef.get().startSignal.decrement() } + this._state.value = State.RUNNING } actual fun join(): Throwable? { diff --git a/modules/low-level/sockets/src/linuxMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/sockets/EpollSocketEventLoop.kt b/modules/low-level/sockets/src/linuxMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/sockets/EpollSocketEventLoop.kt index 1828c65..a3a4735 100644 --- a/modules/low-level/sockets/src/linuxMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/sockets/EpollSocketEventLoop.kt +++ b/modules/low-level/sockets/src/linuxMain/kotlin/ru/landgrafhomyak/bgtu/networks0/low_level/sockets/EpollSocketEventLoop.kt @@ -32,6 +32,7 @@ import ru.landgrafhomyak.bgtu.networks0.low_level.multithreading.withLock import ru.landgrafhomyak.bgtu.networks0.utilities.CloseableRefCounter import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose1 import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose2 +import ru.landgrafhomyak.utility.highlevel_try_finally.tryFinallyChain import ru.landgrafhomyak.bgtu.networks0.low_level.multithreading.Mutex as TMutex @OptIn(ExperimentalForeignApi::class) @@ -226,20 +227,12 @@ class EpollSocketEventLoop : SocketEventLoopScope.Closeable { this@EpollSocketEventLoop._waitForRead(this.__metadata) override fun _free() { - safeAutoClose1( - action = { this@EpollSocketEventLoop._remove(this.__metadata) }, - finally = { - safeAutoClose1( - action = this@EpollSocketEventLoop._socketsCount::decref, - finally = { - safeAutoClose1( - action = this.__metadata::close, - finally = { super.close() } - ) - } - ) - } - ) + tryFinallyChain { chain -> + chain.action { this@EpollSocketEventLoop._remove(this.__metadata) } + chain.action { this@EpollSocketEventLoop._socketsCount.decref() } + chain.action { this.__metadata.close() } + chain.action { super._free() } + } } } } diff --git a/modules/utilities/src/commonMain/kotlin/ru/landgrafhomyak/bgtu/networks0/utilities/atomics.kt b/modules/utilities/src/commonMain/kotlin/ru/landgrafhomyak/bgtu/networks0/utilities/atomics.kt index 54bf856..02c425f 100644 --- a/modules/utilities/src/commonMain/kotlin/ru/landgrafhomyak/bgtu/networks0/utilities/atomics.kt +++ b/modules/utilities/src/commonMain/kotlin/ru/landgrafhomyak/bgtu/networks0/utilities/atomics.kt @@ -14,7 +14,7 @@ fun AtomicLong.compareAndExchange(expected: Long, newValue: Long): Long { fun AtomicRef.compareAndExchange(expected: T, newValue: T): T { while (true) { val old = this.value - if (old === expected) return old + if (old !== expected) return old if (this.compareAndSet(old, newValue)) return old } } diff --git a/programs/test/src/linuxX64Main/kotlin/main.kt b/programs/test/src/linuxX64Main/kotlin/main.kt index 2caa46e..3bdea24 100644 --- a/programs/test/src/linuxX64Main/kotlin/main.kt +++ b/programs/test/src/linuxX64Main/kotlin/main.kt @@ -19,17 +19,18 @@ class EventLoopRoutine(private val loop: EpollSocketEventLoop) : Thread.Routine fun main() { try { - val loop = EpollSocketEventLoop() + EpollSocketEventLoop().use { loop -> + Thread(EventLoopRoutine(loop)).use { loopThread -> + loopThread.start() + loop.pinger_IPv4("8.8.8.8").use { googleSock -> - val loopThread = Thread(EventLoopRoutine(loop)) - loopThread.start() - - val googleSock = loop.pinger_IPv4("8.8.8.8") - - runBlocking { - while (true) { - print("google: ${googleSock.ping(1000u)}ms\n") - delay(1000) + runBlocking { + for (i in 0..10) { + print("google: ${googleSock.ping(1000u)}ms\n") + delay(1000) + } + } + } } } } catch (e: Throwable) { diff --git a/settings.gradle.kts b/settings.gradle.kts index 3489e3f..a952c27 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -30,4 +30,5 @@ include(":modules:low-level:c-interop-utilities") include(":modules:low-level:multithreading") include(":modules:low-level:sockets") include(":modules:icmp") -include(":programs:test") \ No newline at end of file +include(":programs:test") +include(":programs:test2") \ No newline at end of file