Skip to content

Handle different ThreadDestructor signatures between Android NDK 27 and NDK 28 #3249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Sources/NIOPosix/ThreadPosix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,30 @@ private func sysPthread_create(
)
#else
var handleLinux = pthread_t()
#if os(Android)
// NDK 27 signature:
// int pthread_create(pthread_t* _Nonnull __pthread_ptr, pthread_attr_t const* _Nullable __attr, void* _Nonnull (* _Nonnull __start_routine)(void* _Nonnull), void* _Nullable);
func coerceThreadDestructor(
_ destructor: @escaping ThreadDestructor
) -> (@convention(c) (UnsafeMutableRawPointer) -> UnsafeMutableRawPointer) {
destructor
}

// NDK 28 signature:
// int pthread_create(pthread_t* _Nonnull __pthread_ptr, pthread_attr_t const* _Nullable __attr, void* _Nullable (* _Nonnull __start_routine)(void* _Nullable), void* _Nullable);
func coerceThreadDestructor(
_ destructor: @escaping ThreadDestructor
) -> (@convention(c) (UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer?) {
unsafeBitCast(destructor, to: (@convention(c) (UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer?).self)
}
#else
// Linux doesn't change the signature
func coerceThreadDestructor(_ destructor: ThreadDestructor) -> ThreadDestructor { destructor }
#endif
let result = pthread_create(
&handleLinux,
nil,
destructor,
coerceThreadDestructor(destructor),
args
)
#endif
Expand Down
Loading