Original code by Pekka Enberg.

Signed-off-by: Vegard Nossum <vegard.nos...@gmail.com>
---
 include/vm/gc.h |    1 +
 vm/gc.c         |   59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/include/vm/gc.h b/include/vm/gc.h
index b833219..05e324c 100644
--- a/include/vm/gc.h
+++ b/include/vm/gc.h
@@ -8,6 +8,7 @@ void gc_init(void);
 void gc_attach_thread(void);
 void gc_detach_thread(void);
 
+void gc_start(void);
 void gc_safepoint(void);
 
 #endif
diff --git a/vm/gc.c b/vm/gc.c
index 40f8f2f..c8fc44c 100644
--- a/vm/gc.c
+++ b/vm/gc.c
@@ -1,4 +1,5 @@
 #include <assert.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <pthread.h>
 
@@ -11,7 +12,13 @@ void *gc_safepoint_page;
 static pthread_mutex_t safepoint_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /* Protected by safepoint_mutex */
+static pthread_cond_t everyone_in_cond = PTHREAD_COND_INITIALIZER; 
+static pthread_cond_t everyone_out_cond = PTHREAD_COND_INITIALIZER; 
 static unsigned int nr_threads = 0;
+static unsigned int nr_in_safepoint = 0;
+
+static pthread_cond_t can_continue_cond = PTHREAD_COND_INITIALIZER; 
+static bool can_continue;
 
 void gc_init(void)
 {
@@ -36,6 +43,58 @@ void gc_detach_thread(void)
        pthread_mutex_unlock(&safepoint_mutex);
 }
 
+static void hide_safepoint_guard_page(void)
+{
+       hide_guard_page(gc_safepoint_page);
+}
+
+static void unhide_safepoint_guard_page(void)
+{
+       unhide_guard_page(gc_safepoint_page);
+}
+
+void gc_start(void)
+{
+       pthread_mutex_lock(&safepoint_mutex);
+
+       assert(nr_in_safepoint == 0);
+
+       can_continue = false;
+       hide_safepoint_guard_page();
+
+       while (nr_in_safepoint != nr_threads)
+               pthread_cond_wait(&everyone_in_cond, &safepoint_mutex);
+
+       /* At this point, we know that everyone is in the safepoint. */
+       unhide_safepoint_guard_page();
+
+       /* TODO: Do main GC work here. */
+
+       /* Resume other threads */
+       can_continue = true;
+       pthread_cond_broadcast(&can_continue_cond);
+
+       while (nr_in_safepoint != 0)
+               pthread_cond_wait(&everyone_out_cond, &safepoint_mutex);
+
+       pthread_mutex_unlock(&safepoint_mutex);
+}
+
 void gc_safepoint(void)
 {
+       pthread_mutex_lock(&safepoint_mutex);
+
+       /* Only the GC thread will be waiting for this. */
+       if (++nr_in_safepoint == nr_threads)
+               pthread_cond_signal(&everyone_in_cond);
+
+       /* Block until GC has finished */
+       while (!can_continue)
+               pthread_cond_wait(&can_continue_cond, &safepoint_mutex);
+
+       /* Only the GC thread will be waiting for this. */
+       if (--nr_in_safepoint == 0)
+               pthread_cond_signal(&everyone_out_cond);
+
+       pthread_mutex_unlock(&safepoint_mutex);
 }
-- 
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