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 <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>
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
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
43 _Jv_Mutex_t join_mutex
;
44 _Jv_ConditionVariable_t join_cond
;
46 // This is private data for the thread system layer.
49 // Each thread has its own JNI object.
53 static void finalize_native (jobject ptr
);
55 // This is called from the constructor to initialize the native side
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
80 finalize_native (jobject ptr
)
82 natThread
*nt
= (natThread
*) ptr
;
83 _Jv_ThreadDestroyData (nt
->thread
);
87 java::lang::Thread::countStackFrames (void)
89 // NOTE: This is deprecated in JDK 1.2.
90 throw new UnsupportedOperationException
91 (JvNewStringLatin1 ("Thread.countStackFrames unimplemented"));
96 java::lang::Thread::currentThread (void)
98 return _Jv_ThreadCurrent ();
102 java::lang::Thread::destroy (void)
104 // NOTE: This is marked as unimplemented in the JDK 1.2
106 throw new UnsupportedOperationException
107 (JvNewStringLatin1 ("Thread.destroy unimplemented"));
111 java::lang::Thread::holdsLock (jobject obj
)
114 throw new NullPointerException
;
115 return !_Jv_ObjectCheckMonitor (obj
);
119 java::lang::Thread::interrupt (void)
121 natThread
*nt
= (natThread
*) data
;
122 _Jv_ThreadInterrupt (nt
->thread
);
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
);
141 _Jv_MutexUnlock (&nt
->join_mutex
);
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
;
152 java::lang::Thread::resume (void)
155 throw new UnsupportedOperationException
156 (JvNewStringLatin1 ("Thread.resume unimplemented"));
160 java::lang::Thread::setPriority (jint newPriority
)
163 if (newPriority
< MIN_PRIORITY
|| newPriority
> MAX_PRIORITY
)
164 throw new IllegalArgumentException
;
166 jint gmax
= group
->getMaxPriority();
167 if (newPriority
> gmax
)
170 priority
= newPriority
;
171 natThread
*nt
= (natThread
*) data
;
172 _Jv_ThreadSetPriority (nt
->thread
, priority
);
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)
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
;
198 java::lang::Thread::finish_ ()
200 natThread
*nt
= (natThread
*) data
;
202 group
->removeThread (this);
205 if (_Jv_JVMPI_Notify_THREAD_END
)
209 event
.event_type
= JVMPI_EVENT_THREAD_END
;
210 event
.env_id
= _Jv_GetCurrentJNIEnv ();
213 (*_Jv_JVMPI_Notify_THREAD_END
) (&event
);
220 // Signal any threads that are waiting to join() us.
221 _Jv_MutexLock (&nt
->join_mutex
);
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.
230 _Jv_NotifyThreadStart (java::lang::Thread
* thread
)
233 if (_Jv_JVMPI_Notify_THREAD_START
)
237 jstring thread_name
= thread
->getName ();
238 jstring group_name
= NULL
, parent_name
= NULL
;
239 java::lang::ThreadGroup
*group
= thread
->getThreadGroup ();
243 group_name
= group
->getName ();
244 group
= group
->getParent ();
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];
259 JvGetStringUTFRegion (thread_name
, 0,
260 thread_name
->length(), thread_chars
);
262 JvGetStringUTFRegion (group_name
, 0,
263 group_name
->length(), group_chars
);
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
;
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 ();
281 (*_Jv_JVMPI_Notify_THREAD_START
) (&event
);
288 _Jv_ThreadRun (java::lang::Thread
* thread
)
292 _Jv_NotifyThreadStart (thread
);
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
)
313 java::lang::Thread::start (void)
315 JvSynchronize
sync (this);
317 // Its illegal to re-start() a thread, even if its dead.
319 throw new IllegalThreadStateException
;
322 startable_flag
= false;
323 natThread
*nt
= (natThread
*) data
;
324 _Jv_ThreadStart (this, nt
->thread
, (_Jv_ThreadStartFunc
*) &_Jv_ThreadRun
);
328 java::lang::Thread::stop (java::lang::Throwable
*)
330 throw new UnsupportedOperationException
331 (JvNewStringLatin1 ("Thread.stop unimplemented"));
335 java::lang::Thread::suspend (void)
338 throw new UnsupportedOperationException
339 (JvNewStringLatin1 ("Thread.suspend unimplemented"));
342 static int nextThreadNumber
= 0;
345 java::lang::Thread::gen_name (void)
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-".
356 jchar
*bufend
= (jchar
*) ((char *) buffer
+ sizeof(buffer
));
357 i
= _Jv_FormatInt (bufend
, i
);
358 jchar
*ptr
= bufend
- i
;
359 // Prepend "Thread-".
367 return JvNewString (ptr
, bufend
- ptr
);
371 java::lang::Thread::yield (void)
377 _Jv_GetCurrentJNIEnv ()
379 java::lang::Thread
*t
= _Jv_ThreadCurrent ();
382 return ((natThread
*) t
->data
)->jni_env
;
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.
396 _Jv_AttachCurrentThread(java::lang::Thread
* thread
)
398 if (thread
== NULL
|| thread
->startable_flag
== false)
400 thread
->startable_flag
= false;
401 thread
->alive_flag
= true;
402 natThread
*nt
= (natThread
*) thread
->data
;
403 _Jv_ThreadRegister (nt
->thread
);
408 _Jv_AttachCurrentThread(jstring name
, java::lang::ThreadGroup
* group
)
410 java::lang::Thread
*thread
= _Jv_ThreadCurrent ();
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
);
422 _Jv_AttachCurrentThreadAsDaemon(jstring name
, java::lang::ThreadGroup
* group
)
424 java::lang::Thread
*thread
= _Jv_ThreadCurrent ();
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
);
437 _Jv_DetachCurrentThread (void)
439 java::lang::Thread
*t
= _Jv_ThreadCurrent ();
443 _Jv_ThreadUnRegister ();
444 // Release the monitors.