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
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>
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
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
44 _Jv_Mutex_t join_mutex
;
45 _Jv_ConditionVariable_t join_cond
;
47 // This is private data for the thread system layer.
50 // Each thread has its own JNI object.
54 static void finalize_native (jobject ptr
);
56 // This is called from the constructor to initialize the native side
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
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
);
84 #ifdef _Jv_HaveMutexDestroy
85 _Jv_MutexDestroy (&nt
->join_mutex
);
87 _Jv_FreeJNIEnv(nt
->jni_env
);
91 java::lang::Thread::countStackFrames (void)
93 // NOTE: This is deprecated in JDK 1.2.
94 throw new UnsupportedOperationException
95 (JvNewStringLatin1 ("Thread.countStackFrames unimplemented"));
100 java::lang::Thread::currentThread (void)
102 return _Jv_ThreadCurrent ();
106 java::lang::Thread::holdsLock (jobject obj
)
109 throw new NullPointerException
;
110 return !_Jv_ObjectCheckMonitor (obj
);
114 java::lang::Thread::interrupt (void)
117 natThread
*nt
= (natThread
*) data
;
118 _Jv_ThreadInterrupt (nt
->thread
);
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
);
137 _Jv_MutexUnlock (&nt
->join_mutex
);
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
;
148 java::lang::Thread::resume (void)
151 throw new UnsupportedOperationException
152 (JvNewStringLatin1 ("Thread.resume unimplemented"));
156 java::lang::Thread::setPriority (jint newPriority
)
159 if (newPriority
< MIN_PRIORITY
|| newPriority
> MAX_PRIORITY
)
160 throw new IllegalArgumentException
;
162 jint gmax
= group
->getMaxPriority();
163 if (newPriority
> gmax
)
166 priority
= newPriority
;
167 natThread
*nt
= (natThread
*) data
;
168 _Jv_ThreadSetPriority (nt
->thread
, priority
);
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)
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
;
194 java::lang::Thread::finish_ ()
196 natThread
*nt
= (natThread
*) data
;
198 group
->removeThread (this);
201 if (_Jv_JVMPI_Notify_THREAD_END
)
205 event
.event_type
= JVMPI_EVENT_THREAD_END
;
206 event
.env_id
= _Jv_GetCurrentJNIEnv ();
209 (*_Jv_JVMPI_Notify_THREAD_END
) (&event
);
216 // Signal any threads that are waiting to join() us.
217 _Jv_MutexLock (&nt
->join_mutex
);
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.
226 _Jv_NotifyThreadStart (java::lang::Thread
* thread
)
229 if (_Jv_JVMPI_Notify_THREAD_START
)
233 jstring thread_name
= thread
->getName ();
234 jstring group_name
= NULL
, parent_name
= NULL
;
235 java::lang::ThreadGroup
*group
= thread
->getThreadGroup ();
239 group_name
= group
->getName ();
240 group
= group
->getParent ();
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];
255 JvGetStringUTFRegion (thread_name
, 0,
256 thread_name
->length(), thread_chars
);
258 JvGetStringUTFRegion (group_name
, 0,
259 group_name
->length(), group_chars
);
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
;
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 ();
277 (*_Jv_JVMPI_Notify_THREAD_START
) (&event
);
284 _Jv_ThreadRun (java::lang::Thread
* thread
)
288 _Jv_NotifyThreadStart (thread
);
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
)
309 java::lang::Thread::start (void)
311 JvSynchronize
sync (this);
313 // Its illegal to re-start() a thread, even if its dead.
315 throw new IllegalThreadStateException
;
318 startable_flag
= false;
319 natThread
*nt
= (natThread
*) data
;
320 _Jv_ThreadStart (this, nt
->thread
, (_Jv_ThreadStartFunc
*) &_Jv_ThreadRun
);
324 java::lang::Thread::stop (java::lang::Throwable
*)
327 throw new UnsupportedOperationException
328 (JvNewStringLatin1 ("Thread.stop unimplemented"));
332 java::lang::Thread::suspend (void)
335 throw new UnsupportedOperationException
336 (JvNewStringLatin1 ("Thread.suspend unimplemented"));
339 static int nextThreadNumber
= 0;
342 java::lang::Thread::gen_name (void)
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-".
353 jchar
*bufend
= (jchar
*) ((char *) buffer
+ sizeof(buffer
));
354 i
= _Jv_FormatInt (bufend
, i
);
355 jchar
*ptr
= bufend
- i
;
356 // Prepend "Thread-".
364 return JvNewString (ptr
, bufend
- ptr
);
368 java::lang::Thread::yield (void)
374 _Jv_GetCurrentJNIEnv ()
376 java::lang::Thread
*t
= _Jv_ThreadCurrent ();
379 return ((natThread
*) t
->data
)->jni_env
;
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.
393 _Jv_AttachCurrentThread(java::lang::Thread
* thread
)
395 if (thread
== NULL
|| thread
->startable_flag
== false)
397 thread
->startable_flag
= false;
398 thread
->alive_flag
= true;
399 natThread
*nt
= (natThread
*) thread
->data
;
400 _Jv_ThreadRegister (nt
->thread
);
405 _Jv_AttachCurrentThread(jstring name
, java::lang::ThreadGroup
* group
)
407 java::lang::Thread
*thread
= _Jv_ThreadCurrent ();
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
);
419 _Jv_AttachCurrentThreadAsDaemon(jstring name
, java::lang::ThreadGroup
* group
)
421 java::lang::Thread
*thread
= _Jv_ThreadCurrent ();
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
);
434 _Jv_DetachCurrentThread (void)
436 java::lang::Thread
*t
= _Jv_ThreadCurrent ();
440 _Jv_ThreadUnRegister ();
441 // Release the monitors.