Don't attach to actual Java threads (or already attached ones)
[strongswan.git] / src / frontends / android / jni / libandroidbridge / android_jni.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "android_jni.h"
19
20 #include <library.h>
21 #include <threading/thread_value.h>
22
23 /**
24 * JVM
25 */
26 static JavaVM *android_jvm;
27
28 jclass *android_charonvpnservice_class;
29
30 /**
31 * Thread-local variable. Only used because of the destructor
32 */
33 static thread_value_t *androidjni_threadlocal;
34
35 /**
36 * Thread-local destructor to ensure that a native thread is detached
37 * from the JVM even if androidjni_detach_thread() is not called.
38 */
39 static void attached_thread_cleanup(void *arg)
40 {
41 (*android_jvm)->DetachCurrentThread(android_jvm);
42 }
43
44 /*
45 * Described in header
46 */
47 void androidjni_attach_thread(JNIEnv **env)
48 {
49 if ((*android_jvm)->GetEnv(android_jvm, (void**)env,
50 JNI_VERSION_1_6) == JNI_OK)
51 { /* already attached or even a Java thread */
52 return;
53 }
54 (*android_jvm)->AttachCurrentThread(android_jvm, env, NULL);
55 /* use a thread-local value with a destructor that automatically detaches
56 * the thread from the JVM before it terminates, if not done manually */
57 androidjni_threadlocal->set(androidjni_threadlocal, (void*)*env);
58 }
59
60 /*
61 * Described in header
62 */
63 void androidjni_detach_thread()
64 {
65 if (androidjni_threadlocal->get(androidjni_threadlocal))
66 { /* only do this if we actually attached this thread */
67 androidjni_threadlocal->set(androidjni_threadlocal, NULL);
68 (*android_jvm)->DetachCurrentThread(android_jvm);
69 }
70 }
71
72 /**
73 * Called when this library is loaded by the JVM
74 */
75 jint JNI_OnLoad(JavaVM *vm, void *reserved)
76 {
77 JNIEnv *env;
78
79 android_jvm = vm;
80
81 if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
82 {
83 return -1;
84 }
85
86 androidjni_threadlocal = thread_value_create(attached_thread_cleanup);
87
88 android_charonvpnservice_class =
89 (*env)->NewGlobalRef(env, (*env)->FindClass(env,
90 JNI_PACKAGE_STRING "/CharonVpnService"));
91
92 return JNI_VERSION_1_6;
93 }
94
95 /**
96 * Called when this library is unloaded by the JVM (which never happens on
97 * Android)
98 */
99 void JNI_OnUnload(JavaVM *vm, void *reserved)
100 {
101 androidjni_threadlocal->destroy(androidjni_threadlocal);
102 }
103