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/test_counter_mutex.c | |
parent | 63382f447a312b4d40ac1bcd8090b2cf2c626bf1 (diff) | |
download | wolfbones-53945f7fa4249557bacc6597758dc71bc365533e.tar.gz wolfbones-53945f7fa4249557bacc6597758dc71bc365533e.tar.bz2 |
added a mutex around a counter test
Diffstat (limited to 'tests/threads/test_counter_mutex.c')
-rw-r--r-- | tests/threads/test_counter_mutex.c | 96 |
1 files changed, 96 insertions, 0 deletions
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; +} |