Signed-off-by: Vegard Nossum <vegard.nos...@gmail.com>
---
 include/vm/gc.h        |    4 ++++
 test/arch-x86/Makefile |    1 +
 test/jit/Makefile      |    1 +
 test/vm/gc-stub.c      |    9 +++++++++
 vm/gc.c                |   25 +++++++++++++++++++++++++
 vm/thread.c            |    5 +++++
 6 files changed, 45 insertions(+), 0 deletions(-)
 create mode 100644 test/vm/gc-stub.c

diff --git a/include/vm/gc.h b/include/vm/gc.h
index 82766ca..b833219 100644
--- a/include/vm/gc.h
+++ b/include/vm/gc.h
@@ -4,6 +4,10 @@
 extern void *gc_safepoint_page;
 
 void gc_init(void);
+
+void gc_attach_thread(void);
+void gc_detach_thread(void);
+
 void gc_safepoint(void);
 
 #endif
diff --git a/test/arch-x86/Makefile b/test/arch-x86/Makefile
index 72bf355..b28e62a 100644
--- a/test/arch-x86/Makefile
+++ b/test/arch-x86/Makefile
@@ -79,6 +79,7 @@ TOPLEVEL_OBJS := \
        test/jit/bytecode-to-ir-stub.o \
        test/libharness/libharness.o \
        test/vm/classloader-stub.o \
+       test/vm/gc-stub.o \
        test/vm/preload-stub.o \
        test/vm/signal-stub.o \
        test/vm/stack-trace-stub.o
diff --git a/test/jit/Makefile b/test/jit/Makefile
index 724d575..25a0966 100644
--- a/test/jit/Makefile
+++ b/test/jit/Makefile
@@ -66,6 +66,7 @@ TOPLEVEL_OBJS := \
        test/libharness/libharness.o \
        test/vm/class-stub.o \
        test/vm/classloader-stub.o \
+       test/vm/gc-stub.o \
        test/vm/object-stub.o \
        test/vm/preload-stub.o \
        test/vm/jni-stub.o \
diff --git a/test/vm/gc-stub.c b/test/vm/gc-stub.c
new file mode 100644
index 0000000..f5025f3
--- /dev/null
+++ b/test/vm/gc-stub.c
@@ -0,0 +1,9 @@
+#include "vm/gc.h"
+
+void gc_attach_thread(void)
+{
+}
+
+void gc_detach_thread(void)
+{
+}
diff --git a/vm/gc.c b/vm/gc.c
index c1e6f42..40f8f2f 100644
--- a/vm/gc.c
+++ b/vm/gc.c
@@ -1,9 +1,18 @@
+#include <assert.h>
+#include <stdio.h>
+#include <pthread.h>
+
 #include "lib/guard-page.h"
 #include "vm/die.h"
 #include "vm/gc.h"
 
 void *gc_safepoint_page;
 
+static pthread_mutex_t safepoint_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+/* Protected by safepoint_mutex */
+static unsigned int nr_threads = 0;
+
 void gc_init(void)
 {
        gc_safepoint_page = alloc_guard_page(false);
@@ -11,6 +20,22 @@ void gc_init(void)
                die("Couldn't allocate GC safepoint guard page");
 }
 
+void gc_attach_thread(void)
+{
+       pthread_mutex_lock(&safepoint_mutex);
+       ++nr_threads;
+       pthread_mutex_unlock(&safepoint_mutex);
+}
+
+void gc_detach_thread(void)
+{
+       assert(nr_threads > 0);
+
+       pthread_mutex_lock(&safepoint_mutex);
+       --nr_threads;
+       pthread_mutex_unlock(&safepoint_mutex);
+}
+
 void gc_safepoint(void)
 {
 }
diff --git a/vm/thread.c b/vm/thread.c
index 38d77d3..e752b2a 100644
--- a/vm/thread.c
+++ b/vm/thread.c
@@ -27,6 +27,7 @@
 #include "vm/call.h"
 #include "vm/class.h"
 #include "vm/die.h"
+#include "vm/gc.h"
 #include "vm/object.h"
 #include "vm/preload.h"
 #include "vm/signal.h"
@@ -114,12 +115,16 @@ static void vm_thread_attach_thread(struct vm_thread 
*thread)
        pthread_mutex_lock(&threads_mutex);
        list_add(&thread->list_node, &thread_list);
        pthread_mutex_unlock(&threads_mutex);
+
+       gc_attach_thread();
 }
 
 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);
-- 
1.6.0.6


------------------------------------------------------------------------------
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