ug4
threading.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2011: Steinbeis Forschungszentrum (STZ Ölbronn)
3  * Author: Michael Hoffer
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #include <jni.h>
34 #include <stddef.h>
35 #include "messaging.h"
36 #include "bindings_vrl.h"
37 
38 #ifndef THREADING_H
39 #define THREADING_H
40 namespace ug {
41  namespace vrl {
42  namespace threading {
43 
60  };
61 
66  public:
67 
73  this->type = type;
74 
75  switch (type) {
76  case ATTACH_FAILED:
77  {
78  UG_LOG("UG-VRL: Attaching thread failed in "
81  << EMPHASIZE_END
82  << " in line: " << __LINE__ << " !");
83  }
84  break;
85  case DETACH_FAILED:
86  {
87  UG_LOG("UG-VRL: Detaching thread failed in "
90  << EMPHASIZE_END
91  << " in line: " << __LINE__ << " !");
92  }
93  break;
94  case NOT_ATTACHED:
95  {
96  UG_LOG("UG-VRL: Thread not attached in "
99  << EMPHASIZE_END
100  << " in line: " << __LINE__ << " !");
101  }
102  break;
103  }
104  }
105 
107  };
108 
116  inline JNIEnv* attachThread(JavaVM* javaVM) {
117 
118  // The following code raised a warning in newer GCC versions:
119  // "dereferencing type-punned pointer will break strict-aliasing rules"
120  // That is why we do it differently now, although this code
121  // is officially used:
122  // JNIEnv* localEnv = NULL;
123  //
124  // int result = javaVM->AttachCurrentThread(
125  // (void **) (&localEnv), NULL);
126 
127  JNIEnv** localEnvPtrPtr;
128  JNIEnv* localEnv = NULL;
129  localEnvPtrPtr = &localEnv;
130 
131  int result = javaVM->AttachCurrentThread(
132  (void **) (localEnvPtrPtr), NULL);
133 
134  if (result < 0) {
136  }
137 
138  return localEnv;
139  }
140 
146  inline void detachThread(JavaVM* javaVM) {
147 
148  int result = javaVM->DetachCurrentThread();
149 
150  if (result < 0) {
152  }
153  }
154 
161  inline JNIEnv* getEnv(JavaVM* javaVM) {
162 
163  // The following code raised a warning in newer GCC versions:
164  // "dereferencing type-punned pointer will break strict-aliasing rules"
165  // That is why we do it differently now, although this code
166  // is officially used:
167  // JNIEnv* localEnv = NULL;
168  //
169  // jint result = javaVM->GetEnv(
170  // (void **) (&localEnv), JNI_VERSION_1_2);
171 
172  JNIEnv** localEnvPtrPtr;
173  JNIEnv* localEnv = NULL;
174  localEnvPtrPtr = &localEnv;
175 
176  jint result = javaVM->GetEnv(
177  (void **) (localEnvPtrPtr), JNI_VERSION_1_2);
178 
179  if (result != JNI_OK) {
181  }
182 
183  return localEnv;
184  }
185  } // threading::
186  } // vrl::
187 } // ug::
188 
189 #endif /* THREADING_H */
190 
#define PRETTY_FUNCTION
Definition: bindings_vrl.h:45
Definition: threading.h:65
ExceptionType type
Definition: threading.h:106
JNIThreadException(ExceptionType type)
Definition: threading.h:72
#define UG_LOG(msg)
Definition: log.h:367
#define EMPHASIZE_BEGIN
Definition: messaging.h:43
#define EMPHASIZE_END
Definition: messaging.h:44
JNIEnv * getEnv(JavaVM *javaVM)
Definition: threading.h:161
void detachThread(JavaVM *javaVM)
Definition: threading.h:146
JNIEnv * attachThread(JavaVM *javaVM)
Definition: threading.h:116
ExceptionType
Definition: threading.h:47
@ ATTACH_FAILED
Definition: threading.h:51
@ NOT_ATTACHED
Definition: threading.h:59
@ DETACH_FAILED
Definition: threading.h:55
static JavaVM * javaVM
Definition: bindings_vrl.cpp:64
the ug namespace