source: rrlib_jni/jvm.cpp @ 24:d13dfecf897b

Last change on this file since 24:d13dfecf897b was 20:c4f1bedb7be9, checked in by Tobias Föhst <foehst@…>, 7 years ago

Added and updated license information

File size: 3.4 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/jni/jvm.cpp
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2013-05-07
27 *
28 */
29//----------------------------------------------------------------------
30#include "rrlib/jni/jvm.h"
31
32//----------------------------------------------------------------------
33// External includes (system with <>, local with "")
34//----------------------------------------------------------------------
35
36//----------------------------------------------------------------------
37// Internal includes with ""
38//----------------------------------------------------------------------
39
40//----------------------------------------------------------------------
41// Debugging
42//----------------------------------------------------------------------
43#include <cassert>
44
45//----------------------------------------------------------------------
46// Namespace usage
47//----------------------------------------------------------------------
48
49//----------------------------------------------------------------------
50// Namespace declaration
51//----------------------------------------------------------------------
52namespace rrlib
53{
54namespace jni
55{
56
57//----------------------------------------------------------------------
58// Forward declarations / typedefs / enums
59//----------------------------------------------------------------------
60
61//----------------------------------------------------------------------
62// Const values
63//----------------------------------------------------------------------
64
65//----------------------------------------------------------------------
66// Implementation
67//----------------------------------------------------------------------
68
69namespace internal
70{
71
72/*! Pointer to JavaVM. Set at jvm initialization - needs to be done manually if libraries/jni is not used */
73JavaVM* jvm = NULL;
74
75/*! (Cached) Pointer to JNIEnv of current thread */
76__thread JNIEnv* env = NULL;
77
78JNIEnv* AttachThread()
79{
80  assert(jvm != NULL && "No Java VM set - cannot attach thread and get JNIEnv");
81  JNIEnv* result = NULL;
82  __attribute__((unused)) // prevents warning in release mode
83  jint res = jvm->AttachCurrentThread((void**) & result, NULL);
84  assert(res >= 0 && "Java VM Thread Attach failed");
85  env = result;
86  return result;
87}
88
89} // namespace internal
90
91void SetJavaVM(JavaVM* jvm)
92{
93  if (jvm == internal::jvm)
94  {
95    return;
96  }
97  assert(internal::jvm == NULL && "Java VM already set to a different instance");
98  internal::jvm = jvm;
99}
100//----------------------------------------------------------------------
101// End of namespace declaration
102//----------------------------------------------------------------------
103}
104}
Note: See TracBrowser for help on using the repository browser.