diff options
author | Nicholas Noll <nbnoll@eml.cc> | 2020-04-19 09:23:31 -0700 |
---|---|---|
committer | Nicholas Noll <nbnoll@eml.cc> | 2020-04-19 09:23:31 -0700 |
commit | 60ce7fba21a6d37c0acbe152039fbc3d0e692bf0 (patch) | |
tree | 73fa0295dace25c23a00f4ec4e654b4f4fb85619 /sys/libn/test.c | |
parent | 1ae9a10d56fca8fe585e77533c49e5c9d680ff12 (diff) |
chore: reorganized structure to allow for more parallel projects
Diffstat (limited to 'sys/libn/test.c')
-rw-r--r-- | sys/libn/test.c | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/sys/libn/test.c b/sys/libn/test.c new file mode 100644 index 0000000..3f8e608 --- /dev/null +++ b/sys/libn/test.c @@ -0,0 +1,105 @@ +#include <u.h> + +uintptr +printtest(coro *c, uintptr d) +{ + printf("--> Recieved %lu\n", d); + d = coro·yield(c, d+10); + printf("--> Now %lu\n", d); + + return d; +} + +uintptr +sequence(coro *c, uintptr start) +{ + int d = start; + for (;;) { + coro·yield(c, d++); + } + + return d; +} + +struct PrimeMsg +{ + coro *seq; + int p; +}; + +uintptr +filter(coro *c, uintptr data) +{ + int x, p; + coro *seq; + struct PrimeMsg *msg; + + // Need to copy relevant variables onto the local stack + // Data is volatile. + msg = (struct PrimeMsg*)data; + seq = msg->seq; + p = msg->p; + + for (;;) { + x = coro·yield(seq, x); + if (x % p != 0) { + x = coro·yield(c, x); + } + } + + return 0; +} + +int +main() +{ + int i; + coro *c[4]; + uintptr d; + + printf("Starting singleton test\n"); + + for (i = 0; i < arrlen(c); i++) { + c[i] = coro·new(0, &printtest); + } + + /* Singleton test */ + d = 0; + for (i = 0; i < 10; i++) { + d = coro·yield(c[0], d); + } + + printf("Starting triplet test\n"); + + /* Triplet test */ + for (i = 0; i < 10; i++) { + d = coro·yield(c[1], d); + d = coro·yield(c[2], d+100); + d = coro·yield(c[3], d+200); + } + + for (i = 0; i < arrlen(c); i++) { + coro·free(c[i]); + } + + /* Prime sieve */ + printf("Starting prime test\n"); + uintptr num; + coro *cur, *seq[50]; + + num = 2; + seq[0] = coro·new(4096, &sequence); + cur = *seq; + + num = coro·yield(cur, num); + for (i = 1; i < arrlen(seq); i++) { + seq[i] = coro·new(4096, &filter); + struct PrimeMsg msg = { + .seq = cur, + .p = num, + }; + cur = seq[i]; + num = coro·yield(cur, (uintptr)&msg); + printf("--> prime number %lu\n", num); + } +} |