From 98056a520a5717b17a4e0ca9e2883ea3f06a6235 Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 14 Jul 2020 17:05:59 +0200 Subject: [PATCH] Added Thread to retrieve information about the current thread --- .../net/dankito/utils/multiplatform/Thread.kt | 15 +++++++++ .../net/dankito/utils/multiplatform/Thread.kt | 33 +++++++++++++++++++ .../net/dankito/utils/multiplatform/Thread.kt | 20 +++++++++++ 3 files changed, 68 insertions(+) create mode 100644 common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Thread.kt create mode 100644 common/src/iosMain/kotlin/net/dankito/utils/multiplatform/Thread.kt create mode 100644 common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Thread.kt diff --git a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Thread.kt b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Thread.kt new file mode 100644 index 00000000..fb41ae07 --- /dev/null +++ b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Thread.kt @@ -0,0 +1,15 @@ +package net.dankito.utils.multiplatform + + +expect class Thread() { + + companion object { + + val current: Thread + + } + + + val threadName: String + +} \ No newline at end of file diff --git a/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/Thread.kt b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/Thread.kt new file mode 100644 index 00000000..211c3a01 --- /dev/null +++ b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/Thread.kt @@ -0,0 +1,33 @@ +package net.dankito.utils.multiplatform + +import platform.Foundation.NSOperationQueue +import platform.Foundation.NSThread + + +actual class Thread(private val thread: NSThread) { + + actual companion object { + + actual val current: Thread + get() = Thread(NSThread.currentThread) + + } + + + actual constructor() : this(NSThread()) + + + actual val threadName: String + get() { + thread.name?.let { name -> + if (name.isNotBlank()) { + return name + } + } + + return thread.description + ?: NSOperationQueue.currentQueue?.underlyingQueue?.description + ?: "Could not retrieve thread's name" + } + +} \ No newline at end of file diff --git a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Thread.kt b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Thread.kt new file mode 100644 index 00000000..c4e9dddc --- /dev/null +++ b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Thread.kt @@ -0,0 +1,20 @@ +package net.dankito.utils.multiplatform + + +actual class Thread(private val thread: java.lang.Thread) : java.lang.Thread() { + + actual companion object { + + actual val current: Thread + get() = Thread(currentThread()) + + } + + + actual constructor() : this(java.lang.Thread()) + + + actual val threadName: String + get() = thread.name + +} \ No newline at end of file