* src/threads/thread.cpp (threads_thread_is_alive): Recognize parked states.
[cacao.git] / src / native / native.hpp
1 /* src/native/native.hpp - native library support
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5    Copyright (C) 2008 Theobroma Systems Ltd.
6
7    This file is part of CACAO.
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22    02110-1301, USA.
23
24 */
25
26
27 #ifndef _NATIVE_HPP
28 #define _NATIVE_HPP
29
30 #include <stdint.h>
31
32 #ifdef __cplusplus
33 #include <map>
34 #include <set>
35 #endif
36
37 #include "native/jni.hpp"
38
39 #include "vm/class.hpp"
40 #include "vm/global.h"
41 #include "vm/loader.hpp"
42 #include "vm/method.h"
43 #include "vm/os.hpp"
44 #include "vm/utf8.h"
45
46
47 /* defines ********************************************************************/
48
49 #define NATIVE_METHODS_COUNT    sizeof(methods) / sizeof(JNINativeMethod)
50
51
52 #define NATIVE_LIBRARY_PREFIX     "lib"
53
54 #if defined(__DARWIN__)
55 # define NATIVE_LIBRARY_SUFFIX    ".dylib"
56 #else
57 # define NATIVE_LIBRARY_SUFFIX    ".so"
58 #endif
59
60
61 #ifdef __cplusplus
62
63 #if defined(ENABLE_DL)
64 /**
65  * Represents a native library.
66  */
67 class NativeLibrary {
68 private:
69         utf*           _filename;    ///< Name of the native library.
70         classloader_t* _classloader; ///< Defining classloader.
71         void*          _handle;      ///< Filesystem handle.
72
73 public:
74         NativeLibrary(utf* filename, classloader_t* classloader = 0, void* handle = 0) : _filename(filename), _classloader(classloader), _handle(handle) {}
75         NativeLibrary(void* handle) : _filename(0), _classloader(0), _handle(handle) {}
76
77         inline classloader_t* get_classloader() const { return _classloader; }
78         inline utf*           get_filename   () const { return _filename; }
79         inline void*          get_handle     () const { return _handle; }
80
81         void* open();
82         void  close();
83         bool  load(JNIEnv* env);
84         bool  is_loaded();
85         void* resolve_symbol(utf* symbolname) const;
86 };
87
88
89 /**
90  * Table containing all loaded native libraries.
91  */
92 class NativeLibraries {
93 private:
94         Mutex _mutex; ///< Mutex to make the container thread-safe.
95         typedef std::multimap<classloader_t*, NativeLibrary> MAP;
96         MAP _libraries;
97
98 private:
99         // Comparator class.
100         class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, utf*, bool> {
101         public:
102                 bool operator() (std::pair<classloader_t*, NativeLibrary> args, const utf* filename) const
103                 {
104                         return (args.second.get_filename() == filename);
105                 }
106         };
107
108 public:
109         void  add(NativeLibrary& library);
110         bool  is_loaded(NativeLibrary& library);
111         void* resolve_symbol(utf* symbolname, classloader_t* classloader);
112 };
113 #endif
114
115
116 /**
117  * Represents a native method.
118  */
119 class NativeMethod {
120 private:
121         utf*  _classname;  ///< Class name.
122         utf*  _name;       ///< Method name.
123         utf*  _descriptor; ///< Method signature.
124         void* _function;   ///< Pointer to the native function.
125
126         friend bool operator< (const NativeMethod& first, const NativeMethod& second);
127
128 public:
129         NativeMethod(utf* classname, utf* name, utf* signature, void* function) : _classname(classname), _name(name), _descriptor(signature), _function(function) {}
130         NativeMethod(methodinfo* m) : _classname(m->clazz->name), _name(m->name), _descriptor(m->descriptor), _function(0) {}
131
132         inline void* get_function() const { return _function; }
133 };
134
135
136 /**
137  * Table containing all native methods registered with the VM.
138  */
139 class NativeMethods {
140 private:
141         Mutex _mutex;
142         std::set<NativeMethod> _methods;
143
144 private:
145         // Comparator class.
146         class comparator : public std::binary_function<std::pair<classloader_t*, NativeLibrary>, utf*, bool> {
147         public:
148                 bool operator() (std::pair<classloader_t*, NativeLibrary> args, const utf* filename) const
149                 {
150                         return (args.second.get_filename() == filename);
151                 }
152         };
153         
154 public:
155         void  register_methods(utf* classname, const JNINativeMethod* methods, size_t count);
156         void* resolve_method(methodinfo* m);
157         void* find_registered_method(methodinfo* m);
158 };
159
160 #endif
161
162 /* function prototypes ********************************************************/
163
164 #ifdef __cplusplus
165 extern "C" {
166 #endif
167
168 java_handle_t *native_new_and_init(classinfo *c);
169 java_handle_t *native_new_and_init_string(classinfo *c, java_handle_t *s);
170
171 #ifdef __cplusplus
172 } // extern "C"
173 #endif
174
175 #endif // _NATIVE_HPP
176
177
178 /*
179  * These are local overrides for various environment variables in Emacs.
180  * Please do not remove this and leave it at the end of the file, where
181  * Emacs will automagically detect them.
182  * ---------------------------------------------------------------------
183  * Local variables:
184  * mode: c++
185  * indent-tabs-mode: t
186  * c-basic-offset: 4
187  * tab-width: 4
188  * End:
189  * vim:noexpandtab:sw=4:ts=4:
190  */