on master help sign in
Basic concurrency now working

(But with an almighty hack that I don't understand... calling usleep after creating a thread appears necessary otherwise a bus error occurs seemingly non-deterministically)

commit3b272845b4c033dd8d70af23eb3475211449d123 authorEdwin Brady <eb@cs.st-andrews.ac.uk> dateSat 22 Sep 2012, 16:12 (14 years ago) parentc02a58c
2 files changed+17−16
rts/idris_rts.c+16−15 src/IRTS/CodegenC.hs+1−1
rts/idris_rts.c+16−15
@@ -183,6 +183,9 @@ void dumpVal(VAL v) {
183183 }
184184 printf("] ");
185185 break;
186+ case STRING:
187+ printf("STR[%s]", v->info.str);
188+ break;
186189 case FWD:
187190 printf("FWD ");
188191 dumpVal((VAL)(v->info.ptr));
@@ -340,14 +343,11 @@ void* runThread(void* arg) {
340343 ThreadData* td = (ThreadData*)arg;
341344 VM* vm = td->vm;
342345
343− printf("We're off\n");
344−
345346 TOP(0) = td->arg;
346347 BASETOP(0);
347348 ADDTOP(1);
348349 td->fn(vm, NULL);
349350
350− printf("We're done\n");
351351 free(td);
352352}
353353
@@ -358,20 +358,16 @@ void* vmThread(VM* callvm, func f, VAL arg) {
358358 size_t stacksize;
359359
360360 pthread_attr_init(&attr);
361− pthread_attr_getstacksize (&attr, &stacksize);
362− pthread_attr_setstacksize (&attr, stacksize*64);
363− printf("Default stack size = %li\n", stacksize);
361+// pthread_attr_getstacksize (&attr, &stacksize);
362+// pthread_attr_setstacksize (&attr, stacksize*64);
364363
365364 ThreadData *td = malloc(sizeof(ThreadData));
366365 td->vm = vm;
367366 td->fn = f;
368367 td->arg = copyTo(vm, arg);
369368
370− dumpVal(arg);
371− dumpVal(td->arg);
372−
373− printf("Threading\n");
374369 pthread_create(&t, &attr, runThread, td);
370+ usleep(100);
375371 return vm;
376372}
377373
@@ -419,10 +415,11 @@ VAL copyTo(VM* vm, VAL x) {
419415// Add a message to another VM's message queue
420416void sendMessage(VM* sender, VM* dest, VAL msg) {
421417 VAL dmsg = copyTo(dest, msg);
418+
422419 pthread_mutex_lock(&(dest->inbox_lock));
423420
424421 *(dest->inbox_write) = dmsg;
425−
422+
426423 dest->inbox_write++;
427424 if (dest->inbox_write >= dest->inbox_end) {
428425 dest->inbox_write = dest->inbox;
@@ -441,10 +438,14 @@ void sendMessage(VM* sender, VM* dest, VAL msg) {
441438
442439// block until there is a message in the queue
443440VAL recvMessage(VM* vm) {
444− pthread_mutex_lock(&vm->inbox_block);
445− pthread_cond_wait(&vm->inbox_waiting, &vm->inbox_block);
441+ VAL msg = NULL;
446442
447− VAL msg = *(vm->inbox_ptr);
443+ while (msg == NULL) {
444+ pthread_mutex_lock(&vm->inbox_block);
445+ pthread_cond_wait(&vm->inbox_waiting, &vm->inbox_block);
446+ pthread_mutex_unlock(&vm->inbox_block);
447+ msg = *(vm->inbox_ptr);
448+ }
448449 if (msg != NULL) {
449450 pthread_mutex_lock(&(vm->inbox_lock));
450451 *(vm->inbox_ptr) = NULL;
@@ -458,7 +459,7 @@ VAL recvMessage(VM* vm) {
458459 exit(-1);
459460 }
460461
461− pthread_mutex_unlock(&vm->inbox_block);
462+ return msg;
462463}
463464
464465void stackOverflow() {
src/IRTS/CodegenC.hs+1−1
@@ -237,7 +237,7 @@ doOp v LStdIn [] = v ++ "MKPTR(vm, stdin)"
237237doOp v LStdOut [] = v ++ "MKPTR(vm, stdout)"
238238doOp v LStdErr [] = v ++ "MKPTR(vm, stderr)"
239239
240−doOp v LFork [x] = v ++ "vmThread(vm, " ++ cname (MN 0 "EVAL") ++ ", " ++ creg x ++ ")"
240+doOp v LFork [x] = v ++ "MKPTR(vm, vmThread(vm, " ++ cname (MN 0 "EVAL") ++ ", " ++ creg x ++ "))"
241241doOp v LVMPtr [] = v ++ "MKPTR(vm, vm)"
242242doOp v LNoOp [x] = ""
243243doOp _ _ _ = "FAIL"