new native threads
[cacao.git] / src / native / vm / VMObject.c
1 /* nat/VMObject.c - java/lang/Object
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Roman Obermaiser
28
29    Changes: Joseph Wenninger
30
31    $Id: VMObject.c 1377 2004-08-01 22:01:00Z stefan $
32
33 */
34
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include "exceptions.h"
39 #include "jni.h"
40 #include "builtin.h"
41 #include "loader.h"
42 #include "native.h"
43 #include "options.h"
44 #include "mm/boehm.h"
45 #include "threads/locks.h"
46 #include "toolbox/logging.h"
47 #include "toolbox/memory.h"
48 #include "java_lang_Cloneable.h"
49 #include "java_lang_Object.h"
50
51
52 /*
53  * Class:     java/lang/Object
54  * Method:    clone
55  * Signature: ()Ljava/lang/Object;
56  */
57 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_VMObject_clone(JNIEnv *env, jclass clazz, java_lang_Cloneable *this)
58 {
59         classinfo *c;
60         java_lang_Object *new;
61         arraydescriptor *desc;
62     
63         if ((desc = this->header.vftbl->arraydesc) != NULL) {
64                 /* We are cloning an array */
65         
66                 u4 size = desc->dataoffset + desc->componentsize * ((java_arrayheader *) this)->size;
67         
68                 new = (java_lang_Object *) heap_allocate(size, (desc->arraytype == ARRAYTYPE_OBJECT), NULL);
69                 memcpy(new, this, size);
70 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
71                 initObjectLock(new);
72 #endif
73         
74                 return new;
75         }
76     
77     /* We are cloning a non-array */
78     if (!builtin_instanceof((java_objectheader *) this, class_java_lang_Cloneable)) {
79         *exceptionptr =
80                         new_exception(string_java_lang_CloneNotSupportedException);
81         return NULL;
82     }
83
84     /* XXX should use vftbl */
85     c = this->header.vftbl->class;
86     new = (java_lang_Object *) builtin_new(c);
87
88     if (!new)
89         return NULL;
90
91     memcpy(new, this, c->instancesize);
92 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
93         initObjectLock(new);
94 #endif
95
96     return new;
97 }
98
99
100 /*
101  * Class:     java/lang/Object
102  * Method:    notify
103  * Signature: ()V
104  */
105 JNIEXPORT void JNICALL Java_java_lang_VMObject_notify(JNIEnv *env, jclass clazz, java_lang_Object *this)
106 {
107         if (runverbose)
108                 log_text("java_lang_Object_notify called");
109
110 #if defined(USE_THREADS)
111         signal_cond_for_object(&this->header);
112 #endif
113 }
114
115
116 /*
117  * Class:     java/lang/Object
118  * Method:    notifyAll
119  * Signature: ()V
120  */
121 JNIEXPORT void JNICALL Java_java_lang_VMObject_notifyAll(JNIEnv *env, jclass clazz, java_lang_Object *this)
122 {
123         if (runverbose)
124                 log_text("java_lang_Object_notifyAll called");
125
126 #if defined(USE_THREADS)
127         broadcast_cond_for_object(&this->header);
128 #endif
129 }
130
131
132 /*
133  * Class:     java/lang/Object
134  * Method:    wait
135  * Signature: (J)V
136  */
137 JNIEXPORT void JNICALL Java_java_lang_VMObject_wait(JNIEnv *env, jclass clazz, java_lang_Object *this, s8 time, s4 par3)
138 {
139         if (runverbose)
140                 log_text("java_lang_VMObject_wait called");
141
142 #if defined(USE_THREADS)
143         wait_cond_for_object(&this->header, time, par3);
144 #endif
145 }
146
147
148 /*
149  * These are local overrides for various environment variables in Emacs.
150  * Please do not remove this and leave it at the end of the file, where
151  * Emacs will automagically detect them.
152  * ---------------------------------------------------------------------
153  * Local variables:
154  * mode: c
155  * indent-tabs-mode: t
156  * c-basic-offset: 4
157  * tab-width: 4
158  * End:
159  */