on master help sign in
Message passing fixes in VM

requireAlloc always needs to be paried with doneAlloc or the VM may deadlock. Also, need to set the VM we're allocating on in copyTo with pthread_setspecific

commitcdfaa9f4f490a75aaa01684443bf6deba91e59ce authorEdwin Brady <ecb10@st-andrews.ac.uk> dateSat 7 Feb 2015, 22:31 (12 years ago) parentcb005bc
2 files changed+22−4
rts/idris_gmp.c+12−2 rts/idris_rts.c+10−2
rts/idris_gmp.c+12−2
@@ -27,6 +27,7 @@ VAL MKBIGC(VM* vm, char* val) {
2727 mpz_t* bigint;
2828
2929 VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0);
30+ idris_doneAlloc();
3031 bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));
3132
3233 mpz_init(*bigint);
@@ -43,6 +44,7 @@ VAL MKBIGM(VM* vm, void* big) {
4344
4445 mpz_t* bigint;
4546 VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0);
47+ idris_doneAlloc();
4648 bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));
4749
4850 mpz_init(*bigint);
@@ -59,6 +61,7 @@ VAL MKBIGMc(VM* vm, void* big) {
5961
6062 mpz_t* bigint;
6163 VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0);
64+ idris_doneAlloc();
6265 bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));
6366
6467 mpz_init_set(*bigint, *((mpz_t*)big));
@@ -74,6 +77,7 @@ VAL MKBIGUI(VM* vm, unsigned long val) {
7477
7578 mpz_t* bigint;
7679 VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0);
80+ idris_doneAlloc();
7781 bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));
7882
7983 mpz_init_set_ui(*bigint, val);
@@ -89,6 +93,7 @@ VAL MKBIGSI(VM* vm, signed long val) {
8993
9094 mpz_t* bigint;
9195 VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0);
96+ idris_doneAlloc();
9297 bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));
9398
9499 mpz_init_set_si(*bigint, val);
@@ -105,6 +110,7 @@ VAL GETBIG(VM * vm, VAL x) {
105110 if (ISINT(x)) {
106111 mpz_t* bigint;
107112 VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0);
113+ idris_doneAlloc();
108114 bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));
109115
110116 mpz_init(*bigint);
@@ -115,6 +121,7 @@ VAL GETBIG(VM * vm, VAL x) {
115121
116122 return cl;
117123 } else {
124+ idris_doneAlloc();
118125 switch(GETTY(x)) {
119126 case FWD:
120127 return GETBIG(vm, x->info.ptr);
@@ -129,6 +136,7 @@ VAL bigAdd(VM* vm, VAL x, VAL y) {
129136
130137 mpz_t* bigint;
131138 VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0);
139+ idris_doneAlloc();
132140 bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));
133141 mpz_add(*bigint, GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y)));
134142 SETTY(cl, BIGINT);
@@ -141,6 +149,7 @@ VAL bigSub(VM* vm, VAL x, VAL y) {
141149
142150 mpz_t* bigint;
143151 VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0);
152+ idris_doneAlloc();
144153 bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));
145154 mpz_sub(*bigint, GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y)));
146155 SETTY(cl, BIGINT);
@@ -153,6 +162,7 @@ VAL bigMul(VM* vm, VAL x, VAL y) {
153162
154163 mpz_t* bigint;
155164 VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0);
165+ idris_doneAlloc();
156166 bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));
157167 mpz_mul(*bigint, GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y)));
158168 SETTY(cl, BIGINT);
@@ -165,6 +175,7 @@ VAL bigDiv(VM* vm, VAL x, VAL y) {
165175
166176 mpz_t* bigint;
167177 VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0);
178+ idris_doneAlloc();
168179 bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));
169180 mpz_tdiv_q(*bigint, GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y)));
170181 SETTY(cl, BIGINT);
@@ -177,6 +188,7 @@ VAL bigMod(VM* vm, VAL x, VAL y) {
177188
178189 mpz_t* bigint;
179190 VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0);
191+ idris_doneAlloc();
180192 bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));
181193 mpz_mod(*bigint, GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y)));
182194 SETTY(cl, BIGINT);
@@ -258,8 +270,6 @@ VAL idris_bigMod(VM* vm, VAL x, VAL y) {
258270}
259271
260272int bigEqConst(VAL x, int c) {
261− idris_requireAlloc(IDRIS_MAXGMP);
262−
263273 if (ISINT(x)) { return (GETINT(x) == c); }
264274 else {
265275 int rv = mpz_cmp_si(GETMPZ(x), c);
rts/idris_rts.c+10−2
@@ -1005,7 +1005,7 @@ void* vmThread(VM* callvm, func f, VAL arg) {
10051005
10061006// VM is assumed to be a different vm from the one x lives on
10071007
1008−VAL copyTo(VM* vm, VAL x) {
1008+VAL doCopyTo(VM* vm, VAL x) {
10091009 int i, ar;
10101010 VAL* argptr;
10111011 Closure* cl;
@@ -1022,7 +1022,7 @@ VAL copyTo(VM* vm, VAL x) {
10221022
10231023 argptr = (VAL*)(cl->info.c.args);
10241024 for(i = 0; i < ar; ++i) {
1025− *argptr = copyTo(vm, *((VAL*)(x->info.c.args)+i)); // recursive version
1025+ *argptr = doCopyTo(vm, *((VAL*)(x->info.c.args)+i)); // recursive version
10261026 argptr++;
10271027 }
10281028 }
@@ -1063,6 +1063,14 @@ VAL copyTo(VM* vm, VAL x) {
10631063 return cl;
10641064}
10651065
1066+VAL copyTo(VM* vm, VAL x) {
1067+ VM* current = pthread_getspecific(vm_key);
1068+ pthread_setspecific(vm_key, vm);
1069+ VAL ret = doCopyTo(vm, x);
1070+ pthread_setspecific(vm_key, current);
1071+ return ret;
1072+}
1073+
10661074// Add a message to another VM's message queue
10671075void idris_sendMessage(VM* sender, VM* dest, VAL msg) {
10681076 // FIXME: If GC kicks in in the middle of the copy, we're in trouble.