From 32522ec7ea11dbaf67d5a8d3ee32e83a95affdea Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Mon, 8 Oct 2012 01:00:08 +0400 Subject: Enable test_stack for Win32 (with native threads) * tests/test_stack.c (main): Do not skip for Win32 (i.e., skip test only on VxWorks). * tests/test_stack.c (USE_WINTHREADS): Define new macro for Win32 but not Cygwin or pthreads-w32. * tests/test_stack.c: Include windows.h (instead of pthread.h) if USE_WINTHREADS. * tests/test_stack.c: Include sys/timeb.h (instead of [sys/]time.h) if USE_WINTHREADS or AO_USE_WIN32_PTHREADS. * tests/test_stack.c (get_msecs): Implement for Win32 (except for Cygwin) using ftime() (unless NO_TIME). * tests/test_stack.c (run_one_test): Use DWORD return type and WINAPI calling conventions if USE_WINTHREADS. * tests/test_stack.c (main): Declare "thread_id" local variable if USE_WINTHREADS; declare threads[] local variable of HANDLE type (instead of pthread_t) if USE_WINTHREADS; use CreateThread/GetLastError and WaitForSingleObject (instead of pthread_create and pthread_join) if USE_WINTHREADS; cast between integer value and pointer (passed to pthread_create) via size_t. * tests/test_stack.c (main): Print pushes/pops/threads summary even if NO_TIMES. --- tests/test_stack.c | 79 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 19 deletions(-) (limited to 'tests') diff --git a/tests/test_stack.c b/tests/test_stack.c index d068eb2..ff18fa6 100644 --- a/tests/test_stack.c +++ b/tests/test_stack.c @@ -17,12 +17,8 @@ #include -#if (((defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)) \ - || defined(_MSC_VER) || defined(_WIN32_WINCE)) \ - && !defined(AO_USE_WIN32_PTHREADS)) \ - || defined(__vxworks) +#if defined(__vxworks) - /* Skip the test if no pthreads. */ int main(void) { printf("test skipped\n"); @@ -31,7 +27,18 @@ #else -#include +#if ((defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)) \ + || defined(_MSC_VER) || defined(_WIN32_WINCE)) \ + && !defined(AO_USE_WIN32_PTHREADS) +# define USE_WINTHREADS +#endif + +#ifdef USE_WINTHREADS +# include +#else +# include +#endif + #include #include "atomic_ops_stack.h" /* includes atomic_ops.h as well */ @@ -40,7 +47,18 @@ # define MAX_NTHREADS 100 #endif -#ifndef NO_TIMES +#ifdef NO_TIMES +# define get_msecs() 0 +#elif defined(USE_WINTHREADS) || defined(AO_USE_WIN32_PTHREADS) +# include + long long get_msecs(void) + { + struct timeb tb; + + ftime(&tb); + return (long long)tb.time * 1000 + tb.millitm; + } +#else /* Unix */ # include # include /* Need 64-bit long long support */ @@ -51,9 +69,7 @@ gettimeofday(&tv, 0); return (long long)tv.tv_sec * 1000 + tv.tv_usec/1000; } -#else -# define get_msecs() 0 -#endif +#endif /* !NO_TIMES */ typedef struct le { AO_t next; @@ -146,7 +162,11 @@ volatile AO_t ops_performed = 0; } #endif -void * run_one_test(void * arg) +#ifdef USE_WINTHREADS + DWORD WINAPI run_one_test(LPVOID arg) +#else + void * run_one_test(void * arg) +#endif { list_element * t[MAX_NTHREADS + 1]; long index = (long)arg; @@ -211,7 +231,12 @@ int main(int argc, char **argv) for (nthreads = 1; nthreads <= max_nthreads; ++nthreads) { int i; - pthread_t thread[MAX_NTHREADS]; +# ifdef USE_WINTHREADS + DWORD thread_id; + HANDLE thread[MAX_NTHREADS]; +# else + pthread_t thread[MAX_NTHREADS]; +# endif int list_length = nthreads*(nthreads+1)/2; long long start_time; list_element * le; @@ -226,8 +251,15 @@ int main(int argc, char **argv) for (i = 1; i < nthreads; ++i) { int code; - if ((code = pthread_create(thread+i, 0, run_one_test, - (void *)(long)i)) != 0) { +# ifdef USE_WINTHREADS + thread[i] = CreateThread(NULL, 0, run_one_test, (LPVOID)(size_t)i, + 0, &thread_id); + code = thread[i] != NULL ? 0 : (int)GetLastError(); +# else + code = pthread_create(&thread[i], 0, run_one_test, + (void *)(size_t)i); +# endif + if (code != 0) { fprintf(stderr, "Thread creation failed %u\n", code); exit(3); } @@ -237,7 +269,14 @@ int main(int argc, char **argv) run_one_test(0); for (i = 1; i < nthreads; ++i) { int code; - if ((code = pthread_join(thread[i], 0)) != 0) { + +# ifdef USE_WINTHREADS + code = WaitForSingleObject(thread[i], INFINITE) == WAIT_OBJECT_0 ? + 0 : (int)GetLastError(); +# else + code = pthread_join(thread[i], 0); +# endif + if (code != 0) { fprintf(stderr, "Thread join failed %u\n", code); abort(); } @@ -253,7 +292,6 @@ int main(int argc, char **argv) while ((le = (list_element *)AO_stack_pop(&the_list)) != 0) free(le); } -# ifndef NO_TIMES for (nthreads = 1; nthreads <= max_nthreads; ++nthreads) { unsigned long sum = 0; @@ -263,14 +301,17 @@ int main(int argc, char **argv) for (exper_n = 0; exper_n < N_EXPERIMENTS; ++exper_n) { # if defined(VERBOSE) - printf("[%lu] ", times[nthreads][exper_n]); + printf(" [%lu]", times[nthreads][exper_n]); # endif sum += times[nthreads][exper_n]; } +# ifndef NO_TIMES printf(" %lu msecs\n", (sum + N_EXPERIMENTS/2)/N_EXPERIMENTS); +# else + printf(" completed\n"); +# endif } -# endif /* !NO_TIMES */ return 0; } -#endif /* !_MSC_VER */ +#endif -- cgit v1.2.1