We need to keep track of running threads when stopping the world. This
means that we must exclude threads that are waiting on monitor. This
patch introduces a vm_nr_threads_running() that will be used in vm/gc.c.

Cc: Tomek Grabiec <tgrab...@gmail.com>
Cc: Vegard Nossum <vegard.nos...@gmail.com>
Signed-off-by: Pekka Enberg <penb...@cs.helsinki.fi>
---
 include/vm/thread.h |    2 ++
 vm/thread.c         |   37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 0 deletions(-)

diff --git a/include/vm/thread.h b/include/vm/thread.h
index 39084a1..afb5ede 100644
--- a/include/vm/thread.h
+++ b/include/vm/thread.h
@@ -32,6 +32,8 @@ struct vm_exec_env {
        struct vm_thread *thread;
 };
 
+unsigned int vm_nr_threads_running(void);
+
 extern __thread struct vm_exec_env current_exec_env;
 
 static inline struct vm_exec_env *vm_get_exec_env(void)
diff --git a/vm/thread.c b/vm/thread.c
index c2fefbf..64052ca 100644
--- a/vm/thread.c
+++ b/vm/thread.c
@@ -52,6 +52,8 @@ static pthread_cond_t thread_terminate_cond = 
PTHREAD_COND_INITIALIZER;
 
 static int nr_non_daemons;
 
+static unsigned int nr_threads_running;
+
 static struct list_head thread_list;
 
 #define thread_for_each(this) list_for_each_entry(this, &thread_list, 
list_node)
@@ -103,17 +105,52 @@ static bool vm_thread_is_daemon(struct vm_thread *thread)
        return field_get_int(jthread, vm_java_lang_Thread_daemon) != 0;
 }
 
+unsigned int vm_nr_threads_running(void)
+{
+       unsigned int nr;
+
+       pthread_mutex_lock(&threads_mutex);
+       nr = nr_threads_running;
+       pthread_mutex_unlock(&threads_mutex);
+
+       return nr;
+}
+
+static void update_thread_count(enum vm_thread_state state)
+{
+       pthread_mutex_lock(&threads_mutex);
+
+       switch (state) {
+       case VM_THREAD_STATE_NEW:
+               break;
+       case VM_THREAD_STATE_RUNNABLE:
+               nr_threads_running++;
+               break;
+       case VM_THREAD_STATE_BLOCKED:
+       case VM_THREAD_STATE_TERMINATED:
+       case VM_THREAD_STATE_TIMED_WAITING:
+       case VM_THREAD_STATE_WAITING:
+               nr_threads_running--;
+               break;
+       }
+
+       pthread_mutex_unlock(&threads_mutex);
+}
+
 void vm_thread_set_state(struct vm_thread *thread, enum vm_thread_state state)
 {
        pthread_mutex_lock(&thread->mutex);
        thread->state = state;
        pthread_mutex_unlock(&thread->mutex);
+
+       update_thread_count(state);
 }
 
 static void vm_thread_attach_thread(struct vm_thread *thread)
 {
        pthread_mutex_lock(&threads_mutex);
        list_add(&thread->list_node, &thread_list);
+       nr_threads_running++;
        pthread_mutex_unlock(&threads_mutex);
 
        gc_attach_thread();
-- 
1.5.6.3


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Jatovm-devel mailing list
Jatovm-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jatovm-devel

Reply via email to