* src/threads/thread.cpp: thread_new no longer adds to active list.
[cacao.git] / src / threads / threadlist.hpp
index 2a9f2059c296ce538ca1e60be51fe0d01424ff15..0d6043aa72f0bf3499f80f2e25e11e12ff640b9b 100644 (file)
@@ -1,6 +1,6 @@
 /* src/threads/threadlist.hpp - different thread-lists
 
-   Copyright (C) 2008
+   Copyright (C) 1996-2011
    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
 #include "toolbox/list.hpp"
 
+#include "vm/global.h"
+
 
 /* ThreadList *****************************************************************/
 
 #ifdef __cplusplus
 
-using std::list;
-
 class ThreadList {
 private:
        static Mutex               _mutex;              // a mutex for all thread lists
 
-       static list<threadobject*> _active_thread_list; // list of active threads
-       static list<threadobject*> _free_thread_list;   // list of free threads
-       static list<int32_t>       _free_index_list;    // list of free thread indexes
+       static List<threadobject*> _active_thread_list; // list of active threads
+       static List<threadobject*> _free_thread_list;   // list of free threads
+       static List<int32_t>       _free_index_list;    // list of free thread indexes
 
+       // Thread counters visible to Java.
+       static int32_t             _number_of_started_java_threads;
+       static int32_t             _number_of_active_java_threads;
+       static int32_t             _peak_of_active_java_threads;
+
+       // Thread counters for internal usage.
        static int32_t             _number_of_non_daemon_threads;
 
-       static inline void          remove_from_active_thread_list(threadobject* t);
-       static inline void          add_to_free_thread_list(threadobject* t);
-       static inline void          add_to_free_index_list(int32_t index);
+       static void                 remove_from_active_thread_list(threadobject* t);
+       static void                 add_to_free_thread_list(threadobject* t);
+       static void                 add_to_free_index_list(int32_t index);
 
 private:
        // Comparator class.
@@ -66,31 +72,66 @@ private:
        };
 
 public:
-       static inline void          lock()   { _mutex.lock(); }
-       static inline void          unlock() { _mutex.unlock(); }
+       static void                 lock()   { _mutex.lock(); }
+       static void                 unlock() { _mutex.unlock(); }
 
-       // TODO make private
-       static inline void          add_to_active_thread_list(threadobject* t);
+       static void                 add_to_active_thread_list(threadobject* t);
 
-       static void                 dump_threads();
-       static inline threadobject* get_main_thread();
+       // Thread management methods.
+       static threadobject*        get_main_thread();
        static threadobject*        get_free_thread();
        static int32_t              get_free_thread_index();
-       static int32_t              get_number_of_non_daemon_threads();
        static threadobject*        get_thread_by_index(int32_t index);
        static threadobject*        get_thread_from_java_object(java_handle_t* h);
        static void                 release_thread(threadobject* t);
+
+       // Thread listing methods.
+       static void                 get_active_threads(List<threadobject*> &list);
+       static void                 get_active_java_threads(List<threadobject*> &list);
+
+       // Thread counting methods visible to Java.
+       static int32_t              get_number_of_started_java_threads();
+       static int32_t              get_number_of_active_java_threads();
+       static int32_t              get_number_of_daemon_java_threads();
+       static int32_t              get_peak_of_active_java_threads();
+       static void                 reset_peak_of_active_java_threads();
+
+       // Thread counting methods for internal use.
+       static int32_t              get_number_of_active_threads();
+       static int32_t              get_number_of_non_daemon_threads();
+
+       // Debugging methods.
+       static void                 dump_threads();
+};
+
+struct ThreadListLocker {
+       ThreadListLocker()  { ThreadList::lock(); }
+       ~ThreadListLocker()  { ThreadList::unlock(); }
 };
 
 
 inline void ThreadList::add_to_active_thread_list(threadobject* t)
 {
+       lock();
        _active_thread_list.push_back(t);
+
+       // Update counter variables.
+       if ((t->flags & THREAD_FLAG_INTERNAL) == 0) {
+               _number_of_started_java_threads++;
+               _number_of_active_java_threads++;
+               _peak_of_active_java_threads = MAX(_peak_of_active_java_threads, _number_of_active_java_threads);
+       }
+       unlock();
 }
 
 inline void ThreadList::remove_from_active_thread_list(threadobject* t)
 {
        _active_thread_list.remove(t);
+
+       // Update counter variables.
+       if ((t->flags & THREAD_FLAG_INTERNAL) == 0) {
+               _number_of_active_java_threads--;
+       }
 }
 
 inline void ThreadList::add_to_free_thread_list(threadobject* t)
@@ -108,6 +149,31 @@ inline threadobject* ThreadList::get_main_thread()
        return _active_thread_list.front();
 }
 
+inline int32_t ThreadList::get_number_of_active_threads()
+{
+       return _active_thread_list.size();
+}
+
+inline int32_t ThreadList::get_number_of_started_java_threads()
+{
+       return _number_of_started_java_threads;
+}
+
+inline int32_t ThreadList::get_number_of_active_java_threads()
+{
+       return _number_of_active_java_threads;
+}
+
+inline int32_t ThreadList::get_peak_of_active_java_threads()
+{
+       return _peak_of_active_java_threads;
+}
+
+inline void ThreadList::reset_peak_of_active_java_threads()
+{
+       _peak_of_active_java_threads = _number_of_active_java_threads;
+}
+
 #else
 
 typedef struct ThreadList ThreadList;