on master help sign in
modify RTS so that it can be built single-threaded if pthreads are not available.
commitc399b55e072159e157ccf77c754fdc0e69901fb6 authorstepcut <jeremy@n-heptane.com> dateSat 8 Mar 2014, 15:50 (13 years ago) parent780a0a6
4 files changed+33−11
config.mk+1 rts/idris_gc.c+2−1 rts/idris_rts.c+21−8 rts/idris_rts.h+9−2
config.mk+1
@@ -36,3 +36,4 @@ else
3636endif
3737endif
3838
39+#define HAS_PTHREAD
rts/idris_gc.c+2−1
@@ -111,10 +111,11 @@ void idris_gc(VM* vm) {
111111 for(root = vm->valstack; root < vm->valstack_top; ++root) {
112112 *root = copy(vm, *root);
113113 }
114+#ifdef HAS_PTHREAD
114115 for(root = vm->inbox_ptr; root < vm->inbox_write; ++root) {
115116 *root = copy(vm, *root);
116117 }
117−
118+#endif
118119 vm->ret = copy(vm, vm->ret);
119120 vm->reg1 = copy(vm, vm->reg1);
120121
rts/idris_rts.c+21−8
@@ -4,7 +4,9 @@
44#include <unistd.h>
55#include <stdarg.h>
66#include <assert.h>
7+#ifdef HAS_PTHREAD
78#include <pthread.h>
9+#endif
810
911#include "idris_rts.h"
1012#include "idris_gc.h"
@@ -30,7 +32,7 @@ VM* init_vm(int stack_size, size_t heap_size,
3032
3133 vm->ret = NULL;
3234 vm->reg1 = NULL;
33−
35+#ifdef HAS_PTHREAD
3436 vm->inbox = malloc(1024*sizeof(VAL));
3537 memset(vm->inbox, 0, 1024*sizeof(VAL));
3638 vm->inbox_end = vm->inbox + 1024;
@@ -44,7 +46,7 @@ VM* init_vm(int stack_size, size_t heap_size,
4446
4547 vm->max_threads = max_threads;
4648 vm->processes = 0;
47−
49+#endif
4850 STATS_LEAVE_INIT(vm->stats)
4951 return vm;
5052}
@@ -52,14 +54,16 @@ VM* init_vm(int stack_size, size_t heap_size,
5254Stats terminate(VM* vm) {
5355 Stats stats = vm->stats;
5456 STATS_ENTER_EXIT(stats)
55−
57+#ifdef HAS_PTHREAD
5658 free(vm->inbox);
59+#endif
5760 free(vm->valstack);
5861 free_heap(&(vm->heap));
59−
62+#ifdef HAS_PTHREAD
6063 pthread_mutex_destroy(&(vm -> inbox_lock));
6164 pthread_mutex_destroy(&(vm -> inbox_block));
6265 pthread_cond_destroy(&(vm -> inbox_waiting));
66+#endif
6367 free(vm);
6468
6569 STATS_LEAVE_EXIT(stats)
@@ -70,18 +74,21 @@ void idris_requireAlloc(VM* vm, size_t size) {
7074 if (!(vm->heap.next + size < vm->heap.end)) {
7175 idris_gc(vm);
7276 }
73−
77+#ifdef HAS_PTHREAD
7478 int lock = vm->processes > 0;
7579 if (lock) { // not message passing
7680 pthread_mutex_lock(&vm->alloc_lock);
7781 }
82+#endif
7883}
7984
8085void idris_doneAlloc(VM* vm) {
86+#ifdef HAS_PTHREAD
8187 int lock = vm->processes > 0;
8288 if (lock) { // not message passing
8389 pthread_mutex_unlock(&vm->alloc_lock);
8490 }
91+#endif
8592}
8693
8794int space(VM* vm, size_t size) {
@@ -90,16 +97,18 @@ int space(VM* vm, size_t size) {
9097
9198void* allocate(VM* vm, size_t size, int outerlock) {
9299// return malloc(size);
100+#ifdef HAS_PTHREAD
93101 int lock = vm->processes > 0 && !outerlock;
94102
95103 if (lock) { // not message passing
96104 pthread_mutex_lock(&vm->alloc_lock);
97105 }
106+#endif
98107
99108 if ((size & 7)!=0) {
100109 size = 8 + ((size >> 3) << 3);
101110 }
102−
111+
103112 size_t chunk_size = size + sizeof(size_t);
104113
105114 if (vm->heap.next + chunk_size < vm->heap.end) {
@@ -111,16 +120,19 @@ void* allocate(VM* vm, size_t size, int outerlock) {
111120 assert(vm->heap.next <= vm->heap.end);
112121
113122 memset(ptr, 0, size);
114−
123+#ifdef HAS_PTHREAD
115124 if (lock) { // not message passing
116125 pthread_mutex_unlock(&vm->alloc_lock);
117126 }
127+#endif
118128 return ptr;
119129 } else {
120130 idris_gc(vm);
131+#ifdef HAS_PTHREAD
121132 if (lock) { // not message passing
122133 pthread_mutex_unlock(&vm->alloc_lock);
123134 }
135+#endif
124136 return allocate(vm, size, 0);
125137 }
126138
@@ -738,6 +750,7 @@ typedef struct {
738750 VAL arg;
739751} ThreadData;
740752
753+#ifdef HAS_PTHREAD
741754void* runThread(void* arg) {
742755 ThreadData* td = (ThreadData*)arg;
743756 VM* vm = td->vm;
@@ -927,7 +940,7 @@ VAL idris_recvMessage(VM* vm) {
927940
928941 return msg;
929942}
930−
943+#endif
931944int __idris_argc;
932945char **__idris_argv;
933946
rts/idris_rts.h+9−2
@@ -6,7 +6,9 @@
66#include <string.h>
77#include <unistd.h>
88#include <stdarg.h>
9+#ifdef HAS_PTHREAD
910#include <pthread.h>
11+#endif
1012#include <stdint.h>
1113
1214#include "idris_heap.h"
@@ -66,6 +68,11 @@ typedef struct Closure {
6668 } info;
6769} Closure;
6870
71+#if HAS_PTHREAD
72+typedef void* pthread_mutex_t;
73+typedef void* pthread_cond_t;
74+#endif
75+
6976typedef struct {
7077 VAL* valstack;
7178 VAL* valstack_top;
@@ -73,7 +80,7 @@ typedef struct {
7380 VAL* stack_max;
7481
7582 Heap heap;
76−
83+#ifdef HAS_PTHREAD
7784 pthread_mutex_t inbox_lock;
7885 pthread_mutex_t inbox_block;
7986 pthread_mutex_t alloc_lock;
@@ -87,7 +94,7 @@ typedef struct {
8794
8895 int processes; // Number of child processes
8996 int max_threads; // maximum number of threads to run in parallel
90−
97+#endif
9198 Stats stats;
9299
93100 VAL ret;