diff options
author | csilvers <csilvers@6b5cf1ce-ec42-a296-1ba9-69fdba395a50> | 2007-11-29 23:39:24 +0000 |
---|---|---|
committer | csilvers <csilvers@6b5cf1ce-ec42-a296-1ba9-69fdba395a50> | 2007-11-29 23:39:24 +0000 |
commit | 11b02f7aebd05cf39f6f93bdd48786909f99f34e (patch) | |
tree | 660c613e74cffd643ee4a7d0f0cb170057abbf7d /src/tests/testutil.cc | |
parent | 49b74b9508797f8aafe6b86e62e7efc4ec200e48 (diff) | |
download | gperftools-11b02f7aebd05cf39f6f93bdd48786909f99f34e.tar.gz |
Thu Nov 29 07:59:43 2007 Google Inc. <opensource@google.com>
* google-perftools: version 0.94 release
* PORTING: MinGW/Msys support -- runs same code as MSVC does (csilvers)
* PORTING: Add NumCPUs support for Mac OS X (csilvers)
* Work around a sscanf bug in glibc(?) (waldemar)
* Fix Windows MSVC bug triggered by thread deletion (csilvers)
* Fix bug that triggers in MSVC /O2: missing volatile (gpike)
* March-of-time support: quiet warnings/errors for gcc 4.2, OS X 10.5
* Modify pprof so it works without nm: useful for windows (csilvers)
* pprof: Support filtering for CPU profiles (cgd)
* Bugfix: have realloc report to hooks in all situations (maxim)
* Speed improvement: replace slow memcpy with std::copy (soren)
* Speed: better iterator efficiency in RecordRegionRemoval (soren)
* Speed: minor speed improvements via better bitfield alignment (gpike)
* Documentation: add documentation of binary profile output (cgd)
git-svn-id: http://gperftools.googlecode.com/svn/trunk@40 6b5cf1ce-ec42-a296-1ba9-69fdba395a50
Diffstat (limited to 'src/tests/testutil.cc')
-rw-r--r-- | src/tests/testutil.cc | 72 |
1 files changed, 64 insertions, 8 deletions
diff --git a/src/tests/testutil.cc b/src/tests/testutil.cc index cb523de..9ab4c3c 100644 --- a/src/tests/testutil.cc +++ b/src/tests/testutil.cc @@ -36,7 +36,12 @@ #include <stdlib.h> // for NULL, abort() #include "tests/testutil.h" -#if defined(NO_THREADS) || !defined(HAVE_PTHREADS) +struct FunctionAndId { + void (*ptr_to_function)(int); + int id; +}; + +#if defined(NO_THREADS) || !(defined(HAVE_PTHREADS) || defined(WIN32)) extern "C" void RunThread(void (*fn)()) { (*fn)(); @@ -53,19 +58,70 @@ extern "C" void RunManyThreadsWithId(void (*fn)(int), int count, int) { (*fn)(i); // stacksize doesn't make sense in a non-threaded context } -#else // NO_THREADS || !HAVE_PTHREAD +#elif defined(WIN32) + +#define WIN32_LEAN_AND_MEAN /* We always want minimal includes */ +#include <windows.h> + +extern "C" { + // This helper function has the signature that pthread_create wants. + DWORD WINAPI RunFunctionInThread(LPVOID ptr_to_ptr_to_fn) { + (**static_cast<void (**)()>(ptr_to_ptr_to_fn))(); // runs fn + return 0; + } + + DWORD WINAPI RunFunctionInThreadWithId(LPVOID ptr_to_fnid) { + FunctionAndId* fn_and_id = static_cast<FunctionAndId*>(ptr_to_fnid); + (*fn_and_id->ptr_to_function)(fn_and_id->id); // runs fn + return 0; + } + + void RunManyThreads(void (*fn)(), int count) { + DWORD dummy; + HANDLE* hThread = new HANDLE[count]; + for (int i = 0; i < count; i++) { + hThread[i] = CreateThread(NULL, 0, RunFunctionInThread, &fn, 0, &dummy); + if (hThread[i] == NULL) ExitProcess(i); + } + WaitForMultipleObjects(count, hThread, TRUE, INFINITE); + for (int i = 0; i < count; i++) { + CloseHandle(hThread[i]); + } + delete[] hThread; + } + + void RunThread(void (*fn)()) { + RunManyThreads(fn, 1); + } + + void RunManyThreadsWithId(void (*fn)(int), int count, int stacksize) { + DWORD dummy; + HANDLE* hThread = new HANDLE[count]; + FunctionAndId* fn_and_ids = new FunctionAndId[count]; + for (int i = 0; i < count; i++) { + fn_and_ids[i].ptr_to_function = fn; + fn_and_ids[i].id = i; + hThread[i] = CreateThread(NULL, stacksize, RunFunctionInThreadWithId, + &fn_and_ids[i], 0, &dummy); + if (hThread[i] == NULL) ExitProcess(i); + } + WaitForMultipleObjects(count, hThread, TRUE, INFINITE); + for (int i = 0; i < count; i++) { + CloseHandle(hThread[i]); + } + delete[] fn_and_ids; + delete[] hThread; + } +} + +#else // not NO_THREADS, not !HAVE_PTHREAD, not WIN32 #include <pthread.h> #define SAFE_PTHREAD(fncall) do { if ((fncall) != 0) abort(); } while (0) extern "C" { - struct FunctionAndId { - void (*ptr_to_function)(int); - int id; - }; - -// This helper function has the signature that pthread_create wants. + // This helper function has the signature that pthread_create wants. static void* RunFunctionInThread(void *ptr_to_ptr_to_fn) { (**static_cast<void (**)()>(ptr_to_ptr_to_fn))(); // runs fn return NULL; |