projects
/
cacao.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
* src/threads/thread.cpp: Break a reference cycle.
[cacao.git]
/
src
/
threads
/
thread.cpp
diff --git
a/src/threads/thread.cpp
b/src/threads/thread.cpp
index 15bd67c5898826a6025cd91ea238c058cec4be1e..b111ca90f70bdaf74e9f073f93bf49e970ed65e0 100644
(file)
--- a/
src/threads/thread.cpp
+++ b/
src/threads/thread.cpp
@@
-49,6
+49,7
@@
#include "vm/jit/builtin.hpp"
#include "vm/class.hpp"
#include "vm/exceptions.hpp"
#include "vm/jit/builtin.hpp"
#include "vm/class.hpp"
#include "vm/exceptions.hpp"
+#include "vm/finalizer.hpp"
#include "vm/globals.hpp"
#include "vm/javaobjects.hpp"
#include "vm/method.hpp"
#include "vm/globals.hpp"
#include "vm/javaobjects.hpp"
#include "vm/method.hpp"
@@
-212,7
+213,7
@@
static bool thread_create_object(threadobject *t, java_handle_t *name, java_hand
// Set the Java object in the thread data-structure. This
// indicates that the thread is attached to the VM.
// Set the Java object in the thread data-structure. This
// indicates that the thread is attached to the VM.
- t
hread_set_object(t,
jlt.get_handle());
+ t
->object = LLNI_DIRECT(
jlt.get_handle());
return ThreadRuntime::invoke_thread_initializer(jlt, t, thread_method_init, name, group);
}
return ThreadRuntime::invoke_thread_initializer(jlt, t, thread_method_init, name, group);
}
@@
-368,13
+369,15
@@
static threadobject *thread_new(int32_t flags)
void thread_free(threadobject *t)
{
void thread_free(threadobject *t)
{
- /* Set the reference to the Java object to NULL. */
+ java_handle_t *h = LLNI_WRAP(t->object);
+ java_lang_Thread jlt(h);
+ ThreadRuntime::clear_heap_reference(jlt);
- thread_set_object(t, NULL);
+ /* Set the reference to the Java object to NULL. */
- /* Release the thread. */
+ t->object = 0;
- ThreadList::
releas
e_thread(t);
+ ThreadList::
deactivat
e_thread(t);
}
}
@@
-389,16
+392,16
@@
void thread_free(threadobject *t)
*******************************************************************************/
*******************************************************************************/
+static void thread_cleanup_finalizer(java_handle_t *h, void *data)
+{
+ threadobject *t = reinterpret_cast<threadobject*>(data);
+ ThreadList::release_thread(t, false);
+}
+
bool threads_thread_start_internal(utf *name, functionptr f)
{
threadobject *t;
bool threads_thread_start_internal(utf *name, functionptr f)
{
threadobject *t;
- /* Enter the join-mutex, so if the main-thread is currently
- waiting to join all threads, the number of non-daemon threads
- is correct. */
-
- threads_mutex_join_lock();
-
/* Create internal thread data-structure. */
t = thread_new(THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON);
/* Create internal thread data-structure. */
t = thread_new(THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON);
@@
-407,15
+410,14
@@
bool threads_thread_start_internal(utf *name, functionptr f)
ThreadList::add_to_active_thread_list(t);
ThreadList::add_to_active_thread_list(t);
- /* The thread is flagged as (non-)daemon thread, we can leave the
- mutex. */
-
- threads_mutex_join_unlock();
-
/* Create the Java thread object. */
/* Create the Java thread object. */
- if (!thread_create_object(t, javastring_new(name), threadgroup_system))
+ if (!thread_create_object(t, javastring_new(name), threadgroup_system)) {
+ ThreadList::release_thread(t, true);
return false;
return false;
+ }
+
+ Finalizer::attach_custom_finalizer(LLNI_WRAP(t->object), thread_cleanup_finalizer, t);
/* Start the thread. */
/* Start the thread. */
@@
-441,35
+443,32
@@
void threads_thread_start(java_handle_t *object)
{
java_lang_Thread jlt(object);
{
java_lang_Thread jlt(object);
- /* Enter the join-mutex, so if the main-thread is currently
- waiting to join all threads, the number of non-daemon threads
- is correct. */
-
- threads_mutex_join_lock();
-
/* Create internal thread data-structure. */
/* Create internal thread data-structure. */
- threadobject* t = thread_new(THREAD_FLAG_JAVA);
-
+ u4 flags = THREAD_FLAG_JAVA;
#if defined(ENABLE_JAVASE)
/* Is this a daemon thread? */
#if defined(ENABLE_JAVASE)
/* Is this a daemon thread? */
- if (jlt.get_daemon()
== true
)
-
t->
flags |= THREAD_FLAG_DAEMON;
+ if (jlt.get_daemon())
+ flags |= THREAD_FLAG_DAEMON;
#endif
#endif
+ threadobject* t = thread_new(flags);
+
/* Link the two objects together. */
/* Link the two objects together. */
- t
hread_set_object(t,
object);
+ t
->object = LLNI_DIRECT(
object);
/* Add the thread to the thread list. */
ThreadList::add_to_active_thread_list(t);
/* Add the thread to the thread list. */
ThreadList::add_to_active_thread_list(t);
-
threads_mutex_join_unlock
();
+
Atomic::write_memory_barrier
();
ThreadRuntime::setup_thread_vmdata(jlt, t);
ThreadRuntime::setup_thread_vmdata(jlt, t);
+ Finalizer::attach_custom_finalizer(LLNI_WRAP(t->object), thread_cleanup_finalizer, t);
+
/* Start the thread. Don't pass a function pointer (NULL) since
we want Thread.run()V here. */
/* Start the thread. Don't pass a function pointer (NULL) since
we want Thread.run()V here. */
@@
-502,18
+501,13
@@
bool thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
if (result == true)
return true;
if (result == true)
return true;
- /* Enter the join-mutex, so if the main-thread is currently
- waiting to join all threads, the number of non-daemon threads
- is correct. */
-
- threads_mutex_join_lock();
-
/* Create internal thread data structure. */
/* Create internal thread data structure. */
- t = thread_new(THREAD_FLAG_JAVA);
-
+ u4 flags = THREAD_FLAG_JAVA;
if (isdaemon)
if (isdaemon)
- t->flags |= THREAD_FLAG_DAEMON;
+ flags |= THREAD_FLAG_DAEMON;
+
+ t = thread_new(flags);
/* Store the internal thread data-structure in the TSD. */
/* Store the internal thread data-structure in the TSD. */
@@
-526,8
+520,6
@@
bool thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
ThreadList::add_to_active_thread_list(t);
ThreadList::add_to_active_thread_list(t);
- threads_mutex_join_unlock();
-
DEBUGTHREADS("attaching", t);
/* Get the thread name. */
DEBUGTHREADS("attaching", t);
/* Get the thread name. */
@@
-566,8
+558,10
@@
bool thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
/* Create the Java thread object. */
/* Create the Java thread object. */
- if (!thread_create_object(t, name, group))
+ if (!thread_create_object(t, name, group)) {
+ ThreadList::release_thread(t, true);
return false;
return false;
+ }
/* The thread is completely initialized. */
/* The thread is completely initialized. */
@@
-662,10
+656,10
@@
bool thread_detach_current_external_thread(void)
void thread_fprint_name(threadobject *t, FILE *stream)
{
void thread_fprint_name(threadobject *t, FILE *stream)
{
- if (
thread_get_object(
t) == NULL)
+ if (
LLNI_WRAP(t->objec
t) == NULL)
vm_abort("");
vm_abort("");
- java_lang_Thread jlt(
thread_get_object(
t));
+ java_lang_Thread jlt(
LLNI_WRAP(t->objec
t));
ThreadRuntime::print_thread_name(jlt, stream);
}
ThreadRuntime::print_thread_name(jlt, stream);
}
@@
-682,7
+676,7
@@
void thread_fprint_name(threadobject *t, FILE *stream)
void thread_print_info(threadobject *t)
{
void thread_print_info(threadobject *t)
{
- java_lang_Thread jlt(
thread_get_object(
t));
+ java_lang_Thread jlt(
LLNI_WRAP(t->objec
t));
/* Print as much as we can when we are in state NEW. */
/* Print as much as we can when we are in state NEW. */
@@
-894,13
+888,9
@@
void thread_set_state_terminated(threadobject *t)
{
/* Set the state inside a lock. */
{
/* Set the state inside a lock. */
- ThreadList::lock();
-
thread_set_state(t, THREAD_STATE_TERMINATED);
DEBUGTHREADS("is TERMINATED", t);
thread_set_state(t, THREAD_STATE_TERMINATED);
DEBUGTHREADS("is TERMINATED", t);
-
- ThreadList::unlock();
}
}
@@
-1012,8
+1002,6
@@
void thread_set_interrupted(threadobject *t, bool interrupted)
void thread_handle_set_priority(java_handle_t *th, int priority)
{
void thread_handle_set_priority(java_handle_t *th, int priority)
{
- ThreadListLocker l;
-
threadobject *t = thread_get_thread(th);
/* For GNU classpath, this should not happen, because both
setPriority() and start() are synchronized. */
threadobject *t = thread_get_thread(th);
/* For GNU classpath, this should not happen, because both
setPriority() and start() are synchronized. */
@@
-1031,8
+1019,6
@@
void thread_handle_set_priority(java_handle_t *th, int priority)
bool thread_handle_is_interrupted(java_handle_t *th)
{
bool thread_handle_is_interrupted(java_handle_t *th)
{
- ThreadListLocker l;
-
threadobject *t = thread_get_thread(th);
return t ? thread_is_interrupted(t) : false;
}
threadobject *t = thread_get_thread(th);
return t ? thread_is_interrupted(t) : false;
}
@@
-1047,8
+1033,6
@@
bool thread_handle_is_interrupted(java_handle_t *th)
void thread_handle_interrupt(java_handle_t *th)
{
void thread_handle_interrupt(java_handle_t *th)
{
- ThreadListLocker l;
-
threadobject *t = thread_get_thread(th);
/* For GNU classpath, this should not happen, because both
interrupt() and start() are synchronized. */
threadobject *t = thread_get_thread(th);
/* For GNU classpath, this should not happen, because both
interrupt() and start() are synchronized. */
@@
-1066,8
+1050,6
@@
void thread_handle_interrupt(java_handle_t *th)
int thread_handle_get_state(java_handle_t *th)
{
int thread_handle_get_state(java_handle_t *th)
{
- ThreadListLocker l;
-
threadobject *t = thread_get_thread(th);
return t ? cacaothread_get_state(t) : THREAD_STATE_NEW;
}
threadobject *t = thread_get_thread(th);
return t ? cacaothread_get_state(t) : THREAD_STATE_NEW;
}