2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / lang / natThread.cc
blob235f950c2c1da2ed21bd170cc1f6d5beb5f4b491
1 // natThread.cc - Native part of Thread class.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
13 #include <stdlib.h>
15 #include <gcj/cni.h>
16 #include <jvm.h>
17 #include <java-threads.h>
19 #include <java/lang/Thread.h>
20 #include <java/lang/ThreadGroup.h>
21 #include <java/lang/IllegalArgumentException.h>
22 #include <java/lang/UnsupportedOperationException.h>
23 #include <java/lang/IllegalThreadStateException.h>
24 #include <java/lang/InterruptedException.h>
25 #include <java/lang/NullPointerException.h>
27 #include <jni.h>
29 #ifdef ENABLE_JVMPI
30 #include <jvmpi.h>
31 #endif
35 // This structure is used to represent all the data the native side
36 // needs. An object of this type is assigned to the `data' member of
37 // the Thread class.
38 struct natThread
40 // These are used to interrupt sleep and join calls. We can share a
41 // condition variable here since it only ever gets notified when the thread
42 // exits.
43 _Jv_Mutex_t join_mutex;
44 _Jv_ConditionVariable_t join_cond;
46 // This is private data for the thread system layer.
47 _Jv_Thread_t *thread;
49 // Each thread has its own JNI object.
50 JNIEnv *jni_env;
53 static void finalize_native (jobject ptr);
55 // This is called from the constructor to initialize the native side
56 // of the Thread.
57 void
58 java::lang::Thread::initialize_native (void)
60 natThread *nt = (natThread *) _Jv_AllocBytes (sizeof (natThread));
62 // The native thread data is kept in a Object field, not a RawData, so that
63 // the GC allocator can be used and a finalizer run after the thread becomes
64 // unreachable. Note that this relies on the GC's ability to finalize
65 // non-Java objects. FIXME?
66 data = reinterpret_cast<jobject> (nt);
68 // Register a finalizer to clean up the native thread resources.
69 _Jv_RegisterFinalizer (data, finalize_native);
71 _Jv_MutexInit (&nt->join_mutex);
72 _Jv_CondInit (&nt->join_cond);
73 nt->thread = _Jv_ThreadInitData (this);
74 // FIXME: if JNI_ENV is set we will want to free it. It is
75 // malloc()d.
76 nt->jni_env = NULL;
79 static void
80 finalize_native (jobject ptr)
82 natThread *nt = (natThread *) ptr;
83 _Jv_ThreadDestroyData (nt->thread);
86 jint
87 java::lang::Thread::countStackFrames (void)
89 // NOTE: This is deprecated in JDK 1.2.
90 throw new UnsupportedOperationException
91 (JvNewStringLatin1 ("Thread.countStackFrames unimplemented"));
92 return 0;
95 java::lang::Thread *
96 java::lang::Thread::currentThread (void)
98 return _Jv_ThreadCurrent ();
101 void
102 java::lang::Thread::destroy (void)
104 // NOTE: This is marked as unimplemented in the JDK 1.2
105 // documentation.
106 throw new UnsupportedOperationException
107 (JvNewStringLatin1 ("Thread.destroy unimplemented"));
110 jboolean
111 java::lang::Thread::holdsLock (jobject obj)
113 if (!obj)
114 throw new NullPointerException;
115 return !_Jv_ObjectCheckMonitor (obj);
118 void
119 java::lang::Thread::interrupt (void)
121 natThread *nt = (natThread *) data;
122 _Jv_ThreadInterrupt (nt->thread);
125 void
126 java::lang::Thread::join (jlong millis, jint nanos)
128 if (millis < 0 || nanos < 0 || nanos > 999999)
129 throw new IllegalArgumentException;
131 Thread *current = currentThread ();
133 // Here `NT' is the native structure for the thread we are trying to join.
134 natThread *nt = (natThread *) data;
136 // Now wait for: (1) an interrupt, (2) the thread to exit, or (3)
137 // the timeout to occur.
138 _Jv_MutexLock (&nt->join_mutex);
139 if (! isAlive ())
141 _Jv_MutexUnlock (&nt->join_mutex);
142 return;
144 _Jv_CondWait (&nt->join_cond, &nt->join_mutex, millis, nanos);
145 _Jv_MutexUnlock (&nt->join_mutex);
147 if (current->isInterrupted (true))
148 throw new InterruptedException;
151 void
152 java::lang::Thread::resume (void)
154 checkAccess ();
155 throw new UnsupportedOperationException
156 (JvNewStringLatin1 ("Thread.resume unimplemented"));
159 void
160 java::lang::Thread::setPriority (jint newPriority)
162 checkAccess ();
163 if (newPriority < MIN_PRIORITY || newPriority > MAX_PRIORITY)
164 throw new IllegalArgumentException;
166 jint gmax = group->getMaxPriority();
167 if (newPriority > gmax)
168 newPriority = gmax;
170 priority = newPriority;
171 natThread *nt = (natThread *) data;
172 _Jv_ThreadSetPriority (nt->thread, priority);
175 void
176 java::lang::Thread::sleep (jlong millis, jint nanos)
178 if (millis < 0 || nanos < 0 || nanos > 999999)
179 throw new IllegalArgumentException;
181 if (millis == 0 && nanos == 0)
182 ++nanos;
184 Thread *current = currentThread ();
186 // We use a condition variable to implement sleeping so that an
187 // interrupt can wake us up.
188 natThread *nt = (natThread *) current->data;
189 _Jv_MutexLock (&nt->join_mutex);
190 _Jv_CondWait (&nt->join_cond, &nt->join_mutex, millis, nanos);
191 _Jv_MutexUnlock (&nt->join_mutex);
193 if (current->isInterrupted (true))
194 throw new InterruptedException;
197 void
198 java::lang::Thread::finish_ ()
200 natThread *nt = (natThread *) data;
202 group->removeThread (this);
204 #ifdef ENABLE_JVMPI
205 if (_Jv_JVMPI_Notify_THREAD_END)
207 JVMPI_Event event;
209 event.event_type = JVMPI_EVENT_THREAD_END;
210 event.env_id = _Jv_GetCurrentJNIEnv ();
212 _Jv_DisableGC ();
213 (*_Jv_JVMPI_Notify_THREAD_END) (&event);
214 _Jv_EnableGC ();
216 #endif
218 group = NULL;
220 // Signal any threads that are waiting to join() us.
221 _Jv_MutexLock (&nt->join_mutex);
222 alive_flag = false;
223 _Jv_CondNotifyAll (&nt->join_cond, &nt->join_mutex);
224 _Jv_MutexUnlock (&nt->join_mutex);
227 // Run once at thread startup, either when thread is attached or when
228 // _Jv_ThreadRun is called.
229 static void
230 _Jv_NotifyThreadStart (java::lang::Thread* thread)
232 #ifdef ENABLE_JVMPI
233 if (_Jv_JVMPI_Notify_THREAD_START)
235 JVMPI_Event event;
237 jstring thread_name = thread->getName ();
238 jstring group_name = NULL, parent_name = NULL;
239 java::lang::ThreadGroup *group = thread->getThreadGroup ();
241 if (group)
243 group_name = group->getName ();
244 group = group->getParent ();
246 if (group)
247 parent_name = group->getName ();
250 int thread_len = thread_name ? JvGetStringUTFLength (thread_name) : 0;
251 int group_len = group_name ? JvGetStringUTFLength (group_name) : 0;
252 int parent_len = parent_name ? JvGetStringUTFLength (parent_name) : 0;
254 char thread_chars[thread_len + 1];
255 char group_chars[group_len + 1];
256 char parent_chars[parent_len + 1];
258 if (thread_name)
259 JvGetStringUTFRegion (thread_name, 0,
260 thread_name->length(), thread_chars);
261 if (group_name)
262 JvGetStringUTFRegion (group_name, 0,
263 group_name->length(), group_chars);
264 if (parent_name)
265 JvGetStringUTFRegion (parent_name, 0,
266 parent_name->length(), parent_chars);
268 thread_chars[thread_len] = '\0';
269 group_chars[group_len] = '\0';
270 parent_chars[parent_len] = '\0';
272 event.event_type = JVMPI_EVENT_THREAD_START;
273 event.env_id = NULL;
274 event.u.thread_start.thread_name = thread_chars;
275 event.u.thread_start.group_name = group_chars;
276 event.u.thread_start.parent_name = parent_chars;
277 event.u.thread_start.thread_id = (jobjectID) thread;
278 event.u.thread_start.thread_env_id = _Jv_GetCurrentJNIEnv ();
280 _Jv_DisableGC ();
281 (*_Jv_JVMPI_Notify_THREAD_START) (&event);
282 _Jv_EnableGC ();
284 #endif
287 void
288 _Jv_ThreadRun (java::lang::Thread* thread)
292 _Jv_NotifyThreadStart (thread);
293 thread->run ();
295 catch (java::lang::Throwable *t)
297 // Uncaught exceptions are forwarded to the ThreadGroup. If
298 // this results in an uncaught exception, that is ignored.
301 thread->group->uncaughtException (thread, t);
303 catch (java::lang::Throwable *f)
305 // Nothing.
309 thread->finish_ ();
312 void
313 java::lang::Thread::start (void)
315 JvSynchronize sync (this);
317 // Its illegal to re-start() a thread, even if its dead.
318 if (!startable_flag)
319 throw new IllegalThreadStateException;
321 alive_flag = true;
322 startable_flag = false;
323 natThread *nt = (natThread *) data;
324 _Jv_ThreadStart (this, nt->thread, (_Jv_ThreadStartFunc *) &_Jv_ThreadRun);
327 void
328 java::lang::Thread::stop (java::lang::Throwable *)
330 throw new UnsupportedOperationException
331 (JvNewStringLatin1 ("Thread.stop unimplemented"));
334 void
335 java::lang::Thread::suspend (void)
337 checkAccess ();
338 throw new UnsupportedOperationException
339 (JvNewStringLatin1 ("Thread.suspend unimplemented"));
342 static int nextThreadNumber = 0;
344 jstring
345 java::lang::Thread::gen_name (void)
347 jint i;
348 jclass sync = &java::lang::Thread::class$;
350 JvSynchronize dummy(sync);
351 i = ++nextThreadNumber;
354 // Use an array large enough for "-2147483648"; i.e. 11 chars, + "Thread-".
355 jchar buffer[7+11];
356 jchar *bufend = (jchar *) ((char *) buffer + sizeof(buffer));
357 i = _Jv_FormatInt (bufend, i);
358 jchar *ptr = bufend - i;
359 // Prepend "Thread-".
360 *--ptr = '-';
361 *--ptr = 'd';
362 *--ptr = 'a';
363 *--ptr = 'e';
364 *--ptr = 'r';
365 *--ptr = 'h';
366 *--ptr = 'T';
367 return JvNewString (ptr, bufend - ptr);
370 void
371 java::lang::Thread::yield (void)
373 _Jv_ThreadYield ();
376 JNIEnv *
377 _Jv_GetCurrentJNIEnv ()
379 java::lang::Thread *t = _Jv_ThreadCurrent ();
380 if (t == NULL)
381 return NULL;
382 return ((natThread *) t->data)->jni_env;
385 void
386 _Jv_SetCurrentJNIEnv (JNIEnv *env)
388 java::lang::Thread *t = _Jv_ThreadCurrent ();
389 JvAssert (t != NULL);
390 ((natThread *) t->data)->jni_env = env;
393 // Attach the current native thread to an existing (but unstarted) Thread
394 // object. Returns -1 on failure, 0 upon success.
395 jint
396 _Jv_AttachCurrentThread(java::lang::Thread* thread)
398 if (thread == NULL || thread->startable_flag == false)
399 return -1;
400 thread->startable_flag = false;
401 thread->alive_flag = true;
402 natThread *nt = (natThread *) thread->data;
403 _Jv_ThreadRegister (nt->thread);
404 return 0;
407 java::lang::Thread*
408 _Jv_AttachCurrentThread(jstring name, java::lang::ThreadGroup* group)
410 java::lang::Thread *thread = _Jv_ThreadCurrent ();
411 if (thread != NULL)
412 return thread;
413 if (name == NULL)
414 name = java::lang::Thread::gen_name ();
415 thread = new java::lang::Thread (NULL, group, NULL, name);
416 _Jv_AttachCurrentThread (thread);
417 _Jv_NotifyThreadStart (thread);
418 return thread;
421 java::lang::Thread*
422 _Jv_AttachCurrentThreadAsDaemon(jstring name, java::lang::ThreadGroup* group)
424 java::lang::Thread *thread = _Jv_ThreadCurrent ();
425 if (thread != NULL)
426 return thread;
427 if (name == NULL)
428 name = java::lang::Thread::gen_name ();
429 thread = new java::lang::Thread (NULL, group, NULL, name);
430 thread->setDaemon (true);
431 _Jv_AttachCurrentThread (thread);
432 _Jv_NotifyThreadStart (thread);
433 return thread;
436 jint
437 _Jv_DetachCurrentThread (void)
439 java::lang::Thread *t = _Jv_ThreadCurrent ();
440 if (t == NULL)
441 return -1;
443 _Jv_ThreadUnRegister ();
444 // Release the monitors.
445 t->finish_ ();
447 return 0;