From 5d4b0dc3c9e7d3130ee07dfc5cfe4dd7007667cf Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 24 May 2014 00:05:50 +0800 Subject: [PATCH] Add basic worker and its tests --- config.h.in | 3 +++ include/r3_workers.h | 12 +++++++++++ src/workers.c | 47 ++++++++++++++++++++++++++++++++++++++++ tests/Makefile.am | 3 +++ tests/check_worker.c | 51 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 116 insertions(+) create mode 100644 include/r3_workers.h create mode 100644 src/workers.c create mode 100644 tests/check_worker.c diff --git a/config.h.in b/config.h.in index 14e8361..d8bd9f2 100644 --- a/config.h.in +++ b/config.h.in @@ -28,6 +28,9 @@ /* Define to 1 if you have the `memset' function. */ #undef HAVE_MEMSET +/* Define to 1 if you have the header file. */ +#undef HAVE_PTHREAD_H + /* Define to 1 if your system has a GNU libc compatible `realloc' function, and to 0 otherwise. */ #undef HAVE_REALLOC diff --git a/include/r3_workers.h b/include/r3_workers.h new file mode 100644 index 0000000..7468977 --- /dev/null +++ b/include/r3_workers.h @@ -0,0 +1,12 @@ +/* + * r3_workers.h + * Copyright (C) 2014 c9s + * + * Distributed under terms of the MIT license. + */ + +#ifndef R3_WORKERS_H +#define R3_WORKERS_H + + +#endif /* !R3_WORKERS_H */ diff --git a/src/workers.c b/src/workers.c new file mode 100644 index 0000000..20035ef --- /dev/null +++ b/src/workers.c @@ -0,0 +1,47 @@ +/* + * workers.c + * Copyright (C) 2014 c9s + * + * Distributed under terms of the MIT license. + */ +#include "r3.h" +#include "r3_workers.h" + +#include +#include +#include + + +void *PrintHello(void *threadid) +{ + long tid; + tid = (long)threadid; + printf("Hello World! It's me, thread #%ld!\n", tid); + pthread_exit(NULL); +} + +typedef struct { + int thread_id; + node * matched_node; +} feedback_payload; + +void *r3_feedback_worker(void * data) { + feedback_payload * payload = (feedback_payload*) data; // pointer cast + + return NULL; +} + +void r3_launch_feedback_worker(feedback_payload * data) { + int rc; + pthread_t worker_thread; + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + rc = pthread_create(&worker_thread, &attr, r3_feedback_worker, (void*) data); +} + + +void r3_worker_stop() { + pthread_exit(NULL); +} + diff --git a/tests/Makefile.am b/tests/Makefile.am index dce7b05..0d71368 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -43,6 +43,9 @@ check_tree_SOURCES = check_tree.c TESTS += check_slug check_slug_SOURCES = check_slug.c +TESTS += check_worker +check_worker_SOURCES = check_worker.c + check_PROGRAMS = $(TESTS) diff --git a/tests/check_worker.c b/tests/check_worker.c new file mode 100644 index 0000000..e1c2dfa --- /dev/null +++ b/tests/check_worker.c @@ -0,0 +1,51 @@ +/* + * check_worker.c + * Copyright (C) 2014 c9s + * + * Distributed under terms of the MIT license. + */ +#include "config.h" +#include +#include +#include +#include "r3.h" +#include "r3_str.h" +#include "r3_workers.h" +#include "str_array.h" +#include "bench.h" + +START_TEST (test_feedback_worker) +{ + node * n = r3_tree_create(1); + + r3_tree_insert_path(n, "/foo/bar/baz", NULL); + r3_tree_insert_path(n, "/foo/bar/qux", NULL); + r3_tree_insert_path(n, "/foo/bar/quux", NULL); + r3_tree_insert_path(n, "/foo/bar/corge", NULL); + r3_tree_insert_path(n, "/foo/bar/grault", NULL); + r3_tree_insert_path(n, "/garply/grault/foo", NULL); + r3_tree_insert_path(n, "/garply/grault/bar", NULL); + r3_tree_compile(n); + + r3_tree_free(n); +} +END_TEST + + +Suite* r3_suite (void) { + Suite *suite = suite_create("Worker Test"); + TCase *tcase = tcase_create("worker test"); + tcase_add_test(tcase, test_feedback_worker); + suite_add_tcase(suite, tcase); + return suite; +} + +int main (int argc, char *argv[]) { + int number_failed; + Suite *suite = r3_suite(); + SRunner *runner = srunner_create(suite); + srunner_run_all(runner, CK_NORMAL); + number_failed = srunner_ntests_failed(runner); + srunner_free(runner); + return number_failed; +}