We need this to implement GC during which threads
should not be created nor deleted.

Signed-off-by: Tomek Grabiec <tgrab...@gmail.com>
---
 include/vm/thread.h |    2 ++
 vm/thread.c         |   37 ++++++++++++++++++++++++++++++-------
 2 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/include/vm/thread.h b/include/vm/thread.h
index f6e6551..e4ac546 100644
--- a/include/vm/thread.h
+++ b/include/vm/thread.h
@@ -59,5 +59,7 @@ char *vm_thread_get_name(struct vm_thread *thread);
 bool vm_thread_is_interrupted(struct vm_thread *thread);
 bool vm_thread_interrupted(struct vm_thread *thread);
 void vm_thread_interrupt(struct vm_thread *thread);
+void vm_lock_thread_count(void);
+void vm_unlock_thread_count(void);
 
 #endif
diff --git a/vm/thread.c b/vm/thread.c
index a0770b1..76e4f2e 100644
--- a/vm/thread.c
+++ b/vm/thread.c
@@ -58,6 +58,9 @@ static struct list_head thread_list;
 
 #define thread_for_each(this) list_for_each_entry(this, &thread_list, 
list_node)
 
+static bool thread_count_locked;
+static pthread_cond_t thread_count_lock_cond = PTHREAD_COND_INITIALIZER;
+
 static void vm_thread_free(struct vm_thread *thread)
 {
        free(thread);
@@ -150,29 +153,23 @@ void vm_thread_set_state(struct vm_thread *thread, enum 
vm_thread_state 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();
 }
 
+/* The caller must hold threads_mutex */
 static void vm_thread_detach_thread(struct vm_thread *thread)
 {
        vm_thread_set_state(thread, VM_THREAD_STATE_TERMINATED);
 
        gc_detach_thread();
-
-       pthread_mutex_lock(&threads_mutex);
-
        list_del(&thread->list_node);
 
        if (!vm_thread_is_daemon(thread))
                nr_non_daemons--;
 
        pthread_cond_broadcast(&thread_terminate_cond);
-       pthread_mutex_unlock(&threads_mutex);
 }
 
 int init_threading(void)
@@ -254,7 +251,12 @@ static void *vm_thread_entry(void *arg)
        if (exception_occurred())
                vm_print_exception(exception_occurred());
 
+       pthread_mutex_lock(&threads_mutex);
+       while (thread_count_locked)
+               pthread_cond_wait(&thread_count_lock_cond, &threads_mutex);
+
        vm_thread_detach_thread(vm_thread_self());
+       pthread_mutex_unlock(&threads_mutex);
 
        return NULL;
 }
@@ -284,13 +286,19 @@ int vm_thread_start(struct vm_object *vmthread)
                pthread_mutex_unlock(&threads_mutex);
        }
 
+       pthread_mutex_lock(&threads_mutex);
+       while (thread_count_locked)
+               pthread_cond_wait(&thread_count_lock_cond, &threads_mutex);
+
        vm_thread_attach_thread(thread);
 
        if (pthread_create(&thread->posix_id, NULL, &vm_thread_entry, thread)) {
                vm_thread_detach_thread(thread);
+               pthread_mutex_unlock(&threads_mutex);
                return -1;
        }
 
+       pthread_mutex_unlock(&threads_mutex);
        return 0;
 }
 
@@ -359,3 +367,18 @@ void vm_thread_interrupt(struct vm_thread *thread)
        pthread_cond_broadcast(&mon->cond);
        pthread_mutex_unlock(&mon->mutex);
 }
+
+void vm_lock_thread_count(void)
+{
+       pthread_mutex_lock(&threads_mutex);
+       thread_count_locked = true;
+       pthread_mutex_unlock(&threads_mutex);
+}
+
+void vm_unlock_thread_count(void)
+{
+       pthread_mutex_lock(&threads_mutex);
+       thread_count_locked = false;
+       pthread_cond_broadcast(&thread_count_lock_cond);
+       pthread_mutex_unlock(&threads_mutex);
+}
-- 
1.6.0.4


------------------------------------------------------------------------------
Return on Information:
Google Enterprise Search pays you back
Get the facts.
http://p.sf.net/sfu/google-dev2dev
_______________________________________________
Jatovm-devel mailing list
Jatovm-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jatovm-devel

Reply via email to