From 48ae25d35a83febdff1b921acc5e41c71a60fda7 Mon Sep 17 00:00:00 2001 From: Pithikos Date: Tue, 17 Jan 2017 19:58:44 +0000 Subject: [PATCH] Add API function to get number of working threads --- README.md | 1 + tests/api.sh | 33 ++++++++++++++++++++++++++++ tests/normal_compile.sh | 1 + tests/src/api.c | 48 +++++++++++++++++++++++++++++++++++++++++ thpool.c | 5 +++++ thpool.h | 22 +++++++++++++++++++ 6 files changed, 110 insertions(+) create mode 100755 tests/api.sh create mode 100644 tests/src/api.c diff --git a/README.md b/README.md index 29bf53c..2e51b7e 100644 --- a/README.md +++ b/README.md @@ -50,3 +50,4 @@ For a deeper look into the documentation check in the [thpool.h](https://github. | ***thpool_destroy(thpool)*** | This will destroy the threadpool. If jobs are currently being executed, then it will wait for them to finish. | | ***thpool_pause(thpool)*** | All threads in the threadpool will pause no matter if they are idle or executing work. | | ***thpool_resume(thpool)*** | If the threadpool is paused, then all threads will resume from where they were. | +| ***thpool_num_threads_working(thpool)*** | Will return the number of currently working threads. | diff --git a/tests/api.sh b/tests/api.sh new file mode 100755 index 0000000..17f23e3 --- /dev/null +++ b/tests/api.sh @@ -0,0 +1,33 @@ +#! /bin/bash + +# +# This file has several tests to check that the API +# works to an acceptable standard. +# + +. funcs.sh + + +# ---------------------------- Tests ----------------------------------- + + +function test_api { + echo "Testing API calls.." + compile src/api.c + output=`./test` + if [[ $? != 0 ]]; then + err "$output" "$output" + exit 1 + fi +} + + + +# Run tests +test_api + + + + + +echo "No API errors" diff --git a/tests/normal_compile.sh b/tests/normal_compile.sh index 3613606..aada642 100755 --- a/tests/normal_compile.sh +++ b/tests/normal_compile.sh @@ -9,6 +9,7 @@ # ---------------------------- Tests ----------------------------------- . threadpool.sh +. api.sh . pause_resume.sh . heap_stack_garbage.sh . memleaks.sh diff --git a/tests/src/api.c b/tests/src/api.c new file mode 100644 index 0000000..ad2c1ed --- /dev/null +++ b/tests/src/api.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include "../../thpool.h" + + +void sleep_2_secs(){ + sleep(2); + puts("SLEPT"); +} + + +int main(int argc, char *argv[]){ + + int num = 0; + threadpool thpool; + + /* Test if we can get the current number of working threads */ + thpool = thpool_init(10); + thpool_add_work(thpool, (void*)sleep_2_secs, NULL); + thpool_add_work(thpool, (void*)sleep_2_secs, NULL); + thpool_add_work(thpool, (void*)sleep_2_secs, NULL); + thpool_add_work(thpool, (void*)sleep_2_secs, NULL); + sleep(1); + num = thpool_num_threads_working(thpool); + if (thpool_num_threads_working(thpool) != 4) { + printf("Expected 4 threads working, got %d", num); + return -1; + }; + + /* Test (same as above) */ + thpool = thpool_init(5); + thpool_add_work(thpool, (void*)sleep_2_secs, NULL); + thpool_add_work(thpool, (void*)sleep_2_secs, NULL); + sleep(1); + num = thpool_num_threads_working(thpool); + if (num != 2) { + printf("Expected 2 threads working, got %d", num); + return -1; + }; + + // thpool_destroy(thpool); + + // sleep(1); // Sometimes main exits before thpool_destroy finished 100% + + return 0; +} diff --git a/thpool.c b/thpool.c index 4a7fea0..f269899 100644 --- a/thpool.c +++ b/thpool.c @@ -253,6 +253,11 @@ void thpool_resume(thpool_* thpool_p) { } +int thpool_num_threads_working(thpool_* thpool_p){ + return thpool_p->num_threads_working; +} + + diff --git a/thpool.h b/thpool.h index 03edeef..e5d0d1b 100644 --- a/thpool.h +++ b/thpool.h @@ -158,6 +158,28 @@ void thpool_resume(threadpool); */ void thpool_destroy(threadpool); + +/** + * @brief Show currently working threads + * + * Working threads are the threads that are performing work (not idle). + * + * @example + * int main() { + * threadpool thpool1 = thpool_init(2); + * threadpool thpool2 = thpool_init(2); + * .. + * printf("Working threads: %d\n", thpool_num_threads_working(thpool1)); + * .. + * return 0; + * } + * + * @param threadpool the threadpool of interest + * @return integer number of threads working + */ +int thpool_num_threads_working(threadpool); + + #ifdef __cplusplus } #endif