]> git.ozlabs.org Git - ccan/blobdiff - ccan/antithread/test/run-lock.c
Add antithread. Not finished, but useful as example of module whose
[ccan] / ccan / antithread / test / run-lock.c
diff --git a/ccan/antithread/test/run-lock.c b/ccan/antithread/test/run-lock.c
new file mode 100644 (file)
index 0000000..b486e0a
--- /dev/null
@@ -0,0 +1,61 @@
+#include "antithread/antithread.c"
+#include <assert.h>
+#include <unistd.h>
+#include "tap/tap.h"
+
+#define NUM_RUNS 100
+
+static void *test(struct at_pool *atp, int *val)
+{
+       unsigned int i;
+
+       if (at_read_parent(atp) != test) {
+               diag("Woah, at_read said bad");
+               return NULL;
+       }
+
+       /* We increment val, then sleep a little. */
+       for (i = 0; i < NUM_RUNS; i++) {
+               at_lock(val);
+               (*(volatile int *)val)++;
+               usleep(i * 100);
+               at_unlock(val);
+               usleep(i * 100);
+       }
+
+       return val;
+};
+
+int main(int argc, char *argv[])
+{
+       struct at_pool *atp;
+       struct athread *at;
+       int *val, i;
+
+       plan_tests(3);
+
+       atp = at_pool(1*1024*1024);
+       assert(atp);
+       val = talloc_zero(at_pool_ctx(atp), int);
+       at = at_run(atp, test, val);
+       assert(at);
+
+       ok1(*val == 0);
+
+       at_tell(at, test);
+
+       /* We increment val, then sleep a little. */
+       for (i = 0; i < NUM_RUNS; i++) {
+               at_lock(val);
+               (*(volatile int *)val)++;
+               usleep(i * 100);
+               at_unlock(val);
+               usleep(i * 100);
+       }
+       ok1(at_read(at) == val);
+       talloc_free(at);
+
+       ok1(*val == NUM_RUNS*2);
+
+       return exit_status();
+}