-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathJavaThreadScope.h
More file actions
34 lines (26 loc) · 790 Bytes
/
JavaThreadScope.h
File metadata and controls
34 lines (26 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class JavaThreadScope {
public:
JavaThreadScope(JVMLauncher* launcher) : attached(false), launcher(launcher){
jint res = launcher->m_RunningJVMInstance->GetEnv((void**)&(launcher->m_JVMEnv), NULL);
res = launcher->m_RunningJVMInstance->AttachCurrentThread((void**)&(launcher->m_JVMEnv), NULL);
if (res != JNI_OK) {
attached = false;
}
else {
attached = true;
}
}
JavaThreadScope(const JavaThreadScope&) = delete;
JavaThreadScope& operator=(const JavaThreadScope&) = delete;
virtual ~JavaThreadScope() {
if (attached) {
launcher->m_RunningJVMInstance->DetachCurrentThread();
attached = false;
}
}
JNIEnv *GetEnv() const { return launcher->m_JVMEnv; }
bool isAttached() const { return attached; }
private:
bool attached;
JVMLauncher* launcher;
};