Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / lang / natThread.cc
blobe79ab11b9c600a07ec5b9c53a7cdfcdf483dabd1
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 <gnu/gcj/RawDataManaged.h>
20 #include <java/lang/Thread.h>
21 #include <java/lang/ThreadGroup.h>
22 #include <java/lang/IllegalArgumentException.h>
23 #include <java/lang/UnsupportedOperationException.h>
24 #include <java/lang/IllegalThreadStateException.h>
25 #include <java/lang/InterruptedException.h>
26 #include <java/lang/NullPointerException.h>
28 #include <jni.h>
30 #ifdef ENABLE_JVMPI
31 #include <jvmpi.h>
32 #endif
36 // This structure is used to represent all the data the native side
37 // needs. An object of this type is assigned to the `data' member of
38 // the Thread class.
39 struct natThread
41 // These are used to interrupt sleep and join calls. We can share a
42 // condition variable here since it only ever gets notified when the thread
43 // exits.
44 _Jv_Mutex_t join_mutex;
45 _Jv_ConditionVariable_t join_cond;
47 // This is private data for the thread system layer.
48 _Jv_Thread_t *thread;
50 // Each thread has its own JNI object.
51 JNIEnv *jni_env;
54 static void finalize_native (jobject ptr);
56 // This is called from the constructor to initialize the native side
57 // of the Thread.
58 void
59 java::lang::Thread::initialize_native (void)
61 natThread *nt = (natThread *) _Jv_AllocBytes (sizeof (natThread));
63 data = (gnu::gcj::RawDataManaged *) nt;
65 // Register a finalizer to clean up the native thread resources.
66 _Jv_RegisterFinalizer (data, finalize_native);
68 _Jv_MutexInit (&nt->join_mutex);
69 _Jv_CondInit (&nt->join_cond);
70 nt->thread = _Jv_ThreadInitData (this);
71 // FIXME: if JNI_ENV is set we will want to free it. It is
72 // malloc()d.
73 nt->jni_env = NULL;
76 static void
77 finalize_native (jobject ptr)
79 natThread *nt = (natThread *) ptr;
80 _Jv_ThreadDestroyData (nt->thread);
81 #ifdef _Jv_HaveCondDestroy
82 _Jv_CondDestroy (&nt->join_cond);
83 #endif
84 #ifdef _Jv_HaveMutexDestroy
85 _Jv_MutexDestroy (&nt->join_mutex);
86 #endif
87 _Jv_FreeJNIEnv(nt->jni_env);
90 jint
91 java::lang::Thread::countStackFrames (void)
93 // NOTE: This is deprecated in JDK 1.2.
94 throw new UnsupportedOperationException
95 (JvNewStringLatin1 ("Thread.countStackFrames unimplemented"));
96 return 0;
99 java::lang::Thread *
100 java::lang::Thread::currentThread (void)
102 return _Jv_ThreadCurrent ();
105 jboolean
106 java::lang::Thread::holdsLock (jobject obj)
108 if (!obj)
109 throw new NullPointerException;
110 return !_Jv_ObjectCheckMonitor (obj);
113 void
114 java::lang::Thread::interrupt (void)
116 checkAccess ();
117 natThread *nt = (natThread *) data;
118 _Jv_ThreadInterrupt (nt->thread);
121 void
122 java::lang::Thread::join (jlong millis, jint nanos)
124 if (millis < 0 || nanos < 0 || nanos > 999999)
125 throw new IllegalArgumentException;
127 Thread *current = currentThread ();
129 // Here `NT' is the native structure for the thread we are trying to join.
130 natThread *nt = (natThread *) data;
132 // Now wait for: (1) an interrupt, (2) the thread to exit, or (3)
133 // the timeout to occur.
134 _Jv_MutexLock (&nt->join_mutex);
135 if (! isAlive ())
137 _Jv_MutexUnlock (&nt->join_mutex);
138 return;
140 _Jv_CondWait (&nt->join_cond, &nt->join_mutex, millis, nanos);
141 _Jv_MutexUnlock (&nt->join_mutex);
143 if (current->isInterrupted (true))
144 throw new InterruptedException;
147 void
148 java::lang::Thread::resume (void)
150 checkAccess ();
151 throw new UnsupportedOperationException
152 (JvNewStringLatin1 ("Thread.resume unimplemented"));
155 void
156 java::lang::Thread::setPriority (jint newPriority)
158 checkAccess ();
159 if (newPriority < MIN_PRIORITY || newPriority > MAX_PRIORITY)
160 throw new IllegalArgumentException;
162 jint gmax = group->getMaxPriority();
163 if (newPriority > gmax)
164 newPriority = gmax;
166 priority = newPriority;
167 natThread *nt = (natThread *) data;
168 _Jv_ThreadSetPriority (nt->thread, priority);
171 void
172 java::lang::Thread::sleep (jlong millis, jint nanos)
174 if (millis < 0 || nanos < 0 || nanos > 999999)
175 throw new IllegalArgumentException;
177 if (millis == 0 && nanos == 0)
178 ++nanos;
180 Thread *current = currentThread ();
182 // We use a condition variable to implement sleeping so that an
183 // interrupt can wake us up.
184 natThread *nt = (natThread *) current->data;
185 _Jv_MutexLock (&nt->join_mutex);
186 _Jv_CondWait (&nt->join_cond, &nt->join_mutex, millis, nanos);
187 _Jv_MutexUnlock (&nt->join_mutex);
189 if (current->isInterrupted (true))
190 throw new InterruptedException;
193 void
194 java::lang::Thread::finish_ ()
196 natThread *nt = (natThread *) data;
198 group->removeThread (this);
200 #ifdef ENABLE_JVMPI
201 if (_Jv_JVMPI_Notify_THREAD_END)
203 JVMPI_Event event;
205 event.event_type = JVMPI_EVENT_THREAD_END;
206 event.env_id = _Jv_GetCurrentJNIEnv ();
208 _Jv_DisableGC ();
209 (*_Jv_JVMPI_Notify_THREAD_END) (&event);
210 _Jv_EnableGC ();
212 #endif
214 group = NULL;
216 // Signal any threads that are waiting to join() us.
217 _Jv_MutexLock (&nt->join_mutex);
218 alive_flag = false;
219 _Jv_CondNotifyAll (&nt->join_cond, &nt->join_mutex);
220 _Jv_MutexUnlock (&nt->join_mutex);
223 // Run once at thread startup, either when thread is attached or when
224 // _Jv_ThreadRun is called.
225 static void
226 _Jv_NotifyThreadStart (java::lang::Thread* thread)
228 #ifdef ENABLE_JVMPI
229 if (_Jv_JVMPI_Notify_THREAD_START)
231 JVMPI_Event event;
233 jstring thread_name = thread->getName ();
234 jstring group_name = NULL, parent_name = NULL;
235 java::lang::ThreadGroup *group = thread->getThreadGroup ();
237 if (group)
239 group_name = group->getName ();
240 group = group->getParent ();
242 if (group)
243 parent_name = group->getName ();
246 int thread_len = thread_name ? JvGetStringUTFLength (thread_name) : 0;
247 int group_len = group_name ? JvGetStringUTFLength (group_name) : 0;
248 int parent_len = parent_name ? JvGetStringUTFLength (parent_name) : 0;
250 char thread_chars[thread_len + 1];
251 char group_chars[group_len + 1];
252 char parent_chars[parent_len + 1];
254 if (thread_name)
255 JvGetStringUTFRegion (thread_name, 0,
256 thread_name->length(), thread_chars);
257 if (group_name)
258 JvGetStringUTFRegion (group_name, 0,
259 group_name->length(), group_chars);
260 if (parent_name)
261 JvGetStringUTFRegion (parent_name, 0,
262 parent_name->length(), parent_chars);
264 thread_chars[thread_len] = '\0';
265 group_chars[group_len] = '\0';
266 parent_chars[parent_len] = '\0';
268 event.event_type = JVMPI_EVENT_THREAD_START;
269 event.env_id = NULL;
270 event.u.thread_start.thread_name = thread_chars;
271 event.u.thread_start.group_name = group_chars;
272 event.u.thread_start.parent_name = parent_chars;
273 event.u.thread_start.thread_id = (jobjectID) thread;
274 event.u.thread_start.thread_env_id = _Jv_GetCurrentJNIEnv ();
276 _Jv_DisableGC ();
277 (*_Jv_JVMPI_Notify_THREAD_START) (&event);
278 _Jv_EnableGC ();
280 #endif
283 void
284 _Jv_ThreadRun (java::lang::Thread* thread)
288 _Jv_NotifyThreadStart (thread);
289 thread->run ();
291 catch (java::lang::Throwable *t)
293 // Uncaught exceptions are forwarded to the ThreadGroup. If
294 // this results in an uncaught exception, that is ignored.
297 thread->group->uncaughtException (thread, t);
299 catch (java::lang::Throwable *f)
301 // Nothing.
305 thread->finish_ ();
308 void
309 java::lang::Thread::start (void)
311 JvSynchronize sync (this);
313 // Its illegal to re-start() a thread, even if its dead.
314 if (!startable_flag)
315 throw new IllegalThreadStateException;
317 alive_flag = true;
318 startable_flag = false;
319 natThread *nt = (natThread *) data;
320 _Jv_ThreadStart (this, nt->thread, (_Jv_ThreadStartFunc *) &_Jv_ThreadRun);
323 void
324 java::lang::Thread::stop (java::lang::Throwable *)
326 checkAccess ();
327 throw new UnsupportedOperationException
328 (JvNewStringLatin1 ("Thread.stop unimplemented"));
331 void
332 java::lang::Thread::suspend (void)
334 checkAccess ();
335 throw new UnsupportedOperationException
336 (JvNewStringLatin1 ("Thread.suspend unimplemented"));
339 static int nextThreadNumber = 0;
341 jstring
342 java::lang::Thread::gen_name (void)
344 jint i;
345 jclass sync = &java::lang::Thread::class$;
347 JvSynchronize dummy(sync);
348 i = ++nextThreadNumber;
351 // Use an array large enough for "-2147483648"; i.e. 11 chars, + "Thread-".
352 jchar buffer[7+11];
353 jchar *bufend = (jchar *) ((char *) buffer + sizeof(buffer));
354 i = _Jv_FormatInt (bufend, i);
355 jchar *ptr = bufend - i;
356 // Prepend "Thread-".
357 *--ptr = '-';
358 *--ptr = 'd';
359 *--ptr = 'a';
360 *--ptr = 'e';
361 *--ptr = 'r';
362 *--ptr = 'h';
363 *--ptr = 'T';
364 return JvNewString (ptr, bufend - ptr);
367 void
368 java::lang::Thread::yield (void)
370 _Jv_ThreadYield ();
373 JNIEnv *
374 _Jv_GetCurrentJNIEnv ()
376 java::lang::Thread *t = _Jv_ThreadCurrent ();
377 if (t == NULL)
378 return NULL;
379 return ((natThread *) t->data)->jni_env;
382 void
383 _Jv_SetCurrentJNIEnv (JNIEnv *env)
385 java::lang::Thread *t = _Jv_ThreadCurrent ();
386 JvAssert (t != NULL);
387 ((natThread *) t->data)->jni_env = env;
390 // Attach the current native thread to an existing (but unstarted) Thread
391 // object. Returns -1 on failure, 0 upon success.
392 jint
393 _Jv_AttachCurrentThread(java::lang::Thread* thread)
395 if (thread == NULL || thread->startable_flag == false)
396 return -1;
397 thread->startable_flag = false;
398 thread->alive_flag = true;
399 natThread *nt = (natThread *) thread->data;
400 _Jv_ThreadRegister (nt->thread);
401 return 0;
404 java::lang::Thread*
405 _Jv_AttachCurrentThread(jstring name, java::lang::ThreadGroup* group)
407 java::lang::Thread *thread = _Jv_ThreadCurrent ();
408 if (thread != NULL)
409 return thread;
410 if (name == NULL)
411 name = java::lang::Thread::gen_name ();
412 thread = new java::lang::Thread (NULL, group, NULL, name);
413 _Jv_AttachCurrentThread (thread);
414 _Jv_NotifyThreadStart (thread);
415 return thread;
418 java::lang::Thread*
419 _Jv_AttachCurrentThreadAsDaemon(jstring name, java::lang::ThreadGroup* group)
421 java::lang::Thread *thread = _Jv_ThreadCurrent ();
422 if (thread != NULL)
423 return thread;
424 if (name == NULL)
425 name = java::lang::Thread::gen_name ();
426 thread = new java::lang::Thread (NULL, group, NULL, name);
427 thread->setDaemon (true);
428 _Jv_AttachCurrentThread (thread);
429 _Jv_NotifyThreadStart (thread);
430 return thread;
433 jint
434 _Jv_DetachCurrentThread (void)
436 java::lang::Thread *t = _Jv_ThreadCurrent ();
437 if (t == NULL)
438 return -1;
440 _Jv_ThreadUnRegister ();
441 // Release the monitors.
442 t->finish_ ();
444 return 0;