From 58f1c192c2aa58d959ecb69197b2650b27f2eb7d Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Thu, 3 Jun 2010 22:03:27 +0200 Subject: can pass thread data now (Linux), adapted tests --- include/wolf/threads/threads.h | 9 +++------ src/threads/threads.c | 8 ++++---- tests/threads/test_counter_mutex.c | 2 +- tests/threads/test_create_join.c | 11 +++++++++-- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/include/wolf/threads/threads.h b/include/wolf/threads/threads.h index 73c5236..bd49fb9 100644 --- a/include/wolf/threads/threads.h +++ b/include/wolf/threads/threads.h @@ -48,9 +48,6 @@ typedef pthread_t wolf_thread_t; typedef void *( *wolf_thread_func_t )( void * ); -wolf_error_t wolf_thread_create( wolf_thread_t *thread, wolf_thread_func_t func ); -wolf_error_t wolf_thread_join( wolf_thread_t *thread ); - #endif /* HAVE_PTHREADS */ #ifdef _WIN32 @@ -65,11 +62,11 @@ typedef uintptr_t wolf_thread_t; typedef void ( *wolf_thread_func_t )( void * ); -wolf_error_t wolf_thread_create( wolf_thread_t *thread, wolf_thread_func_t func ); -wolf_error_t wolf_thread_join( wolf_thread_t *thread ); - #endif /* _WIN32 */ +wolf_error_t wolf_thread_create( wolf_thread_t *thread, wolf_thread_func_t func, void *data ); +wolf_error_t wolf_thread_join( wolf_thread_t *thread ); + #ifdef __cplusplus } #endif diff --git a/src/threads/threads.c b/src/threads/threads.c index 0c99697..1ff019d 100644 --- a/src/threads/threads.c +++ b/src/threads/threads.c @@ -23,7 +23,7 @@ #include /* for assertions */ #include "port/unused.h" -wolf_error_t wolf_thread_create( wolf_thread_t *thread, wolf_thread_func_t func ) { +wolf_error_t wolf_thread_create( wolf_thread_t *thread, wolf_thread_func_t func, void *data ) { pthread_attr_t attr; int res; @@ -36,7 +36,7 @@ wolf_error_t wolf_thread_create( wolf_thread_t *thread, wolf_thread_func_t func } /* create the thread */ - res = pthread_create( thread, &attr, func, NULL ); + res = pthread_create( thread, &attr, func, data ); if( res == EINVAL ) { return WOLF_ERR_INVALID_VALUE; } else if( res != 0 ) { @@ -68,13 +68,13 @@ wolf_error_t wolf_thread_join( wolf_thread_t *thread ) { #include /* for errno and EXXX values */ -wolf_error_t wolf_thread_create( wolf_thread_t *thread, wolf_thread_func_t func ) { +wolf_error_t wolf_thread_create( wolf_thread_t *thread, wolf_thread_func_t func, void *data ) { /* _beginthread doesn't signal termination. Unusable!, * CreateThread is for DLLs and surpasses CRT code, not good, * so we use _beginthreadex (this must be adapted if somebody * wants to write a DLL with these functions.. (see also POCO) */ - *thread = _beginthreadex( NULL, 0, func, NULL, 0, NULL ); + *thread = _beginthreadex( NULL, 0, func, data, 0, NULL ); if( thread < 0 ) { switch( errno ) { case EINVAL: diff --git a/tests/threads/test_counter_mutex.c b/tests/threads/test_counter_mutex.c index af28f6f..b827d15 100644 --- a/tests/threads/test_counter_mutex.c +++ b/tests/threads/test_counter_mutex.c @@ -72,7 +72,7 @@ int main( void ) { for( j = 0; j < NOF_LOOPS; j++ ) { for( i = 0; i < NOF_THREADS; i++ ) { - error = wolf_thread_create( &thread[i], thread_func ); + error = wolf_thread_create( &thread[i], thread_func, NULL ); if( error != WOLF_OK ) { fprintf( stderr, "Unable to start thread %d: %d\n", i, error ); return EXIT_FAILURE; diff --git a/tests/threads/test_create_join.c b/tests/threads/test_create_join.c index 12b39d3..0eaa6a6 100644 --- a/tests/threads/test_create_join.c +++ b/tests/threads/test_create_join.c @@ -25,14 +25,19 @@ #define NOF_ITERATIONS 10000 static WOLF_THREAD_RETURN_DECL thread_func( void *thread_data ) { + int thread_id = *((int *)thread_data); int i; + printf( "Starting thread %d\n", thread_id ); + fflush( stdout ); + for( i = 0; i < NOF_ITERATIONS; i++ ) { printf( "%d\n", i ); fflush( stdout ); } - WOLF_UNUSED( thread_data ); + printf( "Terminating thread %d\n", thread_id ); + fflush( stdout ); WOLF_THREAD_RETURN } @@ -41,9 +46,11 @@ int main( void ) { int i; wolf_thread_t thread[NOF_THREADS]; wolf_error_t error; + int thread_data[NOF_THREADS]; for( i = 0; i < NOF_THREADS; i++ ) { - error = wolf_thread_create( &thread[i], thread_func ); + thread_data[i] = i; + error = wolf_thread_create( &thread[i], thread_func, (void *)&thread_data[i] ); if( error != WOLF_OK ) { fprintf( stderr, "Unable to start thread %d: %d\n", i, error ); return EXIT_FAILURE; -- cgit v1.2.3-54-g00ecf