From 746cf897c3d3ba4950c70a62a98824f34ef7a8b2 Mon Sep 17 00:00:00 2001 From: pithikos Date: Fri, 6 Mar 2015 16:11:07 +0000 Subject: [PATCH] Works now even when compiled with optimization flags --- tests/README.md | 22 ++++++++++++++++++---- tests/funcs | 5 +++++ tests/memleaks | 4 ++-- tests/normal_compile | 16 ++++++++++++++++ tests/optimized_compile | 15 +++++++++++++++ tests/pause_resume | 2 +- tests/threadpool | 2 +- tests/wait | 4 ++-- thpool.c | 4 ++-- 9 files changed, 62 insertions(+), 12 deletions(-) create mode 100755 tests/normal_compile create mode 100755 tests/optimized_compile diff --git a/tests/README.md b/tests/README.md index 25844b3..3496718 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,11 +1,25 @@ Tests ------------------------------------------------------------------------ -For memory leaks run `./memleaks`. This might take a while since valgrind -needs one second to init each thread. +**Case tests** +```` +memleaks - Will run tests for memory leaks. valgrind is being used for this. + Notice that valgrind requires one second to init each thread. +threadpool - Will run general functional tests for the threadpool. +pause_resume - Will test the synchronisation of the threadpool from the user. +wait - Will run tests to assuse that the wait() function works correctly. -For the general functional tests run `./threadpool`. +```` -**On errors:** + +**Compilation cases** +```` +normal_compile - Will run all tests above against a simply compiled threadpool. +optimized_compile - Will run all tests but against a binary that was compiled + with optimization flags. +```` + + +**On errors** Check the created log file `error.log` diff --git a/tests/funcs b/tests/funcs index 60c11fb..69dd351 100644 --- a/tests/funcs +++ b/tests/funcs @@ -37,3 +37,8 @@ function err { #string #log echo "$2" >> error.log exit 1 } + + +function compile { #cfilepath + gcc $COMPILATION_FLAGS "$1" ../thpool.c -pthread -o test +} diff --git a/tests/memleaks b/tests/memleaks index 14a9f0c..15c6ac2 100755 --- a/tests/memleaks +++ b/tests/memleaks @@ -13,7 +13,7 @@ function test_thread_free { #threads echo "Testing creation and destruction of threads(=$1)" - gcc src/no_work.c ../thpool.c -pthread -o test + compile src/no_work.c output=$(valgrind --leak-check=full --track-origins=yes ./test "$1" 2>&1 > /dev/null) heap_usage=$(echo "$output" | grep "total heap usage") allocs=$(extract_num "[0-9]* allocs" "$heap_usage") @@ -32,7 +32,7 @@ function test_thread_free { #threads # This is the same with test_many_thread_allocs but multiplied function test_thread_free_multi { #threads #times echo "Testing multiple threads creation and destruction in pool(threads=$1 times=$2)" - gcc src/no_work.c ../thpool.c -pthread -o test + compile src/no_work.c for ((i = 1; i <= $2; i++)); do python -c "import sys; sys.stdout.write('$i/$2\r')" output=$(valgrind --leak-check=full --track-origins=yes ./test "$1" 2>&1 > /dev/null) diff --git a/tests/normal_compile b/tests/normal_compile new file mode 100755 index 0000000..3afe877 --- /dev/null +++ b/tests/normal_compile @@ -0,0 +1,16 @@ +#! /bin/bash + +# +# This will run all tests for a simple compilation +# + + + +# ---------------------------- Tests ----------------------------------- + +. threadpool +. pause_resume +. memleaks +. wait + +echo "No errors" diff --git a/tests/optimized_compile b/tests/optimized_compile new file mode 100755 index 0000000..a476a02 --- /dev/null +++ b/tests/optimized_compile @@ -0,0 +1,15 @@ +#! /bin/bash + +# +# This file will run all tests but with a binary that has +# been compiled with optimization flags. +# + + + +# ---------------------------- Tests ----------------------------------- + +COMPILATION_FLAGS='-g -O' +. normal_compile + +echo "No optimization errors" diff --git a/tests/pause_resume b/tests/pause_resume index d8ea81e..44c67e1 100755 --- a/tests/pause_resume +++ b/tests/pause_resume @@ -13,7 +13,7 @@ function test_pause_resume_est7secs { #threads echo "Pause and resume test for 7 secs with $1 threads" - gcc src/pause_resume.c ../thpool.c -pthread -o test + compile src/pause_resume.c realsecs=$(/usr/bin/time -f '%e' ./test "$1" 2>&1 > /dev/null) threshold=1.00 # in secs diff --git a/tests/threadpool b/tests/threadpool index a0bb208..110c74b 100755 --- a/tests/threadpool +++ b/tests/threadpool @@ -10,7 +10,7 @@ function test_mass_addition { #endsum #threads echo "Adding up to $1 with $2 threads" - gcc src/conc_increment.c ../thpool.c -pthread -o test + compile src/conc_increment.c output=$(./test $1 $2) num=$(echo $output | awk '{print $(NF)}') if [ "$num" == "$1" ]; then diff --git a/tests/wait b/tests/wait index 45025b7..ab25a8f 100755 --- a/tests/wait +++ b/tests/wait @@ -13,7 +13,7 @@ function test_wait_each_job { #threads #jobs echo "Will test waiting for each job ($1 threads, $2 jobs)" - gcc src/wait.c ../thpool.c -pthread -o test + compile src/wait.c realsecs=$(time_exec ./test $2 $1 1) threshold=1.00 # in secs @@ -29,7 +29,7 @@ function test_wait_each_job { #threads #jobs function test_wait_pool { #threads #jobs echo "Will test waiting for whole threadpool ($1 threads, $2 jobs)" - gcc src/wait.c ../thpool.c -pthread -o test + compile src/wait.c realsecs=$(time_exec ./test $2 $1 0) threshold=1.00 # in secs diff --git a/thpool.c b/thpool.c index 236a6a4..e655eb8 100644 --- a/thpool.c +++ b/thpool.c @@ -69,8 +69,8 @@ typedef struct thread{ /* Threadpool */ typedef struct thpool_{ thread** threads; /* pointer to threads */ - int num_threads_alive; /* threads currently alive */ - int num_threads_working; /* threads currently working */ + volatile int num_threads_alive; /* threads currently alive */ + volatile int num_threads_working; /* threads currently working */ pthread_mutex_t thcount_lock; /* used for thread count etc */ jobqueue* jobqueue_p; /* pointer to the job queue */ } thpool_;