Merge branch 'master' of git.cs.unc.edu:pavel/FasTC

This commit is contained in:
Pavel Krajcevski 2014-01-29 14:38:09 -05:00
commit bf7ef5cd9e
6 changed files with 75 additions and 31 deletions

View file

@ -68,21 +68,30 @@
#include <cassert> #include <cassert>
#include <Windows.h> #include <Windows.h>
#include <WinBase.h>
class StopWatchImpl { class StopWatchImpl {
public: public:
uint64 frequency; uint64 frequency;
uint64 start; uint64 start;
uint64 stop; uint64 stop;
#ifndef __MINGW32__
uintptr_t affinityMask; uintptr_t affinityMask;
#endif
StopWatchImpl() : StopWatchImpl() :
start(0), stop(0), affinityMask(0) start(0), stop(0)
#ifndef __MINGW32__
, affinityMask(0)
#endif
{ {
// Initialize the performance counter frequency. // Initialize the performance counter frequency.
LARGE_INTEGER perfQuery; LARGE_INTEGER perfQuery;
BOOL supported = QueryPerformanceFrequency(&perfQuery); #ifndef NDEBUG
assert(supported == TRUE); assert(QueryPerformanceFrequency(&perfQuery));
#else
QueryPerformanceFrequency(&perfQuery);
#endif
this->frequency = perfQuery.QuadPart; this->frequency = perfQuery.QuadPart;
} }
}; };
@ -110,42 +119,56 @@ StopWatch::~StopWatch() {
// Start the stopwatch. // Start the stopwatch.
void StopWatch::Start() void StopWatch::Start()
{ {
#ifndef __MINGW32__
// MSDN recommends setting the thread affinity to avoid bugs in the BIOS and HAL. // MSDN recommends setting the thread affinity to avoid bugs in the BIOS and HAL.
// Create an affinity mask for the current processor. // Create an affinity mask for the current processor.
impl->affinityMask = (DWORD_PTR)1 << GetCurrentProcessorNumber(); impl->affinityMask = (DWORD_PTR)1 << GetCurrentProcessorNumber();
HANDLE currThread = GetCurrentThread(); HANDLE currThread = GetCurrentThread();
DWORD_PTR prevAffinityMask = SetThreadAffinityMask(currThread, impl->affinityMask); DWORD_PTR prevAffinityMask = SetThreadAffinityMask(currThread, impl->affinityMask);
assert(prevAffinityMask != 0); assert(prevAffinityMask != 0);
#endif
// Query the performance counter. // Query the performance counter.
LARGE_INTEGER perfQuery; LARGE_INTEGER perfQuery;
BOOL result = QueryPerformanceCounter(&perfQuery); #ifndef NDEBUG
assert(result); assert(QueryPerformanceCounter(&perfQuery));
#else
QueryPerformanceCounter(&perfQuery);
#endif
impl->start = perfQuery.QuadPart; impl->start = perfQuery.QuadPart;
#ifndef __MINGW32__
// Restore the thread's affinity mask. // Restore the thread's affinity mask.
prevAffinityMask = SetThreadAffinityMask(currThread, prevAffinityMask); prevAffinityMask = SetThreadAffinityMask(currThread, prevAffinityMask);
assert(prevAffinityMask != 0); assert(prevAffinityMask != 0);
#endif
} }
// Stop the stopwatch. // Stop the stopwatch.
void StopWatch::Stop() void StopWatch::Stop()
{ {
#ifndef __MINGW32__
// MSDN recommends setting the thread affinity to avoid bugs in the BIOS and HAL. // MSDN recommends setting the thread affinity to avoid bugs in the BIOS and HAL.
// Use the affinity mask that was created in the Start function. // Use the affinity mask that was created in the Start function.
HANDLE currThread = GetCurrentThread(); HANDLE currThread = GetCurrentThread();
DWORD_PTR prevAffinityMask = SetThreadAffinityMask(currThread, impl->affinityMask); DWORD_PTR prevAffinityMask = SetThreadAffinityMask(currThread, impl->affinityMask);
assert(prevAffinityMask != 0); assert(prevAffinityMask != 0);
#endif
// Query the performance counter. // Query the performance counter.
LARGE_INTEGER perfQuery; LARGE_INTEGER perfQuery;
BOOL result = QueryPerformanceCounter(&perfQuery); #ifndef NDEBUG
assert(result); assert(QueryPerformanceCounter(&perfQuery));
#else
QueryPerformanceCounter(&perfQuery);
#endif
impl->stop = perfQuery.QuadPart; impl->stop = perfQuery.QuadPart;
#ifndef __MINGW32__
// Restore the thread's affinity mask. // Restore the thread's affinity mask.
prevAffinityMask = SetThreadAffinityMask(currThread, prevAffinityMask); prevAffinityMask = SetThreadAffinityMask(currThread, prevAffinityMask);
assert(prevAffinityMask != 0); assert(prevAffinityMask != 0);
#endif
} }
// Reset the stopwatch. // Reset the stopwatch.
@ -153,7 +176,9 @@ void StopWatch::Reset()
{ {
impl->start = 0; impl->start = 0;
impl->stop = 0; impl->stop = 0;
#ifndef __MINGW32__
impl->affinityMask = 0; impl->affinityMask = 0;
#endif
} }
// Get the elapsed time in seconds. // Get the elapsed time in seconds.

View file

@ -123,8 +123,11 @@ void TCThread::Join() {
((TCThreadImpl *)m_Impl)->Join(); ((TCThreadImpl *)m_Impl)->Join();
} }
#ifdef WIN32
#undef Yield
#endif
void TCThread::Yield() { void TCThread::Yield() {
#ifdef __APPLE__ #if defined(__APPLE__) || defined(__MINGW32__)
int result = sched_yield(); int result = sched_yield();
#else #else
int result = pthread_yield(); int result = pthread_yield();
@ -135,7 +138,11 @@ void TCThread::Yield() {
} }
uint64 TCThread::ThreadID() { uint64 TCThread::ThreadID() {
#ifdef __MINGW32__
return static_cast<uint64>(pthread_self().x);
#else
return static_cast<uint64>(pthread_self()); return static_cast<uint64>(pthread_self());
#endif
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -268,7 +268,7 @@ WorkerThread::EAction WorkerQueue::AcceptThreadData(uint32 threadIdx) {
void WorkerQueue::GetStartForThread(const uint32 threadIdx, uint32 (&start)[2]) { void WorkerQueue::GetStartForThread(const uint32 threadIdx, uint32 (&start)[2]) {
assert(threadIdx >= 0); assert(threadIdx >= 0);
assert(threadIdx < int(m_NumThreads)); assert(threadIdx < m_NumThreads);
assert(m_Offsets[threadIdx] >= 0); assert(m_Offsets[threadIdx] >= 0);
const uint32 blockIdx = m_Offsets[threadIdx]; const uint32 blockIdx = m_Offsets[threadIdx];
@ -277,7 +277,7 @@ void WorkerQueue::GetStartForThread(const uint32 threadIdx, uint32 (&start)[2])
void WorkerQueue::GetEndForThread(const uint32 threadIdx, uint32 (&end)[2]) { void WorkerQueue::GetEndForThread(const uint32 threadIdx, uint32 (&end)[2]) {
assert(threadIdx >= 0); assert(threadIdx >= 0);
assert(threadIdx < int(m_NumThreads)); assert(threadIdx < m_NumThreads);
assert(m_Offsets[threadIdx] >= 0); assert(m_Offsets[threadIdx] >= 0);
assert(m_NumBlocks[threadIdx] >= 0); assert(m_NumBlocks[threadIdx] >= 0);

View file

@ -105,7 +105,7 @@ macro(config_compiler_and_linker)
set(cxx_no_rtti_flags "") set(cxx_no_rtti_flags "")
endif() endif()
if (CMAKE_USE_PTHREADS_INIT) # The pthreads library is available and allowed. if (CMAKE_USE_PTHREADS_INIT AND NOT MINGW) # The pthreads library is available and allowed.
set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=1") set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=1")
else() else()
set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=0") set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=0")

View file

@ -120,7 +120,9 @@ namespace internal {
// Valid only for fast death tests. Indicates the code is running in the // Valid only for fast death tests. Indicates the code is running in the
// child process of a fast style death test. // child process of a fast style death test.
#ifndef GTEST_OS_WINDOWS
static bool g_in_fast_death_test_child = false; static bool g_in_fast_death_test_child = false;
#endif
// Returns a Boolean value indicating whether the caller is currently // Returns a Boolean value indicating whether the caller is currently
// executing in the context of the death test child process. Tools such as // executing in the context of the death test child process. Tools such as
@ -852,7 +854,9 @@ DeathTest::TestRole NoExecDeathTest::AssumeRole() {
// Event forwarding to the listeners of event listener API mush be shut // Event forwarding to the listeners of event listener API mush be shut
// down in death test subprocesses. // down in death test subprocesses.
GetUnitTestImpl()->listeners()->SuppressEventForwarding(); GetUnitTestImpl()->listeners()->SuppressEventForwarding();
#ifndef GTEST_OS_WINDOWS
g_in_fast_death_test_child = true; g_in_fast_death_test_child = true;
#endif
return EXECUTE_TEST; return EXECUTE_TEST;
} else { } else {
GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1])); GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1]));

View file

@ -52,13 +52,21 @@
#include "FileStream.h" #include "FileStream.h"
#define _CRT_SECURE_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN
#include <Windows.h> #include <Windows.h>
#include <strsafe.h>
#include <stdio.h> #include <cassert>
#include <assert.h> #include <cstdio>
#include <cstdlib>
#include <cstring>
void ErrorExit(LPTSTR lpszFunction) #ifdef _MSC_VER
#define snprintf(out, outSz, fmt, ...) _snprintf_s(out, outSz, _TRUNCATE, fmt, __VA_ARGS__)
#define strncpy(dst, src, dstSz) strncpy_s(dst, dstSz, src, _TRUNCATE)
#endif
void ErrorExit(LPCSTR lpszFunction)
{ {
// Retrieve the system error message for the last-error code // Retrieve the system error message for the last-error code
@ -80,10 +88,10 @@ void ErrorExit(LPTSTR lpszFunction)
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf, snprintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR), LocalSize(lpDisplayBuf) / sizeof(CHAR),
TEXT("%s failed with error %d: %s"), "%s failed with error %lu: %s",
lpszFunction, dw, lpMsgBuf); lpszFunction, dw, (LPCSTR)lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf); LocalFree(lpMsgBuf);
@ -150,7 +158,7 @@ FileStream::FileStream(const CHAR *filename, EFileMode mode)
: m_Impl(new FileStreamImpl(filename, mode)) : m_Impl(new FileStreamImpl(filename, mode))
, m_Mode(mode) , m_Mode(mode)
{ {
strncpy_s(m_Filename, filename, kMaxFilenameSz); strncpy(m_Filename, filename, kMaxFilenameSz);
m_Filename[kMaxFilenameSz - 1] = CHAR('\0'); m_Filename[kMaxFilenameSz - 1] = CHAR('\0');
} }
@ -159,7 +167,7 @@ FileStream::FileStream(const FileStream &other)
, m_Mode(other.m_Mode) , m_Mode(other.m_Mode)
{ {
m_Impl->IncreaseReferenceCount(); m_Impl->IncreaseReferenceCount();
strncpy_s(m_Filename, other.m_Filename, kMaxFilenameSz); strncpy(m_Filename, other.m_Filename, kMaxFilenameSz);
} }
FileStream &FileStream::operator=(const FileStream &other) { FileStream &FileStream::operator=(const FileStream &other) {
@ -178,7 +186,7 @@ FileStream &FileStream::operator=(const FileStream &other) {
m_Impl->IncreaseReferenceCount(); m_Impl->IncreaseReferenceCount();
m_Mode = other.m_Mode; m_Mode = other.m_Mode;
strncpy_s(m_Filename, other.m_Filename, kMaxFilenameSz); strncpy(m_Filename, other.m_Filename, kMaxFilenameSz);
return *this; return *this;
} }
@ -203,8 +211,8 @@ int32 FileStream::Read(uint8 *buf, uint32 bufSz) {
m_Mode == eFileMode_WriteBinaryAppend m_Mode == eFileMode_WriteBinaryAppend
) { ) {
CHAR errStr[256]; CHAR errStr[256];
_sntprintf_s(errStr, 256, "Cannot read from file '%s': File opened for reading.", m_Filename); snprintf(errStr, 256, "Cannot read from file '%s': File opened for reading.", m_Filename);
OutputDebugString(errStr); OutputDebugString(errStr);
return -2; return -2;
} }
@ -215,7 +223,7 @@ int32 FileStream::Read(uint8 *buf, uint32 bufSz) {
DWORD oldPosition = SetFilePointer(fp, 0, NULL, FILE_CURRENT); DWORD oldPosition = SetFilePointer(fp, 0, NULL, FILE_CURRENT);
if(INVALID_SET_FILE_POINTER == oldPosition) { if(INVALID_SET_FILE_POINTER == oldPosition) {
CHAR errStr[256]; CHAR errStr[256];
_sntprintf_s(errStr, 256, "Error querying the file position before reading from file '%s'(0x%x).", m_Filename, GetLastError()); snprintf(errStr, 256, "Error querying the file position before reading from file '%s'(0x%lx).", m_Filename, GetLastError());
OutputDebugString(errStr); OutputDebugString(errStr);
return -1; return -1;
} }
@ -224,7 +232,7 @@ int32 FileStream::Read(uint8 *buf, uint32 bufSz) {
BOOL success = ReadFile(fp, buf, bufSz, &amtRead, NULL); BOOL success = ReadFile(fp, buf, bufSz, &amtRead, NULL);
if(!success) { if(!success) {
CHAR errStr[256]; CHAR errStr[256];
_sntprintf_s(errStr, 256, "Error reading from file '%s'.", m_Filename); snprintf(errStr, 256, "Error reading from file '%s'.", m_Filename);
OutputDebugString(errStr); OutputDebugString(errStr);
return -1; return -1;
} }
@ -232,7 +240,7 @@ int32 FileStream::Read(uint8 *buf, uint32 bufSz) {
DWORD newPosition = SetFilePointer(fp, 0, NULL, FILE_CURRENT); DWORD newPosition = SetFilePointer(fp, 0, NULL, FILE_CURRENT);
if(INVALID_SET_FILE_POINTER == newPosition) { if(INVALID_SET_FILE_POINTER == newPosition) {
CHAR errStr[256]; CHAR errStr[256];
_sntprintf_s(errStr, 256, "Error querying the file position after reading from file '%s'(0x%x).", m_Filename, GetLastError()); snprintf(errStr, 256, "Error querying the file position after reading from file '%s'(0x%lx).", m_Filename, GetLastError());
OutputDebugString(errStr); OutputDebugString(errStr);
return -1; return -1;
} }
@ -246,7 +254,7 @@ int32 FileStream::Write(const uint8 *buf, uint32 bufSz) {
m_Mode == eFileMode_ReadBinary m_Mode == eFileMode_ReadBinary
) { ) {
CHAR errStr[256]; CHAR errStr[256];
_sntprintf_s(errStr, 256, "Cannot write to file '%s': File opened for writing.", m_Filename); snprintf(errStr, 256, "Cannot write to file '%s': File opened for writing.", m_Filename);
OutputDebugString(errStr); OutputDebugString(errStr);
return -2; return -2;
} }
@ -265,7 +273,7 @@ int32 FileStream::Write(const uint8 *buf, uint32 bufSz) {
if(INVALID_SET_FILE_POINTER == dwPos) { if(INVALID_SET_FILE_POINTER == dwPos) {
CHAR errStr[256]; CHAR errStr[256];
_sntprintf_s(errStr, 256, "Error querying the file position before reading to file '%s'(0x%x).", m_Filename, GetLastError()); snprintf(errStr, 256, "Error querying the file position before reading to file '%s'(0x%lx).", m_Filename, GetLastError());
OutputDebugString(errStr); OutputDebugString(errStr);
return -1; return -1;
} }
@ -279,7 +287,7 @@ int32 FileStream::Write(const uint8 *buf, uint32 bufSz) {
if(!success) { if(!success) {
CHAR errStr[256]; CHAR errStr[256];
_sntprintf_s(errStr, 256, "Error writing to file '%s'.", m_Filename); snprintf(errStr, 256, "Error writing to file '%s'.", m_Filename);
OutputDebugString(errStr); OutputDebugString(errStr);
return -1; return -1;
} }
@ -296,7 +304,7 @@ int32 FileStream::Tell() {
DWORD pos = SetFilePointer(fp, 0, NULL, FILE_CURRENT); DWORD pos = SetFilePointer(fp, 0, NULL, FILE_CURRENT);
if(INVALID_SET_FILE_POINTER == pos) { if(INVALID_SET_FILE_POINTER == pos) {
CHAR errStr[256]; CHAR errStr[256];
_sntprintf_s(errStr, 256, "Error querying the file position before reading to file '%s'(0x%x).", m_Filename, GetLastError()); snprintf(errStr, 256, "Error querying the file position before reading to file '%s'(0x%lx).", m_Filename, GetLastError());
OutputDebugString(errStr); OutputDebugString(errStr);
return -1; return -1;
} }