diff options
author | Andreas Baumann <abaumann@yahoo.com> | 2009-04-01 12:46:14 +0200 |
---|---|---|
committer | Andreas Baumann <abaumann@yahoo.com> | 2009-04-01 12:46:14 +0200 |
commit | 53945f7fa4249557bacc6597758dc71bc365533e (patch) | |
tree | 99a6c8d412625c4346545918f53e6d3ccfb00a23 /tests/threads | |
parent | 63382f447a312b4d40ac1bcd8090b2cf2c626bf1 (diff) | |
download | wolfbones-53945f7fa4249557bacc6597758dc71bc365533e.tar.gz wolfbones-53945f7fa4249557bacc6597758dc71bc365533e.tar.bz2 |
added a mutex around a counter test
Diffstat (limited to 'tests/threads')
-rw-r--r-- | tests/threads/GNUmakefile | 5 | ||||
-rw-r--r-- | tests/threads/Makefile.W32 | 5 | ||||
-rw-r--r-- | tests/threads/test_counter_mutex.c | 96 |
3 files changed, 104 insertions, 2 deletions
diff --git a/tests/threads/GNUmakefile b/tests/threads/GNUmakefile index 62c5b8a..c3a4fb5 100644 --- a/tests/threads/GNUmakefile +++ b/tests/threads/GNUmakefile @@ -7,7 +7,8 @@ INCLUDE_LIBS = \ $(TOPDIR)/src/libwolf.a TEST_BINS = \ - test_create_join$(EXE) + test_create_join$(EXE) \ + test_counter_mutex$(EXE) -include $(TOPDIR)/makefiles/gmake/sub.mk @@ -20,3 +21,5 @@ local_distclean: local_test: @echo "Testing creating, joining of threads.." @./test_create_join >/dev/null + @echo "Testing mutex.." + @./test_counter_mutex >/dev/null diff --git a/tests/threads/Makefile.W32 b/tests/threads/Makefile.W32 index 84b8610..b80c2a6 100644 --- a/tests/threads/Makefile.W32 +++ b/tests/threads/Makefile.W32 @@ -11,7 +11,8 @@ INCLUDE_LIBS = \ $(TOPDIR)\src\wolf.lib TEST_BINS = \ - test_create_join.exe + test_create_join.exe \ + test_counter_mutex.exe !INCLUDE $(TOPDIR)\makefiles\nmake\sub.mk @@ -27,3 +28,5 @@ local_distclean: local_test: @echo Testing creating,joining of threads.. @test_create_join >NUL + @echo Testing mutex.. + @test_counter_mutex >NUL diff --git a/tests/threads/test_counter_mutex.c b/tests/threads/test_counter_mutex.c new file mode 100644 index 0000000..3872473 --- /dev/null +++ b/tests/threads/test_counter_mutex.c @@ -0,0 +1,96 @@ +#include "threads/threads.h" +#include "threads/mutex.h" +#include "port/unused.h" + +#include <stdlib.h> /* for EXIT_SUCCESS, EXIT_FAILURE */ +#include <stdio.h> /* for fprintf */ + + +#define USE_MUTEX 1 + +#define NOF_LOOPS 10 +#define NOF_THREADS 10 +#if USE_MUTEX +#define NOF_ITERATIONS 1000000 +#else +#define NOF_ITERATIONS 10000000 +#endif + +static int counter = 0; +#if USE_MUTEX +static wolf_mutex_t counter_mutex; +#endif + +static WOLF_THREAD_RETURN_DECL thread_func( void *thread_data ) { + int i; + + for( i = 0; i < NOF_ITERATIONS; i++ ) { +#if USE_MUTEX + wolf_mutex_lock( &counter_mutex ); +#endif + counter++; +#if USE_MUTEX + wolf_mutex_unlock( &counter_mutex ); +#endif + } + + WOLF_UNUSED( thread_data ); + + WOLF_THREAD_RETURN +} + +int main( void ) { + int i; + int j; + wolf_thread_t thread[NOF_THREADS]; + wolf_error_t error; + +#if USE_MUTEX + error = wolf_mutex_init( &counter_mutex ); + if( error != WOLF_OK ) { + fprintf( stderr, "Unable to create mutex: %d\n", error ); + return EXIT_FAILURE; + } +#endif + + for( j = 0; j < NOF_LOOPS; j++ ) { + for( i = 0; i < NOF_THREADS; i++ ) { + error = wolf_thread_create( &thread[i], thread_func ); + if( error != WOLF_OK ) { + fprintf( stderr, "Unable to start thread %d: %d\n", i, error ); + return EXIT_FAILURE; + } + printf( "Created thread %d\n", i ); + fflush( stdout ); + } + + for( i = 0; i < NOF_THREADS; i++ ) { + error = wolf_thread_join( &thread[i] ); + if( error != WOLF_OK ) { + fprintf( stderr, "Unable to join thread %d: %d\n", i, error ); + return EXIT_FAILURE; + } + printf( "Joined thread %d\n", i ); + fflush( stdout ); + } + + printf( "Value of counter: %d\n", counter ); + + if( counter != ( j + 1 ) * NOF_THREADS * NOF_ITERATIONS ) { + fprintf( stderr, "Counter miscounted, is %d, must %d be!\n", + counter, ( j + 1 ) * NOF_THREADS * NOF_ITERATIONS ); + return EXIT_FAILURE; + } + } + +#if USE_MUTEX + error = wolf_mutex_destroy( &counter_mutex ); + if( error != WOLF_OK ) { + fprintf( stderr, "Unable to destroy mutex: %d\n", error ); + return EXIT_FAILURE; + } +#endif + + + return EXIT_SUCCESS; +} |