diff options
author | rodri <rgl@antares-labs.eu> | 2024-09-02 14:04:04 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2024-09-02 14:04:04 +0000 |
commit | 40bfb73938e5b1875cd60a54cda98aa1a99e0792 (patch) | |
tree | ab065b5fea1fc14396fc7da4191df7a6f7c267c0 | |
parent | 877f2c0b88321839b939d1031ad7e5a386d89dc0 (diff) | |
download | threadpool-40bfb73938e5b1875cd60a54cda98aa1a99e0792.tar.gz threadpool-40bfb73938e5b1875cd60a54cda98aa1a99e0792.tar.bz2 threadpool-40bfb73938e5b1875cd60a54cda98aa1a99e0792.zip |
new: main5 (main4 but with blocks instead of lines.)
-rw-r--r-- | main5.c | 154 | ||||
-rw-r--r-- | mkfile | 1 | ||||
-rw-r--r-- | readme | 1 |
3 files changed, 156 insertions, 0 deletions
@@ -0,0 +1,154 @@ +#include <u.h> +#include <libc.h> +#include <thread.h> +#include <draw.h> +#include <memdraw.h> + +typedef struct Ttask Ttask; +typedef struct Tpool Tpool; + +struct Ttask +{ + void (*fn)(void*); + void *arg; +}; + +struct Tpool +{ + ulong nprocs; + Ref issued; + Ref complete; + + Channel *subq; /* task submission queue */ + Channel *done; /* task completion signal */ +}; + +void +threadloop(void *arg) +{ + Tpool *pool; + Ttask *task; + + pool = arg; + + while((task = recvp(pool->subq)) != nil){ + task->fn(task->arg); + incref(&pool->complete); + nbsend(pool->done, nil); + } +} + +Tpool * +mkthreadpool(ulong nprocs) +{ + Tpool *tp; + + tp = malloc(sizeof *tp); + memset(tp, 0, sizeof *tp); + tp->nprocs = nprocs; + tp->subq = chancreate(sizeof(void*), nprocs); + tp->done = chancreate(sizeof(void*), 0); + while(nprocs--) + proccreate(threadloop, tp, mainstacksize); + return tp; +} + +void +threadpoolexec(Tpool *tp, void (*fn)(void*), void *arg) +{ + Ttask *t; + + t = malloc(sizeof *t); + t->fn = fn; + t->arg = arg; + + sendp(tp->subq, t); + incref(&tp->issued); +} + +typedef struct Targs Targs; +struct Targs +{ + Memimage *i; + ulong off; + ulong len; +}; +void +fillpix(void *arg) +{ + Targs *imgop; + Point p; + ulong *fb, *fbb, *fbe, pix; + double α; + + imgop = arg; + fb = (ulong*)byteaddr(imgop->i, ZP); + fbb = fb + imgop->off; + fbe = fbb + imgop->len; + + while(fbb < fbe){ + p.x = (fbb-fb)%Dx(imgop->i->r); + p.y = (fbb-fb)/Dx(imgop->i->r); + α = atan2(p.y, p.x); + pix = α*25523UL*25523UL/* + truerand()*/; + *fbb++ = pix|0xFF<<24; + } +} + +void +usage(void) +{ + fprint(2, "usage: %s [-t] [-n nprocs]\n", argv0); + exits(nil); +} + +void +threadmain(int argc, char *argv[]) +{ + static int W = 1000, H = 1000; + Tpool *pool; + Targs *t; + Memimage *img; + int i, stride; + int threaded; + int nprocs; + + threaded = 0; + nprocs = 8; + ARGBEGIN{ + case 't': threaded++; break; + case 'n': nprocs = strtoul(EARGF(usage()), nil, 0); break; + default: usage(); + }ARGEND; + if(argc != 0) + usage(); + + if(memimageinit() != 0) + sysfatal("memimageinit: %r"); + + img = allocmemimage(Rect(0,0,W,H), XRGB32); + t = malloc(nprocs*sizeof(*t)); + stride = W*H/nprocs; + if(threaded){ + pool = mkthreadpool(nprocs); + + for(i = 0; i < nprocs; i++){ + t[i] = (Targs){img, i*stride, i == nprocs-1? W*H-i*stride: stride}; + threadpoolexec(pool, fillpix, &t[i]); + } + + while(pool->issued.ref != pool->complete.ref) + recvp(pool->done); + + writememimage(1, img); + + threadexitsall(nil); + } + + for(i = 0; i < nprocs; i++){ + t[i] = (Targs){img, i*stride, i == nprocs-1? W*H-i*stride: stride}; + fillpix(&t[i]); + } + writememimage(1, img); + exits(nil); +} @@ -5,5 +5,6 @@ TARG=main1\ main2\ main3\ main4\ + main5\ </sys/src/cmd/mkmany @@ -6,3 +6,4 @@ Thread pool experiments. - main2: rendezvous point task queue - main3: qlocked task queue - main4: channel-based memimage line raster task + - main5: channel-based memimage block raster task |