on master help sign in
Fix leak of ThreadData
73e0a98Jonas Claeson
rts/idris_rts.c+10−11
@@ -72,6 +72,7 @@ VM* init_vm(int stack_size, size_t heap_size,
72
72
73 vm->max_threads = max_threads;
73 vm->max_threads = max_threads;
74 vm->processes = 0;
74 vm->processes = 0;
75+ vm->creator = NULL;
75
76
76#else
77#else
77 global_vm = vm;
78 global_vm = vm;
@@ -784,7 +785,6 @@ VAL idris_systemInfo(VM* vm, VAL index) {
784#ifdef HAS_PTHREAD
785#ifdef HAS_PTHREAD
785typedef struct {
786typedef struct {
786 VM* vm; // thread's VM
787 VM* vm; // thread's VM
787− VM* callvm; // calling thread's VM
788 func fn;
788 func fn;
789 VAL arg;
789 VAL arg;
790} ThreadData;
790} ThreadData;
@@ -792,21 +792,17 @@ typedef struct {
792void* runThread(void* arg) {
792void* runThread(void* arg) {
793 ThreadData* td = (ThreadData*)arg;
793 ThreadData* td = (ThreadData*)arg;
794 VM* vm = td->vm;
794 VM* vm = td->vm;
795− VM* callvm = td->callvm;
795+ func fn = td->fn;
796
796
797 init_threaddata(vm);
797 init_threaddata(vm);
798
798
799 TOP(0) = td->arg;
799 TOP(0) = td->arg;
800 BASETOP(0);
800 BASETOP(0);
801 ADDTOP(1);
801 ADDTOP(1);
802− td->fn(vm, NULL);
803− callvm->processes--;
804−
805 free(td);
802 free(td);
803+ fn(vm, NULL);
806
804
807− // Stats stats =
805+ assert(0); // will not be reached
808− terminate(vm);
809− // aggregate_stats(&(td->vm->stats), &stats);
810 return NULL;
806 return NULL;
811}
807}
812
808
@@ -814,6 +810,7 @@ void* vmThread(VM* callvm, func f, VAL arg) {
814 VM* vm = init_vm(callvm->stack_max - callvm->valstack, callvm->heap.size,
810 VM* vm = init_vm(callvm->stack_max - callvm->valstack, callvm->heap.size,
815 callvm->max_threads);
811 callvm->max_threads);
816 vm->processes=1; // since it can send and receive messages
812 vm->processes=1; // since it can send and receive messages
813+ vm->creator = callvm;
817 pthread_t t;
814 pthread_t t;
818 pthread_attr_t attr;
815 pthread_attr_t attr;
819// size_t stacksize;
816// size_t stacksize;
@@ -822,9 +819,8 @@ void* vmThread(VM* callvm, func f, VAL arg) {
822// pthread_attr_getstacksize (&attr, &stacksize);
819// pthread_attr_getstacksize (&attr, &stacksize);
823// pthread_attr_setstacksize (&attr, stacksize*64);
820// pthread_attr_setstacksize (&attr, stacksize*64);
824
821
825− ThreadData *td = malloc(sizeof(ThreadData));
822+ ThreadData *td = malloc(sizeof(ThreadData)); // free'd in runThread
826 td->vm = vm;
823 td->vm = vm;
827− td->callvm = callvm;
828 td->fn = f;
824 td->fn = f;
829 td->arg = copyTo(vm, arg);
825 td->arg = copyTo(vm, arg);
830
826
@@ -841,7 +837,10 @@ void* vmThread(VM* callvm, func f, VAL arg) {
841}
837}
842
838
843void* idris_stopThread(VM* vm) {
839void* idris_stopThread(VM* vm) {
844− close_vm(vm);
840+ if (vm->creator != NULL) {
841+ vm->creator->processes--;
842+ }
843+ terminate(vm);
845 pthread_exit(NULL);
844 pthread_exit(NULL);
846 return NULL;
845 return NULL;
847}
846}
rts/idris_rts.h+1
@@ -152,6 +152,7 @@ struct VM {
152
152
153 int processes; // Number of child processes
153 int processes; // Number of child processes
154 int max_threads; // maximum number of threads to run in parallel
154 int max_threads; // maximum number of threads to run in parallel
155+ struct VM* creator; // The VM that created this VM, NULL for root VM
155#endif
156#endif
156 Stats stats;
157 Stats stats;
157
158