[MLton-commit] r6446
spoons at mlton.org
spoons at mlton.org
Mon Mar 3 07:32:03 PST 2008
Functions used by SML parallel library.
These functions are imported into the SML portion of the
parallel library. They provide synchronization support (locks
and atomic integer operations) as well as informational and
profiling data.
----------------------------------------------------------------------
A mlton/branches/shared-heap-multicore/runtime/gc/parallel.c
A mlton/branches/shared-heap-multicore/runtime/gc/parallel.h
U mlton/branches/shared-heap-multicore/runtime/gc.c
U mlton/branches/shared-heap-multicore/runtime/gc.h
----------------------------------------------------------------------
Added: mlton/branches/shared-heap-multicore/runtime/gc/parallel.c
===================================================================
--- mlton/branches/shared-heap-multicore/runtime/gc/parallel.c 2008-03-03 15:31:16 UTC (rev 6445)
+++ mlton/branches/shared-heap-multicore/runtime/gc/parallel.c 2008-03-03 15:32:02 UTC (rev 6446)
@@ -0,0 +1,121 @@
+
+#include <pthread.h>
+#include <time.h>
+#include "platform.h"
+
+/* num of holding thread or -1 if no one*/
+volatile int32_t *Parallel_mutexes;
+
+void Parallel_init (void) {
+ GC_state s = pthread_getspecific (gcstate_key);
+
+ if (!Proc_isInitialized (s)) {
+ Parallel_mutexes = (int32_t *) malloc (s->numberOfProcs * sizeof (int32_t));
+
+ /* Set up call-back state in each worker thread */
+ /* XXX hack copy the call-from-c-handler into the worker threads
+ assumes this is called by the primary thread */
+ for (int proc = 0; proc < s->numberOfProcs; proc++) {
+ s->procStates[proc].callFromCHandlerThread = pointerToObjptr(
+ GC_copyThread (s, objptrToPointer(s->callFromCHandlerThread,
+ s->heap->start)),
+ s->heap->start);
+
+ Parallel_mutexes[proc] = -1;
+ }
+ /* Now wake them up! */
+ Proc_signalInitialization (s);
+ }
+}
+
+Int32 Parallel_processorNumber (void) {
+ GC_state s = pthread_getspecific (gcstate_key);
+ return Proc_processorNumber (s);
+}
+
+Int32 Parallel_numberOfProcessors (void) {
+ GC_state s = pthread_getspecific (gcstate_key);
+ return s->numberOfProcs;
+}
+
+
+Word64 Parallel_maxBytesLive (void) {
+ GC_state s = pthread_getspecific (gcstate_key);
+ return (uint64_t)s->cumulativeStatistics->maxBytesLiveSinceReset;
+}
+
+void Parallel_resetBytesLive (void) {
+ GC_state s = pthread_getspecific (gcstate_key);
+ s->cumulativeStatistics->maxBytesLiveSinceReset = 0;
+}
+
+
+static void maybeWaitForGC (GC_state s) {
+ if (Proc_threadInSection (s)) {
+ //fprintf (stderr, "waiting for gc [%d]\n", Proc_processorNumber (s));
+
+ /* XXX hack? */
+ ENTER0 (s);
+ LEAVE0 (s);
+ }
+}
+
+//struct rusage ru_lock;
+
+void Parallel_lock (Int32 p) {
+ GC_state s = pthread_getspecific (gcstate_key);
+ int32_t myNumber = Proc_processorNumber (s);
+
+ //fprintf (stderr, "lock\n");
+
+ /*
+ if (needGCTime (s))
+ startTiming (&ru_lock);
+ */
+
+ do {
+ AGAIN:
+ maybeWaitForGC (s);
+ if (Parallel_mutexes[p] >= 0)
+ goto AGAIN;
+ } while (not __sync_bool_compare_and_swap (&Parallel_mutexes[p],
+ -1,
+ myNumber));
+ /*
+ if (needGCTime (s))
+ stopTiming (&ru_lock, &s->cumulativeStatistics->ru_lock);
+ */
+}
+
+void Parallel_unlock (Int32 p) {
+ GC_state s = pthread_getspecific (gcstate_key);
+ int32_t myNumber = Proc_processorNumber (s);
+
+ //fprintf (stderr, "unlock %d\n", Parallel_holdingMutex);
+
+ if (not __sync_bool_compare_and_swap (&Parallel_mutexes[p],
+ myNumber,
+ -1)) {
+ fprintf (stderr, "can't unlock if you don't hold the lock\n");
+ }
+}
+
+Int32 Parallel_fetchAndAdd (pointer p, Int32 v) {
+ //fprintf (stderr, "fetchAndAdd\n");
+ /*
+ Int32 res = __sync_fetch_and_add ((Int32 *)p, v);
+ asm volatile ("mfence");
+ */
+
+ asm volatile ("lock; xaddl %0,%1"
+ : "+q" (v) // output
+ : "m" (*p) // input
+ : "memory"); // clobbered
+ // asm volatile ("mfence");
+
+ return v;
+}
+
+bool Parallel_compareAndSwap (pointer p, Int32 old, Int32 new) {
+ return __sync_bool_compare_and_swap ((Int32 *)p, old, new);
+}
Added: mlton/branches/shared-heap-multicore/runtime/gc/parallel.h
===================================================================
--- mlton/branches/shared-heap-multicore/runtime/gc/parallel.h 2008-03-03 15:31:16 UTC (rev 6445)
+++ mlton/branches/shared-heap-multicore/runtime/gc/parallel.h 2008-03-03 15:32:02 UTC (rev 6446)
@@ -0,0 +1,18 @@
+
+#if (defined (MLTON_GC_INTERNAL_BASIS))
+
+void Parallel_init (void);
+
+void Parallel_lock (Int32);
+void Parallel_unlock (Int32);
+
+Int32 Parallel_processorNumber (void);
+Int32 Parallel_numberOfProcessors (void);
+Word64 Parallel_maxBytesLive (void);
+void Parallel_resetBytesLive (void);
+
+Int32 Parallel_fetchAndAdd (pointer p, Int32 v);
+bool Parallel_compareAndSwap (pointer p, Int32 old, Int32 new);
+
+#endif /* (defined (MLTON_GC_INTERNAL_BASIS)) */
+
Modified: mlton/branches/shared-heap-multicore/runtime/gc.c
===================================================================
--- mlton/branches/shared-heap-multicore/runtime/gc.c 2008-03-03 15:31:16 UTC (rev 6445)
+++ mlton/branches/shared-heap-multicore/runtime/gc.c 2008-03-03 15:32:02 UTC (rev 6446)
@@ -51,6 +51,7 @@
#include "gc/object.c"
#include "gc/objptr.c"
#include "gc/pack.c"
+#include "gc/parallel.c"
#include "gc/pointer.c"
#include "gc/profiling.c"
#include "gc/rusage.c"
Modified: mlton/branches/shared-heap-multicore/runtime/gc.h
===================================================================
--- mlton/branches/shared-heap-multicore/runtime/gc.h 2008-03-03 15:31:16 UTC (rev 6445)
+++ mlton/branches/shared-heap-multicore/runtime/gc.h 2008-03-03 15:32:02 UTC (rev 6446)
@@ -80,5 +80,7 @@
#include "gc/pack.h"
#include "gc/size.h"
#include "gc/share.h"
+#include "gc/parallel.h"
+#include "gc/processor.h"
#endif /* _MLTON_GC_H_ */
More information about the MLton-commit
mailing list