Skip to content

Thread detach hook for Java JNI on Android #259

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 1 commit into from
Jun 21, 2017

Conversation

johnno1962
Copy link
Contributor

This is a resubmission of a callback hook required when mixing multi threaded Swift and Java on Android. When Java JNI has been used on a thread the thread needs to be detached from JNI before the thread exits to free local variables or the application will crash e.g. https://github.com/SwiftJava/java_swift/blob/master/Sources/JavaJNI.swift#L25.
Introducing this hook gives any future Java integration code a chance to do this.

@MadCoder
Copy link
Contributor

@swift-ci please test

@MadCoder MadCoder merged commit c87c6bb into swiftlang:master Jun 21, 2017
@johnno1962
Copy link
Contributor Author

johnno1962 commented Jun 21, 2017

Thanks @MadCoder, good to have this tied down. While you’re here, there are in fact three problems building libdispatch on Android which you can probably provide an authoritative answer on.

The first is membarrier is not available:

diff --git a/src/shims/lock.h b/src/shims/lock.h
index 99c5563..34e1559 100644
--- a/src/shims/lock.h
+++ b/src/shims/lock.h
@@ -57,11 +57,11 @@ _dispatch_lock_owner(dispatch_lock lock_value)
 }
 
 #elif defined(__linux__)
 
 #include <linux/futex.h>
-#if !defined(__x86_64__) && !defined(__i386__) && !defined(__s390x__)
+#if !defined(__x86_64__) && !defined(__i386__) && !defined(__s390x__) && !defined(__ANDROID__)
 #include <linux/membarrier.h>
 #endif
 #include <unistd.h>
 #include <sys/syscall.h>   /* For SYS_xxx definitions */
 
@@ -471,11 +471,11 @@ _dispatch_once_gate_tryenter(dispatch_once_gate_t l)
 
 DISPATCH_ALWAYS_INLINE
 static inline dispatch_once_t
 _dispatch_once_xchg_done(dispatch_once_t *pred)
 {
-#if defined(__i386__) || defined(__x86_64__) || defined(__s390x__)
+#if defined(__i386__) || defined(__x86_64__) || defined(__s390x__) || defined(__ANDROID__)
        // On Intel, any load is a load-acquire, so we don't need to be fancy
        // same for s390x
        return os_atomic_xchg(pred, DLOCK_ONCE_DONE, release);
 #elif defined(__linux__)
        if (unlikely(syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0) < 0)) {

And the seems to be some problem with the MACRO “TRASHIT”

diff --git a/src/internal.h b/src/internal.h
index 0536db1..ecbfc8f 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -377,13 +377,15 @@ DISPATCH_EXPORT DISPATCH_NOTHROW void dispatch_atfork_child(void);
 // sys/queue.h debugging
 #if defined(__linux__)
 #define QUEUE_MACRO_DEBUG 1
 #else
 #undef TRASHIT
-#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
 #endif
 #endif // DISPATCH_DEBUG
+#ifndef TRASHIT
+#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
+#endif
 #define _TAILQ_TRASH_ENTRY(elm, field) do { \
                        TRASHIT((elm)->field.tqe_next); \
                        TRASHIT((elm)->field.tqe_prev); \
                } while (0)
 #define _TAILQ_TRASH_HEAD(head) do { \

And finally libdispatch needs to be linked to libswiftCore.so while it is being built.

Do you want me to file a new PR for the solutions to the first two problems?

@MadCoder
Copy link
Contributor

This review is not the place to discuss it. however, the dispatch once thing is wrong on android, arm gives no such promise.

@johnno1962
Copy link
Contributor Author

Any suggestions as to what the proper solution might be?

@MadCoder
Copy link
Contributor

see #228 . If you aren't fluent with arm memory model, you should leave that patch to someone who does.

@johnno1962
Copy link
Contributor Author

Fair enough. WIll do. Thanks for your help

das pushed a commit that referenced this pull request Jul 31, 2017
Thread detach hook for Java JNI on Android

Signed-off-by: Daniel A. Steffen <dsteffen@apple.com>
@das das removed the darwin pending label Aug 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants