@@ -90,7 +90,7 @@ void* allocate(VM* vm, size_t size) {
91void* allocCon(VM* vm, int arity) {
91void* allocCon(VM* vm, int arity) {
92 Closure* cl = allocate(vm, sizeof(Closure) + sizeof(VAL)*arity);
92 Closure* cl = allocate(vm, sizeof(Closure) + sizeof(VAL)*arity);
93− cl -> ty = CON;
93+ SETTY(cl, CON);
94 if (arity == 0) {
94 if (arity == 0) {
95 cl -> info.c.args = NULL;
95 cl -> info.c.args = NULL;
@@ -104,7 +104,7 @@ void* allocCon(VM* vm, int arity) {
105VAL MKFLOAT(VM* vm, double val) {
105VAL MKFLOAT(VM* vm, double val) {
106 Closure* cl = allocate(vm, sizeof(Closure));
106 Closure* cl = allocate(vm, sizeof(Closure));
107− cl -> ty = FLOAT;
107+ SETTY(cl, FLOAT);
108 cl -> info.f = val;
108 cl -> info.f = val;
109 return cl;
109 return cl;
@@ -112,7 +112,7 @@ VAL MKFLOAT(VM* vm, double val) {
112VAL MKSTR(VM* vm, char* str) {
112VAL MKSTR(VM* vm, char* str) {
113 Closure* cl = allocate(vm, sizeof(Closure) + // Type) + sizeof(char*) +
113 Closure* cl = allocate(vm, sizeof(Closure) + // Type) + sizeof(char*) +
114 sizeof(char)*strlen(str)+1);
114 sizeof(char)*strlen(str)+1);
115− cl -> ty = STRING;
115+ SETTY(cl, STRING);
116 cl -> info.str = (char*)cl + sizeof(Closure);
116 cl -> info.str = (char*)cl + sizeof(Closure);
118 strcpy(cl -> info.str, str);
118 strcpy(cl -> info.str, str);
@@ -121,7 +121,7 @@ VAL MKSTR(VM* vm, char* str) {
122VAL MKPTR(VM* vm, void* ptr) {
122VAL MKPTR(VM* vm, void* ptr) {
123 Closure* cl = allocate(vm, sizeof(Closure));
123 Closure* cl = allocate(vm, sizeof(Closure));
124− cl -> ty = PTR;
124+ SETTY(cl, PTR);
125 cl -> info.ptr = ptr;
125 cl -> info.ptr = ptr;
126 return cl;
126 return cl;
@@ -185,7 +185,7 @@ void dumpVal(VAL v) {
185 printf("%d ", (int)(GETINT(v)));
185 printf("%d ", (int)(GETINT(v)));
188− switch(v->ty) {
188+ switch(GETTY(v)) {
189 case CON:
189 case CON:
190 printf("%d[", v->info.c.tag);
190 printf("%d[", v->info.c.tag);
191 for(i = 0; i < v->info.c.arity; ++i) {
191 for(i = 0; i < v->info.c.arity; ++i) {
@@ -209,7 +209,7 @@ void dumpVal(VAL v) {
210VAL idris_castIntStr(VM* vm, VAL i) {
210VAL idris_castIntStr(VM* vm, VAL i) {
211 Closure* cl = allocate(vm, sizeof(Closure) + sizeof(char)*16);
211 Closure* cl = allocate(vm, sizeof(Closure) + sizeof(char)*16);
212− cl -> ty = STRING;
212+ SETTY(cl, STRING);
213 cl -> info.str = (char*)cl + sizeof(Closure);
213 cl -> info.str = (char*)cl + sizeof(Closure);
214 sprintf(cl -> info.str, "%d", (int)(GETINT(i)));
214 sprintf(cl -> info.str, "%d", (int)(GETINT(i)));
215 return cl;
215 return cl;
@@ -226,7 +226,7 @@ VAL idris_castStrInt(VM* vm, VAL i) {
227VAL idris_castFloatStr(VM* vm, VAL i) {
227VAL idris_castFloatStr(VM* vm, VAL i) {
228 Closure* cl = allocate(vm, sizeof(Closure) + sizeof(char)*32);
228 Closure* cl = allocate(vm, sizeof(Closure) + sizeof(char)*32);
229− cl -> ty = STRING;
229+ SETTY(cl, STRING);
230 cl -> info.str = (char*)cl + sizeof(Closure);
230 cl -> info.str = (char*)cl + sizeof(Closure);
231 sprintf(cl -> info.str, "%g", GETFLOAT(i));
231 sprintf(cl -> info.str, "%g", GETFLOAT(i));
232 return cl;
232 return cl;
@@ -242,7 +242,7 @@ VAL idris_concat(VM* vm, VAL l, VAL r) {
242 // dumpVal(l);
242 // dumpVal(l);
243 // printf("\n");
243 // printf("\n");
244 Closure* cl = allocate(vm, sizeof(Closure) + strlen(ls) + strlen(rs) + 1);
244 Closure* cl = allocate(vm, sizeof(Closure) + strlen(ls) + strlen(rs) + 1);
245− cl -> ty = STRING;
245+ SETTY(cl, STRING);
246 cl -> info.str = (char*)cl + sizeof(Closure);
246 cl -> info.str = (char*)cl + sizeof(Closure);
247 strcpy(cl -> info.str, ls);
247 strcpy(cl -> info.str, ls);
248 strcat(cl -> info.str, rs);
248 strcat(cl -> info.str, rs);
@@ -317,7 +317,7 @@ VAL idris_strCons(VM* vm, VAL x, VAL xs) {
317 char *xstr = GETSTR(xs);
317 char *xstr = GETSTR(xs);
318 Closure* cl = allocate(vm, sizeof(Closure) +
318 Closure* cl = allocate(vm, sizeof(Closure) +
319 strlen(xstr) + 2);
319 strlen(xstr) + 2);
320− cl -> ty = STRING;
320+ SETTY(cl, STRING);
321 cl -> info.str = (char*)cl + sizeof(Closure);
321 cl -> info.str = (char*)cl + sizeof(Closure);
322 cl -> info.str[0] = (char)(GETINT(x));
322 cl -> info.str[0] = (char)(GETINT(x));
323 strcpy(cl -> info.str+1, xstr);
323 strcpy(cl -> info.str+1, xstr);
@@ -332,7 +332,7 @@ VAL idris_strRev(VM* vm, VAL str) {
332 char *xstr = GETSTR(str);
332 char *xstr = GETSTR(str);
333 Closure* cl = allocate(vm, sizeof(Closure) +
333 Closure* cl = allocate(vm, sizeof(Closure) +
334 strlen(xstr) + 1);
334 strlen(xstr) + 1);
335− cl -> ty = STRING;
335+ SETTY(cl, STRING);
336 cl -> info.str = (char*)cl + sizeof(Closure);
336 cl -> info.str = (char*)cl + sizeof(Closure);
337 int y = 0;
337 int y = 0;
338 int x = strlen(xstr);
338 int x = strlen(xstr);
@@ -379,7 +379,7 @@ void* vmThread(VM* callvm, func f, VAL arg) {
379 td->arg = copyTo(vm, arg);
379 td->arg = copyTo(vm, arg);
381 pthread_create(&t, &attr, runThread, td);
381 pthread_create(&t, &attr, runThread, td);
382− usleep(100);
382+// usleep(100);
383 return vm;
383 return vm;
@@ -393,7 +393,7 @@ VAL copyTo(VM* vm, VAL x) {
393 if (x==NULL || ISINT(x)) {
393 if (x==NULL || ISINT(x)) {
394 return x;
394 return x;
396− switch(x->ty) {
396+ switch(GETTY(x)) {
397 case CON:
397 case CON:
398 cl = allocCon(vm, x->info.c.arity);
398 cl = allocCon(vm, x->info.c.arity);
399 cl->info.c.tag = x->info.c.tag;
399 cl->info.c.tag = x->info.c.tag;
@@ -430,6 +430,8 @@ void sendMessage(VM* sender, VM* dest, VAL msg) {
431 VAL dmsg = copyTo(dest, msg);
431 VAL dmsg = copyTo(dest, msg);
434+// printf("Sending [lock]...\n");
433 pthread_mutex_lock(&(dest->inbox_lock));
435 pthread_mutex_lock(&(dest->inbox_lock));
435 *(dest->inbox_write) = dmsg;
437 *(dest->inbox_write) = dmsg;
@@ -445,21 +447,36 @@ void sendMessage(VM* sender, VM* dest, VAL msg) {
447 // Wake up the other thread
449 // Wake up the other thread
450+ pthread_mutex_lock(&dest->inbox_block);
448− pthread_cond_signal(&(dest->inbox_waiting));
451+ pthread_cond_signal(&dest->inbox_waiting);
452+ pthread_mutex_unlock(&dest->inbox_block);
454+// printf("Sending [signalled]...\n");
450 pthread_mutex_unlock(&(dest->inbox_lock));
456 pthread_mutex_unlock(&(dest->inbox_lock));
457+// printf("Sending [unlock]...\n");
453// block until there is a message in the queue
460// block until there is a message in the queue
454VAL recvMessage(VM* vm) {
461VAL recvMessage(VM* vm) {
455 VAL msg = NULL;
462 VAL msg = NULL;
463+ struct timespec timeout;
466+ pthread_mutex_lock(&vm->inbox_block);
467+ msg = *(vm->inbox_ptr);
457 while (msg == NULL) {
468 while (msg == NULL) {
458− pthread_mutex_lock(&vm->inbox_block);
469+// printf("No message yet\n");
470+// printf("Waiting [lock]...\n");
471+ timeout.tv_sec = time (NULL) + 3;
472+ timeout.tv_nsec = 0;
459− pthread_cond_wait(&vm->inbox_waiting, &vm->inbox_block);
473+ status = pthread_cond_timedwait(&vm->inbox_waiting, &vm->inbox_block,
460− pthread_mutex_unlock(&vm->inbox_block);
474+ &timeout);
475+// printf("Waiting [unlock]... %d\n", status);
461 msg = *(vm->inbox_ptr);
476 msg = *(vm->inbox_ptr);
478+ pthread_mutex_unlock(&vm->inbox_block);
463 if (msg != NULL) {
480 if (msg != NULL) {
464 pthread_mutex_lock(&(vm->inbox_lock));
481 pthread_mutex_lock(&(vm->inbox_lock));
465 *(vm->inbox_ptr) = NULL;
482 *(vm->inbox_ptr) = NULL;