static int32_t SocketInterrupted(const OsNetworkHandle* aHandle) { int32_t interrupted; OsMutexLock(aHandle->iCtx->iMutex); interrupted = aHandle->iInterrupted; OsMutexUnlock(aHandle->iCtx->iMutex); return interrupted; }
static int OsNetworkHandle_IsInterrupted(OsNetworkHandle* aHandle) { int state; OsMutexLock(aHandle->iMutex); state = aHandle->iFlagInterrupted; OsMutexUnlock(aHandle->iMutex); return state; }
int32_t OsNetworkInterrupt(THandle aHandle, int32_t aInterrupt) { int32_t err = 0; OsNetworkHandle* handle = (OsNetworkHandle*)aHandle; OsMutexLock(handle->iCtx->iMutex); handle->iInterrupted = aInterrupt; if (aInterrupt != 0) { (void)WSASetEvent(handle->iEvent); } else { (void)WSAResetEvent(handle->iEvent); } OsMutexUnlock(handle->iCtx->iMutex); return err; }
static void OsNetworkHandle_SetInterrupted(OsNetworkHandle* aHandle, int aNewInterruptState) { OsMutexLock(aHandle->iMutex); if ( aNewInterruptState != (int) aHandle->iFlagInterrupted ) { aHandle->iFlagInterrupted = aNewInterruptState; #if INT_ENABLED if ( aHandle->iFlagInterrupted ) post_int(aHandle); else clear_int(aHandle); #endif } OsMutexUnlock(aHandle->iMutex); }
uint64_t OsTimeInUs(OsContext* aContext) { struct timeval now, diff, adjustedNow; OsMutexLock(aContext->iMutex); gettimeofday(&now, NULL); /* if time has moved backwards, calculate by how much and add this to aContext->iTimeAdjustment */ if (now.tv_sec < aContext->iPrevTime.tv_sec || (now.tv_sec == aContext->iPrevTime.tv_sec && now.tv_usec < aContext->iPrevTime.tv_usec)) { diff = subtractTimeval(&aContext->iPrevTime, &now); fprintf(stderr, "WARNING: clock moved backwards by %llu.%03llusecs\n", (unsigned long long)diff.tv_sec, (unsigned long long)(diff.tv_usec/1000)); aContext->iTimeAdjustment = addTimeval(&aContext->iTimeAdjustment, &diff); } aContext->iPrevTime = now; /* stash current time to allow the next call to spot any backwards move */ adjustedNow = addTimeval(&now, &aContext->iTimeAdjustment); /* add any previous backwards moves to the time */ diff = subtractTimeval(&adjustedNow, &aContext->iStartTime); /* how long since we started, ignoring any backwards moves */ OsMutexUnlock(aContext->iMutex); return (uint64_t)diff.tv_sec * 1000000 + diff.tv_usec; }
int32_t OsNetworkInterrupt(THandle aHandle, int32_t aInterrupt) { int32_t err = 0; OsNetworkHandle* handle = (OsNetworkHandle*)aHandle; OsMutexLock(gMutex); handle->iInterrupted = aInterrupt; int32_t val = 1; if (aInterrupt != 0) { if (TEMP_FAILURE_RETRY(write(handle->iPipe[1], &val, sizeof(val))) == -1) { err = -1; } } else { while (TEMP_FAILURE_RETRY(read(handle->iPipe[0], &val, sizeof(val))) > 0) { ; } } OsMutexUnlock(gMutex); return err; }
const char* OsStackTraceEntry(THandle aStackTrace, uint32_t aIndex) { #ifndef STACK_TRACE_ENABLE aStackTrace = aStackTrace; aIndex = aIndex; return NULL; #else OsStackTrace* stackTrace = (OsStackTrace*)aStackTrace; OsMutexLock(stackTrace->iOsContext->iMutex); if (stackTrace->iSymbol == NULL) { stackTrace->iSymbol = (SYMBOL_INFO*)calloc(sizeof(SYMBOL_INFO) + 256, 1); if (stackTrace->iSymbol == NULL) { return NULL; } stackTrace->iSymbol->MaxNameLen = 255; stackTrace->iSymbol->SizeOfStruct = sizeof(SYMBOL_INFO); } SymFromAddr(stackTrace->iOsContext->iDebugSymbolHandle, (DWORD64)(stackTrace->iStack[aIndex]), 0, stackTrace->iSymbol); OsMutexUnlock(stackTrace->iOsContext->iMutex); return stackTrace->iSymbol->Name; #endif }
uint64_t OsTimeInUs(OsContext* aContext) { uint64_t now, diff, adjustedNow; FILETIME ft; OsMutexLock(aContext->iMutex); GetSystemTimeAsFileTime(&ft); now = ft.dwHighDateTime; now <<= 32; now |= ft.dwLowDateTime; /* if time has moved backwards, calculate by how much and add this to aContext->iTimeAdjustment */ if (now < aContext->iPrevTime) { diff = aContext->iPrevTime - now; fprintf(stderr, "WARNING: clock moved backwards by %3llums\n", now / 10000); aContext->iTimeAdjustment += diff; } aContext->iPrevTime = now; /* stash current time to allow the next call to spot any backwards move */ adjustedNow = now + aContext->iTimeAdjustment; /* add any previous backwards moves to the time */ diff = adjustedNow - aContext->iStartTime; /* how long since we started, ignoring any backwards moves */ diff /= 10; // GetSystemTimeAsFileTime has units of 100 nano-secs OsMutexUnlock(aContext->iMutex); return diff; }