* src/threads/threadlist.cpp: Fixed strategy for giving out thread indexes.
[cacao.git] / src / threads / threadlist.cpp
index ce055a083e15ff9f2dddd9b19a89472cdd1aff60..a24ba44412c1053f844922ab59eeee38f0ca9e03 100644 (file)
@@ -52,6 +52,8 @@ int32_t             ThreadList::_number_of_active_java_threads;
 int32_t             ThreadList::_peak_of_active_java_threads;
 int32_t             ThreadList::_number_of_non_daemon_threads;
 
+int32_t             ThreadList::_last_index = 0;
+
 
 /**
  * Dumps info for all threads running in the VM.  This function is
@@ -158,13 +160,17 @@ threadobject* ThreadList::get_free_thread()
 {
        threadobject* t = NULL;
 
+       lock();
+
        // Do we have free threads in the free-list?
        if (_free_thread_list.empty() == false) {
                // Yes, get the index and remove it from the free list.
-               threadobject* t = _free_thread_list.front();
+               t = _free_thread_list.front();
                _free_thread_list.remove(t);
        }
 
+       unlock();
+
        return t;
 }
 
@@ -178,6 +184,8 @@ int32_t ThreadList::get_free_thread_index()
 {
        int32_t index;
 
+       lock();
+
        // Do we have free indexes in the free-list?
        if (_free_index_list.empty() == false) {
                // Yes, get the index and remove it from the free list.
@@ -186,9 +194,11 @@ int32_t ThreadList::get_free_thread_index()
        }
        else {
                // Get a new the thread index.
-               index = _active_thread_list.size() + 1;
+               index = ++_last_index;
        }
 
+       unlock();
+
        return index;
 }
 
@@ -313,18 +323,28 @@ threadobject* ThreadList::get_thread_from_java_object(java_handle_t* h)
        return NULL;
 }
 
+void ThreadList::deactivate_thread(threadobject *t)
+{
+       ThreadListLocker lock;
+       remove_from_active_thread_list(t);
+       threads_impl_clear_heap_pointers(t); // allow it to be garbage collected
+}
 
 /**
  * Release the thread.
  *
  * @return free thread index
  */
-void ThreadList::release_thread(threadobject* t)
+void ThreadList::release_thread(threadobject* t, bool needs_deactivate)
 {
        lock();
 
-       // Move thread from active thread list to free thread list.
-       remove_from_active_thread_list(t);
+       if (needs_deactivate)
+               // Move thread from active thread list to free thread list.
+               remove_from_active_thread_list(t);
+       else
+               assert(!t->is_in_active_list);
+
        add_to_free_thread_list(t);
 
        // Add thread index to free index list.
@@ -340,7 +360,6 @@ extern "C" {
        void ThreadList_lock() { ThreadList::lock(); }
        void ThreadList_unlock() { ThreadList::unlock(); }
        void ThreadList_dump_threads() { ThreadList::dump_threads(); }
-       void ThreadList_release_thread(threadobject* t) { ThreadList::release_thread(t); }
        threadobject* ThreadList_get_free_thread() { return ThreadList::get_free_thread(); }
        int32_t ThreadList_get_free_thread_index() { return ThreadList::get_free_thread_index(); }
        void ThreadList_add_to_active_thread_list(threadobject* t) { ThreadList::add_to_active_thread_list(t); }