/* This starts the timer */ NFCSTATUS phOsalNfc_Timer_Start(uint32_t TimerId, uint32_t dueTimeMsec, ppCallBck_t pCallback, void *pContext) { uint32_t uIndex; LONGLONG DueTime; uint32_t uWindow = (dueTimeMsec < 50) ? 0 : (dueTimeMsec / 4); if (NULL == gpphOsalNfc_Context || PH_OSALNFC_TIMER_ID_INVALID == TimerId) { return PHNFCSTVAL(CID_NFC_OSAL, NFCSTATUS_INVALID_PARAMETER); } uIndex = TimerId - PH_OSAL_TIMER_BASE_ADDRESS; // Convert dueTimeMsec to relative filetime units (100ns) DueTime = Int32x32To64(dueTimeMsec, -10000); EnterCriticalSection(&gpphOsalNfc_Context->TimerLock); SetThreadpoolTimer(gpphOsalNfc_Context->TimerList[uIndex].pTimer, (FILETIME*)&DueTime, 0, uWindow); gpphOsalNfc_Context->TimerList[uIndex].pCallback = pCallback; gpphOsalNfc_Context->TimerList[uIndex].pContext = pContext; LeaveCriticalSection(&gpphOsalNfc_Context->TimerLock); return NFCSTATUS_SUCCESS; }
void MI_CALL Perf_Indication_DisableIndications( _In_opt_ Perf_Indication_Self* self, _In_ MI_Context* indicationsContext, _In_opt_z_ const MI_Char* nameSpace, _In_opt_z_ const MI_Char* className) { MI_UNREFERENCED_PARAMETER(nameSpace); MI_UNREFERENCED_PARAMETER(className); #ifdef _MSC_VER if (self) { self->indicationContext = 0; self->shutdownCalled = MI_TRUE; SetThreadpoolTimer(self->indicationTimer, NULL, 0, 0); WaitForThreadpoolTimerCallbacks(self->indicationTimer, TRUE); CloseThreadpoolTimer(self->indicationTimer); self->shutdownCalled = MI_FALSE; } #endif MI_PostResult(indicationsContext, MI_RESULT_OK); }
void MI_CALL Perf_Indication_EnableIndications( _In_opt_ Perf_Indication_Self* self, _In_ MI_Context* indicationsContext, _In_opt_z_ const MI_Char* nameSpace, _In_opt_z_ const MI_Char* className) { #ifdef _MSC_VER /* NOTE: Do not call MI_PostResult on this context */ MI_UNREFERENCED_PARAMETER(nameSpace); MI_UNREFERENCED_PARAMETER(className); if (self) { FILETIME now; ZeroMemory(&now, sizeof(FILETIME)); self->indicationContext = indicationsContext; self->indicationTimer = CreateThreadpoolTimer(TimerCallback, self, NULL); SetThreadpoolTimer(self->indicationTimer, &now, 0, 1); } #endif }
static void CALLBACK TimerCallback( __inout PTP_CALLBACK_INSTANCE instance, __inout_opt PVOID context, __inout PTP_TIMER timer ) { MI_Result result = MI_RESULT_FAILED; Perf_Indication_Self* self; Perf_Indication indicationInstance = {{0}}; if(!context) { return; } self = (Perf_Indication_Self*)context; result = Perf_Indication_Construct(&indicationInstance, self->indicationContext); if(result != MI_RESULT_OK) return; result = FillInstance(self->indicationContext, &indicationInstance.__instance, self->keyCounter++); if(result != MI_RESULT_OK) return; Perf_Indication_Post(&indicationInstance, self->indicationContext, 0, 0); Perf_Indication_Destruct(&indicationInstance); if(self->shutdownCalled != MI_TRUE) { // Clean up the previous timer. Do not do a WaitForThreadpoolTimerCallbacks as this will hang. // Instead directly call CloseThreadPoolTimer. This asynchronously closes the timer when outstanding calls are done. // As the timer settings are for 1 event only, there will be no outstanding calls, but only the current call. SetThreadpoolTimer(self->indicationTimer, NULL, 0, 0); CloseThreadpoolTimer(self->indicationTimer); FILETIME now; ZeroMemory(&now, sizeof(FILETIME)); self->indicationTimer = CreateThreadpoolTimer(TimerCallback, self, NULL); SetThreadpoolTimer(self->indicationTimer, &now, 0, 1); } }
void phOsalNfc_Timer_Delete(uint32_t TimerId) { uint32_t uIndex; // // In various places in the code, timers are initialized only // after they are first needed. Despite this fact, they are // deleted unconditionally when their context is being deleted // Intead of adding an if statement in all places where // timers are being deleted, this check is added to prevent // a NULL deref. // if (NULL == gpphOsalNfc_Context || PH_OSALNFC_TIMER_ID_INVALID == TimerId) { return; } uIndex = TimerId - PH_OSAL_TIMER_BASE_ADDRESS; EnterCriticalSection(&gpphOsalNfc_Context->TimerLock); if(gpphOsalNfc_Context->TimerList[uIndex].pTimer != NULL) { SetThreadpoolTimer(gpphOsalNfc_Context->TimerList[uIndex].pTimer, NULL, 0, 0); LeaveCriticalSection(&gpphOsalNfc_Context->TimerLock); WaitForThreadpoolTimerCallbacks(gpphOsalNfc_Context->TimerList[uIndex].pTimer, TRUE); EnterCriticalSection(&gpphOsalNfc_Context->TimerLock); CloseThreadpoolTimer(gpphOsalNfc_Context->TimerList[uIndex].pTimer); gpphOsalNfc_Context->TimerList[uIndex].pTimer = NULL; gpphOsalNfc_Context->TimerList[uIndex].pCallback = NULL; gpphOsalNfc_Context->TimerList[uIndex].pContext = NULL; gpphOsalNfc_Context->TimerList[uIndex].bFired = FALSE; uIndex = 0; while (uIndex < PH_MAX_OSAL_NUM_TIMERS) { if (gpphOsalNfc_Context->TimerList[uIndex].pTimer != NULL) { break; } uIndex++; } } LeaveCriticalSection(&gpphOsalNfc_Context->TimerLock); }
{ std::cerr << "Watchdog timer expired: Scilab killed" << std::endl; ExitProcess(1); } void timeout_process_after(int timeoutDelay) { auto timerid = CreateThreadpoolTimer(kill_process_callback, nullptr, nullptr); FILETIME FileDueTime; ULARGE_INTEGER ulDueTime; // Set the timer to fire in the delay in seconds ulDueTime.QuadPart = (ULONGLONG) - (timeoutDelay * 10 * 1000 * 1000); FileDueTime.dwHighDateTime = ulDueTime.HighPart;
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, PTSTR, int) { //TcpClient(); _tcscpy_s(g_szCaption, _countof(g_szCaption), TEXT("Timed Message Box")); // How many seconds we'll give the user to respond g_nSecLeft = 10; // Create the threadpool timer object PTP_TIMER lpTimer = CreateThreadpoolTimer(MsgBoxTimeoutCallback, NULL, NULL); if (lpTimer == NULL) { TCHAR szMsg[MAX_PATH]; StringCchPrintf(szMsg, _countof(szMsg), TEXT("Impossible to create the timer: %u"), GetLastError()); MessageBox(NULL, szMsg, TEXT("Error"), MB_OK | MB_ICONERROR); return(-1); } // Start the timer in one second to trigger every 1 second ULARGE_INTEGER ulRelativeStartTime; ulRelativeStartTime.QuadPart = (LONGLONG) -(10000000); // start in 1 second FILETIME ftRelativeStartTime; ftRelativeStartTime.dwHighDateTime = ulRelativeStartTime.HighPart; ftRelativeStartTime.dwLowDateTime = ulRelativeStartTime.LowPart; SetThreadpoolTimer( lpTimer, &ftRelativeStartTime, 1000, // Triggers every 1000 milliseconds 0 ); // Display the message box MessageBox(NULL, TEXT("You have 10 seconds to respond"), g_szCaption, MB_OK); // Clean up the timer CloseThreadpoolTimer(lpTimer); // Let us know if the user responded or if we timed out MessageBox( NULL, (g_nSecLeft == 1) ? TEXT("Timeout") : TEXT("User responded"), TEXT("Result"), MB_OK); return(0); }
HRESULT CDriverSimBroadcast::Uninitialize() { MethodEntry("..."); HRESULT hr = S_OK; if (_TpBroadcast != nullptr) { SetThreadpoolTimer(_TpBroadcast, nullptr, 0, 0); WaitForThreadpoolTimerCallbacks(_TpBroadcast, TRUE); CloseThreadpoolTimer(_TpBroadcast); _TpBroadcast = nullptr; } _pSocketListener = nullptr; MethodReturnHR(hr); }
HRESULT CDriverSimBroadcast::Initialize(_In_ CSocketListener* pSocketListener) { MethodEntry("..."); _pSocketListener = pSocketListener; HRESULT hr = InitBroadcastMessage(); if (SUCCEEDED(hr)) { _TpBroadcast = CreateThreadpoolTimer(&s_BroadcastTimerFired, (PVOID)this, nullptr); if (_TpBroadcast == nullptr) { hr = HRESULT_FROM_WIN32(GetLastError()); } } if (SUCCEEDED(hr)) { // Convert milliseconds to relative filetime units (100ns) LONGLONG dueTime = Int32x32To64(BROADCAST_INTERVAL_MS, -10000); SetThreadpoolTimer(_TpBroadcast, (FILETIME*)&dueTime, BROADCAST_INTERVAL_MS, 3000); } MethodReturnHR(hr); }
NFCSTATUS phOsalNfc_Timer_Stop(uint32_t TimerId) { uint32_t uIndex; if (NULL == gpphOsalNfc_Context || PH_OSALNFC_TIMER_ID_INVALID == TimerId) { return PHNFCSTVAL(CID_NFC_OSAL, NFCSTATUS_INVALID_PARAMETER); } uIndex = TimerId - PH_OSAL_TIMER_BASE_ADDRESS; SetThreadpoolTimer(gpphOsalNfc_Context->TimerList[uIndex].pTimer, NULL, 0, 0); WaitForThreadpoolTimerCallbacks(gpphOsalNfc_Context->TimerList[uIndex].pTimer, TRUE); EnterCriticalSection(&gpphOsalNfc_Context->TimerLock); gpphOsalNfc_Context->TimerList[uIndex].bFired = FALSE; gpphOsalNfc_Context->TimerList[uIndex].pCallback = NULL; gpphOsalNfc_Context->TimerList[uIndex].pContext = NULL; LeaveCriticalSection(&gpphOsalNfc_Context->TimerLock); return NFCSTATUS_SUCCESS; }