Skip to content

Thread detach hook for Java JNI on Android #250

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

Closed
wants to merge 11 commits into from
14 changes: 14 additions & 0 deletions private/queue_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,20 @@ void
dispatch_async_enforce_qos_class_f(dispatch_queue_t queue,
void *_Nullable context, dispatch_function_t work);

#ifdef __ANDROID__
/*!
* @function _dispatch_install_thread_detach_callback
*
* @param callback
* Function to be called before each worker thread exits to detach JVM.
*
* Hook to be able to detach threads from the Java JVM before they exit.
* If JNI has been used on a thread on Android it needs to have been
* "detached" before the thread exits or the application will crash.
*/
DISPATCH_EXPORT
void _dispatch_install_thread_detach_callback(dispatch_function_t cb);
#endif

__END_DECLS

Expand Down
17 changes: 17 additions & 0 deletions src/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,18 @@ gettid(void)
if ((f) && tsd->k) ((void(*)(void*))(f))(tsd->k); \
} while (0)

#ifdef __ANDROID__
static void (*_dispatch_thread_detach_callback)(void);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's static then the overlay which is a dynamic library can't pick it up.

@das suggested to me the right place is to export the symbol is from private/queue_private.h or private/private.h and also to use silgen like you did.

Copy link
Contributor Author

@johnno1962 johnno1962 May 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global symbol is now a function _dispatch_set_detach_callback_ptr as it doesn’t seem possible to express a global var in a silgen. I’ve moved it's declaration into queue_private.h.


void
_dispatch_install_thread_detach_callback(dispatch_function_t cb)
{
if (os_atomic_xchg(&_dispatch_thread_detach_callback, cb, relaxed)) {
DISPATCH_CLIENT_CRASH(0, "Installing a thread detach callback twice");
}
}
#endif

void
_libdispatch_tsd_cleanup(void *ctx)
{
Expand All @@ -885,6 +897,11 @@ _libdispatch_tsd_cleanup(void *ctx)
_tsd_call_cleanup(dispatch_voucher_key, _voucher_thread_cleanup);
_tsd_call_cleanup(dispatch_deferred_items_key,
_dispatch_deferred_items_cleanup);
#ifdef __ANDROID__
if (_dispatch_thread_detach_callback) {
_dispatch_thread_detach_callback();
}
#endif
tsd->tid = 0;
}

Expand Down
9 changes: 9 additions & 0 deletions src/swift/Queue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ public extension DispatchQueue {
let p = Unmanaged.passRetained(v).toOpaque()
dispatch_queue_set_specific(self.__wrapped, k, p, _destructDispatchSpecificValue)
}

#if os(Android)
@_silgen_name("_dispatch_install_thread_detach_callback")
private static func _dispatch_install_thread_detach_callback(_ cb: @escaping @convention(c) () -> Void)

public static func setThreadDetachCallback(_ cb: @escaping @convention(c) () -> Void) {
_dispatch_install_thread_detach_callback(cb)
}
#endif
}

private func _destructDispatchSpecificValue(ptr: UnsafeMutableRawPointer?) {
Expand Down