From d2b44ec9fc7bf4687dae74edfe708395461b5b6e Mon Sep 17 00:00:00 2001 From: glenda Date: Fri, 9 Sep 2022 21:06:16 +0000 Subject: =?UTF-8?q?Cleanup=20threads=20and=20add=20a=20=E2=8E=95TASKS=20sy?= =?UTF-8?q?stem=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- concurrency.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'concurrency.c') diff --git a/concurrency.c b/concurrency.c index be1d62c..ec75474 100644 --- a/concurrency.c +++ b/concurrency.c @@ -195,7 +195,15 @@ newprocfn(void *data) runfunc(sp->func, sp->left, sp->right); } lock(&threadlock); - /* TODO remove thread */ + for(int i = 0; i < nthreads; i++){ + if(threads[i] != td) + continue; + + for(int j = i+1; j < nthreads; j++) + threads[j-1] = threads[j]; + nthreads--; + break; + } unlock(&threadlock); freearray(sp->left); freearray(sp->right); @@ -223,3 +231,41 @@ newthreaddata(void) unlock(&threadlock); return td; } + +Array * +runningtasks(void) +{ + lock(&threadlock); + Array *tasks = allocarray(AtypeInt, 1, nthreads); + tasks->shape[0] = nthreads; + for(int i = 0; i < nthreads; i++) + tasks->intdata[i] = threads[i]->id; + unlock(&threadlock); + return tasks; +} + +Array * +taskproperty(vlong t, vlong p) +{ + ThreadData *td = nil; + Array *res = nil; + lock(&threadlock); + for(int i = 0; i < nthreads && td == nil; i++) + if(threads[i]->id == t) + td = threads[i]; + if(td == nil) + res = mkscalarint(-1); + else + switch(p){ + case 0: + res = mkscalarint(t); /* thread id */ + break; + default: + unlock(&threadlock); + throwerror(L"Invalid task property", EDomain); + break; + } + + unlock(&threadlock); + return res; +} \ No newline at end of file -- cgit v1.2.3