Version
menu_open
link
Wwise SDK 2019.1.11
AkJNIThread.h
1 /*******************************************************************************
2 The content of this file includes portions of the AUDIOKINETIC Wwise Technology
3 released in source code form as part of the SDK installer package.
4 
5 Commercial License Usage
6 
7 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
8 may use this file in accordance with the end user license agreement provided
9 with the software or, alternatively, in accordance with the terms contained in a
10 written agreement between you and Audiokinetic Inc.
11 
12 Apache License Usage
13 
14 Alternatively, this file may be used under the Apache License, Version 2.0 (the
15 "Apache License"); you may not use this file except in compliance with the
16 Apache License. You may obtain a copy of the Apache License at
17 http://www.apache.org/licenses/LICENSE-2.0.
18 
19 Unless required by applicable law or agreed to in writing, software distributed
20 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
21 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
22 the specific language governing permissions and limitations under the License.
23 
24  Version: <VERSION> Build: <BUILDNUMBER>
25  Copyright (c) <COPYRIGHTYEAR> Audiokinetic Inc.
26 *******************************************************************************/
27 
28 #pragma once
29 
30 #include <jni.h>
31 
32 namespace AKPLATFORM
33 {
34  /// Utility class for acquiring a pointer to a valid JNI environment attached to the current thread (with proper cleanup in destructor)
35  class CAkJNIThread
36  {
37  public:
38 
39  CAkJNIThread(JavaVM* jvm) : m_jvm(jvm), m_jEnv(NULL), m_mustDetachExplicitely(false), m_mustPopLocalFrame(false)
40  {
41  AcquireEnv(jvm, 0);
42  }
43 
44  CAkJNIThread(JavaVM* jvm, int numLocalRefs) : m_jvm(jvm), m_jEnv(NULL), m_mustDetachExplicitely(false), m_mustPopLocalFrame(false)
45  {
46  AcquireEnv(jvm, numLocalRefs);
47  }
48 
50  {
51  if (m_mustPopLocalFrame)
52  {
53  m_jEnv->PopLocalFrame(NULL);
54  m_mustPopLocalFrame = false;
55  }
56  if (m_jvm != NULL && m_jEnv != NULL && m_mustDetachExplicitely)
57  {
58  m_jvm->DetachCurrentThread();
59  m_mustDetachExplicitely = false;
60  }
61  }
62 
63  JNIEnv *getEnv()
64  {
65  return m_jEnv;
66  }
67 
68  bool isAttached()
69  {
70  return m_jEnv != NULL;
71  }
72 
73  private:
74  void AcquireEnv(JavaVM* jvm, int numLocalRefs)
75  {
76  if (!jvm)
77  return;
78 
79  jvm->GetEnv((void**)&m_jEnv, JNI_VERSION_1_6);
80  if (m_jEnv == NULL)
81  {
82  JavaVMAttachArgs lJavaVMAttachArgs;
83  lJavaVMAttachArgs.version = JNI_VERSION_1_6;
84  lJavaVMAttachArgs.name = "NativeThread";
85  lJavaVMAttachArgs.group = NULL;
86 
87  jvm->AttachCurrentThread(&m_jEnv, &lJavaVMAttachArgs);
88  m_mustDetachExplicitely = true;
89  }
90  if (m_jEnv && numLocalRefs > 0)
91  {
92  m_mustPopLocalFrame = 0 == m_jEnv->PushLocalFrame(numLocalRefs);
93  }
94  }
95 
96  JavaVM * m_jvm;
97  JNIEnv * m_jEnv;
98  bool m_mustDetachExplicitely: 1;
99  bool m_mustPopLocalFrame: 1;
100  };
101 }
CAkJNIThread(JavaVM *jvm, int numLocalRefs)
Definition: AkJNIThread.h:44
Utility class for acquiring a pointer to a valid JNI environment attached to the current thread (with...
Definition: AkJNIThread.h:36
CAkJNIThread(JavaVM *jvm)
Definition: AkJNIThread.h:39
JNIEnv * getEnv()
Definition: AkJNIThread.h:63

Was this page helpful?

Need Support?

Questions? Problems? Need more info? Contact us, and we can help!

Visit our Support page

Tell us about your project. We're here to help.

Register your project and we'll help you get started with no strings attached!

Get started with Wwise