@@ -4,7 +4,9 @@
4#include <unistd.h>
4#include <unistd.h>
5#include <stdarg.h>
5#include <stdarg.h>
6#include <assert.h>
6#include <assert.h>
7#include <pthread.h>
8#include <pthread.h>
9#include "idris_rts.h"
11#include "idris_rts.h"
10#include "idris_gc.h"
12#include "idris_gc.h"
@@ -30,7 +32,7 @@ VM* init_vm(int stack_size, size_t heap_size,
31 vm->ret = NULL;
33 vm->ret = NULL;
32 vm->reg1 = NULL;
34 vm->reg1 = NULL;
34 vm->inbox = malloc(1024*sizeof(VAL));
36 vm->inbox = malloc(1024*sizeof(VAL));
35 memset(vm->inbox, 0, 1024*sizeof(VAL));
37 memset(vm->inbox, 0, 1024*sizeof(VAL));
36 vm->inbox_end = vm->inbox + 1024;
38 vm->inbox_end = vm->inbox + 1024;
@@ -44,7 +46,7 @@ VM* init_vm(int stack_size, size_t heap_size,
45 vm->max_threads = max_threads;
47 vm->max_threads = max_threads;
46 vm->processes = 0;
48 vm->processes = 0;
48 STATS_LEAVE_INIT(vm->stats)
50 STATS_LEAVE_INIT(vm->stats)
49 return vm;
51 return vm;
@@ -52,14 +54,16 @@ VM* init_vm(int stack_size, size_t heap_size,
52Stats terminate(VM* vm) {
54Stats terminate(VM* vm) {
53 Stats stats = vm->stats;
55 Stats stats = vm->stats;
54 STATS_ENTER_EXIT(stats)
56 STATS_ENTER_EXIT(stats)
56 free(vm->inbox);
58 free(vm->inbox);
57 free(vm->valstack);
60 free(vm->valstack);
58 free_heap(&(vm->heap));
61 free_heap(&(vm->heap));
60 pthread_mutex_destroy(&(vm -> inbox_lock));
63 pthread_mutex_destroy(&(vm -> inbox_lock));
61 pthread_mutex_destroy(&(vm -> inbox_block));
64 pthread_mutex_destroy(&(vm -> inbox_block));
62 pthread_cond_destroy(&(vm -> inbox_waiting));
65 pthread_cond_destroy(&(vm -> inbox_waiting));
65 STATS_LEAVE_EXIT(stats)
69 STATS_LEAVE_EXIT(stats)
@@ -70,18 +74,21 @@ void idris_requireAlloc(VM* vm, size_t size) {
70 if (!(vm->heap.next + size < vm->heap.end)) {
74 if (!(vm->heap.next + size < vm->heap.end)) {
71 idris_gc(vm);
75 idris_gc(vm);
74 int lock = vm->processes > 0;
78 int lock = vm->processes > 0;
75 if (lock) { // not message passing
79 if (lock) { // not message passing
76 pthread_mutex_lock(&vm->alloc_lock);
80 pthread_mutex_lock(&vm->alloc_lock);
80void idris_doneAlloc(VM* vm) {
85void idris_doneAlloc(VM* vm) {
81 int lock = vm->processes > 0;
87 int lock = vm->processes > 0;
82 if (lock) { // not message passing
88 if (lock) { // not message passing
83 pthread_mutex_unlock(&vm->alloc_lock);
89 pthread_mutex_unlock(&vm->alloc_lock);
87int space(VM* vm, size_t size) {
94int space(VM* vm, size_t size) {
@@ -90,16 +97,18 @@ int space(VM* vm, size_t size) {
91void* allocate(VM* vm, size_t size, int outerlock) {
98void* allocate(VM* vm, size_t size, int outerlock) {
92// return malloc(size);
99// return malloc(size);
93 int lock = vm->processes > 0 && !outerlock;
101 int lock = vm->processes > 0 && !outerlock;
95 if (lock) { // not message passing
103 if (lock) { // not message passing
96 pthread_mutex_lock(&vm->alloc_lock);
104 pthread_mutex_lock(&vm->alloc_lock);
99 if ((size & 7)!=0) {
108 if ((size & 7)!=0) {
100 size = 8 + ((size >> 3) << 3);
109 size = 8 + ((size >> 3) << 3);
103 size_t chunk_size = size + sizeof(size_t);
112 size_t chunk_size = size + sizeof(size_t);
105 if (vm->heap.next + chunk_size < vm->heap.end) {
114 if (vm->heap.next + chunk_size < vm->heap.end) {
@@ -111,16 +120,19 @@ void* allocate(VM* vm, size_t size, int outerlock) {
111 assert(vm->heap.next <= vm->heap.end);
120 assert(vm->heap.next <= vm->heap.end);
113 memset(ptr, 0, size);
122 memset(ptr, 0, size);
114−
123+#ifdef HAS_PTHREAD
115 if (lock) { // not message passing
124 if (lock) { // not message passing
116 pthread_mutex_unlock(&vm->alloc_lock);
125 pthread_mutex_unlock(&vm->alloc_lock);
118 return ptr;
128 return ptr;
120 idris_gc(vm);
130 idris_gc(vm);
121 if (lock) { // not message passing
132 if (lock) { // not message passing
122 pthread_mutex_unlock(&vm->alloc_lock);
133 pthread_mutex_unlock(&vm->alloc_lock);
124 return allocate(vm, size, 0);
136 return allocate(vm, size, 0);
@@ -738,6 +750,7 @@ typedef struct {
739} ThreadData;
751} ThreadData;
741void* runThread(void* arg) {
754void* runThread(void* arg) {
742 ThreadData* td = (ThreadData*)arg;
755 ThreadData* td = (ThreadData*)arg;
743 VM* vm = td->vm;
756 VM* vm = td->vm;
@@ -927,7 +940,7 @@ VAL idris_recvMessage(VM* vm) {
928 return msg;
941 return msg;
931int __idris_argc;
944int __idris_argc;
932char **__idris_argv;
945char **__idris_argv;