loke
05/25/2025, 5:40 PMplatform.posix
. However, I'm trying to use pthread_cleanup_push
and it doesn't seem to exist in this package. In fact, I can't see it anywhere. Is this a bug? Should I report it?Adam S
05/26/2025, 5:51 PMpthread_cleanup_push
is defined as a macro, not a function, cinterop can't generate a Kotlin Native binding for itAdam S
05/26/2025, 5:53 PMAdam S
05/26/2025, 5:53 PMloke
05/27/2025, 12:55 PMephemient
05/30/2025, 10:25 PMPOSIX.1 permitsyou cannot (portably) wrap them individuallyandpthread_cleanup_push()
to be implemented as macros that expand to text containing '`{pthread_cleanup_pop()
}`', respectively. For this reason, the caller must ensure that calls to these functions are paired within the same function, and at the same lexical nesting level.' and '
ephemient
05/31/2025, 3:57 AMvoid pthread_push_pop(void (*routine)(void *), void *arg1, void *arg2, bool execute) {
pthread_cleanup_push(routine, arg1);
routine(arg2);
pthread_cleanup_pop(execute);
}
and then use it from Kotlin like
fun invokePinnedFunPtr(arg: NativePtr) {
interpretPointed<Pinned<() -> Unit>>(arg)()
}
fun runFinally(cleanup: () -> Unit, execute: Boolean, block: () -> Unit) {
block.usePinned { block ->
cleanup.usePinned { cleanup ->
pturead_push_pop(staticCFunction(::invokePinnedFunPtr), cleanup.addressOf(0), block.addressOf(0), execute)
}
}
}
ephemient
05/31/2025, 3:58 AMfun runFinally(block: () -> Unit, cleanup: () -> Unit, execute: Boolean) {
var doCleanup = true
try {
block()
doCleanup = execute
} finally {
if (doCleanup) cleanup()
}
}
since the pthread version will just break if the Kotlin functions throw exceptionsephemient
05/31/2025, 3:58 AMloke
05/31/2025, 12:55 PMloke
05/31/2025, 12:56 PMloke
05/31/2025, 1:00 PMpthread_getspecific
, but for some reason the when a Kotlin function is called from the key destructor, I get a crash. Very strange. So I implemented it myself using a hashmap attached to the thread, but I need to clear that out when the thread exits, which is why I looked at pthread_cleanup_push
.ephemient
05/31/2025, 5:54 PM@ThreadLocal
doesn't work for you?ephemient
05/31/2025, 5:55 PMloke
06/02/2025, 12:56 AM@ThreadLocal
a long time ago, but there was some issue with it. Perhaps I misunderstood it, because I tried again and it does seem to work.loke
06/05/2025, 3:49 PM