diff options
Diffstat (limited to 'concurrency.c')
-rw-r--r-- | concurrency.c | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/concurrency.c b/concurrency.c index 99ca589..c01426d 100644 --- a/concurrency.c +++ b/concurrency.c @@ -2,9 +2,11 @@ #include <libc.h> #include <thread.h> #include <bio.h> + #include "apl9.h" -#define STACKSIZE (8*1024*1024) /* 8 MB */ +/* Nasty stuff to get stack size used */ +#include "/sys/src/libthread/threadimpl.h" typedef struct SpawnData SpawnData; struct SpawnData @@ -224,6 +226,7 @@ newthreaddata(Array *name) { ThreadData *td = emallocz(sizeof(ThreadData), 1); td->id = threadid(); + td->requiredstack = REQUIREDSTACK; td->currentdfn = nil; td->mail = 0; td->lastmail = 0; @@ -271,6 +274,12 @@ taskproperty(vlong t, vlong p) case 1: /* thread name */ res = fnSame(td->name); break; + case 2: /* stacksize max */ + res = mkscalarint(STACKSIZE); + break; + case 3: /* used stacksize */ + res = mkscalarint(stackused()); + break; default: unlock(&threadlock); throwerror(L"Invalid task property", EDomain); @@ -279,4 +288,27 @@ taskproperty(vlong t, vlong p) unlock(&threadlock); return res; +} + +int +stackused(void) +{ + int x; + Proc *p = _threadgetproc(); + Thread *t = p->thread; + return STACKSIZE - ((uchar*)&x - (uchar*)t->stk); +} + +int +hasstack(int n) +{ + return (n+stackused()) < STACKSIZE; +} + +void +checkstack(void) +{ + ThreadData *td = getthreaddata(); + if(!hasstack(td->requiredstack)) + throwerror(L"Not enough C stack. Stuff will go bad from now on.", EStack); }
\ No newline at end of file |