[pypy-commit] stmgc default: merge

Raemi noreply at buildbot.pypy.org
Fri Jul 5 09:46:24 CEST 2013


Author: Remi Meier <meierrem at student.ethz.ch>
Branch: 
Changeset: r350:93309df73f62
Date: 2013-07-05 09:46 +0200
http://bitbucket.org/pypy/stmgc/changeset/93309df73f62/

Log:	merge

diff --git a/duhton/duhton.c b/duhton/duhton.c
--- a/duhton/duhton.c
+++ b/duhton/duhton.c
@@ -1,20 +1,40 @@
 #include "duhton.h"
 
+#define DEFAULT_NUM_THREADS 4
 
 int main(int argc, char **argv)
 {
-    char *filename;
-    int interactive;
-    if (argc <= 1) {
+    char *filename = NULL;
+    int interactive = 1;
+	int i;
+	int num_threads = DEFAULT_NUM_THREADS;
+
+	for (i = 1; i < argc; ++i) {
+		if (strcmp(argv[i], "--help") == 0) {
+			printf("Duhton: a simple lisp-like language with STM support\n\n");
+			printf("Usage: duhton [--help] [--num-threads no] [filename]\n");
+			printf("  --help: this help\n");
+			printf("  --num-threads <number>: number of threads (default 4)\n\n");
+			exit(0);
+		} else if (strcmp(argv[i], "--num-threads") == 0) {
+			if (i == argc - 1) {
+				printf("ERROR: --num-threads requires a parameter\n");
+				exit(1);
+			}
+			num_threads = atoi(argv[i + 1]);
+			i++;
+		} else if (strncmp(argv[i], "--", 2) == 0) {
+			printf("ERROR: unrecognized parameter %s\n", argv[i]);
+		} else {
+			filename = argv[i];
+			interactive = 0;
+		}
+	}
+    if (!filename) {
         filename = "-";   /* stdin */
-        interactive = 1;
-    }
-    else {
-        filename = argv[1];
-        interactive = 0;
-    }
+	}
 
-    Du_Initialize();
+    Du_Initialize(num_threads);
 
     while (1) {
         if (interactive) {
diff --git a/duhton/duhton.h b/duhton/duhton.h
--- a/duhton/duhton.h
+++ b/duhton/duhton.h
@@ -138,7 +138,7 @@
                             DuObject *rest, int execute_now);
 DuObject *_Du_GetGlobals(void);
 
-void Du_Initialize(void);
+void Du_Initialize(int);
 void Du_Finalize(void);
 #define Du_Globals        (_Du_GetGlobals())
 
@@ -182,5 +182,7 @@
 }
 #endif
 
+extern pthread_t *all_threads;
+extern int all_threads_count;
 
 #endif  /* _DUHTON_H_ */
diff --git a/duhton/glob.c b/duhton/glob.c
--- a/duhton/glob.c
+++ b/duhton/glob.c
@@ -1,6 +1,8 @@
 #include "duhton.h"
 #include <sys/select.h>
 
+pthread_t *all_threads;
+int all_threads_count;
 
 static void _du_getargs1(const char *name, DuObject *cons, DuObject *locals,
                          DuObject **a)
@@ -561,9 +563,11 @@
     return Du_None;
 }
 
-void Du_Initialize(void)
+void Du_Initialize(int num_threads)
 {
     stm_initialize();
+	all_threads_count = num_threads;
+	all_threads = (pthread_t*)malloc(sizeof(pthread_t) * num_threads);
 
     DuFrame_SetBuiltinMacro(Du_Globals, "progn", Du_Progn);
     DuFrame_SetBuiltinMacro(Du_Globals, "setq", du_setq);
diff --git a/duhton/transaction.c b/duhton/transaction.c
--- a/duhton/transaction.c
+++ b/duhton/transaction.c
@@ -2,10 +2,6 @@
 #include <pthread.h>
 #include <unistd.h>
 
-#ifndef NUM_THREADS
-#define NUM_THREADS  4
-#endif
-
 
 static DuConsObject du_pending_transactions = {
     DuOBJECT_HEAD_INIT(DUTYPE_CONS),
@@ -21,15 +17,13 @@
 static void run_all_threads(void)
 {
     int i;
-    pthread_t th[NUM_THREADS];
-
-    for (i = 0; i < NUM_THREADS; i++) {
-        int status = pthread_create(&th[i], NULL, run_thread, NULL);
+    for (i = 0; i < all_threads_count; i++) {
+        int status = pthread_create(&all_threads[i], NULL, run_thread, NULL);
         if (status != 0)
             stm_fatalerror("status != 0\n");
     }
-    for (i = 0; i < NUM_THREADS; i++) {
-        pthread_join(th[i], NULL);
+    for (i = 0; i < all_threads_count; i++) {
+        pthread_join(all_threads[i], NULL);
     }
 }
 
@@ -88,13 +82,13 @@
         else {
             /* nothing to do, wait */
             thread_sleeping++;
-            if (thread_sleeping == NUM_THREADS) {
+            if (thread_sleeping == all_threads_count) {
                 pthread_mutex_unlock(&mutex_sleep);
             }
             stm_commit_transaction();
             pthread_mutex_lock(&mutex_sleep);
             stm_begin_inevitable_transaction();
-            if (thread_sleeping == NUM_THREADS) {
+            if (thread_sleeping == all_threads_count) {
                 pthread_mutex_unlock(&mutex_sleep);
                 return NULL;
             }


More information about the pypy-commit mailing list