Skip to content
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

Add _dispatch_install_thread_detach_callback() on all platforms #841

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 2 additions & 4 deletions private/queue_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,20 +481,18 @@ 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 cb
* Function to be called before each worker thread exits to detach JVM.
* Function to be called before each worker thread exits.
*
* Hook to be able to detach threads from the Java JVM before they exit.
* Hook to e.g. 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(void (*cb)(void));
#endif

__END_DECLS

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

#ifdef __ANDROID__
static void (*_dispatch_thread_detach_callback)(void);

void
Expand All @@ -7336,7 +7335,6 @@ _dispatch_install_thread_detach_callback(void (*cb)(void))
DISPATCH_CLIENT_CRASH(0, "Installing a thread detach callback twice");
}
}
#endif

#if defined(_WIN32)
static bool
Expand Down Expand Up @@ -7390,6 +7388,10 @@ _libdispatch_tsd_cleanup(void *ctx)
}
#endif // defined(_WIN32)

if (_dispatch_thread_detach_callback) {
_dispatch_thread_detach_callback();
}

struct dispatch_tsd *tsd = (struct dispatch_tsd*) ctx;

_tsd_call_cleanup(dispatch_priority_key, NULL);
Expand All @@ -7411,11 +7413,7 @@ _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
2 changes: 0 additions & 2 deletions src/swift/Queue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,9 @@ extension DispatchQueue {
dispatch_queue_set_specific(self.__wrapped, k, p, _destructDispatchSpecificValue)
}

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

private func _destructDispatchSpecificValue(ptr: UnsafeMutableRawPointer?) {
Expand Down
2 changes: 0 additions & 2 deletions src/swift/shims/DispatchOverlayShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,7 @@ static inline void _swift_dispatch_source_set_registration_handler(
dispatch_source_set_registration_handler(source, block);
}

#if defined(__ANDROID__)
extern void _dispatch_install_thread_detach_callback(void (*cb)(void));
#endif

static inline void _swift_dispatch_retain(dispatch_object_t object) {
dispatch_retain(object);
Expand Down