X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Ftests%2Ftest-thread.c;fp=src%2Ftests%2Ftest-thread.c;h=cc676bd0cdaa1a5edcc401b0b275927cded8c47b;hb=98a5bca16007a7c4740c4326ef83768d034aed3e;hp=0000000000000000000000000000000000000000;hpb=1d5157a6c68a790f7a50811f4a3b48b29ee82f58;p=src%2Fapp-framework-binder.git diff --git a/src/tests/test-thread.c b/src/tests/test-thread.c new file mode 100644 index 00000000..cc676bd0 --- /dev/null +++ b/src/tests/test-thread.c @@ -0,0 +1,99 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +#include +#include "../afb-thread.h" + +struct foo { + int value; + int refcount; +}; + +void addref(void *closure) +{ + struct foo *foo = closure; + foo->refcount++; +} + +void unref(void *closure) +{ + struct foo *foo = closure; + if(!--foo->refcount) { + printf("%06d FREE\n", foo->value); + free(foo); + } +} + +void fail(void *closure, const char *status, const char *info) +{ + struct foo *foo = closure; + printf("%06d ERROR %s\n", foo->value, status); +} + +struct afb_req_itf itf = { + .json = NULL, + .get = NULL, + + .success = NULL, + .fail = fail, + + .raw = NULL, + .send = NULL, + + .context_get = NULL, + .context_set = NULL, + + .addref = addref, + .unref = unref, + + .session_close = NULL, + .session_set_LOA = NULL, + + .subscribe = NULL, + .unsubscribe = NULL, + + .subcall = NULL +}; + +void process(struct afb_req req) +{ + struct timespec ts; + struct foo *foo = req.closure; + printf("%06d PROCESS T%d\n", foo->value, (int)syscall(SYS_gettid)); + ts.tv_sec = 0; + ts.tv_nsec = foo->value * 1000; +// nanosleep(&ts, NULL); +} + +int main() +{ + int i; + struct foo *foo; + struct afb_req req; + struct timespec ts; + + req.itf = &itf; + afb_thread_init(4, 1); + for (i = 0 ; i < 10000 ; i++) { + req.closure = foo = malloc(sizeof *foo); + foo->value = i; + foo->refcount = 1; + afb_thread_call(req, process, 5, (&ts) + (i % 4)); + unref(foo); + ts.tv_sec = 0; + ts.tv_nsec = 1000000; +// nanosleep(&ts, NULL); + } + ts.tv_sec = 1; + ts.tv_nsec = 0; + nanosleep(&ts, NULL); + afb_thread_terminate(); +} + + + +